@@ -19,101 +19,100 @@ import graphql.language.StringValue
1919import graphql.schema.CoercingParseLiteralException
2020import graphql.schema.CoercingParseValueException
2121import graphql.schema.CoercingSerializeException
22+ import io.kotest.assertions.throwables.shouldThrow
23+ import io.kotest.core.spec.style.FreeSpec
2224import io.kotest.core.spec.style.FunSpec
25+ import io.kotest.matchers.shouldBe
2326
2427import java.time.Duration
2528
2629import java.time.ZoneOffset.UTC
30+ import java.util.*
2731
2832/* *
2933 * @author Alexey Zhokhov
3034 *
3135 * Test Java 8 ISO 8601 Duration
3236 */
33- class GraphQLDurationTest : FunSpec ({
34-
35- /*
36- def setup() {
37- TimeZone.setDefault(TimeZone.getTimeZone(UTC))
37+ class GraphQLDurationTest : FreeSpec ({
38+
39+ " parseLiteral -> success" - {
40+ listOf(
41+ StringValue ("PT1H30M ") to Duration .ofMinutes(90),
42+ StringValue ("P1DT3H ") to Duration .ofHours(27)
43+ ).forEach { (literal, result) ->
44+ " parse literal ${literal.value} as $result " {
45+ GraphqlDurationCoercing ().parseLiteral(literal) shouldBe result
46+ }
47+ }
3848 }
3949
40- @Unroll
41- def "Duration parse literal #literal.value as #result"() {
42- expect:
43- new GraphqlDurationCoercing().parseLiteral(literal) == result
44-
45- where:
46- literal | result
47- new StringValue('PT1H30M') | Duration.ofMinutes(90)
48- new StringValue('P1DT3H') | Duration.ofHours(27)
50+ " parseLiteral -> fail" - {
51+ listOf(
52+ StringValue (""),
53+ StringValue ("not a duration")
54+ ).forEach { literal ->
55+ " throws exception for invalid $literal " {
56+ shouldThrow<CoercingParseLiteralException > {
57+ GraphqlDurationCoercing ().parseLiteral(literal)
58+ }
59+ }
60+ }
4961 }
5062
51- @Unroll
52- def "Duration parseLiteral throws exception for invalid #literal"() {
53- when:
54- new GraphqlDurationCoercing().parseLiteral(literal)
55-
56- then:
57- thrown(CoercingParseLiteralException)
58-
59- where:
60- literal | _
61- new StringValue('') | _
62- new StringValue('not a duration') | _
63+ " serialize -> success" - {
64+ listOf(
65+ Duration .ofHours(27) to "PT27H "
66+ ).forEach { (value, result) ->
67+ " serialize $value into $result (${result::class .java} )" {
68+ GraphqlDurationCoercing ().serialize(value) shouldBe result
69+ }
70+ }
6371 }
6472
65- @Unroll
66- def "Duration serialize #value into #result (#result.class)"() {
67- expect:
68- new GraphqlDurationCoercing().serialize(value) == result
69-
70- where:
71- value | result
72- Duration.ofHours(27) | 'PT27H'
73+ " serialize -> fail" - {
74+ listOf(
75+ "",
76+ "not a duration",
77+ "1DT3H",
78+ Object ()
79+ ).forEach { value ->
80+ " throws exception for invalid input: $value " {
81+ shouldThrow<CoercingSerializeException > {
82+ GraphqlDurationCoercing ().serialize(value)
83+ }
84+ }
85+ }
7386 }
7487
75- @Unroll
76- def "serialize Duration throws exception for invalid input #value"() {
77- when:
78- new GraphqlDurationCoercing().serialize(value)
79-
80- then:
81- thrown(CoercingSerializeException)
82-
83- where:
84- value | _
85- '' | _
86- 'not a duration' | _
87- '1DT3H' | _
88- new Object() | _
88+ " parseValue -> success" - {
89+ listOf(
90+ "PT1H30M " to Duration .ofMinutes(90),
91+ "P1DT3H " to Duration .ofHours(27)
92+ ).forEach { (value, result) ->
93+ " parse $value into $result (${result::class .java} )" {
94+ GraphqlDurationCoercing ().parseValue(value) shouldBe result
95+ }
96+ }
8997 }
9098
91- @Unroll
92- def "Duration parse #value into #result (#result.class)"() {
93- expect:
94- new GraphqlDurationCoercing().parseValue(value) == result
95-
96- where:
97- value | result
98- 'PT1H30M' | Duration.ofMinutes(90)
99- 'P1DT3H' | Duration.ofHours(27)
99+ " parseValue -> fail" - {
100+ listOf(
101+ "",
102+ "not a date",
103+ "1DT3H",
104+ Object ()
105+ ).forEach { value ->
106+ " throws exception for invalid input: $value " {
107+ shouldThrow<CoercingParseValueException > {
108+ GraphqlDurationCoercing ().parseValue(value)
109+ }
110+ }
111+ }
100112 }
101113
102- @Unroll
103- def "Duration parseValue throws exception for invalid input #value"() {
104- when:
105- new GraphqlDurationCoercing().parseValue(value)
106-
107- then:
108- thrown(CoercingParseValueException)
109-
110- where:
111- value | _
112- '' | _
113- 'not a date' | _
114- '1DT3H' | _
115- new Object() | _
114+ }) {
115+ init {
116+ TimeZone .setDefault(TimeZone .getTimeZone(UTC ))
116117 }
117- */
118-
119- })
118+ }
0 commit comments