require 'minitest/autorun' require 'dtext/dtext' class DTextTest < Minitest::Test def assert_parse(expected, input) assert_equal(expected, DTextRagel.parse(input)) end def test_mentions assert_parse('

@bob

', "@bob") assert_parse('

hi @bob

', "hi @bob") assert_parse('

this is not @.@ @_@ @bob

', "this is not @.@ @_@ @bob") assert_parse('

this is an email@address.com and should not trigger

', "this is an email@address.com and should not trigger") assert_parse('

multiple @bob @anna

', "multiple @bob @anna") assert_equal('

hi @bob

', DTextRagel.parse("hi @bob", :disable_mentions => true)) end def test_sanitize_heart assert_parse('

<3

', "<3") end def test_sanitize_less_than assert_parse('

<

', "<") end def test_sanitize_greater_than assert_parse('

>

', ">") end def test_sanitize_ampersand assert_parse('

&

', "&") end def test_wiki_links assert_parse("

a b c

", "a [[b]] c") end def test_wiki_links_spoiler assert_parse("

a spoiler c

", "a [[spoiler]] c") end def test_wiki_links_nested_b assert_parse("

[[tag]]

", "[b][[[/b]tag[b]]][/b]") end def test_spoilers_inline assert_parse("

this is an inline spoiler.

", "this is [spoiler]an inline spoiler[/spoiler].") end def test_spoilers_block assert_parse("

this is

a block spoiler

.

", "this is\n\n[spoiler]\na block spoiler\n[/spoiler].") end def test_spoilers_with_no_closing_tag_1 assert_parse("

this is a spoiler with no closing tag

new text

", "[spoiler]this is a spoiler with no closing tag\n\nnew text") end def test_spoilers_with_no_closing_tag_2 assert_parse("

this is a spoiler with no closing tag
new text

", "[spoiler]this is a spoiler with no closing tag\nnew text") end def test_spoilers_with_no_closing_tag_block assert_parse("

this is a block spoiler with no closing tag

", "[spoiler]\nthis is a block spoiler with no closing tag") end def test_spoilers_nested assert_parse("

this is a nested spoiler

", "[spoiler]this is [spoiler]a nested[/spoiler] spoiler[/spoiler]") end def test_paragraphs assert_parse("

abc

", "abc") end def test_paragraphs_with_newlines_1 assert_parse("

a
b
c

", "a\nb\nc") end def test_paragraphs_with_newlines_2 assert_parse("

a

b

", "a\n\nb") end def test_headers assert_parse("

header

", "h1. header") assert_parse("

header

", "* a\n\nh1. header\n* list") end def test_headers_with_ids assert_parse("

header

", "h1#blah-blah. header") end def test_headers_with_ids_with_quote assert_parse("

h1#blah-"blah. header

", "h1#blah-\"blah. header") end def test_quote_blocks assert_parse('

test

', "[quote]\ntest\n[/quote]") end def test_quote_blocks_with_list assert_parse("

abc

", "[quote]\n* hello\n* there\n[/quote]\nabc") assert_parse("

abc

", "[quote]\n* hello\n* there\n\n[/quote]\nabc") end def test_quote_blocks_nested assert_parse("

a

b

c

", "[quote]\na\n[quote]\nb\n[/quote]\nc\n[/quote]") end def test_quote_blocks_nested_spoiler assert_parse("

a
blah
c

", "[quote]\na\n[spoiler]blah[/spoiler]\nc[/quote]") assert_parse("

a

blah

c

", "[quote]\na\n\n[spoiler]blah[/spoiler]\n\nc[/quote]") end def test_quote_blocks_nested_expand assert_parse("

a

b

c

", "[quote]\na\n[expand]\nb\n[/expand]\nc\n[/quote]") end def test_code assert_parse("
for (i=0; i<5; ++i) {\n  printf(1);\n}\n\nexit(1);
", "[code]for (i=0; i<5; ++i) {\n printf(1);\n}\n\nexit(1);") end def test_urls assert_parse('

a http://test.com b

', 'a http://test.com b') end def test_urls_with_newline assert_parse('

http://test.com
b

', "http://test.com\nb") end def test_urls_with_paths assert_parse('

a http://test.com/~bob/image.jpg b

', 'a http://test.com/~bob/image.jpg b') end def test_urls_with_fragment assert_parse('

a http://test.com/home.html#toc b

', 'a http://test.com/home.html#toc b') end def test_auto_urls assert_parse('

a http://test.com. b

', 'a http://test.com. b') end def test_auto_urls_in_parentheses assert_parse('

a (http://test.com) b

', 'a (http://test.com) b') end def test_old_style_links assert_parse('

test

', '"test":http://test.com') end def test_old_style_links_with_inline_tags assert_parse('

test

', '"[i]test[/i]":http://test.com') end def test_old_style_links_with_special_entities assert_parse('

"1" 2 & 3

', '"1" "2 & 3":http://three.com') end def test_new_style_links assert_parse('

test

', '"test":[http://test.com]') end def test_new_style_links_with_parentheses assert_parse('

test

', '"test":[http://test.com/(parentheses)]') assert_parse('

(test)

', '("test":[http://test.com/(parentheses)])') assert_parse('

[test]

', '["test":[http://test.com/(parentheses)]]') end def test_lists_1 assert_parse('', '* a') end def test_lists_2 assert_parse('', "* a\n* b") end def test_lists_nested assert_parse('', "* a\n** b") end def test_lists_inline assert_parse('', "* post #1") end def test_lists_not_preceded_by_newline assert_parse('

a
b

', "a\nb\n* c\n* d") end def test_lists_with_multiline_items assert_parse('

a

another one

', "a\n* b\nc\n* d\ne\n\nanother one") assert_parse('

a

another one

', "a\n* b\nc\n** d\ne\n\nanother one") end def test_inline_tags assert_parse('

tag

', "{{tag}}") end def test_inline_tags_conjunction assert_parse('

tag1 tag2

', "{{tag1 tag2}}") end def test_inline_tags_special_entities assert_parse('

<3

', "{{<3}}") end def test_extra_newlines assert_parse('

a

b

', "a\n\n\n\n\n\n\nb\n\n\n\n") end def test_complex_links_1 assert_parse("

2 3 | 5 6

", "[[1|2 3]] | [[4|5 6]]") end def test_complex_links_2 assert_parse("

Tags (Tagging Guidelines | Tag Checklist | Tag Groups)

", "Tags [b]([[howto:tag|Tagging Guidelines]] | [[howto:tag_checklist|Tag Checklist]] | [[Tag Groups]])[/b]") end def test_table assert_parse("
header
post #100
", "[table][thead][tr][th]header[/th][/tr][/thead][tbody][tr][td]post #100[/td][/tr][/tbody][/table]") end def test_table_with_newlines assert_parse("
header
post #100
", "[table]\n[thead]\n[tr]\n[th]header[/th][/tr][/thead][tbody][tr][td]post #100[/td][/tr][/tbody][/table]") end def test_forum_links assert_parse('

topic #1234/p4

', "topic #1234/p4") end def test_boundary_exploit assert_parse('

@mack<

', "@mack<") end def test_expand assert_parse("

hello world

", "[expand]hello world[/expand]") end def test_aliased_expand assert_parse("
hello

blah blah

", "[expand=hello]blah blah[/expand]") end def test_expand_with_nested_code assert_parse("
hello\n
", "[expand]\n[code]\nhello\n[/code]\n[/expand]") end def test_expand_with_nested_list assert_parse("

c

", "[expand]\n* a\n* b\n[/expand]\nc") end def test_inline_mode assert_equal("hello", DTextRagel.parse_inline("hello").strip) end def test_strip assert_equal("hellozworld", DTextRagel.parse_strip("h[b]e[/b]llo[quote]z[/quote]wo[expand]rld[/expand]")) end def test_old_asterisks assert_parse("

hello *world* neutral

", "hello *world* neutral") end def test_utf8_mentions assert_parse('

@葉月

', "@葉月") assert_parse('

Hello @葉月 and @Alice

', "Hello @葉月 and @Alice") assert_parse('

Should not parse 葉月@葉月

', "Should not parse 葉月@葉月") end def test_utf8_links assert_parse('

7893

', '"7893":/posts?tags=approver:葉月') assert_parse('

7893

', '"7893":[/posts?tags=approver:葉月]') assert_parse('

http://danbooru.donmai.us/posts?tags=approver:葉月

', 'http://danbooru.donmai.us/posts?tags=approver:葉月') end end