File: tilt_mapping_test.rb

package info (click to toggle)
ruby-tilt 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: ruby: 4,998; makefile: 7
file content (326 lines) | stat: -rw-r--r-- 8,407 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
require_relative 'test_helper'
require 'tilt/string'

describe 'Tilt::Mapping' do
  _Stub = Class.new
  _Stub2 = Class.new

  before do
    @mapping = Tilt::Mapping.new
  end

  it "registered?" do
    @mapping.register(_Stub, 'foo', 'bar')
    assert @mapping.registered?('foo')
    assert @mapping.registered?('bar')
    refute @mapping.registered?('baz')
  end

  it "unregister" do
    @mapping.register(_Stub, 'foo', 'bar', 'baz')
    @mapping.unregister('baz')
    assert @mapping.registered?('foo')
    assert @mapping.registered?('bar')
    refute @mapping.registered?('baz')
  end

  it "lookups on registered" do
    @mapping.register(_Stub, 'foo', 'bar')
    assert_equal _Stub, @mapping['foo']
    assert_equal _Stub, @mapping['bar']
    assert_equal _Stub, @mapping['hello.foo']
    assert_nil @mapping['foo.baz']
  end

  it "can be dup'd" do
    @mapping.register(_Stub, 'foo')
    other = @mapping.dup
    assert other.registered?('foo')

    # @mapping doesn't leak to other
    @mapping.register(_Stub, 'bar')
    refute other.registered?('bar')

    # other doesn't leak to @mapping
    other.register(_Stub, 'baz')
    refute @mapping.registered?('baz')
  end

  it "#extensions_for" do
    @mapping.register(_Stub, 'foo', 'bar')
    assert_equal ['foo', 'bar'].sort, @mapping.extensions_for(_Stub).sort
  end

  it "supports old-style #register" do
    @mapping.register('foo', _Stub)
    assert_equal _Stub, @mapping['foo']
  end

  it "#new raises if no template engine found" do
    assert_raises(RuntimeError) do
      @mapping.new('foo.nonexistant')
    end
  end

  it "#[] raises if template engine recognized but cannot be loaded" do
    @mapping.register_lazy :NonExistantTemplate,    'tilt/nonexistant_template',    'test-nonexist'
    assert_raises(LoadError) do
      @mapping['foo.test-nonexist']
    end
  end

  describe "lazy with one template class" do
    before do
      @mapping.register_lazy('MyTemplate', 'my_template', 'mt')
      @loaded_before = $LOADED_FEATURES.dup
    end

    after do
      Object.send :remove_const, :MyTemplate if defined? ::MyTemplate
      $LOADED_FEATURES.replace(@loaded_before)
    end

    it "registered?" do
      assert @mapping.registered?('mt')
    end

    it "unregister" do
      @mapping.unregister('mt')
      refute @mapping.registered?('mt')
    end

    it "#extensions_for" do
      assert_equal ['mt'], @mapping.extensions_for('MyTemplate')
    end

    it "basic lookup" do
      req = proc do |file|
        assert_equal 'my_template', file
        class ::MyTemplate; end
        true
      end

      @mapping.stub :require, req do
        klass = @mapping['hello.mt']
        assert_equal ::MyTemplate, klass
      end
    end

    it "doesn't require when template class is present" do
      class ::MyTemplate; end

      req = proc do |file|
        flunk "#require shouldn't be called"
      end

      @mapping.stub :require, req do
        klass = @mapping['hello.mt']
        assert_equal ::MyTemplate, klass
      end
    end

    it "doesn't require when the template class is autoloaded, and then defined" do
      $LOAD_PATH << __dir__
      begin
        Object.autoload :MyTemplate, 'mytemplate'
        did_load = require 'mytemplate'
      ensure
        $LOAD_PATH.delete(__dir__)
      end
      assert did_load, "mytemplate wasn't freshly required"

      req = proc do |file|
        flunk "#require shouldn't be called"
      end

      @mapping.stub :require, req do
        klass = @mapping['hello.mt']
        assert_equal ::MyTemplate, klass
      end
    end

    it "raises NameError when the class name is defined" do
      req = proc do |file|
        # do nothing
      end

      @mapping.stub :require, req do
        assert_raises(NameError) do
          @mapping['hello.mt']
        end
      end
    end
  end

  describe "lazy with two template classes" do
    before do
      @mapping.register_lazy('MyTemplate1', 'my_template1', 'mt')
      @mapping.register_lazy('MyTemplate2', 'my_template2', 'mt')
    end

    after do
      Object.send :remove_const, :MyTemplate1 if defined? ::MyTemplate1
      Object.send :remove_const, :MyTemplate2 if defined? ::MyTemplate2
    end

    it "registered?" do
      assert @mapping.registered?('mt')
    end

    it "only attempt to load the last template" do
      req = proc do |file|
        assert_equal 'my_template2', file
        class ::MyTemplate2; end
        true
      end

      @mapping.stub :require, req do
        klass = @mapping['hello.mt']
        assert_equal ::MyTemplate2, klass
      end
    end

    it "uses the first template if it's present" do
      class ::MyTemplate1; end

      req = proc do |file|
        flunk
      end

      @mapping.stub :require, req do
        klass = @mapping['hello.mt']
        assert_equal ::MyTemplate1, klass
      end
    end

    it "falls back when LoadError is thrown" do
      req = proc do |file|
        raise LoadError unless file == 'my_template1'
        class ::MyTemplate1; end
        true
      end

      @mapping.stub :require, req do
        klass = @mapping['hello.mt']
        assert_equal ::MyTemplate1, klass
      end
    end

    it "raises the first LoadError when everything fails" do
      req = proc do |file|
        raise LoadError, file
      end

      @mapping.stub :require, req do
        err = assert_raises(LoadError) do
          @mapping['hello.mt']
        end

        assert_equal 'my_template2', err.message
      end
    end

    it "handles autoloaded constants" do
      Object.autoload :MyTemplate2, 'my_template2'
      class ::MyTemplate1; end

      assert_equal MyTemplate1, @mapping['hello.mt']
    end
  end

  it "raises NameError on invalid class name" do
    @mapping.register_lazy '#foo', 'my_template', 'mt'

    req = proc do |file|
      # do nothing
    end

    @mapping.stub :require, req do
      assert_raises(NameError) do
        @mapping['hello.mt']
      end
    end
  end

  describe "#templates_for" do
    before do
      @mapping.register _Stub, 'a'
      @mapping.register _Stub2, 'b'
    end

    it "handles multiple engines" do
      assert_equal [_Stub2, _Stub], @mapping.templates_for('hello/world.a.b')
    end
  end
end

describe 'Tilt::FinalizedMapping' do
  next if defined?(JRUBY_VERSION) && JRUBY_VERSION < '9.3'

  _Stub = Class.new
  _Stub2 = Class.new

  before do
    mapping = Tilt::Mapping.new
    mapping.register(_Stub, 'foo', 'bar')
    mapping.register(_Stub2, 'foo', 'baz')
    mapping.register_lazy(:NonExistant, 'tilt/nonexistant', 'str')
    mapping.register_lazy(:StringTemplate, 'tilt/string', 'string')
    @mapping = mapping.finalized
  end

  it "does not allow modification" do
    refute @mapping.respond_to?(:register)
    refute @mapping.respond_to?(:register_lazy)
    refute @mapping.respond_to?(:unregister)
    refute @mapping.respond_to?(:register_pipeline)
    assert @mapping.frozen?
  end

  it "#registered?" do
    assert @mapping.registered?('foo')
    assert @mapping.registered?('bar')
    assert @mapping.registered?('baz')
    assert @mapping.registered?('string')
    refute @mapping.registered?('str')
  end

  it "#[]" do
    assert_equal _Stub2, @mapping['x.foo']
    assert_equal _Stub, @mapping['x.bar']
    assert_equal _Stub2, @mapping['x.baz']
    assert_nil @mapping['x.str']
    assert_equal Tilt::StringTemplate, @mapping['x.string']
  end

  it "#templates_for" do
    assert_equal [_Stub, _Stub2], @mapping.templates_for('x.foo.bar')
    assert_equal [_Stub], @mapping.templates_for('x.bar')
    assert_equal [_Stub2], @mapping.templates_for('x.baz')
    assert_equal [], @mapping.templates_for('x.str')
    assert_equal [Tilt::StringTemplate], @mapping.templates_for('x.string')
  end

  it "#extensions_for" do
    assert_equal ['bar'], @mapping.extensions_for(_Stub)
    assert_equal ['foo', 'baz'], @mapping.extensions_for(_Stub2)
    assert_equal ['string'], @mapping.extensions_for(Tilt::StringTemplate)
    assert_equal [], @mapping.extensions_for(Object)
  end

  it "#clone" do
    m = @mapping.clone
    assert m.frozen?
    assert_equal _Stub2, m['x.foo']
    assert_equal Tilt::StringTemplate, m['x.string']
    assert_nil m['x.str']
  end

  it "#dup" do
    m = @mapping.dup
    assert m.frozen?
    assert_equal _Stub2, m['x.foo']
    assert_equal Tilt::StringTemplate, m['x.string']
    assert_nil m['x.str']
  end
end