/
RenderTagSpec.java
136 lines (108 loc) · 5.24 KB
/
RenderTagSpec.java
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
/*
Copyright 2009-2016 Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.javalite.activeweb.freemarker;
import org.javalite.activeweb.ViewException;
import freemarker.template.TemplateException;
import org.javalite.test.jspec.JSpecSupport;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import static org.javalite.common.Collections.*;
/**
* @author Igor Polevoy
*/
public class RenderTagSpec implements JSpecSupport {
//private Configuration cfg;
FreeMarkerTemplateManager manager = new FreeMarkerTemplateManager();
@Before
public void before() throws IOException {
manager.setTemplateLocation("src/test/views");
}
@Test(expected = ViewException.class)
public void shouldThrowExceptionIfPartialAttributeMissing() {
StringWriter sw = new StringWriter();
manager.merge(new HashMap(), "/partial/main_missing_partial_attribute", sw);
}
@Test
public void shouldRenderPartialRelativeToContainerTemplate() {
StringWriter sw = new StringWriter();
manager.merge(map("fruit", "apple"), "/partial/main_with_simple_partial", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple ");
}
@Test
public void shouldRenderSharedPartial() {
StringWriter sw = new StringWriter();
manager.merge(map("fruit", "apple"), "/partial/main_with_shared_partial", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple ");
}
@Test(expected = ViewException.class)
public void shouldRejectIncorrectlyNamedSharedPartial() {
StringWriter sw = new StringWriter();
manager.merge(map("fruit", "apple"), "/partial/main_with_incorrectly_named_shared_partial", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple");
}
@Test
public void shouldRenderPartialWithCollection() {
StringWriter sw = new StringWriter();
manager.merge(map("fruits", li("apple", "prune", "pear")), "/partial/main_with_collection_partial", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple and the fruit is: prune and the fruit is: pear ");
}
@Test
public void shouldRenderPartialWithCollectionAndCounter() {
StringWriter sw = new StringWriter();
manager.merge(map("fruits", li("apple", "prune", "pear")), "/partial/main_with_collection_partial_and_counter", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple and the count is: 0 " +
"and the fruit is: prune and the count is: 1 " +
"and the fruit is: pear and the count is: 2 ");
}
@Test
public void shouldRenderSharedPartialWithCollection(){
StringWriter sw = new StringWriter();
manager.merge(map("fruits", li("apple", "prune", "pear")), "/partial/main_with_shared_collection_partial", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple and the fruit is: prune and the fruit is: pear ");
}
@Test
public void shouldRenderSpacerWithCollectionPartial(){
StringWriter sw = new StringWriter();
manager.merge(map("fruits", li("apple", "prune", "pear")), "/partial/main_with_collection_partial_and_spacer", sw);
a(sw.toString()).shouldBeEqual("and the fruit is: apple " +
"=========================== " +
"and the fruit is: prune " +
"=========================== " +
"and the fruit is: pear");
}
@Test
public void shouldPassSimpleParameterToPartial(){
StringWriter sw = new StringWriter();
manager.merge(map("fruit_name", "pear"), "/partial/main_with_simple_partial_and_parameter", sw);
a(sw.toString()).shouldEqual("and the fruit name is: pear");
}
@Test
public void shouldPassSimpleParameterToPartialWithCollection(){
StringWriter sw = new StringWriter();
manager.merge(map("fruits", li("apple", "prune", "pear")), "/partial/main_with_collection_and_simple_parameters_for_partial", sw);
a(sw.toString()).shouldEqual("and the fruit is: apple, berry: blueberry " +
"and the fruit is: prune, berry: blueberry " +
"and the fruit is: pear, berry: blueberry");
}
@Test
public void shouldCheckFirstAndLastWithCollection(){
StringWriter sw = new StringWriter();
manager.merge(map("fruits", li("apple", "prune", "pear")), "/partial/main_with_collection_partial_with_first_and_last", sw);
a(sw.toString()).shouldBeEqual( "and the fruit is: apple, first: true, last: false " +
"and the fruit is: prune, first: false, last: false " +
"and the fruit is: pear, first: false, last: true");
}
}