forked from soveran/cuba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.rb
96 lines (70 loc) · 2.3 KB
/
render.rb
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
require_relative "helper"
require "cuba/render"
test "doesn't override the settings if they already exist" do
Cuba.settings[:render] = {
:views => "./test/views",
:template_engine => "haml"
}
Cuba.plugin Cuba::Render
assert_equal "./test/views", Cuba.settings[:render][:views]
assert_equal "haml", Cuba.settings[:render][:template_engine]
end
scope do
setup do
Cuba.plugin Cuba::Render
Cuba.settings[:render][:views] = "./test/views"
Cuba.settings[:render][:template_engine] = "erb"
Cuba.define do
on "home" do
res.write view("home", name: "Agent Smith", title: "Home")
end
on "about" do
res.write partial("about", title: "About Cuba")
end
end
end
test "partial" do
_, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
assert_response body, ["<h1>About Cuba</h1>\n"]
end
test "view" do
_, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\n"]
end
test "partial with str as engine" do
Cuba.settings[:render][:template_engine] = "str"
_, _, body = Cuba.call({ "PATH_INFO" => "/about", "SCRIPT_NAME" => "/" })
assert_response body, ["<h1>About Cuba</h1>\n"]
end
test "view with str as engine" do
Cuba.settings[:render][:template_engine] = "str"
_, _, body = Cuba.call({ "PATH_INFO" => "/home", "SCRIPT_NAME" => "/" })
assert_response body, ["<title>Cuba: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>\n\n"]
end
end
test "caching behavior" do
Thread.current[:_cache] = nil
Cuba.plugin Cuba::Render
Cuba.settings[:render][:views] = "./test/views"
Cuba.define do
on "foo/:i" do |i|
res.write partial("test", title: i)
end
end
10.times do |i|
_, _, resp = Cuba.call({ "PATH_INFO" => "/foo/#{i}", "SCRIPT_NAME" => "" })
end
assert_equal 1, Thread.current[:_cache].instance_variable_get(:@cache).size
end
test "simple layout support" do
Cuba.plugin Cuba::Render
Cuba.define do
on true do
res.write render("test/views/layout-yield.erb") {
render("test/views/content-yield.erb")
}
end
end
_, _, resp = Cuba.call({})
assert_response resp, ["Header\nThis is the actual content.\nFooter\n"]
end