From 03d38dc75d27dfa6f4a5feb06bd681178c78627a Mon Sep 17 00:00:00 2001 From: Christopher Tessum Date: Sat, 23 Jul 2022 13:49:35 -0500 Subject: [PATCH] Allow markdown strings in code replacement --- Project.toml | 7 ++++--- src/Grader.jl | 3 ++- test/runtests.jl | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 9340a2d..7f165fe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Grader" uuid = "d440d0cf-a03a-4c3f-a45e-240b6b8fa1ca" authors = ["Christopher Tessum and contributors"] -version = "0.3.0" +version = "0.3.1" [deps] Espresso = "6912e4f1-e036-58b0-9138-08d1e6358ea9" @@ -16,9 +16,10 @@ Parameters = "0.12" julia = "1" [extras] +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" [targets] -test = ["Test", "Plots", "LinearAlgebra"] +test = ["Test", "Plots", "LinearAlgebra", "Markdown"] diff --git a/src/Grader.jl b/src/Grader.jl index a117884..ebbdeb2 100644 --- a/src/Grader.jl +++ b/src/Grader.jl @@ -186,7 +186,8 @@ function fill_answers(code::AbstractString, answerkey::Dict)::String expr = Espresso.subs(expr, answerkey) io = IOBuffer() print(io, expr) - String(take!(io)) + s = String(take!(io)) + replace(replace(s, r"end$"=>""), r"^begin"=>"") # remove begin and end added above end """ diff --git a/test/runtests.jl b/test/runtests.jl index cea1bc3..1f23ff2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -250,6 +250,24 @@ using Test @test p.score ≈ 1.0 end + @testset "fill answers 2" begin + code = """ + using LinearAlgebra + using Markdown + n = missing + md\"\"\"Testing if markdown strings are okay.\"\"\" + """ + + answer_code = fill_answers(code, Dict( + :(n = missing) => :(n = LinearAlgebra.norm([1, 2, 3])) + )) + + p = Problem() + answer = @runstudent! p answer_code + grade!(p, "norm", "calculate norm", 1, :($answer.n ≈ 3.7416573867739413), "norm is incorrect") + @test p.score ≈ 1.0 + end + @testset "plot" begin using LinearAlgebra @@ -274,7 +292,6 @@ using Test grade!(p, "XY Plot", "Y values", 3, quote ynorm = $norm($student.xy[1][1][:y]) - println(ynorm) ynorm ≈ 159.16343801262903 end, "The Y values are not correct")