File: test_utils.rb

package info (click to toggle)
ruby-sprockets 4.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ruby: 13,014; javascript: 157; makefile: 4
file content (246 lines) | stat: -rw-r--r-- 5,909 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
# frozen_string_literal: true
require 'minitest/autorun'
require 'sprockets/utils'

class TestUtils < Minitest::Test
  include Sprockets::Utils

  def test_duplicable
    skip unless 0.class != Integer # Ruby < 2.4
    objs = [nil, true, false, 1, "foo", :foo, [], {}]
    objs.each do |obj|
      begin
        obj.dup
      rescue TypeError
        refute duplicable?(obj), "can't dup: #{obj.inspect}"
      else
        assert duplicable?(obj), "can dup: #{obj.inspect}"
      end
    end
  end

  def test_hash_reassoc
    h = hash_reassoc({}.freeze, :foo) do |value|
      refute value
      nil
    end
    assert_equal({foo: nil}, h)
    assert h.frozen?

    h = hash_reassoc({}, :foo) do |value|
      refute value
      true
    end
    assert_equal({foo: true}, h)
    assert h.frozen?

    h = hash_reassoc({foo: 1}.freeze, :foo) do |value|
      assert_equal 1, value
      2
    end
    assert_equal({foo: 2}, h)
    assert h.frozen?

    h = hash_reassoc({foo: "bar".freeze}.freeze, :foo) do |value|
      assert_equal "bar", value
      refute value.frozen?
      "baz"
    end
    assert_equal({foo: "baz"}, h)
    assert h.frozen?
    assert h[:foo].frozen?

    h = hash_reassoc({foo: "bar".freeze}.freeze, :foo) do |value|
      assert_equal "bar", value
      refute value.frozen?
      value.sub!("r", "z")
    end
    assert_equal({foo: "baz"}, h)
    assert h.frozen?
    assert h[:foo].frozen?

    h = hash_reassoc({foo: {bar: "baz".freeze}.freeze}.freeze, :foo, :bar) do |value|
      assert_equal "baz", value
      refute value.frozen?
      "biz"
    end
    assert_equal({foo: {bar: "biz"}}, h)
    assert h.frozen?
    assert h[:foo].frozen?
    assert h[:foo][:bar].frozen?
  end

  def test_string_ends_with_semicolon
    assert string_end_with_semicolon?("var foo;")
    refute string_end_with_semicolon?("var foo")

    assert string_end_with_semicolon?("var foo;\n")
    refute string_end_with_semicolon?("var foo\n")

    assert string_end_with_semicolon?("var foo;\n\n")
    refute string_end_with_semicolon?("var foo\n\n")

    assert string_end_with_semicolon?("  var foo;\n  \n")
    refute string_end_with_semicolon?("  var foo\n  \n")

    assert string_end_with_semicolon?("\tvar foo;\n\t\n")
    refute string_end_with_semicolon?("\tvar foo\n\t\n")

    assert string_end_with_semicolon?("var foo\n;\n")
    refute string_end_with_semicolon?("var foo\n\n")
  end

  def test_concat_javascript_sources
    assert_equal "var foo;\n", apply_concat_javascript_sources("".freeze, "var foo;\n".freeze)
    assert_equal "\nvar foo;\n", apply_concat_javascript_sources("\n".freeze, "var foo;\n".freeze)
    assert_equal "var foo;\nvar bar;\n", apply_concat_javascript_sources("var foo;\n".freeze, "var bar;\n".freeze)

    assert_equal " var foo;\n", apply_concat_javascript_sources(" ".freeze, "var foo;\n".freeze)
    assert_equal "var foo;var bar;", apply_concat_javascript_sources("var foo".freeze, "var bar".freeze)
    assert_equal "var foo;var bar;", apply_concat_javascript_sources("var foo;".freeze, "var bar;".freeze)
    assert_equal "var foo;var bar;", apply_concat_javascript_sources("var foo".freeze, "var bar;".freeze)
    assert_equal "var foo;\nvar bar;", apply_concat_javascript_sources("var foo\n".freeze, "var bar;".freeze)
  end

  def apply_concat_javascript_sources(*args)
    args.reduce(+"", &method(:concat_javascript_sources))
  end


  def test_post_order_depth_first_search
    m = []
    m[11] = [4, 5, 10]
    m[4]  = [2, 3]
    m[10] = [8, 9]
    m[2]  = [0, 1]
    m[8]  = [6, 7]

    assert_equal Set.new(0..11), dfs(11) { |n| m[n] }

    m = []
    m[11] = [4, 5, 10]
    m[4]  = [2, 3]
    m[3]  = [1]
    m[5]  = [1, 2]
    m[10] = [8, 9]
    m[2]  = [0, 1]
    m[8]  = [6, 7]
    m[6]  = [5]

    assert_equal Set.new(0..11), dfs(11) { |n| m[n] }
  end

  def test_post_order_depth_first_find_all_paths
    m = []
    m[0] = [1]
    m[1] = [2]
    m[2] = [3]
    m[3] = [4, 5]
    m[4] = [1]

    assert_equal [
      [0],
      [0, 1],
      [0, 1, 2],
      [0, 1, 2, 3],
      [0, 1, 2, 3, 4],
      [0, 1, 2, 3, 5]
    ], dfs_paths([0]) { |n| m[n] || [] }

    assert_equal [
      [1],
      [1, 2],
      [1, 2, 3],
      [1, 2, 3, 4],
      [1, 2, 3, 5]
    ], dfs_paths([1]) { |n| m[n] || [] }

    assert_equal [[5]], dfs_paths([5]) { |n| m[n] || [] }
  end

  module Functions
    module InstanceMethods
      def bar
        2
      end
    end
    include InstanceMethods

    def foo
      1
    end
  end

  module ScopedFunctions
    def foo
      7
    end

    def bar
      8
    end

    def baz
      9
    end
  end

  module OtherScopedFunctions
    def bar
      3
    end

    def baz
      4
    end
  end

  class Context
    include Functions
  end

  def test_module_include
    context = Context.new

    assert context.respond_to?(:foo)
    assert context.respond_to?(:bar)
    refute context.respond_to?(:baz)

    assert_equal 1, context.foo
    assert_equal 2, context.bar

    module_include(Functions, ScopedFunctions) do
      assert context.respond_to?(:foo)
      assert context.respond_to?(:bar)
      assert context.respond_to?(:baz)

      assert_equal 7, context.foo
      assert_equal 8, context.bar
      assert_equal 9, context.baz
    end

    assert context.respond_to?(:foo)
    assert context.respond_to?(:bar)
    refute context.respond_to?(:baz)

    assert_equal 1, context.foo
    assert_equal 2, context.bar

    module_include(Functions, OtherScopedFunctions) do
      assert context.respond_to?(:foo)
      assert context.respond_to?(:bar)
      assert context.respond_to?(:baz)

      assert_equal 1, context.foo
      assert_equal 3, context.bar
      assert_equal 4, context.baz
    end

    assert context.respond_to?(:foo)
    assert context.respond_to?(:bar)
    refute context.respond_to?(:baz)

    assert_equal 1, context.foo
    assert_equal 2, context.bar
  end
end