Skip to content

Commit 1f32e26

Browse files
committed
generate valid names for test modules
1 parent 396641d commit 1f32e26

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
## v0.5.0
55

66
- Now that `@__DIR__` is provided, do not `cd()` into the test directory.
7-
- Allow running tests in the context of a specific module.
7+
- Added an option to run tests in the context of a specific module.
88
- Added an option to suppress any output except for error reports.
9+
- Generate valid names for test modules.
910

1011

1112
## v0.4.1

src/NarrativeTest.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,11 @@ function runtest(test::Test; subs=common_subs(), mod=nothing)
518518
no_output = endswith(test.code.val, ";\n") || test.expect === nothing
519519
# Generate a module object for running the test code.
520520
if mod === nothing
521-
modid = Base.PkgId(test.loc.file)
521+
modid = Base.PkgId(module_name(test.loc.file))
522522
if Base.root_module_exists(modid)
523523
mod = Base.root_module(modid)
524524
else
525-
mod = Module(Symbol(test.loc.file))
525+
mod = Module(Symbol(modid.name))
526526
@eval mod begin
527527
eval(x) = Core.eval($mod, x)
528528
include(p) = Base.include($mod, p)
@@ -625,4 +625,11 @@ function expect2regex(pattern, subs)
625625
return Regex(pattern)
626626
end
627627

628+
# Generate a module name from the filename.
629+
630+
function module_name(file)
631+
file = relpath(abspath(file), abspath(dirname(PROGRAM_FILE)))
632+
return join(titlecase.(split(file, r"[^0-9A-Za-z]+")))
633+
end
634+
628635
end

test/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,15 +478,15 @@ Functions `include` and `eval` are available in the test code.
478478
Macros `@__MODULE__`, `@__DIR__`, `@__FILE__`, `@__LINE__` properly report the
479479
location of the test code.
480480

481-
result = runtest(@__FILE__, "println(@__MODULE__)", expect=@__FILE__)
481+
result = runtest(@__FILE__, "println(@__MODULE__)", expect=string(@__MODULE__))
482482
display(result)
483483
#=>
484484
Test passed at /…/index.md:
485485
println(@__MODULE__)
486486
Expected output:
487-
/…/index.md
487+
IndexMd
488488
Actual output:
489-
/…/index.md
489+
IndexMd
490490
=#
491491

492492
result = runtest(@__FILE__, "println(@__DIR__)", expect=@__DIR__)

0 commit comments

Comments
 (0)