|
@Unroll |
|
class AnotherResultTest extends Specification { |
|
private static AnotherResult result = new AnotherResult( |
|
Parser.parse(ParserTest.bobio) |
|
) |
|
|
|
def get_ok() { |
|
when: |
|
String r = result.key('code').get() |
|
|
|
then: |
|
notThrown(RuntimeException) |
|
r == '0' |
|
} |
|
|
|
def get_ng() { |
|
when: |
|
result.key('invalid').get() |
|
|
|
then: |
|
def e = thrown(RuntimeException) |
|
e.message == 'no such key: invalid' |
|
} |
|
|
|
def getOr() { |
|
expect: |
|
result.key(key).getOr(or) == exp |
|
|
|
where: |
|
key | or || exp |
|
'code' | '0' || '0' |
|
'code' | '99' || '0' |
|
'invalid' | '99' || '99' |
|
} |
|
|
|
def opt() { |
|
expect: |
|
result.key(key).opt() == exp |
|
|
|
where: |
|
key || exp |
|
'code' || Optional.of('0') |
|
'invalid' || Optional.empty() |
|
} |
|
|
|
def map_get_ok() { |
|
when: |
|
Code r = result.key('code').map(Code.&of).get() // 型を見失ってエディタに警告されてしまうな... |
|
|
|
then: |
|
notThrown(RuntimeException) |
|
r == new Code('0') |
|
} |
|
|
|
def map_get_ng() { |
|
when: |
|
Code r = result.key('invalid').<Code> map(Code.&of).get() // こうすると警告は消える |
|
|
|
then: |
|
def e = thrown(RuntimeException) |
|
e.message == 'no such key: invalid' |
|
} |
|
|
|
def map_or() { |
|
expect: |
|
result.key(key).<Code> map(Code.&of).getOr(or) == exp |
|
|
|
where: |
|
key | or || exp |
|
'code' | new Code('0') || new Code('0') |
|
'code' | new Code('99') || new Code('0') |
|
'invalid' | new Code('99') || new Code('99') |
|
} |
|
|
|
def map_opt() { |
|
expect: |
|
result.key(key).<Code> map(Code.&of).opt() == exp |
|
|
|
where: |
|
key || exp |
|
'code' || Optional.of(new Code('0')) |
|
'invalid' || Optional.empty() |
|
} |
|
|
|
def map_enum() { |
|
expect: |
|
result.key('status').map({ it == '1' ? Status.OK : Status.NG }).get() == Status.OK |
|
} |
|
|
|
def map_StartDate() { |
|
expect: |
|
result.key('start').map(DateFormatter.parse_yyyymmdd.andThen(StartDate.&of)).get() == new StartDate(LocalDate.of(2017, 6, 15)) |
|
} |
|
|
|
def map_map_StartDate() { |
|
expect: |
|
result.key('start').map(DateFormatter.parse_yyyymmdd).map(StartDate.&of).get() == new StartDate(LocalDate.of(2017, 6, 15)) |
|
} |
|
} |
from https://gist.github.com/suzuki-hoge/810b75d07bc4854070a264186bc23c4b