@@ -55,7 +55,17 @@ defmodule Flagsmith.Engine.SegmentConditionsTest do
5555 { :NOT_CONTAINS , "bar" , "baz" , true } ,
5656 { :REGEX , "foo" , "[a-z]+" , true } ,
5757 { :REGEX , "FOO" , "[a-z]+" , false } ,
58- { :REGEX , "1.2.3" , "\\ d" , true }
58+ { :REGEX , "1.2.3" , "\\ d" , true } ,
59+ { :MODULO , 1 , "2|0" , false } ,
60+ { :MODULO , 2 , "2|0" , true } ,
61+ { :MODULO , 3 , "2|0" , false } ,
62+ { :MODULO , 34.2 , "4|3" , false } ,
63+ { :MODULO , 35.0 , "4|3" , true } ,
64+ { :MODULO , "dummy" , "3|0" , false } ,
65+ { :MODULO , "1.0.0" , "3|0" , false } ,
66+ { :MODULO , false , "1|3" , false } ,
67+ { :MODULO , 3.5 , "1.5|0.5" , true } ,
68+ { :MODULO , 4 , "1.5|0.5" , false }
5969 ]
6070
6171 test "all conditions" do
@@ -354,4 +364,108 @@ defmodule Flagsmith.Engine.SegmentConditionsTest do
354364
355365 refute Flagsmith.Engine . traits_match_segment_rule ( traits_3 , @ segment_rule_nested_any , 1 , 1 )
356366 end
367+
368+ @ segment_rule_all_is_or_not_set % Segment.Rule {
369+ conditions: [ ] ,
370+ rules: [
371+ % Segment.Rule {
372+ conditions: [
373+ % Segment.Condition {
374+ operator: :IS_SET ,
375+ property_: "test_is_set"
376+ } ,
377+ % Segment.Condition {
378+ operator: :IS_NOT_SET ,
379+ property_: "test_is_not_set"
380+ }
381+ ] ,
382+ rules: [ ] ,
383+ type: :ALL
384+ }
385+ ] ,
386+ type: :ALL
387+ }
388+
389+ test "Segment.Rule IS_SET and IS_NOT_SET" do
390+ # test that the segment matches (both conditions, IS_SET is true, and
391+ # IS_NOT_SET true)
392+ traits_1 = [
393+ % Trait {
394+ trait_key: "test_is_set" ,
395+ trait_value: % Trait.Value { value: true , type: :boolean }
396+ }
397+ ]
398+
399+ assert Flagsmith.Engine . traits_match_segment_rule (
400+ traits_1 ,
401+ @ segment_rule_all_is_or_not_set ,
402+ 1 ,
403+ 1
404+ )
405+
406+ # test that the segment matches even is the test_is_set trait value is false
407+ traits_2 = [
408+ % Trait {
409+ trait_key: "test_is_set" ,
410+ trait_value: % Trait.Value { value: false , type: :boolean }
411+ }
412+ ]
413+
414+ assert Flagsmith.Engine . traits_match_segment_rule (
415+ traits_2 ,
416+ @ segment_rule_all_is_or_not_set ,
417+ 1 ,
418+ 1
419+ )
420+
421+ # refute because `test_is_not_set` is passed as a trait and so the segment
422+ # condition IS_NOT_SET should fail since the segment specifies :ALL for rules
423+ # validations
424+ traits_3 = [
425+ % Trait {
426+ trait_key: "test_is_set" ,
427+ trait_value: % Trait.Value { value: true , type: :boolean }
428+ } ,
429+ % Trait {
430+ trait_key: "test_is_not_set" ,
431+ trait_value: % Trait.Value { value: true , type: :boolean }
432+ }
433+ ]
434+
435+ refute Flagsmith.Engine . traits_match_segment_rule (
436+ traits_3 ,
437+ @ segment_rule_all_is_or_not_set ,
438+ 1 ,
439+ 1
440+ )
441+
442+ # :IS_NOT_SET should still evaluate to false even if the trait value is `false`
443+ # since it's still set
444+ traits_4 = [
445+ % Trait {
446+ trait_key: "test_is_set" ,
447+ trait_value: % Trait.Value { value: true , type: :boolean }
448+ } ,
449+ % Trait {
450+ trait_key: "test_is_not_set" ,
451+ trait_value: % Trait.Value { value: false , type: :boolean }
452+ }
453+ ]
454+
455+ refute Flagsmith.Engine . traits_match_segment_rule (
456+ traits_4 ,
457+ @ segment_rule_all_is_or_not_set ,
458+ 1 ,
459+ 1
460+ )
461+
462+ # :IS_SET should fail since no trait is being passed to match it
463+
464+ refute Flagsmith.Engine . traits_match_segment_rule (
465+ [ ] ,
466+ @ segment_rule_all_is_or_not_set ,
467+ 1 ,
468+ 1
469+ )
470+ end
357471end
0 commit comments