fixed utterly broken tests

This commit is contained in:
Josh Hull 2011-07-27 19:55:29 -07:00
parent 08bfa9cbc7
commit 0d94d070ff
12 changed files with 78 additions and 45 deletions

View File

@ -25,12 +25,10 @@ task :release_js do
end
test_tasks = ['test:generation', 'test:recognition', 'test:integration', 'test:examples', 'test:rdoc_examples']
test_tasks << 'test:js' if `which coffee && which node` && $?.success?
#test_tasks << 'test:js' if `which coffee && which node` && $?.success?
desc "Run all tests"
task :test => test_tasks
require 'pp'
desc "Clean things"
task :clean do
sh 'find . -name "*.rbc" | xargs rm'

View File

@ -13,7 +13,7 @@ class HttpRouter
autoload :Lookup, 'http_router/node/lookup'
autoload :Path, 'http_router/node/path'
attr_reader :priority, :router, :node_position, :parent
attr_reader :router
def initialize(router, parent, matchers = [])
@router, @parent, @matchers = router, parent, matchers
@ -86,12 +86,6 @@ class HttpRouter
@router.root
end
def depth
d, p = 0, @parent
d, p = d + 1, p.parent until p.nil?
d
end
def use_named_captures?
//.respond_to?(:names)
end

View File

@ -8,8 +8,9 @@ class HttpRouter
end
def to_code
"whole_path#{depth} = \"/\#{request.joined_path}\"
if match = #{matcher.inspect}.match(whole_path#{depth}) and match[0].size == whole_path#{depth}.size
id = root.next_counter
"whole_path#{id} = \"/\#{request.joined_path}\"
if match = #{matcher.inspect}.match(whole_path#{id}) and match[0].size == whole_path#{id}.size
request.extra_env['router.regex_match'] = match
old_path = request.path
request.path = ['']

View File

@ -7,12 +7,13 @@ class HttpRouter
end
def to_code
"request.params << (globbed_params#{depth} = [])
id = root.next_counter
"request.params << (globbed_params#{id} = [])
until request.path.empty?
globbed_params#{depth} << request.path.shift
globbed_params#{id} << request.path.shift
#{super}
end
request.path[0,0] = globbed_params#{depth}"
request.path[0,0] = globbed_params#{id}"
end
end
end

View File

@ -12,10 +12,11 @@ class HttpRouter
end
def to_code
"request.params << (globbed_params#{depth} = [])
id = root.next_counter
"request.params << (globbed_params#{id} = [])
remaining_parts = request.path.dup
while !remaining_parts.empty? and match = remaining_parts.first.match(#{@matcher.inspect}) and match[0] == remaining_parts.first
globbed_params#{depth} << remaining_parts.shift
globbed_params#{id} << remaining_parts.shift
request.path = remaining_parts
#{node_to_code}
end

View File

@ -3,14 +3,15 @@ class HttpRouter
class SpanningRegex < Regex
def to_code
params_count = @ordered_indicies.size
"whole_path#{depth} = request.joined_path
if match = #{@matcher.inspect}.match(whole_path#{depth}) and match.begin(0).zero?
original_path#{depth} = request.path.dup
whole_path_var = "whole_path#{root.next_counter}"
"#{whole_path_var} = request.joined_path
if match = #{@matcher.inspect}.match(#{whole_path_var}) and match.begin(0).zero?
_#{whole_path_var} = request.path.dup
" << param_capturing_code << "
remaining_path = whole_path#{depth}[match[0].size + (whole_path#{depth}[match[0].size] == ?/ ? 1 : 0), whole_path#{depth}.size]
remaining_path = #{whole_path_var}[match[0].size + (#{whole_path_var}[match[0].size] == ?/ ? 1 : 0), #{whole_path_var}.size]
request.path = remaining_path.split('/')
#{node_to_code}
request.path = original_path#{depth}
request.path = _#{whole_path_var}
request.params.slice!(#{-params_count.size}, #{params_count})
end
"

View File

@ -6,10 +6,10 @@ class HttpRouter
def initialize(router, path, opts = {})
@router, @original_path, @opts = router, path, opts
if @original_path && @original_path[-1] == ?*
@match_partially = true
path.slice!(-1)
elsif @original_path.nil?
if @original_path
@match_partially = true and path.slice!(-1) if @original_path[-1] == ?*
@original_path[0, 0] = '/' if @original_path[0] != ?/
else
@match_partially = true
end
process_opts

View File

@ -14,6 +14,7 @@ class HttpRouter
else
regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s unless path_validation_regex
code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
dynamic = true
end
else
regex << Regexp.quote(part) unless path_validation_regex
@ -25,11 +26,15 @@ class HttpRouter
target.instance_eval <<-EOT, __FILE__, __LINE__ + 1
def raw_url(args, options)
url = \"#{code}\"
#{"url !~ #{path_validation_regex.inspect} ? nil : " if @dynamic} url
#{path_validation_regex.inspect}.match(url) ? url : nil
end
EOT
else
target.instance_eval "def raw_url(args, options); \"#{code}\"; end", __FILE__, __LINE__
target.instance_eval <<-EOT, __FILE__, __LINE__ + 1
def raw_url(args, options)
\"#{code}\"
end
EOT
end
end
end

View File

@ -1,3 +1,7 @@
{"a": "/:var"}
["/test", "a", {"var":"test"}]
# ["/test", "a", ["test"]]
{"a": "/"}
{"b": "/test"}
{"c": "/test/time"}
@ -76,7 +80,8 @@
["/%C3%A4", "a", {"var": "ä"}]
{"a": {"path": ":var", "var": {"regex": "\\d+"}}}
[null, "a", "asd"]
[null, "a", "asd"]
["/123", "a", "123"]
{"a": "/var"}
["/var?foo%5B%5D=baz&foo%5B%5D=bar", "a", {"foo": ["baz", "bar"]}]

View File

@ -7,7 +7,7 @@ class GenerationTest < AbstractTest
args.compact!
args.map!{|a| a.is_a?(Hash) ? Hash[a.map{|k,v| [k.to_sym, v]}] : a }
result = begin
router.url(name.to_sym, *args)
@router.url(name.to_sym, *args)
rescue HttpRouter::InvalidRouteException
nil
rescue HttpRouter::MissingParameterException
@ -19,4 +19,4 @@ class GenerationTest < AbstractTest
end
end
GenerationTest.run("#{File.dirname(__FILE__)}/common/generate.txt")
GenerationTest.run("#{File.dirname(__FILE__)}/common/generate.txt")

View File

@ -79,13 +79,13 @@ class AbstractTest
def invoke
error("invoke called with no tests or routes") if @tests.empty? || @routes.nil?
router = HttpRouter.new
@router = HttpRouter.new
@routes.case.each do |route_definition|
error("Too many keys! #{route_definition.keys.inspect}") unless route_definition.keys.size == 1
route_name, route_properties = route_definition.keys.first, route_definition.values.first
route = case route_properties
when String
router.add(route_properties)
@router.add(route_properties)
when Hash
opts = {}
route_path = interpret_val(route_properties.delete("path"))
@ -98,13 +98,14 @@ class AbstractTest
route_properties.each do |key, val|
opts[key.to_sym] = interpret_val(val)
end
router.add(route_path, opts)
@router.add(route_path, opts)
else
error("Route isn't a String or hash")
end
route.name(route_name.to_sym)
route.to{|env| [200, {"env-to-test" => env.dup}, [route_name]]}
end
run_tests
print '.'
end
end

View File

@ -1,18 +1,44 @@
require "#{File.dirname(__FILE__)}/generic"
class RecognitionTest < AbstractTest
def run_tests
@tests.map(&:case).each do |(expected_result, name, args)|
args = [args] unless args.is_a?(Array)
args.compact!
args.map!{|a| a.is_a?(Hash) ? Hash[a.map{|k,v| [k.to_sym, v]}] : a }
result = begin
router.url(name.to_sym, *args)
rescue HttpRouter::InvalidRouteException
nil
rescue HttpRouter::MissingParameterException
nil
@tests.map(&:case).each do |(name, req, params)|
env = case req
when String
Rack::MockRequest.env_for(req)
when Hash
e = Rack::MockRequest.env_for(req['path'])
e['REQUEST_METHOD'] = req['method'] if req.key?('method')
e['rack.url_scheme'] = req['scheme'] if req.key?('scheme')
e
end
error("Result #{result.inspect} did not match expectation #{expected_result.inspect}") unless result == expected_result
response = @router.call(env)
case name
when nil
error("Expected no response") unless response.first == 404
when Array
name.each_with_index do |part, i|
case part
when Hash then part.keys.all? or error("#{part.inspect} didn't match #{response[i].inspect}")
else part == response[i] or error("#{part.inspect} didn't match #{response[i].inspect}")
end
end
else
error("Expected #{name} for #{req.inspect} got #{response.inspect}") unless response.last == [name]
end
env['router.params'] ||= {}
params ||= {}
if params['PATH_INFO']
path_info = params.delete("PATH_INFO")
error("path_info #{env['PATH_INFO'].inspect} is not #{path_info.inspect}") unless path_info == env['PATH_INFO']
end
env['router.params'].keys.each do |k|
p_v = params.delete(k.to_s)
v = env['router.params'].delete(k.to_sym)
error("I got #{p_v.inspect} but expected #{v.inspect}") unless p_v == v
end
error("Left over expectations: #{params.inspect}") unless params.empty?
error("Left over matched params: #{env['router.params'].inspect}") unless env['router.params'].empty?
end
print '.'
end