File: test_ruby_parser_extras.rb

package info (click to toggle)
ruby-ruby-parser 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,180 kB
  • sloc: ruby: 124,525; yacc: 35,033; makefile: 11
file content (261 lines) | stat: -rw-r--r-- 7,378 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# encoding: US-ASCII

require "minitest/autorun"
require "ruby_parser_extras"
require "ruby_parser"

class TestStackState < Minitest::Test
  attr_reader :s

  def setup
    @s = RubyParserStuff::StackState.new :test
  end

  def assert_encoding str, default = false
    orig_str = str.dup
    p = RubyParser.latest
    s = nil

    out, err = capture_io {
      s = p.handle_encoding str
    }

    assert_equal orig_str.sub(/\357\273\277/, ""), s

    exp_err = ""

    if defined?(Encoding) then
      assert_equal "UTF-8", s.encoding.to_s, str.inspect
    else
      exp_err = "Skipping magic encoding comment\n" unless default
    end

    assert_equal "", out, str.inspect
    assert_equal exp_err, err, str.inspect # HACK
  end

  def test_handle_encoding_bom
    # bom support, default to utf-8
    assert_encoding "\xEF\xBB\xBF# blah"
    # we force_encode to US-ASCII, then encode to UTF-8 so our lexer will work
    assert_encoding "\xEF\xBB\xBF# encoding: US-ASCII"
  end

  def test_handle_encoding_default
    assert_encoding "blah", :default
  end

  def test_handle_encoding_emacs
    # Q: how many different ways can we screw these up? A: ALL OF THEM

    assert_encoding "# - encoding: utf-8 -"
    assert_encoding "# - encoding:utf-8"
    assert_encoding "# -* coding: UTF-8 -*-"
    assert_encoding "# -*- coding: UTF-8 -*-"
    assert_encoding "# -*- coding: utf-8 -*"
    assert_encoding "# -*- coding: utf-8 -*-"
    assert_encoding "# -*- coding: utf-8; mode: ruby -*-"
    assert_encoding "# -*- coding: utf-8; mode: ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2"
    assert_encoding "# -*- coding:utf-8; mode:ruby; -*-"
    assert_encoding "# -*- encoding: UTF-8 -*-"
    assert_encoding "# -*- encoding: utf-8 -*"
    assert_encoding "# -*- encoding: utf-8 -*-"
    assert_encoding "# -*- mode:ruby; coding:utf-8 -*-"
    assert_encoding "# -*- ruby encoding: utf-8 -*-"
    assert_encoding "# -- encoding: utf-8 --"
    assert_encoding "# ~*~ encoding: utf-8 ~*~"
    assert_encoding "#-*- coding: utf-8 -*-"
    assert_encoding "#-*- coding:utf-8"
    assert_encoding "#--  -*- mode: ruby; encoding: utf-8 -*-\n"
  end

  def test_handle_encoding_wtf
    assert_encoding "# coding : utf-8"
    assert_encoding "# Ruby 1.9: encoding: utf-8"
    assert_encoding "# Encoding: UTF-8 <-- required, please leave this in."
    assert_encoding "# Encoding: UTF-8"
    assert_encoding "# coding: utf-8"
    assert_encoding "# coding:utf-8"
    assert_encoding "# coding=utf-8"
    assert_encoding "# encoding: ASCII"
    assert_encoding "# encoding: ASCII-8BIT"
    assert_encoding "# encoding: ISO-8859-1"
    assert_encoding "# encoding: UTF-8"
    assert_encoding "# encoding: ascii-8bit"
    assert_encoding "# encoding: cp1252"
    assert_encoding "# encoding: euc-jp -*-"
    assert_encoding "# encoding: utf-8 # -*- ruby -*-"
    assert_encoding "# encoding: utf-8 require 'github_api/utils/url'"
    assert_encoding "# encoding: utf-8!"
    assert_encoding "# encoding: utf-8"
    assert_encoding "#<Encoding:UTF-8>"
    assert_encoding "#Encoding: UTF-8"
    assert_encoding "#coding:utf-8"
    assert_encoding "#encoding: UTF-8!"
    assert_encoding "#encoding: UTF-8"
    assert_encoding "#encoding: cp1252"
    assert_encoding "#encoding: sjis"
    assert_encoding "#encoding: utf-8"
  end

  def test_handle_encoding_normal
    assert_encoding "# encoding: UTF-8"
    assert_encoding "# encoding: UTF-8\r\n" # UGH I hate windoze
    assert_encoding "# coding: UTF-8"
    assert_encoding "# encoding = UTF-8"
    assert_encoding "# coding = UTF-8"
  end

  def test_handle_encoding_vim
    assert_encoding "#  vim: set fileencoding=utf-8 filetype=ruby ts=2 : "
    assert_encoding "# vim: fileencoding=UTF-8 ft=ruby syn=ruby ts=2 sw=2 ai eol et si"
    assert_encoding "# vim: fileencoding=UTF-8 nobomb sw=2 ts=2 et"
    assert_encoding "# vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2"
    assert_encoding "# vim: set fileencoding=utf-8"
    assert_encoding "# vim:encoding=UTF-8:"
    assert_encoding "# vim:fileencoding=UTF-8:"
    assert_encoding "# vim:set fileencoding=utf-8 filetype=ruby"
    assert_encoding "# vim:set fileencoding=utf-8:"
  end

  def test_stack_state
    s.push true
    s.push false
    s.lexpop
    assert_equal [false, true], s.stack
  end

  def test_is_in_state
    assert_equal false, s.is_in_state
    s.push false
    assert_equal false, s.is_in_state
    s.push true
    assert_equal true, s.is_in_state
    s.push false
    assert_equal false, s.is_in_state
  end

  def test_lexpop
    assert_equal [false], s.stack
    s.push true
    s.push false
    assert_equal [false, true, false], s.stack
    s.lexpop
    assert_equal [false, true], s.stack
  end

  def test_pop
    assert_equal [false], s.stack
    s.push true
    assert_equal [false, true], s.stack
    assert_equal true, s.pop
    assert_equal [false], s.stack
  end

  def test_push
    assert_equal [false], s.stack
    s.push true
    s.push false
    assert_equal [false, true, false], s.stack
  end
end

class TestEnvironment < Minitest::Test
  def deny t
    assert !t
  end

  def setup
    @env = RubyParserStuff::Environment.new
    @env[:blah] = 42
    assert_equal 42, @env[:blah]
  end

  def test_var_scope_dynamic
    @env.extend :dynamic
    assert_equal 42, @env[:blah]
    @env.unextend
    assert_equal 42, @env[:blah]
  end

  def test_var_scope_static
    @env.extend
    assert_nil @env[:blah]
    @env.unextend
    assert_equal 42, @env[:blah]
  end

  def test_all_dynamic
    expected = { :blah => 42 }

    @env.extend :dynamic
    assert_equal expected, @env.all
    @env.unextend
    assert_equal expected, @env.all
  end

  def test_all_static
    @env.extend
    expected = { }
    assert_equal expected, @env.all

    @env.unextend
    expected = { :blah => 42 }
    assert_equal expected, @env.all
  end

  def test_all_static_deeper
    expected0 = { :blah => 42 }
    expected1 = { :blah => 42, :blah2 => 24 }
    expected2 = { :blah => 27 }

    @env.extend :dynamic
    @env[:blah2] = 24
    assert_equal expected1, @env.all

    @env.extend
    @env[:blah] = 27
    assert_equal expected2, @env.all

    @env.unextend
    assert_equal expected1, @env.all

    @env.unextend
    assert_equal expected0, @env.all
  end
end

class Fake20
  include RubyParserStuff

  def initialize
  end

  def s(*a) # bypass lexer/lineno stuff that RP overrides in
    Kernel.send :s, *a
  end
end

class TestValueExpr < Minitest::Test
  def assert_value_expr exp, input
    assert_equal exp, Fake20.new.value_expr(input.line(1))
  end

  def assert_remove_begin exp, input
    assert_equal exp, Fake20.new.remove_begin(input.line(1))
  end

  def test_value_expr
    assert_value_expr s(:nil),                     s(:begin)
    assert_value_expr s(:nil),                     s(:begin, s(:nil))
    assert_value_expr s(:nil),                     s(:begin, s(:begin, s(:nil)))
    assert_value_expr s(:begin, s(:nil), s(:nil)), s(:begin, s(:nil), s(:nil))
  end

  def test_remove_begin
    assert_remove_begin s(:nil),                     s(:begin)
    assert_remove_begin s(:nil),                     s(:begin, s(:nil))
    assert_remove_begin s(:nil),                     s(:begin, s(:begin, s(:nil)))
    assert_remove_begin s(:begin, s(:nil), s(:nil)), s(:begin, s(:nil), s(:nil))
  end
end