|
| 1 | +package j2html.tags; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import org.junit.Test; |
| 5 | +import static j2html.TagCreator.div; |
| 6 | +import static j2html.TagCreator.each; |
| 7 | +import static j2html.TagCreator.li; |
| 8 | +import static j2html.TagCreator.p; |
| 9 | +import static j2html.TagCreator.pre; |
| 10 | +import static j2html.TagCreator.textarea; |
| 11 | +import static j2html.TagCreator.ul; |
| 12 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 13 | +import static org.hamcrest.Matchers.is; |
| 14 | + |
| 15 | +public class RenderFormattedTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testFormattedTags() throws Exception { |
| 19 | + assertThat(div(p("Hello")).renderFormatted(), is("<div>\n <p>\n Hello\n </p>\n</div>\n")); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testFormattedTags_doesntFormatPre() throws Exception { |
| 24 | + assertThat(div(pre("public void renderModel(Appendable writer, Object model) throws IOException {\n" + |
| 25 | + " writer.append(text);\n" + |
| 26 | + " }")).renderFormatted(), is("<div>\n" + |
| 27 | + " <pre>public void renderModel(Appendable writer, Object model) throws IOException {\n" + |
| 28 | + " writer.append(text);\n" + |
| 29 | + " }</pre>\n" + |
| 30 | + "</div>\n")); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testFormattedTags_doesntFormatTextArea() throws Exception { |
| 35 | + assertThat(div(textarea("fred\ntom")).renderFormatted(), is("<div>\n" + |
| 36 | + " <textarea>fred\n" + |
| 37 | + "tom</textarea>\n" + |
| 38 | + "</div>\n")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testFormattedTags_each() throws Exception { |
| 43 | + assertThat(ul(each(ImmutableList.of(1, 2, 3), i -> li("Number " + i))).renderFormatted(), is( |
| 44 | + "<ul>\n" + |
| 45 | + " \n" + |
| 46 | + " <li>\n" + |
| 47 | + " Number 1\n" + |
| 48 | + " </li>\n" + |
| 49 | + " <li>\n" + |
| 50 | + " Number 2\n" + |
| 51 | + " </li>\n" + |
| 52 | + " <li>\n" + |
| 53 | + " Number 3\n" + |
| 54 | + " </li>\n" + |
| 55 | + " \n" + |
| 56 | + "</ul>\n" |
| 57 | + )); |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments