File: test_route.rb

package info (click to toggle)
ruby-journey 1.0.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 288 kB
  • sloc: ruby: 2,830; javascript: 113; yacc: 42; makefile: 2
file content (109 lines) | stat: -rw-r--r-- 3,584 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'helper'

module Journey
  class TestRoute < MiniTest::Unit::TestCase
    def test_initialize
      app      = Object.new
      path     = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
      defaults = Object.new
      route    = Route.new("name", app, path, {}, defaults)

      assert_equal app, route.app
      assert_equal path, route.path
      assert_equal defaults, route.defaults
    end

    def test_route_adds_itself_as_memo
      app      = Object.new
      path     = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
      defaults = Object.new
      route    = Route.new("name", app, path, {}, defaults)

      route.ast.grep(Nodes::Terminal).each do |node|
        assert_equal route, node.memo
      end
    end

    def test_ip_address
      path  = Path::Pattern.new '/messages/:id(.:format)'
      route = Route.new("name", nil, path, {:ip => '192.168.1.1'},
                        { :controller => 'foo', :action => 'bar' })
      assert_equal '192.168.1.1', route.ip
    end

    def test_default_ip
      path  = Path::Pattern.new '/messages/:id(.:format)'
      route = Route.new("name", nil, path, {},
                        { :controller => 'foo', :action => 'bar' })
      assert_equal(//, route.ip)
    end

    def test_format_empty
      path  = Path::Pattern.new '/messages/:id(.:format)'
      route = Route.new("name", nil, path, {},
                        { :controller => 'foo', :action => 'bar' })

      assert_equal '/messages', route.format({})
    end

    def test_format_with_star
      path  = Path::Pattern.new '/:controller/*extra'
      route = Route.new("name", nil, path, {},
                        { :controller => 'foo', :action => 'bar' })
      assert_equal '/foo/himom', route.format({
        :controller => 'foo',
        :extra      => 'himom',
      })
    end

    def test_connects_all_match
      path  = Path::Pattern.new '/:controller(/:action(/:id(.:format)))'
      route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' })

      assert_equal '/foo/bar/10', route.format({
        :controller => 'foo',
        :action     => 'bar',
        :id         => 10
      })
    end

    def test_extras_are_not_included_if_optional
      path  = Path::Pattern.new '/page/:id(/:action)'
      route = Route.new("name", nil, path, { }, { :action => 'show' })

      assert_equal '/page/10', route.format({ :id => 10 })
    end

    def test_extras_are_not_included_if_optional_with_parameter
      path  = Path::Pattern.new '(/sections/:section)/pages/:id'
      route = Route.new("name", nil, path, { }, { :action => 'show' })

      assert_equal '/pages/10', route.format({:id => 10})
    end

    def test_extras_are_not_included_if_optional_parameter_is_nil
      path  = Path::Pattern.new '(/sections/:section)/pages/:id'
      route = Route.new("name", nil, path, { }, { :action => 'show' })

      assert_equal '/pages/10', route.format({:id => 10, :section => nil})
    end

    def test_score
      path = Path::Pattern.new "/page/:id(/:action)(.:format)"
      specific = Route.new "name", nil, path, {}, {:controller=>"pages", :action=>"show"}

      path = Path::Pattern.new "/:controller(/:action(/:id))(.:format)"
      generic = Route.new "name", nil, path, {}

      knowledge = {:id=>20, :controller=>"pages", :action=>"show"}

      routes = [specific, generic]

      refute_equal specific.score(knowledge), generic.score(knowledge)

      found = routes.sort_by { |r| r.score(knowledge) }.last

      assert_equal specific, found
    end
  end
end