Skip to content

Instantly share code, notes, and snippets.

@suzuki-hoge
Created June 20, 2017 08:04
Show Gist options
  • Save suzuki-hoge/810b75d07bc4854070a264186bc23c4b to your computer and use it in GitHub Desktop.
Save suzuki-hoge/810b75d07bc4854070a264186bc23c4b to your computer and use it in GitHub Desktop.
bobio parser and map wrapper
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%'
]
}
}
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()
}
}
@suzuki-hoge
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment