@@ -211,6 +211,44 @@ module LaunchDarkly
211
211
end
212
212
end
213
213
214
+ describe "hash-like behavior" do
215
+ it "multi-kind contexts return nested contexts" do
216
+ user_context = subject . create ( { key : "user-key" , kind : "user" } )
217
+ org_context = subject . create ( { key : "org-key" , kind : "org" } )
218
+ multi_context = subject . create_multi ( [ user_context , org_context ] )
219
+
220
+ expect ( multi_context . valid? ) . to be true
221
+ expect ( multi_context [ "user" ] ) . to eq ( user_context )
222
+ expect ( multi_context [ "org" ] ) . to eq ( org_context )
223
+ expect ( multi_context [ "no-such-type" ] ) . to be_nil
224
+ end
225
+
226
+ describe "single-kind contexts" do
227
+ it "can retrieve the correct simple attribute value" do
228
+ context = subject . create ( { key : "my-key" , kind : "org" , name : "x" , :"my-attr" => "y" , :"/starts-with-slash" => "z" } )
229
+
230
+ expect ( context [ "kind" ] ) . to eq ( "org" )
231
+ expect ( context [ "key" ] ) . to eq ( "my-key" )
232
+ expect ( context [ "name" ] ) . to eq ( "x" )
233
+ expect ( context [ "my-attr" ] ) . to eq ( "y" )
234
+ expect ( context [ "/starts-with-slash" ] ) . to eq ( "z" )
235
+ expect ( context [ "a-value-that-is-not-set" ] ) . to be_nil
236
+ end
237
+
238
+ it "cannot query subpath/elements" do
239
+ object_value = { a : 1 }
240
+ array_value = [ 1 ]
241
+
242
+ context = subject . create ( { key : "my-key" , kind : "org" , :"obj-attr" => object_value , :"array-attr" => array_value } )
243
+ expect ( context [ "obj-attr" ] ) . to eq ( object_value )
244
+ expect ( context [ :"array-attr" ] ) . to eq ( array_value )
245
+
246
+ expect ( context [ :"/obj-attr/a" ] ) . to be_nil
247
+ expect ( context [ :"/array-attr/0" ] ) . to be_nil
248
+ end
249
+ end
250
+ end
251
+
214
252
describe "value retrieval" do
215
253
describe "supports simple attribute retrieval" do
216
254
it "can retrieve the correct simple attribute value" do
@@ -298,6 +336,38 @@ module LaunchDarkly
298
336
end
299
337
300
338
describe "equality comparisons" do
339
+ it "wrong types are not equal" do
340
+ context = subject . create ( { key : 'context-key' , kind : 'user' } )
341
+ expect ( context ) . to_not eq ( true )
342
+ expect ( context ) . to_not eq ( 3 )
343
+ end
344
+
345
+ it "single-kind context can compare with hash" do
346
+ hash = { key : 'context-key' , kind : 'user' , name : 'Example name' , groups : [ 'test' , 'it' , 'here' ] , address : { street : '123 Easy St' , city : 'Every Town' } ,
347
+ _meta : { privateAttributes : [ 'name' , 'out of order attribute' ] }
348
+ }
349
+ context = subject . create ( hash )
350
+ expect ( context ) . to eq ( hash )
351
+ end
352
+
353
+ it "multi-kind context can compare with hash" do
354
+ hash = {
355
+ kind : "multi" ,
356
+ org : { key : 'org-key' , kind : 'org' } ,
357
+ user : { key : 'user-key' , kind : 'user' } ,
358
+ device : { key : 'device-key' , kind : 'device' } ,
359
+ }
360
+ org_context = subject . create ( hash [ :org ] )
361
+ user_context = subject . create ( hash [ :user ] )
362
+ device_context = subject . create ( hash [ :device ] )
363
+ context = subject . create ( hash )
364
+
365
+ expect ( context ) . to eq ( hash )
366
+ expect ( context [ "org" ] ) . to eq ( hash [ :org ] )
367
+ expect ( context [ "user" ] ) . to eq ( hash [ :user ] )
368
+ expect ( context [ "device" ] ) . to eq ( hash [ :device ] )
369
+ end
370
+
301
371
it "single kind contexts are equal" do
302
372
original_context = subject . create (
303
373
{ key : 'context-key' , kind : 'user' , name : 'Example name' , groups : [ 'test' , 'it' , 'here' ] , address : { street : '123 Easy St' , city : 'Every Town' } ,
0 commit comments