-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathResourceBundleMessageInterpolatorTest.groovy
More file actions
100 lines (76 loc) · 3.85 KB
/
ResourceBundleMessageInterpolatorTest.groovy
File metadata and controls
100 lines (76 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package graphql.validation.interpolation
import graphql.execution.ResultPath
import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLFieldsContainer
import graphql.schema.GraphQLSchema
import graphql.validation.TestUtil
import graphql.validation.rules.ValidationEnvironment
import spock.lang.Specification
import spock.lang.Unroll
class ResourceBundleMessageInterpolatorTest extends Specification {
ValidationEnvironment buildEnv(GraphQLSchema schema, String argName, argValue, MessageInterpolator interpolator, Locale locale) {
GraphQLFieldsContainer fieldsContainer = schema.getObjectType("Query") as GraphQLFieldsContainer
GraphQLFieldDefinition fieldDefinition = fieldsContainer.getFieldDefinition("field")
GraphQLArgument argUnderTest = fieldDefinition.getArgument(argName)
def ruleEnvironment = ValidationEnvironment.newValidationEnvironment()
.argument(argUnderTest)
.validatedValue(argValue)
.validatedType(argUnderTest.getType())
.fieldDefinition(fieldDefinition)
.fieldsContainer(fieldsContainer)
.messageInterpolator(interpolator)
.executionPath(ResultPath.rootPath().segment(fieldDefinition.getName()))
.validatedPath(ResultPath.rootPath().segment(argName))
.locale(locale)
.build()
ruleEnvironment
}
def sdl = """
input InputObject {
name : String
age : Int
}
type Query {
field(arg : String) : String
}
"""
def schema = TestUtil.schema(sdl)
@Unroll
def "can interpolate things : #messageTemplate"() {
def interpolatorUnderTest = new ResourceBundleMessageInterpolator()
def validatedValue = [zig: "zag"]
def messageParams = ["validatedValue": validatedValue, "p1": "pv1", "p2": "pv2", "min": "5", "max": "10", path : "a/b/c", "value" : "100", "inclusive" : true]
ValidationEnvironment validationEnvironment = buildEnv(schema, "arg", validatedValue, interpolatorUnderTest, null)
expect:
def actual = interpolatorUnderTest.interpolate(messageTemplate, messageParams, validationEnvironment)
actual.message == expected
where:
messageTemplate | expected
// resource bundle finding
'graphql.test.message' | 'Test message with expressions : zag and replacements : pv1'
// system level message finding from graphql.validation
'graphql.validation.Size.message' | 'a/b/c size must be between 5 and 10'
// message with el expression
'graphql.validation.DecimalMax.message' | 'a/b/c must be less than or equal to 100'
// expressions
'Could not ${validatedValue.zig}' | 'Could not zag'
// message param replacement
'Must match {p1}' | 'Must match pv1'
}
@Unroll
def "can use formatting and locales"() {
def interpolatorUnderTest = new ResourceBundleMessageInterpolator()
def validatedValue = 42.0
def messageParams = ["validatedValue": validatedValue, "p1": "pv1", "p2": "pv2", "min": "5", "max": "10"]
ValidationEnvironment validationEnvironment = buildEnv(schema, "arg", validatedValue, interpolatorUnderTest, locale)
expect:
def actual = interpolatorUnderTest.interpolate(messageTemplate, messageParams, validationEnvironment)
actual.message == expected
where:
messageTemplate | expected | locale
// resource bundle finding
'''${formatter.format('%1$.2f', validatedValue)}''' | '42,00' | Locale.GERMAN
'''${formatter.format('%1$.2f', validatedValue)}''' | '42.00' | Locale.US
}
}