Skip to content

Commit fd50419

Browse files
committed
Add AST comparison test
1 parent 7246297 commit fd50419

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ jobs:
2020
node-version: 16
2121
- run: npm install
2222
- run: npm test
23+
- if: runner.os == 'Linux'
24+
uses: actions/setup-ruby@v1
25+
with:
26+
ruby-version: '3.1'
27+
- if: runner.os == 'Linux'
28+
run: npm run-script test-compare
2329
test_windows:
2430
runs-on: windows-2019
2531
steps:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"prebuild": "prebuild -r electron -t 3.0.0 -t 4.0.0 -t 4.0.4 -t 5.0.0 --strip && prebuild -t 8.16.0 -t 10.12.0 --strip",
2323
"prebuild:upload": "prebuild --upload-all",
2424
"test": "tree-sitter test && script/parse-examples",
25+
"test-compare": "script/parse-examples --compare",
2526
"test-windows": "tree-sitter test"
2627
},
2728
"repository": "https://github.com/tree-sitter/tree-sitter-ruby",

script/compare-ast.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /bin/bash
2+
set -o pipefail
3+
4+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5+
6+
file="$1"
7+
"${SCRIPT_DIR}/print-ast.rb" "$file" > "$file.1"
8+
if [ "$?" != "0" ]; then
9+
echo "print-ast failure: $file"
10+
rm "$file.1"
11+
exit 1
12+
fi
13+
"${SCRIPT_DIR}/../node_modules/tree-sitter-cli/cli.js" parse "$file" | \
14+
sed 's/ \[[0-9]\+, [0-9]\+\] - \[[0-9]\+, [0-9]\+\]//' | \
15+
tr $'\n' $'\t' | sed 's/\(^\|[[:space:]]*\)(comment)//g' | tr $'\t' $'\n' > "$file.2"
16+
17+
if [ "$?" != "0" ]; then
18+
echo "parse failure: $file"
19+
rm "$file.1" "$file.2"
20+
exit 1
21+
fi
22+
diff "$file.1" "$file.2" > "$file.diff"
23+
if [ "$?" != "0" ]; then
24+
echo "diff: $file"
25+
exit 1
26+
else
27+
rm "$file".{diff,1,2}
28+
fi
29+

script/known_differences.expected

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### differences caused by bugs in tree-sitter-ruby ###
2+
3+
# single quoted heredocs do not contain interpolation
4+
diff: examples/ruby_spec/core/exception/interrupt_spec.rb
5+
diff: examples/ruby_spec/language/fixtures/squiggly_heredoc.rb
6+
diff: examples/ruby_spec/language/heredoc_spec.rb
7+
diff: examples/ruby_spec/library/bigdecimal/BigDecimal_spec.rb
8+
9+
# string array with escape sequences like `%w[\\]`
10+
diff: examples/ruby_spec/language/array_spec.rb
11+
diff: examples/ruby_spec/language/fixtures/classes.rb
12+
13+
# bad escape handling for `/\c#{str}/`
14+
diff: examples/ruby_spec/language/regexp/interpolation_spec.rb
15+
16+
### benign differences ###
17+
18+
# ripper does not distinguish between empty `then` and missing `then`
19+
diff: examples/ruby_spec/language/case_spec.rb
20+
diff: examples/ruby_spec/language/if_spec.rb
21+
diff: examples/ruby_spec/language/pattern_matching_spec.rb
22+
23+
# ripper does not emit events for parentheses in define? and not statements
24+
diff: examples/ruby_spec/language/defined_spec.rb
25+
diff: examples/ruby_spec/language/precedence_spec.rb
26+

script/parse-examples

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,22 @@ success_percent=$(bc -l <<< "100*${success_count}/${example_count}")
3939
printf \
4040
"Successfully parsed %d of %d example files (%.1f%%)\n" \
4141
$success_count $example_count $success_percent
42+
43+
if [ "$1" == "--compare" ]; then
44+
45+
find examples -name '*.rb' | grep -v -f script/known_failures.txt \
46+
| xargs -P8 -n1 script/compare-ast.sh | sort > script/known_differences.actual
47+
48+
difference_count=$(wc -l < "script/known_differences.actual")
49+
50+
(cat script/known_differences.expected | sed -e 's/\s*#.*//' -e '/^$/d' | sort \
51+
| diff --label script/known_differences.expected - script/known_differences.actual \
52+
) || (echo "Files script/known_differences.expected and script/known_differences.actual differ"; exit 1)
53+
54+
rm -f script/known_differences.actual
55+
56+
printf \
57+
"%d of %d example files were parsed differently compared to Ruby::Ripper\n" \
58+
$difference_count $success_count
59+
60+
fi

0 commit comments

Comments
 (0)