前提
- dev-xconnecting: PandocをWindowsにインストールする
- dev-xconnecting: EclipseでSpockを試す
- dev-xconnecting: コマンドラインのコマンドをGroovyから実行する
手順
まずは、Markdownで書かれた、以下のファイル(header.md)を用意する。#Header Test次に、Spockのテストを用意する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class HeaderSpec extends Specification { | |
def "header characterization test"() { | |
when: | |
def proc = "pandoc header.md -o header.html".execute() | |
proc.waitFor() | |
then: | |
def result = new File("header.html").getText('UTF-8') | |
"" == result | |
} | |
} |
このテストでは、PandocでMarkdownファイルをHTMLファイルに変換し、そのHTMLファイルの内容をテストしている。期待値が空文字になっているため、当然このテストは失敗する。
Condition not satisfied:実際の変換結果はheader.htmlに出力されているので、その内容をテストコードに反映する。
"" == result
| |
| <h1 id="header-test">Header Test</h1>
false
39 differences (0% similarity)
(--------------------------------------~-~)
(<h1 id="header-test">Header Test</h1>\r\n)
at HeaderSpec.header characterization test(HeaderSpec.groovy:10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class HeaderSpec extends Specification { | |
def "header characterization test"() { | |
when: | |
def proc = "pandoc header.md -o header.html".execute() | |
proc.waitFor() | |
then: | |
def result = new File("header.html").getText('UTF-8') | |
"""<h1 id="header-test">Header Test</h1> | |
""" == result | |
} | |
} |
再度テストを実行すると、成功するはずのテストが、まだ失敗する。
Condition not satisfied:どうも、期待値と検証値で改行コードが異なるのが原因らしい。期待値と検証値それぞれでnormalizeメソッドを実行して、改行コードの違いを吸収する。
"""<h1 id="header-test">Header Test</h1> //""" == result
| |
| <h1 id="header-test">Header Test</h1>
false
3 differences (92% similarity)
<h1 id="header-test">Header Test</h1>(-~)\n(//)
<h1 id="header-test">Header Test</h1>(\r)\n(--)
at HeaderSpec.header characterization test(HeaderSpec.groovy:10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class HeaderSpec extends Specification { | |
def "header characterization test"() { | |
when: | |
def proc = "pandoc header.md -o header.html".execute() | |
proc.waitFor() | |
then: | |
def result = new File("header.html").getText('UTF-8') | |
"""<h1 id="header-test">Header Test</h1> | |
""".normalize() == result.normalize() | |
} | |
} |
上記の修正後、再度テストを実行すると、無事テストが成功する。