2012-12-19 15:54:47 -05:00
|
|
|
require 'abstract_unit'
|
2013-11-05 23:05:58 -05:00
|
|
|
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
|
2012-12-19 15:54:47 -05:00
|
|
|
assert json['regexp_states']
|
|
|
|
assert json['string_states']
|
|
|
|
assert json['accepting']
|
|
|
|
end
|
|
|
|
|
|
|
|
if system("dot -V 2>/dev/null")
|
|
|
|
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
|
|
|
|
sim = simulator_for ['/foo', '/bar']
|
|
|
|
assert_match sim, '/foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_gt_regexp
|
|
|
|
sim = simulator_for [':foo']
|
|
|
|
assert_match sim, 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_gt_regexp_mix
|
|
|
|
sim = simulator_for ['/get', '/:method/foo']
|
|
|
|
assert_match sim, '/get'
|
|
|
|
assert_match sim, '/get/foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simulate_optional
|
|
|
|
sim = simulator_for ['/foo(/bar)']
|
|
|
|
assert_match sim, '/foo'
|
|
|
|
assert_match sim, '/foo/bar'
|
2012-12-28 18:49:41 -05:00
|
|
|
assert_no_match 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
|
|
|
|
|
|
|
|
match = sim.match '/get'
|
|
|
|
assert_equal [paths.first], match.memos
|
|
|
|
|
|
|
|
match = sim.match '/get/foo'
|
|
|
|
assert_equal [paths.last], match.memos
|
|
|
|
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
|
|
|
|
|
|
|
|
match = sim.match '/articles/new'
|
|
|
|
assert_equal [paths[1], paths[3]], match.memos
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def asts paths
|
|
|
|
parser = Journey::Parser.new
|
|
|
|
paths.map { |x|
|
|
|
|
ast = parser.parse x
|
|
|
|
ast.each { |n| n.memo = ast}
|
|
|
|
ast
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def tt paths
|
|
|
|
x = asts paths
|
|
|
|
builder = GTG::Builder.new Nodes::Or.new x
|
|
|
|
builder.transition_table
|
|
|
|
end
|
|
|
|
|
|
|
|
def simulator_for paths
|
|
|
|
GTG::Simulator.new tt(paths)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|