2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/json/decoding"
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Journey
|
|
|
|
module GTG
|
2012-12-31 12:36:30 -05:00
|
|
|
class TestGeneralizedTable < ActiveSupport::TestCase
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_to_json
|
|
|
|
table = tt %w{
|
|
|
|
/articles(.:format)
|
|
|
|
/articles/new(.:format)
|
|
|
|
/articles/:id/edit(.:format)
|
|
|
|
/articles/:id(.:format)
|
|
|
|
}
|
|
|
|
|
2013-11-05 23:05:58 -05:00
|
|
|
json = ActiveSupport::JSON.decode table.to_json
|
2016-08-06 12:54:50 -04:00
|
|
|
assert json["regexp_states"]
|
|
|
|
assert json["string_states"]
|
|
|
|
assert json["accepting"]
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2017-07-26 11:28:12 -04:00
|
|
|
if system("dot -V", 2 => File::NULL)
|
2012-12-19 15:54:47 -05:00
|
|
|
def test_to_svg
|
|
|
|
table = tt %w{
|
|
|
|
/articles(.:format)
|
|
|
|
/articles/new(.:format)
|
|
|
|
/articles/:id/edit(.:format)
|
|
|
|
/articles/:id(.:format)
|
|
|
|
}
|
|
|
|
svg = table.to_svg
|
|
|
|
assert svg
|
2012-12-28 18:49:41 -05:00
|
|
|
assert_no_match(/DOCTYPE/, svg)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_gt
|
2016-08-06 12:54:50 -04:00
|
|
|
sim = simulator_for ["/foo", "/bar"]
|
2017-05-22 13:09:57 -04:00
|
|
|
assert_match_route sim, "/foo"
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_gt_regexp
|
2016-08-06 12:54:50 -04:00
|
|
|
sim = simulator_for [":foo"]
|
2017-05-22 13:09:57 -04:00
|
|
|
assert_match_route sim, "foo"
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_gt_regexp_mix
|
2016-08-06 12:54:50 -04:00
|
|
|
sim = simulator_for ["/get", "/:method/foo"]
|
2017-05-22 13:09:57 -04:00
|
|
|
assert_match_route sim, "/get"
|
|
|
|
assert_match_route sim, "/get/foo"
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_optional
|
2016-08-06 12:54:50 -04:00
|
|
|
sim = simulator_for ["/foo(/bar)"]
|
2017-05-22 13:09:57 -04:00
|
|
|
assert_match_route sim, "/foo"
|
|
|
|
assert_match_route sim, "/foo/bar"
|
|
|
|
assert_no_match_route sim, "/foo/"
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_match_data
|
|
|
|
path_asts = asts %w{ /get /:method/foo }
|
|
|
|
paths = path_asts.dup
|
|
|
|
|
|
|
|
builder = GTG::Builder.new Nodes::Or.new path_asts
|
|
|
|
tt = builder.transition_table
|
|
|
|
|
|
|
|
sim = GTG::Simulator.new tt
|
|
|
|
|
2017-05-22 13:09:57 -04:00
|
|
|
memos = sim.memos "/get"
|
|
|
|
assert_equal [paths.first], memos
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2017-05-22 13:09:57 -04:00
|
|
|
memos = sim.memos "/get/foo"
|
|
|
|
assert_equal [paths.last], memos
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_match_data_ambiguous
|
|
|
|
path_asts = asts %w{
|
|
|
|
/articles(.:format)
|
|
|
|
/articles/new(.:format)
|
|
|
|
/articles/:id/edit(.:format)
|
|
|
|
/articles/:id(.:format)
|
|
|
|
}
|
|
|
|
|
|
|
|
paths = path_asts.dup
|
|
|
|
ast = Nodes::Or.new path_asts
|
|
|
|
|
|
|
|
builder = GTG::Builder.new ast
|
|
|
|
sim = GTG::Simulator.new builder.transition_table
|
|
|
|
|
2017-05-22 13:09:57 -04:00
|
|
|
memos = sim.memos "/articles/new"
|
|
|
|
assert_equal [paths[1], paths[3]], memos
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 14:20:22 -04:00
|
|
|
def asts(paths)
|
2016-10-28 23:05:58 -04:00
|
|
|
parser = Journey::Parser.new
|
2016-08-06 13:55:02 -04:00
|
|
|
paths.map { |x|
|
|
|
|
ast = parser.parse x
|
2016-08-16 03:30:11 -04:00
|
|
|
ast.each { |n| n.memo = ast }
|
2016-08-06 13:55:02 -04:00
|
|
|
ast
|
|
|
|
}
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-08-06 14:20:22 -04:00
|
|
|
def tt(paths)
|
2016-08-06 13:55:02 -04:00
|
|
|
x = asts paths
|
|
|
|
builder = GTG::Builder.new Nodes::Or.new x
|
|
|
|
builder.transition_table
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-08-06 14:20:22 -04:00
|
|
|
def simulator_for(paths)
|
2016-08-06 13:55:02 -04:00
|
|
|
GTG::Simulator.new tt(paths)
|
|
|
|
end
|
2017-05-22 13:09:57 -04:00
|
|
|
|
|
|
|
def assert_match_route(simulator, path)
|
|
|
|
assert simulator.memos(path), "Simulator should match #{path}."
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_match_route(simulator, path)
|
|
|
|
assert_not simulator.memos(path) { nil }, "Simulator should not match #{path}."
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|