Skip to content

Commit e1d750f

Browse files
authored
Big cleanup of test case reduction utilities (#250)
* Text-based test reduction moved into test_utils * Remove some obsolete utils * Rename reduction utils to something more sensible * List actual reduced failures in package parsing tests
1 parent 1515d8f commit e1d750f

File tree

5 files changed

+110
-204
lines changed

5 files changed

+110
-204
lines changed

test/fuzz_test.jl

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -885,51 +885,6 @@ const cutdown_tokens = [
885885

886886
#-------------------------------------------------------------------------------
887887

888-
function parser_throws_exception(str)
889-
try
890-
JuliaSyntax.parseall(JuliaSyntax.SyntaxNode, str, ignore_errors=true)
891-
false
892-
catch
893-
true
894-
end
895-
end
896-
897-
"""
898-
Reduce test case via combination of bisection and random deletion.
899-
900-
This is suited to randomly generated strings, but it's surprisingly effective
901-
for code-like strings as well.
902-
"""
903-
function rand_reduce(str, parse_failure=parser_throws_exception)
904-
while true
905-
if length(str) <= 1
906-
return str
907-
end
908-
m1 = thisind(str, length(str)÷2)
909-
m2 = nextind(str, m1)
910-
if parse_failure(str[1:m1])
911-
str = str[1:m1]
912-
elseif parse_failure(str[m2:end])
913-
str = str[m2:end]
914-
else
915-
chunklen = clamp(length(str)÷10, 1, 10)
916-
reduced = false
917-
for i = 1:100
918-
m = thisind(str, rand(1:length(str)-chunklen))
919-
s = str[1:m]*str[nextind(str, m+chunklen):end]
920-
if parse_failure(s)
921-
str = s
922-
reduced = true
923-
break
924-
end
925-
end
926-
if !reduced
927-
return str
928-
end
929-
end
930-
end
931-
end
932-
933888
# The parser should never throw an exception. To test whether this is true,
934889
# try passing randomly generated bad input data into it.
935890
function _fuzz_test(bad_input_iter)
@@ -939,7 +894,7 @@ function _fuzz_test(bad_input_iter)
939894
JuliaSyntax.parseall(JuliaSyntax.SyntaxNode, str, ignore_errors=true);
940895
catch exc
941896
!(exc isa InterruptException) || rethrow()
942-
rstr = rand_reduce(str)
897+
rstr = reduce_text(str, parser_throws_exception)
943898
@error "Parser threw exception" rstr exception=current_exceptions()
944899
push!(error_strings, rstr)
945900
end
@@ -1005,7 +960,7 @@ Fuzz test parser against randomly generated binary strings
1005960
"""
1006961
function fuzz_binary(nbytes, N)
1007962
bad_strs = _fuzz_test(String(rand(UInt8, nbytes)) for _ in 1:N)
1008-
rand_reduce.(bad_strs)
963+
reduce_text.(bad_strs, parser_throws_exception)
1009964
end
1010965

1011966
"""

test/parse_packages.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@ end
2525

2626
base_tests_path = joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "test")
2727
@testset "Parse Base tests at $base_tests_path" begin
28-
for f in find_source_in_path(base_tests_path)
29-
@testset "Parse $(relpath(f, base_tests_path))" begin
30-
# In julia-1.6, test/copy.jl had spurious syntax which became the
31-
# multidimensional array syntax in 1.7.
32-
endswith(f, "copy.jl") && v"1.6" <= VERSION < v"1.7" && continue
33-
34-
# syntax.jl has some intentially weird syntax which we parse
35-
# differently than the flisp parser, and some cases which we've
36-
# decided are syntax errors.
37-
endswith(f, "syntax.jl") && continue
28+
test_parse_all_in_path(base_tests_path) do f
29+
# In julia-1.6, test/copy.jl had spurious syntax which became the
30+
# multidimensional array syntax in 1.7.
31+
if endswith(f, "copy.jl") && v"1.6" <= VERSION < v"1.7"
32+
return false
33+
end
3834

39-
@test parsers_agree_on_file(f)
40-
# TODO:
41-
# exprs_equal = endswith(f, "syntax.jl") ?
42-
# exprs_roughly_equal : exprs_equal_no_linenum
43-
# @test parsers_agree_on_file(f; exprs_equal=exprs_equal)
35+
# syntax.jl has some intentially weird syntax which we parse
36+
# differently than the flisp parser, and some cases which we've
37+
# decided are syntax errors.
38+
if endswith(f, "syntax.jl")
39+
return false
4440
end
41+
42+
return true
4543
end
4644
end
4745

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using JuliaSyntax: GreenNode, SyntaxNode,
88
children, child, setchild!, SyntaxHead
99

1010
include("test_utils.jl")
11+
include("fuzz_test.jl")
1112

1213
# Tests for the test_utils go here to allow the utils to be included on their
1314
# own without invoking the tests.

0 commit comments

Comments
 (0)