Sha256: e4bf11b93a60ee6643166e2be4f4806ce717952ebc2a14b0fa53842e083e1df7

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require_relative "spec_helper"

module Nyara
  describe Ext, "route" do
    before :each do
      Ext.clear_route
      @e1 = Route.new{
        @http_method = 'GET'
        @scope = '/hello'
        @prefix = '/hello/'
        @suffix = '(\d+)world'
        @id = :'#1'
        @conv = [:to_i]
        @controller = 'stub'
      }
      @e2 = Route.new{
        @http_method = 'GET'
        @scope = '/hello'
        @prefix = '/hello'
        @suffix = ''
        @id = :'#second'
        @conv = []
        @controller = 'stub2'
      }
      @e3 = Route.new{
        @http_method = 'GET'
        @scope = '/a目录'
        @prefix = '/a目录/'
        @suffix = '(\d+)-(\d+)-(\d+)'
        @id = :'#dir'
        @conv = [:to_i, :to_i, :to_i]
        @controller = 'stub3'
      }
      Ext.register_route @e1
      Ext.register_route @e2
      Ext.register_route @e3
    end

    after :all do
      Ext.clear_route
    end

    it '#register_route sub-prefix optimization' do
      rules = Ext.list_route['GET']
      assert_equal 3, rules.size

      assert_equal false, rules[0].first # first
      assert_equal true, rules[1].first  # is sub of prev
      assert_equal false, rules[2].first # not sub of prev
    end

    it '#lookup_route' do
      scope, cont, args = Ext.lookup_route 'GET', '/hello', nil
      assert_equal @e2.scope, scope
      assert_equal @e2.controller, cont
      assert_equal [:'#second'], args

      scope, cont, args = Ext.lookup_route 'GET', '/hello/3world', nil
      assert_equal @e1.scope, scope
      assert_equal @e1.controller, cont
      assert_equal [:'#1', 3], args

      scope, _ = Ext.lookup_route 'GET', '/world', nil
      assert_equal nil, scope

      scope, _, args = Ext.lookup_route 'GET', '/a目录/2013-6-1', nil
      assert_equal [:'#dir', 2013, 6, 1], args
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nyara-0.1.pre.0 spec/ext_route_spec.rb