File: tilt_liquidtemplate_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 (77 lines) | stat: -rw-r--r-- 2,471 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
require_relative 'test_helper'

checked_describe 'tilt/liquid' do
  it "registered for '.liquid' files" do
    assert_equal Tilt::LiquidTemplate, Tilt['test.liquid']
  end

  it "preparing and evaluating templates on #render" do
    template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
    assert_equal "Hello World!", template.render
  end

  it "can be rendered more than once" do
    template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
    3.times { assert_equal "Hello World!", template.render }
  end

  it "passing locals" do
    template = Tilt::LiquidTemplate.new { "Hey {{ name }}!" }
    assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
  end

  it "options can be set" do
    err = assert_raises(Liquid::SyntaxError) do
      options = { line_numbers: false, error_mode: :strict }
      Tilt::LiquidTemplate.new(options) { "{{%%%}}" }.render
    end
    assert_equal 'Liquid syntax error: Unexpected character % in "{{%%%}}"',
      err.message
  end

  # Object's passed as "scope" to LiquidTemplate may respond to
  # #to_h with a Hash. The Hash's contents are merged underneath
  # Tilt locals.
  _ExampleLiquidScope = Class.new do
    def to_h
      { :beer => 'wet', :whisky => 'wetter' }
    end
  end

  it "combining scope and locals when scope responds to #to_h" do
    template =
      Tilt::LiquidTemplate.new {
        'Beer is {{ beer }} but Whisky is {{ whisky }}.'
      }
    scope = _ExampleLiquidScope.new
    assert_equal "Beer is wet but Whisky is wetter.", template.render(scope)
  end

  it "precedence when locals and scope define same variables" do
    template =
      Tilt::LiquidTemplate.new {
        'Beer is {{ beer }} but Whisky is {{ whisky }}.'
      }
    scope = _ExampleLiquidScope.new
    assert_equal "Beer is great but Whisky is greater.",
      template.render(scope, :beer => 'great', :whisky => 'greater')
  end

  it "handling scopes that do not respond to #to_h" do
    template = Tilt::LiquidTemplate.new { 'Whisky' }
    assert_equal "Whisky", template.render(Object.new)
  end

  it "passing a block for yield" do
    template =
      Tilt::LiquidTemplate.new {
        'Beer is {{ yield }} but Whisky is {{ content }}ter.'
      }
    assert_equal "Beer is wet but Whisky is wetter.",
      template.render({}) { 'wet' }
  end

  it "sets allows_script metadata set to false" do
    assert_equal false, Tilt::LiquidTemplate.new { |t| "Hello World!" }.metadata[:allows_script]
  end
end