Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TestPicker"
uuid = "a64165b9-4409-4de6-85cd-a4e0953bae44"
authors = ["theogf <[email protected]> and contributors"]
version = "1.3.0"
version = "2.0.0"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
4 changes: 4 additions & 0 deletions src/TestPicker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ using TestEnv
using TestEnv: TestEnvError, get_test_dir, isinstalled!

export clear_testenv_cache
export TestBlockInfo
export TestBlockInterface, add_interface!, replace_interface!
export TestItemInterface, add_testitem_interface!

"""
TestInfo
Expand Down Expand Up @@ -153,6 +155,8 @@ Similar to `add_interface!` but empty the interface first before adding the new
"""
replace_interface!(interface::TestBlockInterface) = push!(empty!(INTERFACES), interface)

add_testitem_interface!() = add_interface!(TestItemInterface())

function __init__()
# Add the REPL mode to the current active REPL.
if isdefined(Base, :active_repl)
Expand Down
28 changes: 3 additions & 25 deletions src/testblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,6 @@ function get_matching_files(
)
end

"""
TestBlockInfo

Metadata container for a test block, including its location and identification information.

Stores essential information about a test block's location within a file and provides
a label for identification and display purposes.

# Fields
- `label::String`: Human-readable label for the test block (e.g., test set name)
- `file_name::String`: Name of the file containing the test block
- `line_start::Int`: Starting line number of the test block (1-indexed)
- `line_end::Int`: Ending line number of the test block (1-indexed)
"""
struct TestBlockInfo
label::String
file_name::String
line_start::Int
line_end::Int
end

label(info::TestBlockInfo) = info.label
file_name(info::TestBlockInfo) = info.file_name

"""
build_info_to_syntax(interfaces, root, matched_files) -> (Dict{TestBlockInfo,SyntaxBlock}, Dict{String,TestBlockInfo})

Expand Down Expand Up @@ -294,7 +270,9 @@ function testblock_list(
(; label, file_name, line_start) = blockinfo
test_info = TestInfo(file_name, label, line_start)
(; preamble, testblock, interface) = syntax_block
block_expr = expr_transform(interface, Expr(testblock))
block_expr = expr_transform(
interface, Expr(testblock), blockinfo, get_test_dir_from_pkg(pkg)
)
tried_testset = quote
try
$(block_expr)
Expand Down
67 changes: 63 additions & 4 deletions src/testblockinterface.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
"""
TestBlockInfo

Metadata container for a test block, including its location and identification information.

Stores essential information about a test block's location within a file and provides
a label for identification and display purposes.

# Fields
- `label::String`: Human-readable label for the test block (e.g., test set name)
- `file_name::String`: Name of the file containing the test block
- `line_start::Int`: Starting line number of the test block (1-indexed)
- `line_end::Int`: Ending line number of the test block (1-indexed)
"""
struct TestBlockInfo
label::String
file_name::String
line_start::Int
line_end::Int
end

label(info::TestBlockInfo) = info.label
file_name(info::TestBlockInfo) = info.file_name

"""
TestBlockInterface

Expand Down Expand Up @@ -184,6 +208,8 @@ modifying test behavior, or adapting different test formats.
# Arguments
- `interface::TestBlockInterface`: The test block interface implementation
- `ex::Expr`: The test block expression to transform
- `info::TestBlockInfo` a bunch of metadata about the block that can be used to modify the expression
- `root::AbstractString` the root directory of the test folder.

# Returns
- `Expr`: The transformed expression ready for evaluation
Expand All @@ -194,7 +220,7 @@ The default implementation returns the expression unchanged (identity transforma
# Examples
```julia
# Add timing information to test blocks:
function expr_transform(::TimedTestInterface, ex::Expr)
function expr_transform(::TimedTestInterface, ex::Expr, ::TestBlockInfo, ::AbstractString)
return quote
start_time = time()
result = \$ex
Expand All @@ -205,7 +231,7 @@ function expr_transform(::TimedTestInterface, ex::Expr)
end

# Wrap tests in additional error handling:
function expr_transform(::SafeTestInterface, ex::Expr)
function expr_transform(::SafeTestInterface, ex::Expr, ::TestBlockInfo, ::AbstractString)
return quote
try
\$ex
Expand All @@ -217,7 +243,7 @@ function expr_transform(::SafeTestInterface, ex::Expr)
end
```
"""
function expr_transform(::TestBlockInterface, ex::Expr)
function expr_transform(::TestBlockInterface, ex::Expr, ::TestBlockInfo, ::AbstractString)
return ex
end

Expand Down Expand Up @@ -261,9 +287,42 @@ function istestblock(::StdTestset, node::SyntaxNode)
end

function blocklabel(::StdTestset, node::SyntaxNode)
return sourcetext(JuliaSyntax.children(node)[2])
return sourcetext(only(JuliaSyntax.children(JuliaSyntax.children(node)[2])))
end

function preamble(::StdTestset)
return :(using Test)
end

struct TestItemInterface <: TestBlockInterface end

function istestblock(::TestItemInterface, node::SyntaxNode)
kind(node) == K"macrocall" || return false
nodes = JuliaSyntax.children(node)
isnothing(nodes) && return false
length(nodes) > 1 || return false
kind(first(nodes)) == K"MacroName" || return false
sourcetext(first(nodes)) == "testitem" || return false
# The second node needs to be descriptive `String`.
return kind(nodes[2]) == K"string"
end

function blocklabel(::TestItemInterface, node::SyntaxNode)
return sourcetext(only(JuliaSyntax.children(JuliaSyntax.children(node)[2])))
end

function preamble(::TestItemInterface)
return :(using TestItemRunner)
end

function expr_transform(
::TestItemInterface, ::Expr, (; label, file_name)::TestBlockInfo, root::AbstractString
)
return :(esc(
TestItemRunner.run_tests(
$(dirname(root));
filter=ti ->
(ti.name == $(label) && ti.filename == $(joinpath(root, file_name))),
),
))
end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
TerminalRegressionTests = "98bfdc55-cc95-5876-a49a-74609291cbe0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
12 changes: 9 additions & 3 deletions test/sandbox/test-b.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@testset "Challenge for JuliaSyntax" begin
a = true
@test a == [
"fooskjdaskjhdskdja", "basadkashdalsdhar", "badalsdhasudz", "adaskdasgdasdggasgdsdf"
]
# @test a == [
# "fooskjdaskjhdskdja", "basadkashdalsdhar", "badalsdhasudz", "adaskdasgdasdggasgdsdf"
# ]
end

@testitem "Foo" begin
a = true
@test a == true
@test_nowarn TestPicker.INTERFACES
end
Loading