Skip to content

Commit

Permalink
reorganise tests, dropping Mocking.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Mar 17, 2019
1 parent d65f612 commit 15f71b6
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 155 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["Lyndon White <[email protected]>"]
Cassette = "7057c7e9-c182-5462-911a-8362d720325c"
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Mocking = "78c3b35d-d492-501b-9361-3d52fe80e533"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

Expand Down
1 change: 0 additions & 1 deletion src/MagneticReadHead.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module MagneticReadHead
using Base: invokelatest
using Cassette
using MacroTools
using Mocking
using OrderedCollections

using CodeTracking
Expand Down
2 changes: 1 addition & 1 deletion src/break_action.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ end
breakpoint_hit(meth, statement_ind) = nothing

function iron_repl(metadata::HandEvalMeta, meth, statement_ind)
@mock breakpoint_hit(meth, statement_ind)
breakpoint_hit(meth, statement_ind)
breadcrumbs(meth, statement_ind)

printstyled("Vars: "; color=:light_yellow)
Expand Down
2 changes: 1 addition & 1 deletion src/inner_repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function get_user_input(io=stdin)
while true
# Very cut down REPL input code
# https://github.com/JuliaLang/julia/blob/b8c0ec8a0a2d12533edea72749b37e6089a9d163/stdlib/REPL/src/REPL.jl#L237
line *= @mock better_readline(io)
line *= better_readline(io)
ast = Base.parse_input_line(line)
ast isa Expr && ast.head == :incomplete || break
end
Expand Down
4 changes: 2 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function better_readline(stream = stdin)
end
if Sys.iswindows()
# Apparently in windows it is already pretty nice
return @mock readline(stream)
return readline(stream)
else
#TODO: Put in raw mode, drop control characters etc
return @mock readline(stream)
return readline(stream)
end
end
21 changes: 0 additions & 21 deletions test/run_non_ui_tests.jl

This file was deleted.

137 changes: 19 additions & 118 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,121 +1,22 @@
#==
Note to test implementers:
- For _reasons_, all user interaction test must go in this file
- They can't be `include`ed AFIACT, or the mocks miss some-how.
- All other tests should go in `run_non_ui_tests.jl`
which is included in bottom of this file.
In this file of User Interaction Tests:
- All tests go in their own module so that breakpoints can't leak
- They can not leak because the examples functions we break on are
reincluded anew in each module, thus giving them distinct identity.
- Do not set breakpoiints inside the apply do block. This is an anon function.
Which for some reason makes #256 style bugs more likely.
- The setup_ui_test_module.jl file defines things that each test module needs
==#

module CanHaveNoBreakpoints
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
p_readline = make_readline_patch([])
p_breadcrumbs, record = make_recording_breakpoint_hit_patch()

apply([p_readline, p_breadcrumbs]) do
@iron_debug eg1()
end
@test record == []
using MagneticReadHead
using Test

test_files = (
"test_ui.jl",
"test_behavour.jl",
"test_breadcrumbs.jl",
"test_breakpoint_rules.jl",
"test_inner_repl.jl",
"test_locate.jl",
"test_method_utils.jl",
"test_utils.jl",
"test_pass.jl",
"test_variable_capture.jl",
)

@testset "MagneticReadHead" begin
@testset "$file" for file in test_files
include(file)
end
end



module CanHave1Breakpoint
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
p_readline = make_readline_patch(["CC"])
p_breadcrumbs, record = make_recording_breakpoint_hit_patch()

set_breakpoint!(eg2)
apply([p_readline, p_breadcrumbs]) do
@iron_debug eg1()
end
@test first(record).f == eg2
end
end

module CanHave2Breakpoints
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
p_readline = make_readline_patch(["CC", "CC"])
p_breadcrumbs, record = make_recording_breakpoint_hit_patch()

set_breakpoint!(eg2)
set_breakpoint!(eg3)
apply([p_readline, p_breadcrumbs]) do
@iron_debug eg1()
end
@test first.(record) == [eg2, eg3]
end
end

#########################################################
# Stepping Mode

module CanHave1BreakpointThenStepInThenContinue
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
p_readline = make_readline_patch(["SI", "CC"])
p_breadcrumbs, record = make_recording_breakpoint_hit_patch()

set_breakpoint!(eg2)
apply([p_readline, p_breadcrumbs]) do
@iron_debug eg1()
end
@test first.(record) == [eg2, eg21]
end
end


###############################################
module CanInfluenceCallingEnviroment
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
global zzz = 10

patch = make_readline_patch(["zzz = 20", "CC"])

set_breakpoint!(eg2)
apply(patch) do
@iron_debug eg1()
end
@test zzz==20
end
end


module Abort
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
patch = make_readline_patch(["XX"])

set_breakpoint!(eg2)
apply(patch) do
@test nothing==@iron_debug eg1()
end
end
end


#########################################

println("\n**************************************************")
include("run_non_ui_tests.jl")
55 changes: 45 additions & 10 deletions test/setup_ui_test_module.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
using Mocking
Mocking.enable(force=true)

using Test
using MagneticReadHead
# Note: The patching used to be done with Mocking.jl
# However, that had some weird interactions between modules
# and includes
# So now the patching is done directly.
# Luckily we only have to patch some basic stuff intended only for this

"""
make_readline_patch(text_queue)
Patches `MagneticReadHead.better_readline(io)`, such that each time it is
called it will return succecutive elements from the `text_queue`.
And will error if it is called more than it has canned responses for.
"""
function make_readline_patch(text_queue)
text_state=nothing
return @patch function readline(io)
@eval MagneticReadHead text_state=nothing
@eval MagneticReadHead function better_readline(io)
global text_state
ret = (text_state==nothing ?
iterate(text_queue) :
iterate(text_queue, text_state)
iterate($text_queue) :
iterate($text_queue, text_state)
)
ret == nothing && error("Out of programmed responses")
text, text_state = ret
Expand All @@ -20,16 +29,42 @@ function make_readline_patch(text_queue)
end
end


"""
make_recording_breakpoint_hit_patch()
Patchs the `breakpoint_hit(meth, statement_ind)` method in MagneticReadHead
so that it will record what breakpoints were hit.
this function returns a reference to that record `Vector`.
"""
function make_recording_breakpoint_hit_patch()
record = []
patch = @patch function breakpoint_hit(meth, statement_ind)
push!(record,
@eval MagneticReadHead function breakpoint_hit(meth, statement_ind)
push!($record,
(f=MagneticReadHead.functiontypeof(meth).instance,
method=meth,
statement_ind=statement_ind)
)
end
return patch, record
return record
end

"""
reset_patched_functions()
Patchs `breakpoint_hit` and `better_readline` back to their original state.
"""
function reset_patched_functions!()
@eval MagneticReadHead function breakpoint_hit(meth, statement_ind)
return nothing
end

@eval MagneticReadHead function better_readline(io)
# This is not technically the same as the better_readline defined in utils
# but we will only be doing automated tests so that doesn't matter.
return readline(io)
end
end


include(joinpath(@__DIR__, "demo.jl"))
96 changes: 96 additions & 0 deletions test/test_ui.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#==
In this file of User Interaction Tests:
- All tests go in their own module so that breakpoints can't leak
- They can not leak because the examples functions we break on are
reincluded anew in each module, thus giving them distinct identity.
- The setup_ui_test_module.jl file defines things that each test module needs
==#

module CanHaveNoBreakpoints
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
make_readline_patch([])
record = make_recording_breakpoint_hit_patch()

@iron_debug eg1()
@test record == []
end
end

module CanHave1Breakpoint
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
make_readline_patch(["CC"])
record = make_recording_breakpoint_hit_patch()

set_breakpoint!(eg2)
@iron_debug eg1()
@test first(record).f == eg2
end
end

module CanHave2Breakpoints
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
make_readline_patch(["CC", "CC"])
record = make_recording_breakpoint_hit_patch()

set_breakpoint!(eg2)
set_breakpoint!(eg3)
@iron_debug eg1()
@test first.(record) == [eg2, eg3]
end
end

#########################################################
# Stepping Mode

module CanHave1BreakpointThenStepInThenContinue
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
make_readline_patch(["SI", "CC"])
record = make_recording_breakpoint_hit_patch()

set_breakpoint!(eg2)
@iron_debug eg1()
@test first.(record) == [eg2, eg21]
end
end


###############################################
module CanInfluenceCallingEnviroment
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
global zzz = 10
make_readline_patch(["zzz = 20", "CC"])

set_breakpoint!(eg2)
@iron_debug eg1()
@test zzz==20
end
end


module Abort
include("setup_ui_test_module.jl")

@testset "$(@__MODULE__)" begin
make_readline_patch(["XX"])

set_breakpoint!(eg2)
@test nothing==@iron_debug eg1()
end
end


include("setup_ui_test_module.jl")
reset_patched_functions!()
println("\tUser interaction tests complete")

0 comments on commit 15f71b6

Please sign in to comment.