From 2f63b798e549d13f2d89d30406b2c14500c91b66 Mon Sep 17 00:00:00 2001 From: Takiguchi Date: Tue, 6 Aug 2019 18:09:30 +0200 Subject: [PATCH] Add unit tests for parser service. --- .../core/services/ParserServiceTest.java | 330 ++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 src/test/java/org/codiki/core/services/ParserServiceTest.java diff --git a/src/test/java/org/codiki/core/services/ParserServiceTest.java b/src/test/java/org/codiki/core/services/ParserServiceTest.java new file mode 100644 index 0000000..ce0e3c3 --- /dev/null +++ b/src/test/java/org/codiki/core/services/ParserServiceTest.java @@ -0,0 +1,330 @@ +package org.codiki.core.services; + +import org.apache.commons.lang.StringEscapeUtils; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ParserServiceTest { + + private ParserService parserService; + + @Before + public void setUp() { + parserService = new ParserService(); + } + + /* *********************************************************** */ + /* H E A D E R S P A R S I N G */ + /* *********************************************************** */ + + @Test + public void testParseHeaders_h1() { + final String strInit = "[h1]Header[/h1]"; + final String strExpected = "

Header

"; + + assertThat(parserService.parseHeaders(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseHeaders_h2() { + final String strInit = "[h2]Header[/h2]"; + final String strExpected = "

Header

"; + + assertThat(parserService.parseHeaders(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseHeaders_h3() { + final String strInit = "[h3]Header[/h3]"; + final String strExpected = "

Header

"; + + assertThat(parserService.parseHeaders(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseHeader_onlyCoupleOfTags() { + final String strInit = "[h1][h1]Test[/h1]"; + final String strExpected = "[h1]

Test

"; + + assertThat(parserService.parseHeaders(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseHeader_multipleCouples() { + final String strInit = "[h1]Test1[/h1] [h1]Test2[/h1]"; + final String strExpected = "

Test1

Test2

"; + + assertThat(parserService.parseHeaders(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseHeaders() { + final String strInit = "[h1]Header1[/h1]\n" + + " Some text in header1\n" + + " [h2]Header2[/h2]\n" + + " Some text in header2\n" + + " [h3]Header3[/h3]\n" + + " Some text in header3\n" + + " [h3][h3]Header3[/h3]\n" + + " Some text in header3\n" + + " [h2]Header2[/h2]\n" + + " Some text in header2\n" + + " [h3]Header3[/h3][/h3]\n" + + " Some text in header3\n" + + " [h1]Header1[/h1]\n" + + " Some text in header1"; + + final String strExpected = "

Header1

\n" + + " Some text in header1\n" + + "

Header2

\n" + + " Some text in header2\n" + + "

Header3

\n" + + " Some text in header3\n" + + " [h3]

Header3

\n" + + " Some text in header3\n" + + "

Header2

\n" + + " Some text in header2\n" + + "

Header3[/h3]

\n" + + " Some text in header3\n" + + "

Header1

\n" + + " Some text in header1"; + + assertThat(parserService.parseHeaders(strInit)).isEqualTo(strExpected); + } + + /* *********************************************************** */ + /* B A C K S P A C E S P A R S I N G */ + /* *********************************************************** */ + + @Test + public void testParseBackSpaces() { + final String strInit = "Hello\nworld!"; + final String strExpected = "Hello
world!"; + + assertThat(parserService.parseBackSpaces(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseBackSpaces_endTagCode() { + final String strInit = "[/code]\n\n"; + final String strExpected = "[/code]\n"; + + assertThat(parserService.parseBackSpaces(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseBackSpaces_endTagCode_2() { + final String strInit = "[/code]\n"; + final String strExpected = "[/code]
"; + + assertThat(parserService.parseBackSpaces(strInit)).isEqualTo(strExpected); + } + + /* *********************************************************** */ + /* I M A G E S P A R S I N G */ + /* *********************************************************** */ + + @Test + public void testParseImages_simpleImg() { + final String strInit = StringEscapeUtils.escapeHtml("[img src=\"pathToImage\" /]"); + final String strExpected = "\"\""; + + assertThat(parserService.parseImages(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseImages_subTagAlt() { + final String strInit = StringEscapeUtils.escapeHtml("[img src=\"pathToImage\" alt=\"alternative\" /]"); + final String strExpected = "\"alternative\""; + + assertThat(parserService.parseImages(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseImages_multipleImages() { + final String strInit = StringEscapeUtils.escapeHtml("[img src=\"pathToImage1\" /]\n" + + " [img src=\"pathToImage2\" /]"); + final String strExpected = "\"\"\n" + + " \"\""; + + assertThat(parserService.parseImages(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseImages() { + final String strInit = StringEscapeUtils.escapeHtml("[img src=\"pathToImage1\" /]\n" + + " [img src=\"pathToImage2\" alt=\"alternativeOfImage2\" /]\n" + + " [img src=\"pathToImage3\" lgd=\"legendOfImage3\" /]\n" + + " [img src=\"pathToImage4\" alt=\"alternativeOfImage4\" lgd=\"legendOfImage4\" /]\n" + + " [img src=\"pathToImage5\" \" /]\n" + + " [img src=\"pathToImage6\" src=\"pathToImage6_2\" /]\n" + + " [img src=\"pathToImage7\" alt=\"alternativeOfImage7\" alt=\"alternativeOfImage7_2\" /]\n" + + " [img src=\"pathToImage8\" alt=\"legendOfImage8\" alt=\"legendOfImage8_2\" /]\n" + + " [img alt=\"alternativeOfImage9\" src=\"pathToImage9\" lgd=\"legendOfImage9\" /]\n" + + " [img lgd=\"legendOfImage10\" src=\"pathToImage10\" alt=\"alternativeOfImage10\" /]\n" + + " [img lgd=\"legendOfImage11\" alt=\"alternativeOfImage11\" src=\"pathToImage11\" /]"); + + final String strExpected = "\"\"\n" + + " \"alternativeOfImage2\"\n" + + " [img src="pathToImage3" lgd="legendOfImage3" /]\n" + + " [img src="pathToImage4" alt="alternativeOfImage4" lgd="legendOfImage4" /]\n" + + " [img src="pathToImage5" " /]\n" + + " [img src="pathToImage6" src="pathToImage6_2" /]\n" + + " [img src="pathToImage7" alt="alternativeOfImage7" alt="alternativeOfImage7_2" /]\n" + + " [img src="pathToImage8" alt="legendOfImage8" alt="legendOfImage8_2" /]\n" + + " [img alt="alternativeOfImage9" src="pathToImage9" lgd="legendOfImage9" /]\n" + + " [img lgd="legendOfImage10" src="pathToImage10" alt="alternativeOfImage10" /]\n" + + " [img lgd="legendOfImage11" alt="alternativeOfImage11" src="pathToImage11" /]"; + + assertThat(parserService.parseImages(strInit)).isEqualTo(strExpected); + } + + /* *********************************************************** */ + /* L I N K S P A R S I N G */ + /* *********************************************************** */ + + @Test + public void testParseLinks_simpleLink() { + final String strInit = StringEscapeUtils.escapeHtml("[link href=\"pathToLink\" txt=\"textOfLink\" /]"); + final String strExpected = "textOfLink"; + + assertThat(parserService.parseLinks(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseLinks_multipleLinks() { + final String strInit = StringEscapeUtils.escapeHtml("[link href=\"pathToLink1\" txt=\"textOfLink1\" /]\n" + + " [link href=\"pathToLink2\" txt=\"textOfLink2\" /]"); + final String strExpected = "textOfLink1\n" + + " textOfLink2"; + + assertThat(parserService.parseLinks(strInit)).isEqualTo(strExpected); + } + + /* *********************************************************** */ + /* C O D E P A R S I N G */ + /* *********************************************************** */ + + @Test + public void testParseCode_simpleCode() { + final String strInit = parserService.parseBackSpaces(StringEscapeUtils.escapeHtml("[code lg=\"python\"]\n" + + "def foo():\n" + + " return \"Hello world!\"\n" + + "[/code]\n" + + "\n")); + final String strExpected = "
def foo():\n    return "Hello world!"\n
"; + + assertThat(parserService.parseCode(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseCode_multipleCodes() { + final String strInit = parserService.parseBackSpaces(StringEscapeUtils.escapeHtml("[code lg=\"python\"]\n" + + "def foo():\n" + + " return \"Hello world!\"\n" + + "[/code]\n" + + "\n" + + "[code lg=\"java\"]\n" + + "public static void main(final String... pArgs) {\n" + + " System.out.println(\"Hello world!\");\n" + + "}\n" + + "[/code]\n" + + "\n")); + final String strExpected = "
def foo():\n    return "Hello world!"\n
public static void main(final String... pArgs) {\n    System.out.println("Hello world!");\n}\n
"; + + assertThat(parserService.parseCode(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseLinks_backSlashAndCodeParsing() { + final String strInit = parserService.parseBackSpaces(StringEscapeUtils.escapeHtml("[code lg=\"python\"]\n" + + "def foo():\n" + + " return \"Hello world!\"\n" + + "[/code]\n" + + "\n")); + final String strExpected = "
def foo():\n    return "Hello world!"\n
"; + + assertThat(parserService.parseCode(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParseLinks_backSlashAndCodeParsing_multiple() { + final String strInit = parserService.parseBackSpaces(StringEscapeUtils.escapeHtml("[code lg=\"python\"]\n" + + "def foo():\n" + + " return \"Hello world!\"\n" + + "[/code]\n" + + "\n" + + "[code lg=\"java\"]\n" + + "public static void main(final String... pArgs) {\n" + + " System.out.println(\"Hello world!\");\n" + + "}\n" + + "[/code]\n" + + "\n")); + final String strExpected = "
def foo():\n    return "Hello world!"\n
public static void main(final String... pArgs) {\n    System.out.println("Hello world!");\n}\n
"; + + assertThat(parserService.parseCode(strInit)).isEqualTo(strExpected); + } + + @Test + public void testParse() { + final String strInit = "\n" + + "[h1]Title 1[/h1]\n" + + "[h2]Title 2[/h2]\n" + + "[h3]Title 3[/h3]\n" + + "Some text on multiple\n" + + "lines\n" + + "like\n" + + "here!\n" + + "\n" + + "[img src=\"pathToImage\" alt=\"alternativeOfImage\" /]\n" + + "\n" + + "[code lg=\"java\"]\n" + + "public static void main(final String... pArgs) {\n" + + " System.out.println(\"Hello world!\");\n" + + "}\n" + + "[/code]\n" + + "\n" + + "\n" + + "[link href=\"pathToLink1\" txt=\"textOfLink1\" /]\n" + + "\n" + + "[img src=\"pathToImage2\" alt=\"alternativeOfImage2\" lgd=\"legendOfImage2\" /]\n" + + "[link href=\"pathToLink2\" txt=\"textOfLink2\" /]\n" + + "\n" + + "[code lg=\"python\"]\n" + + "def foo():\n" + + " return \"Hello world!\"\n" + + "[/code]\n" + + "\n" + + ""; + + final String strExpected = "
" + + "

Title 1


" + + "

Title 2


" + + "

Title 3


" + + "Some text on multiple
" + + "lines
" + + "like
" + + "here!
" + + "
" + + "\"alternativeOfImage\"
" + + "
" + + "
" +
+                "public static void main(final String... pArgs) {\n" +
+                "    System.out.println("Hello world!");\n" +
+                "}\n" +
+                "

" + + "textOfLink1
" + + "
" + + "[img src="pathToImage2" alt="alternativeOfImage2" lgd="legendOfImage2" /]
" + + "textOfLink2
" + + "
" + + "
" +
+                "def foo():\n" +
+                "    return "Hello world!"\n" +
+                "
"; + + assertThat(parserService.parse(strInit)).isEqualTo(strExpected); + } +}