File: ruby_spec.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (143 lines) | stat: -rw-r--r-- 4,379 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Ruby do
  let(:subject) { Rouge::Lexers::Ruby.new }

  describe 'lexing' do
    include Support::Lexing

    describe 'method calling' do
      describe 'leading dot' do
        it 'handles whitespace between the receiver and the method' do
          assert_tokens_equal "foo\n  .bar()",
            ['Name', 'foo'],
            ['Text', "\n  "],
            ['Punctuation', '.'],
            ['Name.Function', 'bar'],
            ['Punctuation', '()']
        end

        it 'handles whitespace between the receiver and multiple chained methods' do
          assert_tokens_equal "foo\n  .bar()\n  .baz",
            ['Name', 'foo'],
            ['Text', "\n  "],
            ['Punctuation', '.'],
            ['Name.Function', 'bar'],
            ['Punctuation', '()'],
            ['Text', "\n  "],
            ['Punctuation', '.'],
            ['Name.Function', 'baz']
        end
      end

      describe 'trailing dot' do
        it 'handles whitespace between the receiver and the method' do
          assert_tokens_equal "foo.\n  bar()",
            ['Name', 'foo'],
            ['Punctuation', '.'],
            ['Text', "\n  "],
            ['Name.Function', 'bar'],
            ['Punctuation', '()']
        end

        it 'handles whitespace between the receiver and multiple chained methods' do
          assert_tokens_equal "foo.\n  bar().\n  baz",
            ['Name', 'foo'],
            ['Punctuation', '.'],
            ['Text', "\n  "],
            ['Name.Function', 'bar'],
            ['Punctuation', '().'],
            ['Text', "\n  "],
            ['Name.Function', 'baz']
        end
      end
    end

    describe 'ranges' do
      it 'handles .. as range operator' do
        assert_tokens_equal "1..10",
          ['Literal.Number.Integer', '1'],
          ['Operator', '..'],
          ['Literal.Number.Integer', '10']
      end

      it 'handles ... as range operator' do
        assert_tokens_equal "'a'...'z'",
          ['Literal.String.Single', "'a'"],
          ['Operator', '...'],
          ['Literal.String.Single', "'z'"]
      end
    end

    describe 'numerics' do
      it 'distinguishes Float from Integer' do
        assert_tokens_equal "2.3 + 5",
          ['Literal.Number.Float', '2.3'],
          ['Text', ' '],
          ['Operator', '+'],
          ['Text', ' '],
          ['Literal.Number.Integer', '5']
      end

      it 'identifies Floats with exponent correctly' do
        assert_tokens_equal "12.3e4",
          ['Literal.Number.Float', '12.3e4']
        assert_tokens_equal "5.67e-9",
          ['Literal.Number.Float', '5.67e-9']
        assert_tokens_equal "20.4e+8",
          ['Literal.Number.Float', '20.4e+8']
      end
    end

    describe 'method definition' do
      it 'identifies comparable method' do
        assert_tokens_equal "def <=>(o); end",
          ["Keyword", "def"],
          ["Text", " "],
          ["Name.Function", "<=>"],
          ["Punctuation", "("],
          ["Name", "o"],
          ["Punctuation", ");"],
          ["Text", " "],
          ["Keyword", "end"]
      end
    end
  end

  describe 'guessing' do
    include Support::Guessing

    it 'guesses by filename' do
      assert_guess :filename => 'foo.rb'
      assert_guess :filename => 'foo.ruby'
      assert_guess :filename => 'foo.rbw'
      assert_guess :filename => 'foo.gemspec'
      assert_guess :filename => 'foo.podspec'
      assert_guess :filename => 'Rakefile'
      assert_guess :filename => 'Guardfile'
      assert_guess :filename => 'Gemfile'
      assert_guess :filename => 'foo.rake'
      assert_guess :filename => 'Capfile'
      assert_guess :filename => 'Podfile'
      assert_guess :filename => 'Vagrantfile'
      assert_guess :filename => 'config.ru'
      assert_guess :filename => 'foo.pdf.prawn'
      assert_guess :filename => 'Berksfile'
      assert_guess :filename => 'Deliverfile'
      assert_guess :filename => 'Fastfile'
      assert_guess :filename => 'Appfile'
      assert_guess :filename => 'Thorfile'
      assert_guess :filename => 'foo.thor'
    end

    it 'guesses by mimetype' do
      assert_guess :mimetype => 'text/x-ruby'
      assert_guess :mimetype => 'application/x-ruby'
    end

    it 'guesses by source' do
      assert_guess :source => '#!/usr/local/bin/ruby'
    end
  end
end