-
-
Save suzuki-hoge/810b75d07bc4854070a264186bc23c4b to your computer and use it in GitHub Desktop.
bobio parser and map wrapper
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 ParserTest extends Specification { | |
// @formatter:off | |
public static final String bobio = | |
"""code=0 | |
status=ok | |
number=1 | |
message=50%25 => 80%25""" | |
// @formatter:on | |
def test() { | |
expect: | |
Parser.parse(bobio) == [ | |
'code' : '0', | |
'status' : 'ok', | |
'number' : '1', | |
'message': '50% => 80%' | |
] | |
} | |
} |
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
package bobio_parser | |
import spock.lang.Specification | |
class ResultTest extends Specification { | |
private static Result result = new Result( | |
Parser.parse(ParserTest.bobio) | |
) | |
def get_ok() { | |
when: | |
String r = result.get('code') | |
then: | |
notThrown(RuntimeException) | |
r == '0' | |
} | |
def get_ng() { | |
when: | |
result.get('invalid') | |
then: | |
thrown(RuntimeException) | |
} | |
def as_ok() { | |
when: | |
Code r = result.as('code', Code.&of) // of はコンストラクタの static メソッド. groovy はコンストラクタのメソッド参照が出来ないから. | |
then: | |
notThrown(RuntimeException) | |
r == new Code('0') | |
} | |
def as_ng() { | |
when: | |
result.as('invalid', Code.&of) | |
then: | |
thrown(RuntimeException) | |
} | |
def getOr() { | |
expect: | |
result.getOr(key, or) == exp | |
where: | |
key | or || exp | |
'code' | '0' || '0' | |
'code' | '99' || '0' | |
'invalid' | '99' || '99' | |
} | |
def opt() { | |
expect: | |
result.opt(key) == exp | |
where: | |
key || exp | |
'code' || Optional.of('0') | |
'invalid' || Optional.empty() | |
} | |
def asOr() { | |
expect: | |
result.asOr(key, or, Code.&of) == exp | |
where: | |
key | or || exp | |
'code' | '0' || new Code('0') | |
'code' | '99' || new Code('0') | |
'invalid' | '99' || new Code('99') | |
} | |
def asOpt() { | |
expect: | |
result.asOpt(key, Code.&of) == exp | |
where: | |
key || exp | |
'code' || Optional.of(new Code('0')) | |
'invalid' || Optional.empty() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to https://gist.github.com/suzuki-hoge/2ff7b5901ac5068350b7ef4e2f66c1c5