predicated 0.1.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 +9 -0
- data/lib/predicated.rb +6 -0
- data/lib/predicated/constrain.rb +64 -0
- data/lib/predicated/evaluate.rb +75 -0
- data/lib/predicated/from/callable_object.rb +79 -0
- data/lib/predicated/from/ruby_string.rb +65 -0
- data/lib/predicated/from/url_fragment.rb +97 -0
- data/lib/predicated/gem_check.rb +34 -0
- data/lib/predicated/predicate.rb +92 -0
- data/lib/predicated/print.rb +50 -0
- data/lib/predicated/selector.rb +55 -0
- data/lib/predicated/to/arel.rb +35 -0
- data/lib/predicated/to/sentence.rb +91 -0
- data/lib/predicated/version.rb +3 -0
- data/test/constrain_test.rb +77 -0
- data/test/enumerable_test.rb +60 -0
- data/test/equality_test.rb +21 -0
- data/test/evaluate_test.rb +144 -0
- data/test/from/callable_object_test.rb +102 -0
- data/test/from/ruby_string_test.rb +135 -0
- data/test/from/url_fragment_parser_test.rb +116 -0
- data/test/from/url_fragment_test.rb +37 -0
- data/test/print_test.rb +65 -0
- data/test/selector_test.rb +82 -0
- data/test/suite.rb +4 -0
- data/test/test_helper.rb +42 -0
- data/test/test_helper_with_wrong.rb +5 -0
- data/test/to/arel_test.rb +45 -0
- data/test/to/sentence_test.rb +83 -0
- metadata +109 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
require "test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/selector"
|
4
|
+
include Predicated::Selector
|
5
|
+
|
6
|
+
apropos "part one: selectors on an array (simple enumerable). proving them out more generally." do
|
7
|
+
before do
|
8
|
+
@arr = [1,2,"c",4,"e",6]
|
9
|
+
@arr.extend SelectorEnumerable(
|
10
|
+
:strings => proc{|item|item.is_a?(String)},
|
11
|
+
:numbers => proc{|item|item.is_a?(Numeric)},
|
12
|
+
:less_than_3 => proc{|item|item < 3}
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
test %{selection basics.
|
17
|
+
People often remark that this kind of thing is jQuery-like.
|
18
|
+
I keep thinking I got it from Eric Evans.} do
|
19
|
+
assert{ @arr.select(:strings) == ["c","e"] }
|
20
|
+
assert{ @arr.select(:numbers) == [1,2,4,6] }
|
21
|
+
assert{ @arr.select(:numbers).select(:less_than_3) == [1,2] }
|
22
|
+
|
23
|
+
assert do
|
24
|
+
catch_raise{@arr.select(:less_than_3)}.is_a?(ArgumentError)
|
25
|
+
#because strings don't respond to <
|
26
|
+
#...there's no substitute for knowing what you're doing.
|
27
|
+
end
|
28
|
+
|
29
|
+
#normal select still works
|
30
|
+
assert{ @arr.select{|item|item.is_a?(String)} == ["c","e"] }
|
31
|
+
end
|
32
|
+
|
33
|
+
test "...selector name can be any object" do
|
34
|
+
arr = [1,2,"c",4,"e",6]
|
35
|
+
arr.extend SelectorEnumerable(
|
36
|
+
String => proc{|item|item.is_a?(String)},
|
37
|
+
Numeric => proc{|item|item.is_a?(Numeric)},
|
38
|
+
:less_than_3 => proc{|item|item < 3}
|
39
|
+
)
|
40
|
+
|
41
|
+
assert{ arr.select(String) == ["c","e"] }
|
42
|
+
assert{ arr.select(Numeric) == [1,2,4,6] }
|
43
|
+
assert{ arr.select(Numeric).select(:less_than_3) == [1,2] }
|
44
|
+
end
|
45
|
+
|
46
|
+
test "chaining. also works using varargs" do
|
47
|
+
assert{ @arr.select(:numbers).select(:less_than_3) == [1,2] }
|
48
|
+
assert{ @arr.select(:numbers, :less_than_3) == [1,2] }
|
49
|
+
end
|
50
|
+
|
51
|
+
test "extending twice is additive (not destructive)" do
|
52
|
+
arr = [1,2,"c",4,"e",6]
|
53
|
+
arr.extend SelectorEnumerable(:strings => proc{|item|item.is_a?(String)})
|
54
|
+
arr.extend SelectorEnumerable(:numbers => proc{|item|item.is_a?(Numeric)})
|
55
|
+
|
56
|
+
assert{ arr.select(:strings) == ["c","e"] }
|
57
|
+
assert{ arr.select(:numbers) == [1,2,4,6] }
|
58
|
+
end
|
59
|
+
|
60
|
+
test "works as an include" do
|
61
|
+
class MyArray < Array
|
62
|
+
include SelectorEnumerable(:strings => proc{|item|item.is_a?(String)})
|
63
|
+
include SelectorEnumerable(:numbers => proc{|item|item.is_a?(Numeric)})
|
64
|
+
end
|
65
|
+
arr = MyArray.new
|
66
|
+
arr.replace([1,2,"c",4,"e",6])
|
67
|
+
|
68
|
+
assert{ arr.select(:strings) == ["c","e"] }
|
69
|
+
assert{ arr.select(:numbers) == [1,2,4,6] }
|
70
|
+
end
|
71
|
+
|
72
|
+
test %{memoizes.
|
73
|
+
Selector enumerable assumes an immutable collection.
|
74
|
+
I'm going to use that assumption against it, and cleverly prove that memoization works.
|
75
|
+
(Others might choose to mock in similar circumstances.)} do
|
76
|
+
assert{ @arr.select(:strings) == ["c","e"] }
|
77
|
+
|
78
|
+
@arr << "zzz"
|
79
|
+
|
80
|
+
assert{ @arr.select(:strings) == ["c","e"] }
|
81
|
+
end
|
82
|
+
end
|
data/test/suite.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH.unshift "#{dir}/../lib"
|
3
|
+
$LOAD_PATH.unshift "../wrong/lib"
|
4
|
+
require "rubygems"
|
5
|
+
require "minitest/spec"
|
6
|
+
require "pp"
|
7
|
+
|
8
|
+
#DO NOT REQUIRE WRONG IN HERE
|
9
|
+
#The circularity between projects will cause certain tests to not work.
|
10
|
+
|
11
|
+
class MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
def assert_raise(exception_info_regex)
|
14
|
+
begin
|
15
|
+
yield
|
16
|
+
rescue Exception => e
|
17
|
+
assert{ exception_info_regex =~ "#{e.class.name} #{e.message}" }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
module Kernel
|
24
|
+
alias_method :apropos, :describe
|
25
|
+
|
26
|
+
def xapropos(str)
|
27
|
+
puts "x'd out 'apropos \"#{str}\"'"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class MiniTest::Spec
|
32
|
+
class << self
|
33
|
+
alias_method :test, :it
|
34
|
+
|
35
|
+
def xtest(str)
|
36
|
+
puts "x'd out 'test \"#{str}\"'"
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
MiniTest::Unit.autorun
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
unless RUBY_VERSION=="1.8.6"
|
4
|
+
|
5
|
+
require "predicated/to/arel"
|
6
|
+
include Predicated
|
7
|
+
|
8
|
+
apropos "convert a predicate to an arel where clause" do
|
9
|
+
|
10
|
+
test "operations" do
|
11
|
+
assert { Predicate{ Eq("a",1) }.to_arel == Arel::Predicates::Equality.new("a", 1) }
|
12
|
+
assert { Predicate{ Gt("a",1) }.to_arel == Arel::Predicates::GreaterThan.new("a", 1) }
|
13
|
+
assert { Predicate{ Lt("a",1) }.to_arel == Arel::Predicates::LessThan.new("a", 1) }
|
14
|
+
assert { Predicate{ Gte("a",1) }.to_arel == Arel::Predicates::GreaterThanOrEqualTo.new("a", 1) }
|
15
|
+
assert { Predicate{ Lte("a",1) }.to_arel == Arel::Predicates::LessThanOrEqualTo.new("a", 1) }
|
16
|
+
end
|
17
|
+
|
18
|
+
test "simple and + or" do
|
19
|
+
assert { Predicate{ And(Eq("a", 1),Eq("b", 2)) }.to_arel ==
|
20
|
+
Arel::Predicates::And.new(
|
21
|
+
Arel::Predicates::Equality.new("a", 1),
|
22
|
+
Arel::Predicates::Equality.new("b", 2)
|
23
|
+
) }
|
24
|
+
|
25
|
+
assert { Predicate{ Or(Eq("a", 1),Eq("b", 2)) }.to_arel ==
|
26
|
+
Arel::Predicates::Or.new(
|
27
|
+
Arel::Predicates::Equality.new("a", 1),
|
28
|
+
Arel::Predicates::Equality.new("b", 2)
|
29
|
+
) }
|
30
|
+
end
|
31
|
+
|
32
|
+
test "complex and + or" do
|
33
|
+
assert { Predicate{ Or( And(Eq("a", 1),Eq("b", 2)), Eq("c", 3) ) }.to_arel ==
|
34
|
+
Arel::Predicates::Or.new(
|
35
|
+
Arel::Predicates::And.new(
|
36
|
+
Arel::Predicates::Equality.new("a", 1),
|
37
|
+
Arel::Predicates::Equality.new("b", 2)
|
38
|
+
),
|
39
|
+
Arel::Predicates::Equality.new("c", 3)
|
40
|
+
) }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "test/test_helper_with_wrong"
|
2
|
+
|
3
|
+
require "predicated/to/sentence"
|
4
|
+
include Predicated
|
5
|
+
|
6
|
+
apropos "convert a predicate to an english sentence" do
|
7
|
+
|
8
|
+
after do
|
9
|
+
Operation.reset_verb_phrases
|
10
|
+
end
|
11
|
+
|
12
|
+
test "operations" do
|
13
|
+
assert { Predicate{ Eq("a",1) }.to_sentence == "'a' is equal to 1" }
|
14
|
+
assert { Predicate{ Gt("a",1) }.to_sentence == "'a' is greater than 1" }
|
15
|
+
assert { Predicate{ Lt("a",1) }.to_sentence == "'a' is less than 1" }
|
16
|
+
assert { Predicate{ Gte("a",1) }.to_sentence == "'a' is greater than or equal to 1" }
|
17
|
+
assert { Predicate{ Lte("a",1) }.to_sentence == "'a' is less than or equal to 1" }
|
18
|
+
|
19
|
+
assert { Predicate{ Eq("a",1) }.to_negative_sentence == "'a' is not equal to 1" }
|
20
|
+
assert { Predicate{ Gt("a",1) }.to_negative_sentence == "'a' is not greater than 1" }
|
21
|
+
assert { Predicate{ Lt("a",1) }.to_negative_sentence == "'a' is not less than 1" }
|
22
|
+
assert { Predicate{ Gte("a",1) }.to_negative_sentence == "'a' is not greater than or equal to 1" }
|
23
|
+
assert { Predicate{ Lte("a",1) } .to_negative_sentence == "'a' is not less than or equal to 1" }
|
24
|
+
end
|
25
|
+
|
26
|
+
test "primitive types" do
|
27
|
+
assert { Predicate{ Eq("a",1) }.to_sentence == "'a' is equal to 1" }
|
28
|
+
assert { Predicate{ Eq("a",nil) }.to_sentence == "'a' is equal to nil" }
|
29
|
+
assert { Predicate{ Eq("a",true) }.to_sentence == "'a' is equal to true" }
|
30
|
+
end
|
31
|
+
|
32
|
+
test "complex types" do
|
33
|
+
assert { Predicate{ Eq([1,2],{3=>4}) }.to_sentence == "'[1, 2]' is equal to '{3=>4}'" }
|
34
|
+
end
|
35
|
+
|
36
|
+
test "default verb phrases for unknown methods (which are awkward/ESL-ish)" do
|
37
|
+
assert { Predicate{ Call("abc", :exclude?, "bc") }.to_sentence ==
|
38
|
+
"'abc' is exclude 'bc'" }
|
39
|
+
|
40
|
+
assert { Predicate{ Call("abc", :exclude?, "bc") }.to_negative_sentence ==
|
41
|
+
"'abc' is not exclude 'bc'" }
|
42
|
+
|
43
|
+
assert { Predicate{ Call("abc", :friends_with?, "bc") }.to_sentence ==
|
44
|
+
"'abc' is friends with 'bc'" }
|
45
|
+
end
|
46
|
+
|
47
|
+
test "register methods and their verb phrases" do
|
48
|
+
Operation.register_verb_phrase(:exclude?, "does exclude", "does not exclude")
|
49
|
+
assert { Predicate{ Call("abc", :exclude?, "bc") }.to_sentence ==
|
50
|
+
"'abc' does exclude 'bc'" }
|
51
|
+
|
52
|
+
assert { Predicate{ Call("abc", :exclude?, "bc") }.to_negative_sentence ==
|
53
|
+
"'abc' does not exclude 'bc'" }
|
54
|
+
end
|
55
|
+
|
56
|
+
test "some other common methods have sensible verb phrases by default" do
|
57
|
+
assert { Predicate{ Call("abc", :include?, 'bc') }.to_sentence == "'abc' includes 'bc'" }
|
58
|
+
assert { Predicate{ Call("abc", :include?, 'bc') }.to_negative_sentence == "'abc' does not include 'bc'" }
|
59
|
+
|
60
|
+
assert { Predicate{ Call("abc", :is_a?, String) }.to_sentence == "'abc' is a 'String'" }
|
61
|
+
assert { Predicate{ Call("abc", :is_a?, String) }.to_negative_sentence == "'abc' is not a 'String'" }
|
62
|
+
end
|
63
|
+
|
64
|
+
test "nothing on the far side" do
|
65
|
+
assert { Predicate{ Call("abc", :nil?) }.to_sentence == "'abc' is nil" }
|
66
|
+
assert { Predicate{ Call("abc", :nil?) }.to_negative_sentence == "'abc' is not nil" }
|
67
|
+
end
|
68
|
+
|
69
|
+
test "simple and + or" do
|
70
|
+
assert { Predicate{ And(Eq("a", 1),Eq("b", 2)) }.to_sentence ==
|
71
|
+
"'a' is equal to 1 and 'b' is equal to 2" }
|
72
|
+
|
73
|
+
assert { Predicate{ Or(Eq("a", 1),Eq("b", 2)) }.to_sentence ==
|
74
|
+
"'a' is equal to 1 or 'b' is equal to 2" }
|
75
|
+
|
76
|
+
assert { Predicate{ And(Eq("a", 1),Eq("b", 2)) }.to_negative_sentence ==
|
77
|
+
"This is not true: 'a' is equal to 1 and 'b' is equal to 2" }
|
78
|
+
|
79
|
+
assert { Predicate{ Or(Eq("a", 1),Eq("b", 2)) }.to_negative_sentence ==
|
80
|
+
"This is not true: 'a' is equal to 1 or 'b' is equal to 2" }
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: predicated
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Steve Conover
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-06 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Predicated is a simple predicate model for Ruby.
|
23
|
+
email: sconover@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.markdown
|
30
|
+
files:
|
31
|
+
- lib/predicated/constrain.rb
|
32
|
+
- lib/predicated/evaluate.rb
|
33
|
+
- lib/predicated/from/callable_object.rb
|
34
|
+
- lib/predicated/from/ruby_string.rb
|
35
|
+
- lib/predicated/from/url_fragment.rb
|
36
|
+
- lib/predicated/gem_check.rb
|
37
|
+
- lib/predicated/predicate.rb
|
38
|
+
- lib/predicated/print.rb
|
39
|
+
- lib/predicated/selector.rb
|
40
|
+
- lib/predicated/to/arel.rb
|
41
|
+
- lib/predicated/to/sentence.rb
|
42
|
+
- lib/predicated/version.rb
|
43
|
+
- lib/predicated.rb
|
44
|
+
- README.markdown
|
45
|
+
- test/constrain_test.rb
|
46
|
+
- test/enumerable_test.rb
|
47
|
+
- test/equality_test.rb
|
48
|
+
- test/evaluate_test.rb
|
49
|
+
- test/from/callable_object_test.rb
|
50
|
+
- test/from/ruby_string_test.rb
|
51
|
+
- test/from/url_fragment_parser_test.rb
|
52
|
+
- test/from/url_fragment_test.rb
|
53
|
+
- test/print_test.rb
|
54
|
+
- test/selector_test.rb
|
55
|
+
- test/suite.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/test_helper_with_wrong.rb
|
58
|
+
- test/to/arel_test.rb
|
59
|
+
- test/to/sentence_test.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/sconover/predicated
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project: predicated
|
90
|
+
rubygems_version: 1.3.7
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Predicated is a simple predicate model for Ruby
|
94
|
+
test_files:
|
95
|
+
- test/constrain_test.rb
|
96
|
+
- test/enumerable_test.rb
|
97
|
+
- test/equality_test.rb
|
98
|
+
- test/evaluate_test.rb
|
99
|
+
- test/from/callable_object_test.rb
|
100
|
+
- test/from/ruby_string_test.rb
|
101
|
+
- test/from/url_fragment_parser_test.rb
|
102
|
+
- test/from/url_fragment_test.rb
|
103
|
+
- test/print_test.rb
|
104
|
+
- test/selector_test.rb
|
105
|
+
- test/suite.rb
|
106
|
+
- test/test_helper.rb
|
107
|
+
- test/test_helper_with_wrong.rb
|
108
|
+
- test/to/arel_test.rb
|
109
|
+
- test/to/sentence_test.rb
|