Skip to content

Commit e902742

Browse files
committed
Add String/Int/Double evaluation
1 parent 39e60af commit e902742

File tree

6 files changed

+877
-155
lines changed

6 files changed

+877
-155
lines changed

IntegrationTests/Tests/Integration/OFREPIntegrationTests.swift

+220-1
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,231 @@ struct OFREPIntegrationTests {
8888
reason: .error,
8989
variant: "a"
9090
)
91-
let flag = "static-a-b"
91+
let flag = "static-a"
9292
await #expect(provider.resolution(of: flag, defaultValue: defaultValue, context: nil) == resolution)
9393
}
9494
}
9595
}
9696

97+
@Suite("String Flag Resolution")
98+
struct StringResolutionTests {
99+
@Test("Static", arguments: [("static-a", "a", "value-a"), ("static-b", "b", "value-b")])
100+
func staticBool(flag: String, variant: String, expectedValue: String) async throws {
101+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
102+
103+
try await withOFREPProvider(provider) {
104+
let resolution = OpenFeatureResolution(
105+
value: expectedValue,
106+
error: nil,
107+
reason: .static,
108+
variant: variant,
109+
flagMetadata: [:]
110+
)
111+
await #expect(provider.resolution(of: flag, defaultValue: "default", context: nil) == resolution)
112+
}
113+
}
114+
115+
@Test("Targeting Match")
116+
func targetingMatch() async throws {
117+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
118+
119+
try await withOFREPProvider(provider) {
120+
let resolution = OpenFeatureResolution(
121+
value: "value-b",
122+
error: nil,
123+
reason: .targetingMatch,
124+
variant: "b",
125+
flagMetadata: [:]
126+
)
127+
let flag = "targeting-b"
128+
let context = OpenFeatureEvaluationContext(targetingKey: "swift")
129+
await #expect(provider.resolution(of: flag, defaultValue: "default", context: context) == resolution)
130+
}
131+
}
132+
133+
@Test("No Targeting Match")
134+
func noTargetingMatch() async throws {
135+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
136+
137+
try await withOFREPProvider(provider) {
138+
let resolution = OpenFeatureResolution(
139+
value: "value-a",
140+
error: nil,
141+
reason: .default,
142+
variant: "a",
143+
flagMetadata: [:]
144+
)
145+
let flag = "targeting-b"
146+
await #expect(provider.resolution(of: flag, defaultValue: "default", context: nil) == resolution)
147+
}
148+
}
149+
150+
@Test("Type mismatch")
151+
func typeMismatch() async throws {
152+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
153+
154+
try await withOFREPProvider(provider) {
155+
let resolution = OpenFeatureResolution(
156+
value: "default",
157+
error: OpenFeatureResolutionError(
158+
code: .typeMismatch,
159+
message: #"Expected flag value of type "String" but received "Bool"."#
160+
),
161+
reason: .error,
162+
variant: "on"
163+
)
164+
let flag = "static-on"
165+
await #expect(provider.resolution(of: flag, defaultValue: "default", context: nil) == resolution)
166+
}
167+
}
168+
}
169+
170+
@Suite("Int Flag Resolution")
171+
struct IntResolutionTests {
172+
@Test("Static", arguments: [("static-negative-42", "a", -42), ("static-positive-42", "b", 42)])
173+
func staticBool(flag: String, variant: String, expectedValue: Int) async throws {
174+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
175+
176+
try await withOFREPProvider(provider) {
177+
let resolution = OpenFeatureResolution(
178+
value: expectedValue,
179+
error: nil,
180+
reason: .static,
181+
variant: variant,
182+
flagMetadata: [:]
183+
)
184+
await #expect(provider.resolution(of: flag, defaultValue: 0, context: nil) == resolution)
185+
}
186+
}
187+
188+
@Test("Targeting Match")
189+
func targetingMatch() async throws {
190+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
191+
192+
try await withOFREPProvider(provider) {
193+
let resolution = OpenFeatureResolution(
194+
value: 42,
195+
error: nil,
196+
reason: .targetingMatch,
197+
variant: "b",
198+
flagMetadata: [:]
199+
)
200+
let flag = "targeting-42"
201+
let context = OpenFeatureEvaluationContext(targetingKey: "swift")
202+
await #expect(provider.resolution(of: flag, defaultValue: 0, context: context) == resolution)
203+
}
204+
}
205+
206+
@Test("No Targeting Match")
207+
func noTargetingMatch() async throws {
208+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
209+
210+
try await withOFREPProvider(provider) {
211+
let resolution = OpenFeatureResolution(
212+
value: -42,
213+
error: nil,
214+
reason: .default,
215+
variant: "a",
216+
flagMetadata: [:]
217+
)
218+
let flag = "targeting-42"
219+
await #expect(provider.resolution(of: flag, defaultValue: 0, context: nil) == resolution)
220+
}
221+
}
222+
223+
@Test("Type mismatch")
224+
func typeMismatch() async throws {
225+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
226+
227+
try await withOFREPProvider(provider) {
228+
let resolution = OpenFeatureResolution(
229+
value: 42,
230+
error: OpenFeatureResolutionError(
231+
code: .typeMismatch,
232+
message: #"Expected flag value of type "Int" but received "String"."#
233+
),
234+
reason: .error,
235+
variant: "a"
236+
)
237+
let flag = "static-a"
238+
await #expect(provider.resolution(of: flag, defaultValue: 42, context: nil) == resolution)
239+
}
240+
}
241+
}
242+
243+
@Suite("Double Flag Resolution")
244+
struct DoubleResolutionTests {
245+
@Test("Static", arguments: [("static-negative-42.123", "a", -42.123), ("static-positive-42.123", "b", 42.123)])
246+
func staticBool(flag: String, variant: String, expectedValue: Double) async throws {
247+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
248+
249+
try await withOFREPProvider(provider) {
250+
let resolution = OpenFeatureResolution(
251+
value: expectedValue,
252+
error: nil,
253+
reason: .static,
254+
variant: variant,
255+
flagMetadata: [:]
256+
)
257+
await #expect(provider.resolution(of: flag, defaultValue: 0.0, context: nil) == resolution)
258+
}
259+
}
260+
261+
@Test("Targeting Match")
262+
func targetingMatch() async throws {
263+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
264+
265+
try await withOFREPProvider(provider) {
266+
let resolution = OpenFeatureResolution(
267+
value: 42.123,
268+
error: nil,
269+
reason: .targetingMatch,
270+
variant: "b",
271+
flagMetadata: [:]
272+
)
273+
let flag = "targeting-42.123"
274+
let context = OpenFeatureEvaluationContext(targetingKey: "swift")
275+
await #expect(provider.resolution(of: flag, defaultValue: 0.0, context: context) == resolution)
276+
}
277+
}
278+
279+
@Test("No Targeting Match")
280+
func noTargetingMatch() async throws {
281+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
282+
283+
try await withOFREPProvider(provider) {
284+
let resolution = OpenFeatureResolution(
285+
value: -42.123,
286+
error: nil,
287+
reason: .default,
288+
variant: "a",
289+
flagMetadata: [:]
290+
)
291+
let flag = "targeting-42.123"
292+
await #expect(provider.resolution(of: flag, defaultValue: 0.0, context: nil) == resolution)
293+
}
294+
}
295+
296+
@Test("Type mismatch")
297+
func typeMismatch() async throws {
298+
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)
299+
300+
try await withOFREPProvider(provider) {
301+
let resolution = OpenFeatureResolution(
302+
value: 42.123,
303+
error: OpenFeatureResolutionError(
304+
code: .typeMismatch,
305+
message: #"Expected flag value of type "Double" but received "String"."#
306+
),
307+
reason: .error,
308+
variant: "a"
309+
)
310+
let flag = "static-a"
311+
await #expect(provider.resolution(of: flag, defaultValue: 42.123, context: nil) == resolution)
312+
}
313+
}
314+
}
315+
97316
@Test("Flag not found", arguments: [true, false])
98317
func flagNotFound(defaultValue: Bool) async throws {
99318
let provider = OFREPProvider(serverURL: URL(string: "http://localhost:8016")!)

IntegrationTests/integration.flagd.json

+106-3
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,116 @@
3838
]
3939
}
4040
},
41-
"static-a-b": {
41+
"static-a": {
4242
"state": "ENABLED",
4343
"variants": {
44-
"a": "a",
45-
"b": "b"
44+
"a": "value-a",
45+
"b": "value-b"
4646
},
4747
"defaultVariant": "a"
48+
},
49+
"static-b": {
50+
"state": "ENABLED",
51+
"variants": {
52+
"a": "value-a",
53+
"b": "value-b"
54+
},
55+
"defaultVariant": "b"
56+
},
57+
"targeting-b": {
58+
"state": "ENABLED",
59+
"variants": {
60+
"a": "value-a",
61+
"b": "value-b"
62+
},
63+
"defaultVariant": "a",
64+
"targeting": {
65+
"if": [
66+
{
67+
"===": [
68+
{
69+
"var": "targetingKey"
70+
},
71+
"swift"
72+
]
73+
},
74+
"b"
75+
]
76+
}
77+
},
78+
"static-negative-42": {
79+
"state": "ENABLED",
80+
"variants": {
81+
"a": -42,
82+
"b": 42
83+
},
84+
"defaultVariant": "a"
85+
},
86+
"static-positive-42": {
87+
"state": "ENABLED",
88+
"variants": {
89+
"a": -42,
90+
"b": 42
91+
},
92+
"defaultVariant": "b"
93+
},
94+
"targeting-42": {
95+
"state": "ENABLED",
96+
"variants": {
97+
"a": -42,
98+
"b": 42
99+
},
100+
"defaultVariant": "a",
101+
"targeting": {
102+
"if": [
103+
{
104+
"===": [
105+
{
106+
"var": "targetingKey"
107+
},
108+
"swift"
109+
]
110+
},
111+
"b"
112+
]
113+
}
114+
},
115+
"static-negative-42.123": {
116+
"state": "ENABLED",
117+
"variants": {
118+
"a": -42.123,
119+
"b": 42.123
120+
},
121+
"defaultVariant": "a"
122+
},
123+
"static-positive-42.123": {
124+
"state": "ENABLED",
125+
"variants": {
126+
"a": -42.123,
127+
"b": 42.123
128+
},
129+
"defaultVariant": "b"
130+
},
131+
"targeting-42.123": {
132+
"state": "ENABLED",
133+
"variants": {
134+
"a": -42.123,
135+
"b": 42.123
136+
},
137+
"defaultVariant": "a",
138+
"targeting": {
139+
"if": [
140+
{
141+
"===": [
142+
{
143+
"var": "targetingKey"
144+
},
145+
"swift"
146+
]
147+
},
148+
"b"
149+
]
150+
}
48151
}
49152
}
50153
}

0 commit comments

Comments
 (0)