aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/journey/route/definition/scanner_test.rb
blob: e55ed92cc89f1136b54af8e10bc96ad33b1eec65 (plain) (blame)
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
# frozen_string_literal: true

require "abstract_unit"

module ActionDispatch
  module Journey
    module Definition
      class TestScanner < ActiveSupport::TestCase
        def setup
          @scanner = Scanner.new
        end

        CASES = [
          ["/",       [[:SLASH, "/"]]],
          ["*omg",    [[:STAR, "*omg"]]],
          ["/page",   [[:SLASH, "/"], [:LITERAL, "page"]]],
          ["/page!",  [[:SLASH, "/"], [:LITERAL, "page!"]]],
          ["/page$",  [[:SLASH, "/"], [:LITERAL, "page$"]]],
          ["/page&",  [[:SLASH, "/"], [:LITERAL, "page&"]]],
          ["/page'",  [[:SLASH, "/"], [:LITERAL, "page'"]]],
          ["/page*",  [[:SLASH, "/"], [:LITERAL, "page*"]]],
          ["/page+",  [[:SLASH, "/"], [:LITERAL, "page+"]]],
          ["/page,",  [[:SLASH, "/"], [:LITERAL, "page,"]]],
          ["/page;",  [[:SLASH, "/"], [:LITERAL, "page;"]]],
          ["/page=",  [[:SLASH, "/"], [:LITERAL, "page="]]],
          ["/page@",  [[:SLASH, "/"], [:LITERAL, "page@"]]],
          ['/page\:', [[:SLASH, "/"], [:LITERAL, "page:"]]],
          ['/page\(', [[:SLASH, "/"], [:LITERAL, "page("]]],
          ['/page\)', [[:SLASH, "/"], [:LITERAL, "page)"]]],
          ["/~page",  [[:SLASH, "/"], [:LITERAL, "~page"]]],
          ["/pa-ge",  [[:SLASH, "/"], [:LITERAL, "pa-ge"]]],
          ["/:page",  [[:SLASH, "/"], [:SYMBOL, ":page"]]],
          ["/:page|*foo", [
                            [:SLASH, "/"],
                            [:SYMBOL, ":page"],
                            [:OR, "|"],
                            [:STAR, "*foo"]
                          ]],
          ["/(:page)", [
                        [:SLASH, "/"],
                        [:LPAREN, "("],
                        [:SYMBOL, ":page"],
                        [:RPAREN, ")"],
                      ]],
          ["(/:action)", [
                          [:LPAREN, "("],
                          [:SLASH, "/"],
                          [:SYMBOL, ":action"],
                          [:RPAREN, ")"],
                         ]],
          ["(())", [[:LPAREN, "("],
                   [:LPAREN, "("], [:RPAREN, ")"], [:RPAREN, ")"]]],
          ["(.:format)", [
                          [:LPAREN, "("],
                          [:DOT, "."],
                          [:SYMBOL, ":format"],
                          [:RPAREN, ")"],
                        ]],
        ]

        CASES.each do |pattern, expected_tokens|
          test "Scanning `#{pattern}`" do
            @scanner.scan_setup pattern
            assert_tokens expected_tokens, @scanner, pattern
          end
        end

        private
          def assert_tokens(expected_tokens, scanner, pattern)
            actual_tokens = []
            while token = scanner.next_token
              actual_tokens << token
            end
            assert_equal expected_tokens, actual_tokens, "Wrong tokens for `#{pattern}`"
          end
      end
    end
  end
end