WikiLineクラスを作る
本体を作る(wikiline.rb)
class WikiLine end
テストを作る
$ c2t.rb WikiLine wikiline.rb > test-wikiline.rb require 'rubyunit' require 'wikiline.rb' class TestWikiLine < RUNIT::TestCase def test_new_s assert_instance_of(WikiLine,WikiLine.new) end end
テストを実行する
$ ruby test-wikiline.rb TestWikiLine#test_new_s . Time: 0.000786 OK (1/1 tests 1 asserts)
eachのテストを作る
def test_each_normal r="" assert_equals(<<ANSWER,(r="";WikiLine.new(<<SOURCE).each{|a|r+="---\n#{a}\n"};r)) --- a --- 漢字かな --- efg ANSWER a 漢字かな efg SOURCE end
eachをとりあえず実装する
def each @s.split("\n").each{|line| yield line } end
eachをとりあえずテストしてみる。
$ ruby test-wikiline.rb test_each_normal TestWikiLine#test_each_normal . Time: 0.000954 OK (1/1 tests 1 asserts)
eachのtableタイプのテストを書いてみる
def test_each_table r="" assert_equals(<<ANSWER,(r="";WikiLine.new(<<SOURCE).each{|a|r+="---\n#{a}\n"};r)) --- abc --- |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| --- abd ANSWER abc |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| abd SOURCE end
テストしてみる
$ ruby test-wikiline.rb test_each_table TestWikiLine#test_each_table F. Time: 0.002407 FAILURES!!! Test Results: Run: 1/1(1 asserts) Failures: 1 Errors: 0 Failures: 1 test-wikiline.rb:18:in `test_each_table'(TestWikiLine): expected:<--- abc --- |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| --- abd > but was:<--- abc --- |項目1|項目2|項目3|項目4|項目5|・・・・| --- |項目6|項目7|項目8|項目9|項目10|・・・・| --- abd > (RUNIT::AssertionFailedError) from test-wikiline.rb:16
もちろん失敗するよな・・・作ってないもん(笑)
eachを真面目に実装してみる
SpecialChar={' '=>'pre','|'=>'table',','=>'csv','>'=>'indent','='=>'nlist','-'=>'list'} def each s=@s.split("\n") while a=s.shift while !s.empty?&&SpecialChar[a[0..0]]&&SpecialChar[a[0..0]]==SpecialChar[s[0][0..0]] a+="\n"+s.shift end yield a end end
テストしてみる
$ ruby test-wikiline.rb test_each_table TestWikiLine#test_each_table . Time: 0.001286 OK (1/1 tests 1 asserts)
うまくいったな、んじゃ、もちっと妙なデータを食わせてみよう。
折角だから test_each_normalにまともなデータを食わせることにしよう
まともに作ったデータをtest_each_normalに組み込む
def test_each_normal r="" assert_equals(<<ANSWER,(r="";WikiLine.new(<<SOURCE).each{|a|r+="---\n#{a}\n"};r)) --- 普通の行 --- -リスト1 ---リスト2 -リスト3 --- 普通の行1 --- |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| --- 普通の行2 --- *項目 --- ,CSV,CSV ,CSV1,CSV --- 普通の行3 --- ぷれ1 ぷれ2 プレ4(一つ上は空行) --- 普通の行 ANSWER 普通の行 -リスト1 ---リスト2 -リスト3 普通の行1 |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| 普通の行2 *項目 ,CSV,CSV ,CSV1,CSV 普通の行3 ぷれ1 ぷれ2 プレ4(一つ上は空行) 普通の行 SOURCE end
テストしてみる
$ ruby test-wikiline.rb test_each_normal TestWikiLine#test_each_normal . Time: 0.001651 OK (1/1 tests 1 asserts)
あとは空の時のテスト
def test_each_empty assert_equals("",(r="";WikiLine.new("").each{|a|r+="---\n#{a}\n"};r)) end
テストしてみる
$ ruby test-wikiline.rb test_each_empty TestWikiLine#test_each_empty . Time: 0.000802 OK (1/1 tests 1 asserts)
うまくいった。
Enumerableのテスト
def test_each_enumerable assert_equals(<<ANSWER,WikiLine.new(<<SOURCE).map{|a|"---\n#{a}\n"}) end
テストしてみる
$ ruby test-wikiline.rb test_each_enumerable TestWikiLine#test_each_enumerable E. Time: 0.00216 FAILURES!!! Test Results: Run: 1/1(0 asserts) Failures: 0 Errors: 1 Errors: 1 test-wikiline.rb:34:in `test_each_enumerable'(TestWikiLine): undefined method `map' for #<WikiLin e:0x402c0fa4> (NameError) from test-wikiline.rb:33
あたりまえだな、include Enumerableしてないもん。
ソースにinclude Enumerableとして再度てすと
$ ruby test-wikiline.rb test_each_enumerable TestWikiLine#test_each_enumerable . Time: 0.001615 OK (1/1 tests 1 asserts)
テストはこんなもんでしょう
完成
ソース
class WikiLine include Enumerable SpecialChar={' '=>'pre','|'=>'table',','=>'csv','>'=>'indent','='=>'nlist','-'=>'list'} def initialize(s="") @s=s end def each s=@s.split("\n") while a=s.shift while !s.empty?&&SpecialChar[a[0..0]]&&SpecialChar[a[0..0]]==SpecialChar[s[0][0..0]] a+="\n"+s.shift end yield a end end end
テスト
require 'rubyunit' require 'wikiline.rb' class TestWikiLine < RUNIT::TestCase def test_new_s assert_instance_of(WikiLine,WikiLine.new) end def test_each_empty assert_equals("",(r="";WikiLine.new("").each{|a|r+="---\n#{a}\n"};r)) end def test_each_normal r="" assert_equals(<<ANSWER,(r="";WikiLine.new(<<SOURCE).each{|a|r+="---\n#{a}\n"};r)) --- 普通の行 --- -リスト1 ---リスト2 -リスト3 --- 普通の行1 --- |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| --- 普通の行2 --- *項目 --- ,CSV,CSV ,CSV1,CSV --- 普通の行3 --- ぷれ1 ぷれ2 プレ4(一つ上は空行) --- 普通の行 ANSWER 普通の行 -リスト1 ---リスト2 -リスト3 普通の行1 |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| 普通の行2 *項目 ,CSV,CSV ,CSV1,CSV 普通の行3 ぷれ1 ぷれ2 プレ4(一つ上は空行) 普通の行 SOURCE end def test_each_enumerable assert_equals(<<ANSWER,WikiLine.new(<<SOURCE).map{|a|"---\n#{a}"}.join("\n")+"\n") --- 普通の行 --- -リスト1 ---リスト2 -リスト3 --- 普通の行1 --- |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| --- 普通の行2 --- *項目 --- ,CSV,CSV ,CSV1,CSV --- 普通の行3 --- ぷれ1 ぷれ2 プレ4(一つ上は空行) --- 普通の行 ANSWER 普通の行 -リスト1 ---リスト2 -リスト3 普通の行1 |項目1|項目2|項目3|項目4|項目5|・・・・| |項目6|項目7|項目8|項目9|項目10|・・・・| 普通の行2 *項目 ,CSV,CSV ,CSV1,CSV 普通の行3 ぷれ1 ぷれ2 プレ4(一つ上は空行) 普通の行 SOURCE end end
全体テスト結果
$ ruby test-wikiline.rb TestWikiLine#test_each_empty . TestWikiLine#test_each_enumerable . TestWikiLine#test_each_normal . TestWikiLine#test_new_s . Time: 0.004107 OK (4/4 tests 4 asserts)