/
predicate_spec.rb
83 lines (68 loc) · 3.26 KB
/
predicate_spec.rb
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
module MotionData
describe ComparableKeyPathExpression do
extend ComparableKeyPathExpression::Mixin
it "returns a expression for the left-hand side of a comparison" do
value(:property).expression.keyPath.should == 'property'
value('property.subproperty').expression.keyPath.should == 'property.subproperty'
end
it "returns comparison predicates" do
{
(value(:amount) < 42) => 'amount < 42',
(value(:amount) > 42) => 'amount > 42',
(value(:amount) <= 42) => 'amount <= 42',
(value(:amount) >= 42) => 'amount >= 42',
(value(:amount) != 42) => 'amount != 42',
(value(:amount) == 42) => 'amount == 42',
value(:amount).between?(21, 42) => 'amount BETWEEN {21, 42}',
value(:amount).in?([21, 42]) => 'amount IN {21, 42}',
value(:amount).include?(42) => 'amount CONTAINS 42',
value(:name).beginsWith?('bob') => 'name BEGINSWITH "bob"',
value(:name).endsWith?('bob') => 'name ENDSWITH "bob"',
}.each do |predicate, format|
predicate.predicateFormat.should == format
end
end
it "negates the comparison predicate" do
value(:name).not.beginsWith?('bob').predicateFormat.should == 'NOT name BEGINSWITH "bob"'
end
it "by default returns that no comparison options should be used" do
value(:property).comparisonOptions.should == 0
end
it "adds the option to perform a case-insensitive comparison" do
expression = value(:name)
expression.caseInsensitive.object_id.should == expression.object_id
expression.comparisonOptions.should == NSCaseInsensitivePredicateOption
end
it "adds the option to perform a diacritic-insensitive comparison" do
expression = value(:name)
expression.diacriticInsensitive.object_id.should == expression.object_id
expression.comparisonOptions.should == NSDiacriticInsensitivePredicateOption
end
it "adds the option to perform a locale-sensitive comparison" do
expression = value(:name)
expression.localeSensitive.object_id.should == expression.object_id
expression.comparisonOptions.should == NSLocaleSensitivePredicateOption
end
it "combines comparison options" do
options = value(:name).caseInsensitive.diacriticInsensitive.localeSensitive.comparisonOptions
options.should == NSCaseInsensitivePredicateOption |
NSDiacriticInsensitivePredicateOption |
NSLocaleSensitivePredicateOption
end
it "adds the accumulated comparison options to the comparison" do
predicate = value(:name).caseInsensitive.diacriticInsensitive == 'bob'
predicate.predicateFormat.should == 'name ==[cd] "bob"'
end
end
describe NSPredicate do
extend ComparableKeyPathExpression::Mixin
it "returns a compound `AND` predicate" do
predicate = ( value(:amount) < 42 ).and( value(:amount) > 42 ).and( value(:amount) != 21 )
predicate.predicateFormat.should == '(amount < 42 AND amount > 42) AND amount != 21'
end
it "returns a compound `OR` predicate" do
predicate = ( value(:amount) < 42 ).or( value(:amount) > 42 ).or( value(:amount) != 21 )
predicate.predicateFormat.should == '(amount < 42 OR amount > 42) OR amount != 21'
end
end
end