File: test_library.rb

package info (click to toggle)
ruby-rabl-rails 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 288 kB
  • sloc: ruby: 1,630; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 2,697 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
require 'helper'

class TestLibrary < Minitest::Test
  RablRails::Library.send(:attr_reader, :cached_templates)

  describe 'library' do
    before do
      @library = RablRails::Library.instance
      @library.reset_cache!
      @context = Context.new
      @template = RablRails::CompiledTemplate.new
    end

    describe '#get_rendered_template' do
      it 'compiles and renders template' do
        result = @library.stub :compile_template_from_source, @template do
          @library.get_rendered_template '', @context
        end

        assert_equal '{}', result
      end

      it 'uses for from lookup context' do
        context = Context.new(:xml)
        result = @library.stub :compile_template_from_source, @template do
          RablRails::Renderers::XML.stub :render, '<xml>' do
            @library.get_rendered_template '', context
          end
        end

        assert_equal '<xml>', result
      end

      it 'raises if format is not supported' do
        context = Context.new(:unsupported)
        @library.stub :compile_template_from_source, @template do
          assert_raises(RablRails::Library::UnknownFormat) { @library.get_rendered_template '', context }
        end
      end
    end

    describe '#compile_template_from_source' do
      it 'compiles a template' do
        compiler = MiniTest::Mock.new
        compiler.expect :compile_source, @template, ['attribute :id']

        result = RablRails::Compiler.stub :new, compiler do
          @library.compile_template_from_source('attribute :id', @context)
        end

        assert_equal @template, result
      end

      it 'caches compiled template if option is set' do
        @context.virtual_path = 'users/base'
        template = with_configuration :cache_templates, true do
          @library.compile_template_from_source("attribute :id", @context)
        end

        assert_equal(template, @library.cached_templates['users/base'])
      end

      it 'compiles source without caching it if options is not set' do
        @context.virtual_path = 'users/base'
        with_configuration :cache_templates, false do
          @library.compile_template_from_source("attribute :id", @context)
        end

        assert_empty @library.cached_templates
      end

      it 'caches multiple templates in one compilation' do
        @context.virtual_path = 'users/show'
        with_configuration :cache_templates, true do
          @library.stub :fetch_source, 'attributes :id' do
            @library.compile_template_from_source("child(:account, partial: 'users/_account')", @context)
          end
        end

        assert_equal 2, @library.cached_templates.size
      end
    end
  end
end