+public class SoneTextParserTest {
+
+ private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
+
+ @SuppressWarnings("static-method")
+ @Test
+ public void testPlainText() throws IOException {
+ /* check basic operation. */
+ Iterable<Part> parts = soneTextParser.parse("Test.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
+
+ /* check empty lines at start and end. */
+ parts = soneTextParser.parse("\nTest.\n\n", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
+
+ /* check duplicate empty lines in the text. */
+ parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
+ }
+
+ @SuppressWarnings("static-method")
+ @Test
+ public void testKSKLinks() throws IOException {
+ /* check basic links. */
+ Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
+
+ /* check embedded links. */
+ parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
+
+ /* check embedded links and line breaks. */
+ parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
+ }
+
+ @SuppressWarnings({ "synthetic-access", "static-method" })
+ @Test
+ public void testEmptyLinesAndSoneLinks() throws IOException {
+ SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
+
+ /* check basic links. */
+ Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
+ }
+
+ @SuppressWarnings({ "synthetic-access", "static-method" })
+ @Test
+ public void testEmpyHttpLinks() throws IOException {
+ SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
+
+ /* check empty http links. */
+ Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
+ }
+
+ @Test
+ public void httpLinkWithoutParensEndsAtNextClosingParen() {
+ Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]) – nice!"));
+ }
+
+ @Test
+ public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
+ Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc_(def)|example.sone/abc_(def)|example.sone/abc_(def)]) – nice!"));
+ }
+
+ @Test
+ public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
+ SoneTextParser soneTextParser = new SoneTextParser(null, null);
+ Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]. Nice!"));
+ }
+
+ @Test
+ public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
+ Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]... Nice!"));
+ }
+
+ @Test
+ public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
+ SoneTextParser soneTextParser = new SoneTextParser(null, null);
+ Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc], nice!"));
+ }