|
| 1 | +using JuliaSyntax: ParseStream, @K_str, build_tree, bump_trivia, kind, parse!, peek_full_token, peek_token |
| 2 | +using StringViews |
| 3 | + |
| 4 | +function include_test_file(ti_filter, path::String) |
| 5 | + bytes = read(path) |
| 6 | + stream = ParseStream(bytes) |
| 7 | + tls = task_local_storage() |
| 8 | + tls[:SOURCE_PATH] = path # This is also done by Base.include |
| 9 | + try |
| 10 | + while true |
| 11 | + bump_trivia(stream, skip_newlines=true) |
| 12 | + t = peek_token(stream, 1) |
| 13 | + k = kind(t) |
| 14 | + k == K"EndMarker" && break |
| 15 | + if k == K"@" |
| 16 | + tf = peek_full_token(stream, 2) |
| 17 | + v = @inbounds @view(bytes[tf.first_byte:tf.last_byte]) |
| 18 | + if v == b"testitem" ; _eval_from_stream(stream, path, ti_filter, bytes) |
| 19 | + elseif v == b"testsetup"; _eval_from_stream(stream, path) |
| 20 | + elseif v == b"test_rel" ; _eval_from_stream(stream, path, ti_filter) |
| 21 | + else |
| 22 | + error("Test files must only include `@testitem` and `@testsetup` calls, got an `@$(StringView(v))` at $(path)") # TODO |
| 23 | + end |
| 24 | + else |
| 25 | + error("Test files must only include `@testitem` and `@testsetup` calls, got a $t at $(path)") # TODO |
| 26 | + end |
| 27 | + empty!(stream) |
| 28 | + end |
| 29 | + finally |
| 30 | + delete!(tls, :SOURCE_PATH) |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +_contains(s::AbstractString, pattern::Regex) = occursin(pattern, s) |
| 35 | +_contains(s::AbstractString, pattern::AbstractString) = s == pattern |
| 36 | + |
| 37 | +# unconditionally eval |
| 38 | +function _eval_from_stream(stream, path) |
| 39 | + parse!(stream; rule=:statement) |
| 40 | + ast = build_tree(Expr, stream; filename=path) |
| 41 | + Core.eval(Main, ast) |
| 42 | + return nothing |
| 43 | +end |
| 44 | + |
| 45 | +# test_rel -> apply ti_filter on the parsed ast |
| 46 | +function _eval_from_stream(stream, path, ti_filter) |
| 47 | + parse!(stream; rule=:statement) |
| 48 | + ast = build_tree(Expr, stream; filename=path) |
| 49 | + filtered = ti_filter(ast) |
| 50 | + filtered === nothing || Core.eval(Main, filtered) |
| 51 | + return nothing |
| 52 | +end |
| 53 | + |
| 54 | +# like above, but tries to avoid parsing the ast if it sees from the name identifier token |
| 55 | +# it won't pass the filter |
| 56 | +function _eval_from_stream(stream, path, ti_filter, bytes) |
| 57 | + if ti_filter.name isa Nothing |
| 58 | + parse!(stream; rule=:statement) |
| 59 | + ast = build_tree(Expr, stream; filename=path) |
| 60 | + filtered = ti_filter(ast) |
| 61 | + filtered === nothing || Core.eval(Main, filtered) |
| 62 | + return nothing |
| 63 | + end |
| 64 | + |
| 65 | + name_t = peek_full_token(stream, 4) # 3 was '\"' |
| 66 | + name = @inbounds StringView(@view(bytes[name_t.first_byte:name_t.last_byte])) |
| 67 | + parse!(stream; rule=:statement) |
| 68 | + if _contains(name, ti_filter.name) |
| 69 | + ast = build_tree(Expr, stream; filename=path) |
| 70 | + filtered = ti_filter(ast) |
| 71 | + filtered === nothing || Core.eval(Main, filtered) |
| 72 | + end |
| 73 | + return nothing |
| 74 | +end |
0 commit comments