forked from smithy-lang/smithy-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstraintsTest.kt
136 lines (115 loc) · 4.6 KB
/
ConstraintsTest.kt
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.server.smithy
import io.kotest.inspectors.forAll
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.util.lookup
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestSymbolProvider
class ConstraintsTest {
private val model =
"""
namespace test
service TestService {
version: "123",
operations: [TestOperation]
}
operation TestOperation {
input: TestInputOutput,
output: TestInputOutput,
}
structure TestInputOutput {
map: MapA,
recursive: RecursiveShape
}
structure RecursiveShape {
shape: RecursiveShape,
mapB: MapB
}
@length(min: 1, max: 69)
map MapA {
key: String,
value: MapB
}
map MapB {
key: String,
value: StructureA
}
@uniqueItems
list ListA {
member: MyString
}
@pattern("\\w+")
string MyString
@length(min: 1, max: 69)
string LengthString
structure StructureA {
@range(min: 1, max: 69)
int: Integer,
@required
string: String
}
// This shape is not in the service closure.
structure StructureB {
@pattern("\\w+")
patternString: String,
@required
requiredString: String,
mapA: MapA,
@length(min: 1, max: 5)
mapAPrecedence: MapA
}
structure StructWithInnerDefault {
@default(false)
inner: PrimitiveBoolean
}
""".asSmithyModel(smithyVersion = "2")
private val symbolProvider = serverTestSymbolProvider(model)
private val testInputOutput = model.lookup<StructureShape>("test#TestInputOutput")
private val recursiveShape = model.lookup<StructureShape>("test#RecursiveShape")
private val mapA = model.lookup<MapShape>("test#MapA")
private val mapB = model.lookup<MapShape>("test#MapB")
private val listA = model.lookup<ListShape>("test#ListA")
private val lengthString = model.lookup<StringShape>("test#LengthString")
private val structA = model.lookup<StructureShape>("test#StructureA")
private val structAInt = model.lookup<MemberShape>("test#StructureA\$int")
private val structAString = model.lookup<MemberShape>("test#StructureA\$string")
private val structWithInnerDefault = model.lookup<StructureShape>("test#StructWithInnerDefault")
private val primitiveBoolean = model.lookup<BooleanShape>("smithy.api#PrimitiveBoolean")
@Test
fun `it should detect supported constrained traits as constrained`() {
listOf(listA, mapA, structA, lengthString).forAll {
it.isDirectlyConstrained(symbolProvider) shouldBe true
}
}
@Test
fun `it should not detect unsupported constrained traits as constrained`() {
listOf(structAInt, structAString).forAll {
it.isDirectlyConstrained(symbolProvider) shouldBe false
}
}
@Test
fun `it should evaluate reachability of constrained shapes`() {
mapA.canReachConstrainedShape(model, symbolProvider) shouldBe true
structAInt.canReachConstrainedShape(model, symbolProvider) shouldBe false
listA.canReachConstrainedShape(model, symbolProvider) shouldBe true
// All of these eventually reach `StructureA`, which is constrained because one of its members is `required`.
testInputOutput.canReachConstrainedShape(model, symbolProvider) shouldBe true
mapB.canReachConstrainedShape(model, symbolProvider) shouldBe true
recursiveShape.canReachConstrainedShape(model, symbolProvider) shouldBe true
}
@Test
fun `it should not consider shapes with the default trait as constrained`() {
structWithInnerDefault.canReachConstrainedShape(model, symbolProvider) shouldBe false
primitiveBoolean.isDirectlyConstrained(symbolProvider) shouldBe false
}
}