| [7834] | 1 | require 'test/unit' |
|---|
| 2 | require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' |
|---|
| 3 | require File.dirname(__FILE__) + "/../abstract_unit" |
|---|
| 4 | |
|---|
| 5 | class CompiledTemplateTests < Test::Unit::TestCase |
|---|
| 6 | |
|---|
| 7 | def setup |
|---|
| 8 | @ct = ActionView::CompiledTemplates.new |
|---|
| 9 | @v = Class.new |
|---|
| 10 | @v.send :include, @ct |
|---|
| 11 | @a = './test_compile_template_a.rhtml' |
|---|
| 12 | @b = './test_compile_template_b.rhtml' |
|---|
| 13 | @s = './test_compile_template_link.rhtml' |
|---|
| 14 | end |
|---|
| 15 | def teardown |
|---|
| 16 | [@a, @b, @s].each do |f| |
|---|
| 17 | `rm #{f}` if File.exist?(f) || File.symlink?(f) |
|---|
| 18 | end |
|---|
| 19 | end |
|---|
| 20 | attr_reader :ct, :v |
|---|
| 21 | |
|---|
| 22 | def test_name_allocation |
|---|
| 23 | hi_world = ct.method_names['hi world'] |
|---|
| 24 | hi_sexy = ct.method_names['hi sexy'] |
|---|
| 25 | wish_upon_a_star = ct.method_names['I love seeing decent error messages'] |
|---|
| 26 | |
|---|
| 27 | assert_equal hi_world, ct.method_names['hi world'] |
|---|
| 28 | assert_equal hi_sexy, ct.method_names['hi sexy'] |
|---|
| 29 | assert_equal wish_upon_a_star, ct.method_names['I love seeing decent error messages'] |
|---|
| 30 | assert_equal 3, [hi_world, hi_sexy, wish_upon_a_star].uniq.length |
|---|
| 31 | end |
|---|
| 32 | |
|---|
| 33 | def test_wrap_source |
|---|
| 34 | assert_equal( |
|---|
| 35 | "def aliased_assignment(value)\nself.value = value\nend", |
|---|
| 36 | @ct.wrap_source(:aliased_assignment, [:value], 'self.value = value') |
|---|
| 37 | ) |
|---|
| 38 | |
|---|
| 39 | assert_equal( |
|---|
| 40 | "def simple()\nnil\nend", |
|---|
| 41 | @ct.wrap_source(:simple, [], 'nil') |
|---|
| 42 | ) |
|---|
| 43 | end |
|---|
| 44 | |
|---|
| 45 | def test_compile_source_single_method |
|---|
| 46 | selector = ct.compile_source('doubling method', [:a], 'a + a') |
|---|
| 47 | assert_equal 2, @v.new.send(selector, 1) |
|---|
| 48 | assert_equal 4, @v.new.send(selector, 2) |
|---|
| 49 | assert_equal -4, @v.new.send(selector, -2) |
|---|
| 50 | assert_equal 0, @v.new.send(selector, 0) |
|---|
| 51 | selector |
|---|
| 52 | end |
|---|
| 53 | |
|---|
| 54 | def test_compile_source_two_method |
|---|
| 55 | sel1 = test_compile_source_single_method # compile the method in the other test |
|---|
| 56 | sel2 = ct.compile_source('doubling method', [:a, :b], 'a + b + a + b') |
|---|
| 57 | assert_not_equal sel1, sel2 |
|---|
| 58 | |
|---|
| 59 | assert_equal 2, @v.new.send(sel1, 1) |
|---|
| 60 | assert_equal 4, @v.new.send(sel1, 2) |
|---|
| 61 | |
|---|
| 62 | assert_equal 6, @v.new.send(sel2, 1, 2) |
|---|
| 63 | assert_equal 32, @v.new.send(sel2, 15, 1) |
|---|
| 64 | end |
|---|
| 65 | |
|---|
| 66 | def test_mtime |
|---|
| 67 | t1 = Time.now |
|---|
| 68 | test_compile_source_single_method |
|---|
| 69 | assert (t1..Time.now).include?(ct.mtime('doubling method', [:a])) |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | def test_compile_time |
|---|
| 73 | `echo '#{@a}' > #{@a}; echo '#{@b}' > #{@b}; ln -s #{@a} #{@s}` |
|---|
| 74 | |
|---|
| 75 | v = ActionView::Base.new |
|---|
| 76 | v.base_path = '.' |
|---|
| 77 | v.cache_template_loading = false; |
|---|
| 78 | |
|---|
| 79 | sleep 1 |
|---|
| 80 | t = Time.now |
|---|
| 81 | v.compile_and_render_template(:rhtml, '', @a) |
|---|
| 82 | v.compile_and_render_template(:rhtml, '', @b) |
|---|
| 83 | v.compile_and_render_template(:rhtml, '', @s) |
|---|
| 84 | a_n = v.method_names[@a] |
|---|
| 85 | b_n = v.method_names[@b] |
|---|
| 86 | s_n = v.method_names[@s] |
|---|
| 87 | # all of the files have changed since last compile |
|---|
| 88 | assert v.compile_time[a_n] > t |
|---|
| 89 | assert v.compile_time[b_n] > t |
|---|
| 90 | assert v.compile_time[s_n] > t |
|---|
| 91 | |
|---|
| 92 | sleep 1 |
|---|
| 93 | t = Time.now |
|---|
| 94 | v.compile_and_render_template(:rhtml, '', @a) |
|---|
| 95 | v.compile_and_render_template(:rhtml, '', @b) |
|---|
| 96 | v.compile_and_render_template(:rhtml, '', @s) |
|---|
| 97 | # none of the files have changed since last compile |
|---|
| 98 | assert v.compile_time[a_n] < t |
|---|
| 99 | assert v.compile_time[b_n] < t |
|---|
| 100 | assert v.compile_time[s_n] < t |
|---|
| 101 | |
|---|
| 102 | `rm #{@s}; ln -s #{@b} #{@s}` |
|---|
| 103 | v.compile_and_render_template(:rhtml, '', @a) |
|---|
| 104 | v.compile_and_render_template(:rhtml, '', @b) |
|---|
| 105 | v.compile_and_render_template(:rhtml, '', @s) |
|---|
| 106 | # the symlink has changed since last compile |
|---|
| 107 | assert v.compile_time[a_n] < t |
|---|
| 108 | assert v.compile_time[b_n] < t |
|---|
| 109 | assert v.compile_time[s_n] > t |
|---|
| 110 | |
|---|
| 111 | sleep 1 |
|---|
| 112 | `touch #{@b}` |
|---|
| 113 | t = Time.now |
|---|
| 114 | v.compile_and_render_template(:rhtml, '', @a) |
|---|
| 115 | v.compile_and_render_template(:rhtml, '', @b) |
|---|
| 116 | v.compile_and_render_template(:rhtml, '', @s) |
|---|
| 117 | # the file at the end of the symlink has changed since last compile |
|---|
| 118 | # both the symlink and the file at the end of it should be recompiled |
|---|
| 119 | assert v.compile_time[a_n] < t |
|---|
| 120 | assert v.compile_time[b_n] > t |
|---|
| 121 | assert v.compile_time[s_n] > t |
|---|
| 122 | end |
|---|
| 123 | end |
|---|
| 124 | |
|---|
| 125 | module ActionView |
|---|
| 126 | class Base |
|---|
| 127 | def compile_time |
|---|
| 128 | @@compile_time |
|---|
| 129 | end |
|---|
| 130 | def method_names |
|---|
| 131 | @@method_names |
|---|
| 132 | end |
|---|
| 133 | end |
|---|
| 134 | end |
|---|