examples: annotate examples
[nit.git] / lib / template / test_macro.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module test_macro is test
16
17 import macro
18
19 class TestTemplateString
20 test
21
22 fun test_tpl_parse_1 is test do
23 var tpl = """
24 <!DOCTYPE html>
25 <html lang="en">
26 <head>
27 <title>Dave's Profile</title>
28 </head>
29 <body>
30 <h1>Dave</h1>
31 <p>Profile</p>
32 </body>
33 </html>"""
34 var subject = new TemplateString(tpl)
35 var res = subject.write_to_string
36 assert res == tpl
37 end
38
39 fun test_tpl_parse_2 is test do
40 var tpl = """
41 <!DOCTYPE html>
42 <html lang="en">
43 <head>
44 <title>%TITLE%</title>
45 </head>
46 <body>
47 <h1>%TITLE%</h1>
48 <p>%ARTICLE%</p>
49 </body>
50 </html>"""
51 var subject = new TemplateString(tpl)
52 var res = subject.write_to_string
53 assert res == tpl
54 end
55
56 fun test_tpl_parse_3 is test do
57 var tpl = """
58 <!DOCTYPE html>
59 <html lang="en">
60 <head>
61 <title>%</title>
62 </head>
63 </html>"""
64 var subject = new TemplateString(tpl)
65 var res = subject.write_to_string
66 assert res == tpl
67 end
68
69 fun test_tpl_parse_4 is test do
70 var tpl = """
71 <!DOCTYPE html>
72 <html lang="en">
73 <head>
74 <title>%</title>
75 </head>
76 <body>
77 <h1>%</h1>
78 </body>
79 </html>"""
80 var subject = new TemplateString(tpl)
81 var res = subject.write_to_string
82 assert res == tpl
83 end
84
85 fun test_tpl_parse_5 is test do
86 var tpl = """
87 <!DOCTYPE html>
88 <html lang="en">
89 <head>
90 <title>%</title>
91 </head>
92 <body>
93 <h1>%TITLE%</h1>
94 <p>%ARTICLE%</p>
95 </body>
96 </html>"""
97 var subject = new TemplateString(tpl)
98 var res = subject.write_to_string
99 assert res == tpl
100 end
101
102 fun test_tpl_parse_6 is test do
103 var tpl = """
104 <!DOCTYPE html>
105 <html lang="en">
106 <head>
107 <title>%</title>
108 </head>
109 <body>
110 <h1>%%</h1>
111 </body>
112 </html>"""
113 var subject = new TemplateString(tpl)
114 var res = subject.write_to_string
115 assert res == tpl
116 end
117
118 fun test_tpl_replace_1 is test do
119 var tpl = """
120 <!DOCTYPE html>
121 <html lang="en">
122 <head>
123 <title>%TITLE%</title>
124 </head>
125 <body>
126 <h1>%TITLE%</h1>
127 <p>%ARTICLE%</p>
128 </body>
129 </html>"""
130 var exp = """
131 <!DOCTYPE html>
132 <html lang="en">
133 <head>
134 <title>Hello World!</title>
135 </head>
136 <body>
137 <h1>Hello World!</h1>
138 <p>%ARTICLE%</p>
139 </body>
140 </html>"""
141 var subject = new TemplateString(tpl)
142 subject.replace("TITLE", "Hello World!")
143 var res = subject.write_to_string
144 assert res == exp
145 end
146
147 fun test_tpl_replace_2 is test do
148 var tpl = """
149 <!DOCTYPE html>
150 <html lang="en">
151 <head>
152 <title>%TITLE%</title>
153 </head>
154 <body>
155 <h1>%TITLE%</h1>
156 <p>%BODY%</p>
157 </body>
158 </html>"""
159 var exp = """
160 <!DOCTYPE html>
161 <html lang="en">
162 <head>
163 <title>Hello World!</title>
164 </head>
165 <body>
166 <h1>Hello World!</h1>
167 <p>Some body you want to know...</p>
168 </body>
169 </html>"""
170 var subject = new TemplateString(tpl)
171 subject.replace("TITLE", "Hello World!")
172 subject.replace("BODY", "Some body you want to know...")
173 var res = subject.write_to_string
174 assert res == exp
175 end
176
177 fun test_tpl_replace_3 is test do
178 var tpl = """
179 <!DOCTYPE html>
180 <html lang="en">
181 <head>
182 <title>%TITLE%</title>
183 </head>
184 <body>
185 <h1>%TITLE%</h1>
186 <p>%BODY%</p>
187 </body>
188 </html>"""
189 var exp = """
190 <!DOCTYPE html>
191 <html lang="en">
192 <head>
193 <title>Hello World!</title>
194 </head>
195 <body>
196 <h1>Hello World!</h1>
197 <p><a href="#">Click me!</a></p>
198 </body>
199 </html>"""
200 var link = new Template
201 var subject = new TemplateString(tpl)
202 subject.replace("TITLE", "Hello World!")
203 subject.replace("BODY", link)
204 link.add "<a href=\"#\">"
205 link.add "Click me!"
206 link.add "</a>"
207 var res = subject.write_to_string
208 assert res == exp
209 end
210 end