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
|
require 'tmpdir'
require 'pathname'
require File.expand_path('../teststrap', __FILE__)
class TestPartial
include Rabl::Partials
end
context "Rabl::Partials" do
context "fetch_source with json" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
::Sinatra = stub(Class.new)
File.open(tmp_path + "test.json.rabl", "w") do |f|
f.puts "content"
end
File.open(tmp_path + "test_v1.json.rabl", "w") do |f|
f.puts "content_v1"
end
FileUtils.touch tmp_path + "test_v2.json.rabl"
[TestPartial.new.fetch_source('test', :view_path => tmp_path.to_s),
TestPartial.new.fetch_source('test_v1', :view_path => tmp_path.to_s)]
end
asserts(:first).equals {["content\n", (tmp_path + "test.json.rabl").to_s ]}
asserts(:last).equals {["content_v1\n", (tmp_path + "test_v1.json.rabl").to_s ]}
teardown { Object.send(:remove_const, :Sinatra) }
end
context "fetch_source with rabl" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
::Sinatra = stub(Class.new)
File.open(tmp_path + "test.rabl", "w") do |f|
f.puts "content"
end
TestPartial.new.fetch_source('test', :view_path => tmp_path.to_s)
end
asserts('detects file.rabl') { topic }.equals do
["content\n", (tmp_path + 'test.rabl').to_s]
end
teardown { Object.send(:remove_const, :Sinatra) }
end
context "fetch_source with view_path" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
::Sinatra = stub(Class.new)
File.open(tmp_path + "test.rabl", "w") do |f|
f.puts "content"
end
File.open(tmp_path + "test.json.rabl", "w") do |f|
f.puts "content2"
end
TestPartial.new.fetch_source('test', :view_path => tmp_path.to_s)
end
asserts('detects file.json.rabl first') { topic }.equals do
["content2\n", (tmp_path + 'test.json.rabl').to_s]
end
teardown { Object.send(:remove_const, :Sinatra) }
end
context "fetch source using configured view paths" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
Rabl.configure do |config|
config.view_paths = tmp_path
end
::Sinatra = stub(Class.new)
File.open(tmp_path + "test.rabl", "w") do |f|
f.puts "content"
end
File.open(tmp_path + "test.json.rabl", "w") do |f|
f.puts "content2"
end
TestPartial.new.fetch_source('test')
end
asserts('detects file.json.rabl first') { topic }.equals do
["content2\n", (tmp_path + 'test.json.rabl').to_s]
end
teardown do
Object.send(:remove_const, :Sinatra)
Rabl.configuration.view_paths = []
end
end
context "partial_as_engine using configured view paths" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
File.open(tmp_path + "_test.rabl", "w")
Rabl::Engine.new('', :view_path => tmp_path)
end
asserts('returns new engine with given view_path') do
topic.partial_as_engine('test', :object => {}).view_path
end.equals do
tmp_path
end
teardown do
Rabl.configuration.view_paths = []
end
end
context "fetch source with custom scope" do
context "when Padrino is defined" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
::Padrino = stub(Class.new)
Rabl.configuration.cache_sources = false
@it = TestPartial.new
def @it.context_scope; @context_scope ||= Object.new; end
context_scope = @it.context_scope
def context_scope.settings; end
File.open(tmp_path + "test.json.rabl", "w") { |f| f.puts "content" }
end
asserts('Padrino constant dont break manual lookup') do
@it.fetch_source('test', :view_path => tmp_path.to_s)
end.equals do
["content\n", (tmp_path + "test.json.rabl").to_s ]
end
teardown { Object.send(:remove_const, :Padrino) }
end
context "when Sinatra is defined" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
::Sinatra = stub(Class.new)
Rabl.configuration.cache_sources = false
@it = TestPartial.new
def @it.context_scope; @context_scope ||= Object.new; end
context_scope = @it.context_scope
def context_scope.settings; @settings ||= Object.new end
File.open(tmp_path + "test.json.rabl", "w") { |f| f.puts "content" }
end
asserts('Sinatra constant dont break manual lookup') do
@it.fetch_source((tmp_path + "test").to_s)
end.equals do
["content\n", "/" + (tmp_path + "test.json.rabl").to_s ]
end
teardown { Object.send(:remove_const, :Sinatra) }
end
end
context "fetch source with Rails" do
context "and :view_path" do
helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
setup do
::Rails = stub(Class.new)
::ActionPack = Module.new
::ActionPack::VERSION = Module.new
::ActionPack::VERSION::MAJOR = 3
::ActionPack::VERSION::MINOR = 2
@it = TestPartial.new
def @it.context_scope; @context_scope ||= Object.new; end
def @it.request_format; :json; end
context_scope = @it.context_scope
def context_scope.view_paths; []; end
def context_scope.lookup_context; @lookup_context ||= Object.new; end
lookup_context = context_scope.lookup_context
def lookup_context.rendered_format; :json; end
def lookup_context.find(*args)
raise RuntimeError, 'Something happen with Rails lookup'
end
File.open(tmp_path + "test.json.rabl", "w") { |f| f.puts "content" }
@it
end
asserts('rails lookups dont break manual') do
@it.fetch_source('test', :view_path => tmp_path.to_s)
end.equals do
["content\n", (tmp_path + "test.json.rabl").to_s ]
end
teardown do
Object.send(:remove_const, :Rails)
Object.send(:remove_const, :ActionPack)
Rabl.configuration.view_paths = []
end
end
end # Rails
end # Rabl::Partials
|