1
1
package j2html .tags ;
2
2
3
3
import j2html .Config ;
4
+ import j2html .attributes .Attribute ;
4
5
import j2html .model .DynamicHrefAttribute ;
5
6
import java .io .File ;
6
7
import java .io .FileWriter ;
7
8
8
9
import j2html .tags .specialized .manual .HtmlTag ;
10
+ import org .junit .After ;
11
+ import org .junit .Before ;
9
12
import org .junit .Test ;
10
13
import static j2html .TagCreator .body ;
11
14
import static j2html .TagCreator .div ;
12
15
import static j2html .TagCreator .footer ;
13
16
import static j2html .TagCreator .header ;
14
17
import static j2html .TagCreator .html ;
15
18
import static j2html .TagCreator .iff ;
16
- import static j2html .TagCreator .img ;
17
- import static j2html .TagCreator .input ;
18
19
import static j2html .TagCreator .main ;
19
20
import static j2html .TagCreator .p ;
20
21
import static j2html .TagCreator .tag ;
21
22
import static org .hamcrest .MatcherAssert .assertThat ;
22
23
import static org .hamcrest .Matchers .is ;
24
+ import static org .junit .Assert .fail ;
23
25
24
26
public class TagTest {
25
27
28
+ @ Before
29
+ public void setUp (){
30
+ Config .closeEmptyTags = false ;
31
+ }
32
+
33
+ @ After
34
+ public void tearDown (){
35
+ // Restore Config defaults.
36
+ Config .closeEmptyTags = false ;
37
+ }
38
+
39
+ // TODO Introduce a different concept for a sequence of tags, and require valid names for all Tags.
40
+
41
+ @ Test
42
+ public void unnamed_containers_do_not_render_tags_or_attributes (){
43
+ Tag tag = new ContainerTag (null ).attr ("xyz" , "123" );
44
+ assertThat (tag .render (), is ("" ));
45
+ }
46
+
47
+ @ Test
48
+ public void unnamed_containers_render_children (){
49
+ ContainerTag tag = new ContainerTag (null )
50
+ .with (new EmptyTag ("abc" ))
51
+ .with (new ContainerTag ("def" ))
52
+ .withText ("ghi" );
53
+ assertThat (tag .render (), is ("<abc><def></def>ghi" ));
54
+ }
55
+
56
+ @ Test
57
+ public void named_containers_without_children_only_render_tags_and_attributes (){
58
+ Tag tag = new ContainerTag ("abc" ).attr ("xyz" , "123" );
59
+ assertThat (tag .render (), is ("<abc xyz=\" 123\" ></abc>" ));
60
+ }
61
+
62
+ @ Test
63
+ public void populated_named_containers_render_tags_attributes_and_children (){
64
+ Tag tag = new ContainerTag ("abc" )
65
+ .with (new EmptyTag ("def" ))
66
+ .with (new ContainerTag ("ghi" ))
67
+ .withText ("jkl" )
68
+ .attr ("xyz" , "123" );
69
+ assertThat (tag .render (), is ("<abc xyz=\" 123\" ><def><ghi></ghi>jkl</abc>" ));
70
+ }
71
+
72
+ @ Test
73
+ public void empty_tags_must_be_named (){
74
+ try {
75
+ new EmptyTag (null );
76
+ fail ("Exception was not thrown." );
77
+ }catch (IllegalArgumentException e ){
78
+ assertThat (e .getMessage (), is ("Illegal tag name: null" ));
79
+ }
80
+
81
+ try {
82
+ new EmptyTag ("" );
83
+ fail ("Exception was not thrown." );
84
+ }catch (IllegalArgumentException e ){
85
+ assertThat (e .getMessage (), is ("Illegal tag name: \" \" " ));
86
+ }
87
+ }
88
+
26
89
@ Test
27
- public void testRender () throws Exception {
90
+ public void empty_tags_can_be_configured_to_self_close (){
91
+ // By default they will not be self-closing.
92
+ assertThat (new EmptyTag ("xyz" ).render (), is ("<xyz>" ));
93
+
94
+ Config .closeEmptyTags = true ;
95
+ assertThat (new EmptyTag ("xyz" ).render (), is ("<xyz/>" ));
96
+ }
97
+
98
+ @ Test
99
+ public void attributes_are_rendered_in_the_order_that_they_are_defined (){
100
+ Tag container = new ContainerTag ("abc" )
101
+ .attr ("a" ,"A" )
102
+ .attr (new Attribute ("b" ,"B" ))
103
+ .attr ("c" );
104
+ assertThat (container .render (), is ("<abc a=\" A\" b=\" B\" c></abc>" ));
105
+
106
+ Tag tag = new EmptyTag ("abc" )
107
+ .attr ("c" )
108
+ .attr (new Attribute ("b" ,"B" ))
109
+ .attr ("a" ,"A" );
110
+ assertThat (tag .render (), is ("<abc c b=\" B\" a=\" A\" >" ));
111
+ }
112
+
113
+ @ Test
114
+ public void testRender () {
28
115
ContainerTag testTag = new ContainerTag ("a" );
29
116
testTag .setAttribute ("href" , "http://example.com" );
30
117
assertThat (testTag .render (), is ("<a href=\" http://example.com\" ></a>" ));
@@ -40,33 +127,6 @@ public void testRender() throws Exception {
40
127
assertThat (complexTestTag .render (), is ((expectedResult )));
41
128
}
42
129
43
- @ Test
44
- public void testOpenTag () throws Exception {
45
- ContainerTag testTag = new ContainerTag ("a" );
46
- assertThat (testTag .renderOpenTag (), is ("<a>" ));
47
-
48
- ContainerTag complexTestTag = new ContainerTag ("input" );
49
- complexTestTag .attr ("type" ,"password" ).withId ("password" )
50
- .attr ("name" ,"password" )
51
- .attr ("placeholder" ,"Password" ).attr ("required" );
52
- String expectedResult = "<input type=\" password\" id=\" password\" name=\" password\" placeholder=\" Password\" required>" ;
53
- assertThat (complexTestTag .renderOpenTag (), is (expectedResult ));
54
- }
55
-
56
- @ Test
57
- public void testCloseTag () throws Exception {
58
- ContainerTag testTag = new ContainerTag ("a" );
59
- assertThat (testTag .renderCloseTag (), is ("</a>" ));
60
- }
61
-
62
- @ Test
63
- public void testSelfClosingTags () throws Exception {
64
- Config .closeEmptyTags = true ;
65
- assertThat (img ().withSrc ("/test.png" ).render (), is ("<img src=\" /test.png\" />" ));
66
- assertThat (input ().attr ("type" ,"text" ).render (), is ("<input type=\" text\" />" ));
67
- Config .closeEmptyTags = false ;
68
- }
69
-
70
130
@ Test
71
131
public void testFormattedTags () throws Exception { // better test in ComplexRenderTest.java
72
132
assertThat (div (p ("Hello" )).renderFormatted (), is ("<div>\n <p>\n Hello\n </p>\n </div>\n " ));
@@ -80,13 +140,18 @@ public void testEquals() throws Exception {
80
140
}
81
141
82
142
@ Test
83
- public void testAcceptObjectValueAttribute () throws Exception {
84
- Tag complexTestTag = new ContainerTag ("input" )
85
- .attr ("attr1" , "value1" )
86
- .attr ("attr2" , 2 )
87
- .attr ("attr3" , null );
88
- String expectedResult = "<input attr1=\" value1\" attr2=\" 2\" attr3>" ;
89
- assertThat (complexTestTag .renderOpenTag (), is (expectedResult ));
143
+ public void attribute_values_are_converted_to_strings () throws Exception {
144
+ Tag container = new ContainerTag ("abc" )
145
+ .attr ("string" , "value1" )
146
+ .attr ("integer" , 2 )
147
+ .attr ("none" , null );
148
+ assertThat (container .render (), is ("<abc string=\" value1\" integer=\" 2\" none></abc>" ));
149
+
150
+ Tag tag = new EmptyTag ("abc" )
151
+ .attr ("string" , "value1" )
152
+ .attr ("integer" , 2 )
153
+ .attr ("none" , null );
154
+ assertThat (tag .render (), is ("<abc string=\" value1\" integer=\" 2\" none>" ));
90
155
}
91
156
92
157
@ Test
0 commit comments