wrong 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +114 -25
- data/lib/predicated/Gemfile +15 -0
- data/lib/predicated/LICENSE +20 -0
- data/lib/predicated/README.markdown +191 -0
- data/lib/predicated/Rakefile +51 -0
- data/lib/predicated/lib/predicated.rb +4 -0
- data/lib/predicated/lib/predicated/autogen_call.rb +37 -0
- data/lib/predicated/lib/predicated/constrain.rb +66 -0
- data/lib/predicated/lib/predicated/evaluate.rb +94 -0
- data/lib/predicated/lib/predicated/from/callable_object.rb +108 -0
- data/lib/predicated/lib/predicated/from/json.rb +59 -0
- data/lib/predicated/lib/predicated/from/ruby_code_string.rb +73 -0
- data/lib/predicated/lib/predicated/from/url_part.rb +104 -0
- data/lib/predicated/lib/predicated/from/xml.rb +61 -0
- data/lib/predicated/lib/predicated/gem_check.rb +34 -0
- data/lib/predicated/lib/predicated/predicate.rb +111 -0
- data/lib/predicated/lib/predicated/print.rb +62 -0
- data/lib/predicated/lib/predicated/selectable.rb +102 -0
- data/lib/predicated/lib/predicated/simple_templated_predicate.rb +79 -0
- data/lib/predicated/lib/predicated/string_utils.rb +20 -0
- data/lib/predicated/lib/predicated/to/arel.rb +41 -0
- data/lib/predicated/lib/predicated/to/json.rb +48 -0
- data/lib/predicated/lib/predicated/to/sentence.rb +94 -0
- data/lib/predicated/lib/predicated/to/solr.rb +15 -0
- data/lib/predicated/lib/predicated/to/xml.rb +67 -0
- data/lib/predicated/lib/predicated/version.rb +3 -0
- data/lib/predicated/predicated.gemspec +22 -0
- data/lib/predicated/test/autogen_call_test.rb +40 -0
- data/lib/predicated/test/canonical_transform_cases.rb +63 -0
- data/lib/predicated/test/constrain_test.rb +86 -0
- data/lib/predicated/test/enumerable_test.rb +32 -0
- data/lib/predicated/test/equality_test.rb +32 -0
- data/lib/predicated/test/evaluate_test.rb +149 -0
- data/lib/predicated/test/from/callable_object_canonical_test.rb +43 -0
- data/lib/predicated/test/from/callable_object_test.rb +78 -0
- data/lib/predicated/test/from/json_test.rb +83 -0
- data/lib/predicated/test/from/ruby_code_string_canonical_test.rb +37 -0
- data/lib/predicated/test/from/ruby_code_string_test.rb +103 -0
- data/lib/predicated/test/from/url_part_parser_test.rb +123 -0
- data/lib/predicated/test/from/url_part_test.rb +48 -0
- data/lib/predicated/test/from/xml_test.rb +57 -0
- data/lib/predicated/test/json_conversion_test.rb +33 -0
- data/lib/predicated/test/print_test.rb +66 -0
- data/lib/predicated/test/selectable_test.rb +123 -0
- data/lib/predicated/test/simple_templated_predicate_test.rb +39 -0
- data/lib/predicated/test/suite.rb +2 -0
- data/lib/predicated/test/test_helper.rb +64 -0
- data/lib/predicated/test/test_helper_with_wrong.rb +6 -0
- data/lib/predicated/test/to/arel_test.rb +85 -0
- data/lib/predicated/test/to/json_test.rb +74 -0
- data/lib/predicated/test/to/sentence_test.rb +90 -0
- data/lib/predicated/test/to/solr_test.rb +39 -0
- data/lib/predicated/test/to/xml_test.rb +72 -0
- data/lib/predicated/test/xml_conversion_test.rb +34 -0
- data/lib/predicated/test_integration/arel_integration_test.rb +52 -0
- data/lib/predicated/test_integration/canonical_integration_cases.rb +66 -0
- data/lib/predicated/test_integration/schema.xml +83 -0
- data/lib/predicated/test_integration/solr_integration_test.rb +71 -0
- data/lib/predicated/test_integration/sqlite_db +0 -0
- data/lib/predicated/test_integration/suite.rb +2 -0
- data/lib/predicated/test_integration/usage_test.rb +252 -0
- data/lib/wrong.rb +3 -1
- data/lib/wrong/adapters/test_unit.rb +1 -3
- data/lib/wrong/assert.rb +81 -24
- data/lib/wrong/chunk.rb +145 -0
- data/lib/wrong/message/string_diff.rb +2 -4
- data/lib/wrong/message/test_context.rb +2 -2
- data/lib/wrong/version.rb +2 -2
- data/test/adapters/minitest_test.rb +16 -9
- data/test/adapters/test_unit_test.rb +1 -1
- data/test/assert_test.rb +90 -0
- data/test/catch_raise_test.rb +2 -2
- data/test/chunk_test.rb +236 -0
- data/test/failures_test.rb +109 -74
- data/test/message/array_diff_test.rb +35 -19
- data/test/message/string_diff_test.rb +39 -15
- data/test/message/test_context_text.rb +2 -2
- data/test/test_helper.rb +25 -7
- metadata +86 -33
- data/test/basic_assert_test.rb +0 -38
@@ -0,0 +1,86 @@
|
|
1
|
+
require "./test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/predicate"
|
4
|
+
require "predicated/constrain"
|
5
|
+
include Predicated
|
6
|
+
|
7
|
+
regarding %{constraints are rules about the content and structure of predicates.
|
8
|
+
a predicate might violate a constraint} do
|
9
|
+
|
10
|
+
before do
|
11
|
+
@value_not_equal_to_two =
|
12
|
+
Constraint.new(:name => "Value can't be two",
|
13
|
+
:selectors => [Operation],
|
14
|
+
:check_that => proc{|predicate, ancestors| predicate.right!=2})
|
15
|
+
|
16
|
+
@not_more_than_two_levels_deep =
|
17
|
+
Constraint.new(:name => "Limited to two levels deep",
|
18
|
+
:check_that => proc{|predicate, ancestors| ancestors.length<=2})
|
19
|
+
|
20
|
+
@one = Predicate{Eq(1,1)}
|
21
|
+
@two = Predicate{Eq(2,2)}
|
22
|
+
@three = Predicate{Eq(3,3)}
|
23
|
+
|
24
|
+
@one_and_three = Predicate{And(Eq(1,1), Eq(3,3))}
|
25
|
+
@one_and_two = Predicate{And(Eq(1,1), Eq(2,2))}
|
26
|
+
|
27
|
+
@deeply_nested = Predicate{Or(Or(And(Eq(1,1), Eq(3,3)), Eq(4,4)), Eq(5,5))}
|
28
|
+
end
|
29
|
+
|
30
|
+
test "apply to each predicate - simple" do
|
31
|
+
constraints = Constraints.new.add(@value_not_equal_to_two)
|
32
|
+
|
33
|
+
assert{ constraints.check(@one).pass? }
|
34
|
+
deny { constraints.check(@two).pass? }
|
35
|
+
|
36
|
+
assert{ constraints.check(@one_and_three).pass? }
|
37
|
+
deny { constraints.check(@one_and_two).pass? }
|
38
|
+
end
|
39
|
+
|
40
|
+
test "apply each to each predicate - many constraints" do
|
41
|
+
constraints =
|
42
|
+
Constraints.new.
|
43
|
+
add(@value_not_equal_to_two).
|
44
|
+
add(@not_more_than_two_levels_deep)
|
45
|
+
|
46
|
+
assert{ constraints.check(@one_and_three).pass? }
|
47
|
+
deny { constraints.check(@one_and_two).pass? }
|
48
|
+
|
49
|
+
assert{ constraints.check(@one_and_three).pass? }
|
50
|
+
deny { constraints.check(@deeply_nested).pass? }
|
51
|
+
end
|
52
|
+
|
53
|
+
test "equality" do
|
54
|
+
one = Constraint.new(:name => "Value can't be two",
|
55
|
+
:selectors => [Operation],
|
56
|
+
:check_that => proc{|predicate, ancestors| predicate.right!=2})
|
57
|
+
two = Constraint.new(:name => "Value can't be two",
|
58
|
+
:selectors => [Operation],
|
59
|
+
:check_that => proc{|predicate, ancestors| predicate.right!=2})
|
60
|
+
three = Constraint.new(:name => "Some other constraint",
|
61
|
+
:check_that => proc{|predicate, ancestors| false})
|
62
|
+
|
63
|
+
assert{ one == two }
|
64
|
+
deny { one == three }
|
65
|
+
end
|
66
|
+
|
67
|
+
test %{result contains information about whether the checks passed,
|
68
|
+
which constraints were violated,
|
69
|
+
along with the offending predicates} do
|
70
|
+
constraints = Constraints.new.add(@value_not_equal_to_two)
|
71
|
+
|
72
|
+
result = constraints.check(@one)
|
73
|
+
assert{ result.pass? }
|
74
|
+
assert{ result.violations == {} }
|
75
|
+
|
76
|
+
result = constraints.check(Predicate{And(Eq(1,1), And(Eq(2,2), Eq(3,2)))})
|
77
|
+
deny { result.pass? }
|
78
|
+
assert{
|
79
|
+
result.violations == {
|
80
|
+
@value_not_equal_to_two => [Equal.new(2,2), Equal.new(3,2)]
|
81
|
+
}
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "./test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/predicate"
|
4
|
+
include Predicated
|
5
|
+
|
6
|
+
regarding "you can flip through the predicate tree, like any enumerable. a list of ancestors of each node are provided" do
|
7
|
+
|
8
|
+
test "simple" do
|
9
|
+
assert { Predicate { Eq(1, 2) }.to_a == [[Predicate { Eq(1, 2) }, []]] }
|
10
|
+
end
|
11
|
+
|
12
|
+
test "complex" do
|
13
|
+
the_top = Predicate { And(Eq(1, 2), Or(Eq(3, 4), Eq(5, 6))) }
|
14
|
+
the_or = Predicate { Or(Eq(3, 4), Eq(5, 6)) }
|
15
|
+
assert { the_top.to_a ==
|
16
|
+
[
|
17
|
+
[the_top, []],
|
18
|
+
[Predicate { Eq(1, 2) }, [the_top]],
|
19
|
+
[Predicate { Or(Eq(3, 4), Eq(5, 6)) }, [the_top]],
|
20
|
+
[Predicate { Eq(3, 4) }, [the_top, the_or]],
|
21
|
+
[Predicate { Eq(5, 6) }, [the_top, the_or]]
|
22
|
+
]
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
test "not" do
|
27
|
+
the_top = Predicate { Not(Eq(1, 2)) }
|
28
|
+
assert { the_top.to_a == [[the_top, []], [Predicate { Eq(1, 2) }, [the_top]]] }
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "./test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/predicate"
|
4
|
+
include Predicated
|
5
|
+
|
6
|
+
regarding "prove value equality" do
|
7
|
+
|
8
|
+
test "simple" do
|
9
|
+
assert { Predicate { Eq(1, 1) } == Predicate { Eq(1, 1) } }
|
10
|
+
deny { Predicate { Eq(1, 1) } == Predicate { Eq(1, 99) } }
|
11
|
+
end
|
12
|
+
|
13
|
+
test "unary" do
|
14
|
+
assert { Predicate { Not(Eq(1, 1)) } == Predicate { Not(Eq(1, 1)) } }
|
15
|
+
deny { Predicate { Not(Eq(1, 1)) } == Predicate { Not(Eq(99, 99)) } }
|
16
|
+
end
|
17
|
+
|
18
|
+
test "complex" do
|
19
|
+
assert { Predicate { And(Eq(1, 1), Or(Eq(2, 2), Eq(3, 3))) } ==
|
20
|
+
Predicate { And(Eq(1, 1), Or(Eq(2, 2), Eq(3, 3))) } }
|
21
|
+
|
22
|
+
deny { Predicate { And(Eq(1, 1), Or(Eq(2, 2), Eq(3, 3))) } ==
|
23
|
+
Predicate { And(Eq(1, 1), Or(Eq(2, 99), Eq(3, 3))) } }
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
regarding "predicate base class. not sure I'm happy with the implementation...too tricky" do
|
29
|
+
test "all predicates descend from a predicate base class. it's a marker class" do
|
30
|
+
assert{ And.new(Equal.new(1,1),Equal.new(2,2)).is_a?(Predicate) }
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require "./test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/evaluate"
|
4
|
+
include Predicated
|
5
|
+
|
6
|
+
regarding "evaluate a predicate as boolean logic in ruby. change the context by providing and optional binding." do
|
7
|
+
|
8
|
+
regarding "proving out basic operations" do
|
9
|
+
test "equals" do
|
10
|
+
assert { Predicate { Eq(1, 1) }.evaluate }
|
11
|
+
deny { Predicate { Eq(1, 2) }.evaluate }
|
12
|
+
end
|
13
|
+
|
14
|
+
test "less than" do
|
15
|
+
assert { Predicate { Lt(1, 2) }.evaluate }
|
16
|
+
deny { Predicate { Lt(2, 2) }.evaluate }
|
17
|
+
deny { Predicate { Lt(3, 2) }.evaluate }
|
18
|
+
end
|
19
|
+
|
20
|
+
test "greater than" do
|
21
|
+
deny { Predicate { Gt(1, 2) }.evaluate }
|
22
|
+
deny { Predicate { Gt(2, 2) }.evaluate }
|
23
|
+
assert { Predicate { Gt(3, 2) }.evaluate }
|
24
|
+
end
|
25
|
+
|
26
|
+
test "less than or equal to" do
|
27
|
+
assert { Predicate { Lte(1, 2) }.evaluate }
|
28
|
+
assert { Predicate { Lte(2, 2) }.evaluate }
|
29
|
+
deny { Predicate { Lte(3, 2) }.evaluate }
|
30
|
+
end
|
31
|
+
|
32
|
+
test "greater than or equal to" do
|
33
|
+
deny { Predicate { Gte(1, 2) }.evaluate }
|
34
|
+
assert { Predicate { Gte(2, 2) }.evaluate }
|
35
|
+
assert { Predicate { Gte(3, 2) }.evaluate }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
regarding "comparing values of different data types" do
|
40
|
+
test "strings" do
|
41
|
+
assert { Predicate { Eq("1", "1") }.evaluate }
|
42
|
+
deny { Predicate { Eq("1", 1) }.evaluate }
|
43
|
+
deny { Predicate { Eq("1", nil) }.evaluate }
|
44
|
+
end
|
45
|
+
|
46
|
+
test "booleans" do
|
47
|
+
assert { Predicate { Eq(true, true) }.evaluate }
|
48
|
+
deny { Predicate { Eq(false, true) }.evaluate }
|
49
|
+
|
50
|
+
deny { Predicate { Eq(false, nil) }.evaluate }
|
51
|
+
deny { Predicate { Eq(true, nil) }.evaluate }
|
52
|
+
|
53
|
+
deny { Predicate { Eq("false", false) }.evaluate }
|
54
|
+
deny { Predicate { Eq("true", true) }.evaluate }
|
55
|
+
end
|
56
|
+
|
57
|
+
test "numbers" do
|
58
|
+
assert { Predicate { Eq(1, 1) }.evaluate }
|
59
|
+
assert { Predicate { Eq(1, 1.0) }.evaluate }
|
60
|
+
assert { Predicate { Eq(1.0, 1.0) }.evaluate }
|
61
|
+
deny { Predicate { Eq(1, 2) }.evaluate }
|
62
|
+
deny { Predicate { Eq(1, nil) }.evaluate }
|
63
|
+
end
|
64
|
+
|
65
|
+
test "objects" do
|
66
|
+
assert { Predicate { Eq(Color.new("red"), Color.new("red")) }.evaluate }
|
67
|
+
deny { Predicate { Eq(Color.new("red"), Color.new("BLUE")) }.evaluate }
|
68
|
+
deny { Predicate { Eq(Color.new("red"), 2) }.evaluate }
|
69
|
+
deny { Predicate { Eq(Color.new("red"), "red") }.evaluate }
|
70
|
+
deny { Predicate { Eq(Color.new("red"), nil) }.evaluate }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
regarding "and" do
|
76
|
+
test "left and right must be true" do
|
77
|
+
assert { Predicate { And( Eq(1, 1), Eq(2, 2) ) }.evaluate }
|
78
|
+
deny { Predicate { And( Eq(99, 1), Eq(2, 2) ) }.evaluate }
|
79
|
+
deny { Predicate { And( Eq(1, 1), Eq(99, 2) ) }.evaluate }
|
80
|
+
end
|
81
|
+
|
82
|
+
test "simple true and false work too" do
|
83
|
+
assert { Predicate { And( true, true ) }.evaluate }
|
84
|
+
assert { Predicate { And( true, Eq(2, 2) ) }.evaluate }
|
85
|
+
deny { Predicate { And( true, false ) }.evaluate }
|
86
|
+
deny { Predicate { And( Eq(2, 2), false ) }.evaluate }
|
87
|
+
end
|
88
|
+
|
89
|
+
test "nested" do
|
90
|
+
assert { Predicate { And( true, And(true, true) ) }.evaluate }
|
91
|
+
deny { Predicate { And( false, And(true, true) ) }.evaluate }
|
92
|
+
deny { Predicate { And( true, And(true, false) ) }.evaluate }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
regarding "or" do
|
97
|
+
test "one of left or right must be true" do
|
98
|
+
assert { Predicate { Or(true, true) }.evaluate }
|
99
|
+
assert { Predicate { Or(true, false) }.evaluate }
|
100
|
+
assert { Predicate { Or(false, true) }.evaluate }
|
101
|
+
deny { Predicate { Or(false, false) }.evaluate }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
regarding "not" do
|
106
|
+
test "simple negation" do
|
107
|
+
assert { Predicate { Not(Eq(3, 2)) }.evaluate }
|
108
|
+
deny { Predicate { Not(Eq(2, 2)) }.evaluate }
|
109
|
+
end
|
110
|
+
|
111
|
+
test "complex" do
|
112
|
+
assert { Predicate { Not( And(Eq(2, 2),false) ) }.evaluate }
|
113
|
+
deny { Predicate { Not( And(Eq(2, 2),true) ) }.evaluate }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
regarding "evaluate adds a generic 'call' class. that is, object.message(args)" do
|
118
|
+
test "evaluate simple calls" do
|
119
|
+
assert { Predicate { Call("abc", :include?, "bc") }.evaluate }
|
120
|
+
deny { Predicate { Call("abc", :include?, "ZZ") }.evaluate }
|
121
|
+
end
|
122
|
+
|
123
|
+
test "nil call. call defaults to no args if none are specified" do
|
124
|
+
assert { Predicate { Call(nil, :nil?, []) }.evaluate }
|
125
|
+
deny { Predicate { Call("abc", :nil?, []) }.evaluate }
|
126
|
+
|
127
|
+
assert { Predicate { Call(nil, :nil?) }.evaluate }
|
128
|
+
deny { Predicate { Call("abc", :nil?) }.evaluate }
|
129
|
+
end
|
130
|
+
|
131
|
+
test "inspect" do
|
132
|
+
assert { Call.new("abc", :include?, "bc").inspect == "Call('abc'.include?('bc'))" }
|
133
|
+
end
|
134
|
+
|
135
|
+
test "inspect, empty right hand side" do
|
136
|
+
assert { Call.new("abc", :nil?).inspect == "Call('abc'.nil?)" }
|
137
|
+
end
|
138
|
+
|
139
|
+
test "call equality" do
|
140
|
+
assert { Call.new("abc", :include?, "bc") == Call.new("abc", :include?, "bc") }
|
141
|
+
deny { Call.new("ZZZ", :include?, "bc") == Call.new("abc", :include?, "bc") }
|
142
|
+
deny { Call.new("abc", :zzz, "bc") == Call.new("abc", :include?, "bc") }
|
143
|
+
deny { Call.new("abc", :include?, "ZZZ") == Call.new("abc", :include?, "bc") }
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "./test/test_helper_with_wrong"
|
2
|
+
require "./test/canonical_transform_cases"
|
3
|
+
|
4
|
+
if RUBY_VERSION =~/^1.9/
|
5
|
+
puts "skipping callable object-related tests in 1.9"
|
6
|
+
else
|
7
|
+
|
8
|
+
require "predicated/from/callable_object"
|
9
|
+
include Predicated
|
10
|
+
|
11
|
+
regarding "callable object - canoical transform cases" do
|
12
|
+
include CanonicalTransformCases
|
13
|
+
|
14
|
+
@expectations = {
|
15
|
+
"simple operations" => {
|
16
|
+
"eq" => Predicate.from_callable_object{"a"==3},
|
17
|
+
"gt" => Predicate.from_callable_object{"a">3},
|
18
|
+
"lt" => Predicate.from_callable_object{"a"<3},
|
19
|
+
"gte" => Predicate.from_callable_object{"a">=3},
|
20
|
+
"lte" => Predicate.from_callable_object{"a"<=3}
|
21
|
+
},
|
22
|
+
"primitive types" => {
|
23
|
+
"false" => Predicate.from_callable_object{"a"==false},
|
24
|
+
"true" => Predicate.from_callable_object{"a"==true},
|
25
|
+
"string" => Predicate.from_callable_object{"a"=="yyy"}
|
26
|
+
},
|
27
|
+
"not" => {
|
28
|
+
"simple" => Predicate.from_callable_object{!("a"==true)}
|
29
|
+
},
|
30
|
+
"simple and / or" => {
|
31
|
+
#parens are necessary around AND's in solr in order to force precedence
|
32
|
+
"and" => Predicate.from_callable_object{"a"==1 && "b"==2},
|
33
|
+
"or" => Predicate.from_callable_object{"a"==1 || "b"==2}
|
34
|
+
},
|
35
|
+
"complex and / or" => {
|
36
|
+
"or and" => Predicate.from_callable_object{"a"==1 && "b"==2 || "c"==3}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
create_canonical_tests(@expectations)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
require "./test/canonical_transform_cases"
|
3
|
+
|
4
|
+
|
5
|
+
if RUBY_VERSION =~/^1.9/
|
6
|
+
puts "skipping callable object-related tests in 1.9"
|
7
|
+
else
|
8
|
+
|
9
|
+
require "predicated/from/callable_object"
|
10
|
+
include Predicated
|
11
|
+
|
12
|
+
regarding "convert a ruby callable object - a proc or lambda - into a predicate" do
|
13
|
+
|
14
|
+
regarding "basic operations" do
|
15
|
+
|
16
|
+
test "complex types" do
|
17
|
+
assert_equal Predicate.from_callable_object{Color.new("red")==Color.new("blue")},
|
18
|
+
Predicate{ Eq(Color.new("red"),Color.new("blue")) }
|
19
|
+
|
20
|
+
assert_equal Predicate.from_callable_object{ {1=>2}=={"a"=>"b"} },
|
21
|
+
Predicate{ Eq({1=>2},{"a"=>"b"}) }
|
22
|
+
end
|
23
|
+
|
24
|
+
test "word and / or" do
|
25
|
+
assert_equal Predicate.from_callable_object{1==1 and 2==2}, Predicate{ And(Eq(1,1),Eq(2,2)) }
|
26
|
+
assert_equal Predicate.from_callable_object{1==1 or 2==2}, Predicate{ Or(Eq(1,1),Eq(2,2)) }
|
27
|
+
end
|
28
|
+
|
29
|
+
test "substitute in from the binding" do
|
30
|
+
a = 1
|
31
|
+
b = "1"
|
32
|
+
c = "c"
|
33
|
+
d = Color.new("purple")
|
34
|
+
|
35
|
+
assert_equal Predicate.from_callable_object(binding()){a==1}, Predicate{ Eq(1,1) }
|
36
|
+
assert_equal Predicate.from_callable_object(binding()){b==1}, Predicate{ Eq("1",1) }
|
37
|
+
assert_equal Predicate.from_callable_object(binding()){c==b}, Predicate{ Eq("c","1") }
|
38
|
+
assert_equal Predicate.from_callable_object(binding()){d==d}, Predicate{ Eq(Color.new("purple"),
|
39
|
+
Color.new("purple")) }
|
40
|
+
assert Predicate.from_callable_object(binding()){d==d}.left === d
|
41
|
+
|
42
|
+
assert_equal Predicate.from_callable_object(binding()){a==b && b==c},
|
43
|
+
Predicate{ And(Eq(1,"1"),Eq("1","c")) }
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
test "parens change precedence" do
|
48
|
+
assert_equal Predicate.from_callable_object{1==1 || 2==2 && 3==3},
|
49
|
+
Predicate{ Or( Eq(1,1), And(Eq(2,2),Eq(3,3)) ) }
|
50
|
+
|
51
|
+
assert_equal Predicate.from_callable_object{(1==1 || 2==2) && 3==3},
|
52
|
+
Predicate{ And( Or(Eq(1,1),Eq(2,2)), Eq(3,3) ) }
|
53
|
+
end
|
54
|
+
|
55
|
+
test "works with procs and lambdas" do
|
56
|
+
|
57
|
+
assert_equal Predicate.from_callable_object(proc{1<2}), Predicate{ Lt(1,2) }
|
58
|
+
assert_equal Predicate.from_callable_object(lambda{1<2}), Predicate{ Lt(1,2) }
|
59
|
+
|
60
|
+
a = "aaa"
|
61
|
+
assert_equal Predicate.from_callable_object(proc{a=="bbb"}, binding()),
|
62
|
+
Predicate{ Eq("aaa","bbb") }
|
63
|
+
assert_equal Predicate.from_callable_object(lambda{a=="bbb"}, binding()),
|
64
|
+
Predicate{ Eq("aaa","bbb") }
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
regarding "errors" do
|
70
|
+
test "predicates only" do
|
71
|
+
assert_raises(Predicated::Predicate::DontKnowWhatToDoWithThisSexpError) do
|
72
|
+
Predicate.from_callable_object{a=1}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "./test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/from/json"
|
4
|
+
require "./test/canonical_transform_cases"
|
5
|
+
include Predicated
|
6
|
+
|
7
|
+
regarding "convert a json string to a predicate" do
|
8
|
+
include CanonicalTransformCases
|
9
|
+
|
10
|
+
@expectations = {
|
11
|
+
"simple operations" => {
|
12
|
+
"eq" => Predicate.from_json_str(%{["a","==",3]}),
|
13
|
+
"gt" => Predicate.from_json_str(%{["a",">",3]}),
|
14
|
+
"lt" => Predicate.from_json_str(%{["a","<",3]}),
|
15
|
+
"gte" => Predicate.from_json_str(%{["a",">=",3]}),
|
16
|
+
"lte" => Predicate.from_json_str(%{["a","<=",3]})
|
17
|
+
},
|
18
|
+
"primitive types" => {
|
19
|
+
"false" => Predicate.from_json_str(%{["a","==",false]}),
|
20
|
+
"true" => Predicate.from_json_str(%{["a","==",true]}),
|
21
|
+
"string" => Predicate.from_json_str(%{["a","==","yyy"]})
|
22
|
+
},
|
23
|
+
"not" => {
|
24
|
+
"simple" => Predicate.from_json_str(%{{"not":["a","==",true]}})
|
25
|
+
},
|
26
|
+
"simple and / or" => {
|
27
|
+
#parens are necessary around AND's in solr in order to force precedence
|
28
|
+
"and" => Predicate.from_json_str(%{{"and":[["a","==",1],["b","==",2]]}}),
|
29
|
+
"or" => Predicate.from_json_str(%{{"or":[["a","==",1],["b","==",2]]}})
|
30
|
+
},
|
31
|
+
"complex and / or" => {
|
32
|
+
"or and" => Predicate.from_json_str(%{
|
33
|
+
{
|
34
|
+
"or":[
|
35
|
+
{"and":[["a","==",1],["b","==",2]]},
|
36
|
+
["c","==",3]
|
37
|
+
]
|
38
|
+
}
|
39
|
+
})
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
create_canonical_tests(@expectations)
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
regarding "convert a json structure to a predicate" do
|
48
|
+
include CanonicalTransformCases
|
49
|
+
|
50
|
+
@expectations = {
|
51
|
+
"simple operations" => {
|
52
|
+
"eq" => Predicate.from_json_struct(["a", "==", 3]),
|
53
|
+
"gt" => Predicate.from_json_struct(["a", ">", 3]),
|
54
|
+
"lt" => Predicate.from_json_struct(["a", "<", 3]),
|
55
|
+
"gte" => Predicate.from_json_struct(["a", ">=", 3]),
|
56
|
+
"lte" => Predicate.from_json_struct(["a", "<=", 3])
|
57
|
+
},
|
58
|
+
"primitive types" => {
|
59
|
+
"false" => Predicate.from_json_struct(["a", "==", false]),
|
60
|
+
"true" => Predicate.from_json_struct(["a", "==", true]),
|
61
|
+
"string" => Predicate.from_json_struct(["a", "==", "yyy"])
|
62
|
+
},
|
63
|
+
"not" => {
|
64
|
+
"simple" => Predicate.from_json_struct("not" => ["a", "==", true])
|
65
|
+
},
|
66
|
+
"simple and / or" => {
|
67
|
+
#parens are necessary around AND's in solr in order to force precedence
|
68
|
+
"and" => Predicate.from_json_struct("and" => [["a", "==", 1],["b", "==", 2]]),
|
69
|
+
"or" => Predicate.from_json_struct("or" => [["a", "==", 1],["b", "==", 2]])
|
70
|
+
},
|
71
|
+
"complex and / or" => {
|
72
|
+
"or and" => Predicate.from_json_struct(
|
73
|
+
"or" => [
|
74
|
+
{"and" => [["a", "==", 1],["b", "==", 2]]},
|
75
|
+
["c", "==", 3]
|
76
|
+
]
|
77
|
+
)
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
create_canonical_tests(@expectations)
|
82
|
+
|
83
|
+
end
|