Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Feb 2, 2024
1 parent ea48ff7 commit 023b06c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 18 deletions.
11 changes: 10 additions & 1 deletion test/test_aozora_accent_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_invalid
end
end

def test_use_jisx0213
def test_use_jisx0213_class
Aozora2Html::Tag::Accent.use_jisx0213 = true
str = "〔e'tiquette〕\r\n".to_sjis
strio = StringIO.new(str)
Expand All @@ -42,6 +42,15 @@ def test_use_jisx0213
assert_equal expected, parsed.to_s.to_utf8
end

def test_use_jisx0213
str = "〔e'tiquette〕\r\n".to_sjis
strio = StringIO.new(str)
stream = Jstream.new(strio)
parsed = Aozora2Html::AccentParser.new(stream, '〕'.to_sjis, {}, [], gaiji_dir: 'g_dir/', use_jisx0213: true).process
expected = '〔étiquette'
assert_equal expected, parsed.to_s.to_utf8
end

def teardown
Aozora2Html::Tag::Accent.use_jisx0213 = nil
end
Expand Down
112 changes: 98 additions & 14 deletions test/test_command_parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,108 +98,192 @@ def test_parse_command_warichu2
assert_equal expected, parsed
end

def test_parse_command_unicode
def test_parse_command_unicode_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "※[#「衄のへん+卩」、U+5379、287-2]\r\n"
parsed = parse_text(src)
expected = "&#x5379;<br />\r\n"
assert_equal expected, parsed
end

def test_parse_command_teisei1
def test_parse_command_unicode
src = "※[#「衄のへん+卩」、U+5379、287-2]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = "&#x5379;<br />\r\n"
assert_equal expected, parsed
end

def test_parse_command_teisei1_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "吹喋[#「喋」に「ママ」の注記]\r\n"
parsed = parse_text(src)
expected = "吹<ruby><rb>喋</rb><rp>(</rp><rt>ママ</rt><rp>)</rp></ruby><br />\r\n"
assert_equal expected, parsed
end

def test_parse_command_teisei2
def test_parse_command_teisei1
src = "吹喋[#「喋」に「ママ」の注記]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = "吹<ruby><rb>喋</rb><rp>(</rp><rt>ママ</rt><rp>)</rp></ruby><br />\r\n"
assert_equal expected, parsed
end

def test_parse_command_teisei2_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "紋附だとか[#「紋附だとか」は底本では「絞附だとか」]\r\n"
parsed = parse_text(src)
expected = %Q(紋附だとか<span class="notes">[#「紋附だとか」は底本では「絞附だとか」]</span><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_teisei3
def test_parse_command_teisei2
src = "紋附だとか[#「紋附だとか」は底本では「絞附だとか」]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(紋附だとか<span class="notes">[#「紋附だとか」は底本では「絞附だとか」]</span><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_teisei3_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "私は籠《ざる》[#ルビの「ざる」は底本では「さる」]をさげ\r\n"
parsed = parse_text(src)
expected = %Q(私は<ruby><rb>籠</rb><rp>(</rp><rt>ざる</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ざる」は底本では「さる」]</span>をさげ<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_teisei4
def test_parse_command_teisei3
src = "私は籠《ざる》[#ルビの「ざる」は底本では「さる」]をさげ\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(私は<ruby><rb>籠</rb><rp>(</rp><rt>ざる</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ざる」は底本では「さる」]</span>をさげ<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_teisei4_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "広場へに[#「広場へに」はママ]店でもだそう。\r\n"
parsed = parse_text(src)
expected = %Q(広場へに<span class="notes">[#「広場へに」はママ]</span>店でもだそう。<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_teisei5
def test_parse_command_teisei4
src = "広場へに[#「広場へに」はママ]店でもだそう。\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(広場へに<span class="notes">[#「広場へに」はママ]</span>店でもだそう。<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_teisei5_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "お湯《ゆう》[#ルビの「ゆう」はママ]\r\n"
parsed = parse_text(src)
expected = %Q(お<ruby><rb>湯</rb><rp>(</rp><rt>ゆう</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ゆう」はママ]</span><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_tcy
def test_parse_command_teisei5
src = "お湯《ゆう》[#ルビの「ゆう」はママ]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(お<ruby><rb>湯</rb><rp>(</rp><rt>ゆう</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ゆう」はママ]</span><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_tcy_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "米機B29[#「29」は縦中横]の編隊は、\r\n"
parsed = parse_text(src)
expected = %Q(米機B<span dir="ltr">29</span>の編隊は、<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_tcy2
def test_parse_command_tcy
src = "米機B29[#「29」は縦中横]の編隊は、\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(米機B<span dir="ltr">29</span>の編隊は、<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_tcy2_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "[#縦中横](※[#ローマ数字1、1-13-21])[#縦中横終わり]\r\n"
parsed = parse_text(src)
expected = %Q|<span dir="ltr">(<img src="../../../gaiji/1-13/1-13-21.png" alt="※(ローマ数字1、1-13-21)" class="gaiji" />)</span><br />\r\n|
assert_equal expected, parsed
end

def test_parse_command_kogaki
def test_parse_command_tcy2
src = "[#縦中横](※[#ローマ数字1、1-13-21])[#縦中横終わり]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q|<span dir="ltr">(<img src="../../../gaiji/1-13/1-13-21.png" alt="※(ローマ数字1、1-13-21)" class="gaiji" />)</span><br />\r\n|
assert_equal expected, parsed
end

def test_parse_command_kogaki_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "それ以上である。(5)[#「(5)」は行右小書き]\r\n"
parsed = parse_text(src)
expected = %Q(それ以上である。<sup class="superscript">(5)</sup><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_uetsuki
def test_parse_command_kogaki
src = "それ以上である。(5)[#「(5)」は行右小書き]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(それ以上である。<sup class="superscript">(5)</sup><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_uetsuki_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "22[#「2」は上付き小文字]\r\n"
parsed = parse_text(src)
expected = %Q(2<sup class="superscript">2</sup><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_bouki
def test_parse_command_uetsuki
src = "22[#「2」は上付き小文字]\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(2<sup class="superscript">2</sup><br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_bouki_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "支部長の顔にさっと血が流れ[#「血が流れ」に「×」の傍記]た\r\n"
parsed = parse_text(src)
expected = %Q(支部長の顔にさっと<ruby><rb>血が流れ</rb><rp>(</rp><rt>×&nbsp;×&nbsp;×&nbsp;×</rt><rp>)</rp></ruby>た<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_ruby
def test_parse_command_bouki
src = "支部長の顔にさっと血が流れ[#「血が流れ」に「×」の傍記]た\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(支部長の顔にさっと<ruby><rb>血が流れ</rb><rp>(</rp><rt>×&nbsp;×&nbsp;×&nbsp;×</rt><rp>)</rp></ruby>た<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_ruby_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
src = "グリーンランドの中央部八千尺の氷河地帯にあるといわれる、[#横組み]“Ser-mik-Suah《セルミク・シュアー》”[#横組み終わり]の冥路《よみじ》の国。\r\n"
parsed = parse_text(src)
expected = %Q(グリーンランドの中央部八千尺の氷河地帯にあるといわれる、<span class="yokogumi">“<ruby><rb>Ser-mik-Suah</rb><rp>(</rp><rt>セルミク・シュアー</rt><rp>)</rp></ruby>”</span>の<ruby><rb>冥路</rb><rp>(</rp><rt>よみじ</rt><rp>)</rp></ruby>の国。<br />\r\n)
assert_equal expected, parsed
end

def test_parse_command_ruby
src = "グリーンランドの中央部八千尺の氷河地帯にあるといわれる、[#横組み]“Ser-mik-Suah《セルミク・シュアー》”[#横組み終わり]の冥路《よみじ》の国。\r\n"
parsed = parse_text(src, use_unicode: true)
expected = %Q(グリーンランドの中央部八千尺の氷河地帯にあるといわれる、<span class="yokogumi">“<ruby><rb>Ser-mik-Suah</rb><rp>(</rp><rt>セルミク・シュアー</rt><rp>)</rp></ruby>”</span>の<ruby><rb>冥路</rb><rp>(</rp><rt>よみじ</rt><rp>)</rp></ruby>の国。<br />\r\n)
assert_equal expected, parsed
end

using Aozora2Html::StringRefinements

def parse_text(input_text)
def parse_text(input_text, use_jisx0213: nil, use_unicode: nil)
input = StringIO.new(input_text.to_sjis)
output = StringIO.new
parser = Aozora2Html.new(input, output)
parser = Aozora2Html.new(input, output, use_jisx0213: use_jisx0213, use_unicode: use_unicode)
parser.instance_eval { @section = :tail }
catch(:terminate) do
loop do
Expand Down
14 changes: 12 additions & 2 deletions test/test_gaiji_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,28 @@ def test_espcae!
assert_equal true, egt.escaped?
end

def test_jisx0213
def test_jisx0213_class
Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = true
egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-06-75', 'snowman', gaiji_dir: @gaiji_dir)
assert_equal '&#x2603;', egt.to_s.to_utf8
end

def test_use_unicode
def test_jisx0213
egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-06-75', 'snowman', gaiji_dir: @gaiji_dir, use_jisx0213: true)
assert_equal '&#x2603;', egt.to_s.to_utf8
end

def test_use_unicode_class
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-06-75', 'snowman', '2603', gaiji_dir: @gaiji_dir)
assert_equal '&#x2603;', egt.to_s.to_utf8
end

def test_use_unicode
egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-06-75', 'snowman', '2603', gaiji_dir: @gaiji_dir, use_unicode: true)
assert_equal '&#x2603;', egt.to_s.to_utf8
end

def teardown
Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = false
Aozora2Html::Tag::EmbedGaiji.use_unicode = false
Expand Down
11 changes: 10 additions & 1 deletion test/test_tag_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_parse_gaiji_kaeri
assert_equal expected, command.to_s.to_utf8
end

def test_parse_gaiji_jisx0213
def test_parse_gaiji_jisx0213_class
Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = true
str = "※[#「てへん+劣」、第3水準1-84-77]…\r\n".to_sjis
strio = StringIO.new(str)
Expand All @@ -76,6 +76,15 @@ def test_parse_gaiji_jisx0213
assert_equal expected, command.to_s.to_utf8
end

def test_parse_gaiji_jisx0213
str = "※[#「てへん+劣」、第3水準1-84-77]…\r\n".to_sjis
strio = StringIO.new(str)
stream = Jstream.new(strio)
command, _raw = Aozora2Html::TagParser.new(stream, '…'.to_sjis, {}, [], gaiji_dir: 'g_dir/', use_jisx0213: true).process
expected = '&#x6318;'
assert_equal expected, command.to_s.to_utf8
end

def teardown
Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = @jisx0213
end
Expand Down

0 comments on commit 023b06c

Please sign in to comment.