factbase 0.11.2 → 0.12.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.
- checksums.yaml +4 -4
- data/.rultor.yml +0 -1
- data/lib/factbase/terms/debug.rb +25 -0
- data/lib/factbase/version.rb +1 -1
- data/test/factbase/terms/test_debug.rb +75 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f31200ddcd8de0d737dbabea9aefc051794f86bbae0891f8d624545912972add
|
4
|
+
data.tar.gz: 7c1688856cde3e72f8e9f53e846b39c7cef508a0df95cc88d0631bfae36ce07f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26c83d3a288f8106bfe17856bccb9c1bde6b130384e9a4546456a76c1b6a69fff9c49954789aaeee6413f7cfae638d6f1320f1de4f7ac000b3c552b31449ebbd
|
7
|
+
data.tar.gz: 761d577bc112c4d542667508924406570ff449fbab87af3a18fc0ce7c8a94cccf6d40f400b1b1c3f6bd76d95a51678a680fd17feeee0b89caacbac2b56e8e6f6
|
data/.rultor.yml
CHANGED
data/lib/factbase/terms/debug.rb
CHANGED
@@ -19,4 +19,29 @@ module Factbase::Debug
|
|
19
19
|
puts "#{self} -> #{r}"
|
20
20
|
r
|
21
21
|
end
|
22
|
+
|
23
|
+
def assert(fact, maps, fb)
|
24
|
+
assert_args(2)
|
25
|
+
message = @operands[0]
|
26
|
+
unless message.is_a?(String)
|
27
|
+
raise ArgumentError,
|
28
|
+
"A string expected as first argument of 'assert', but '#{message}' provided"
|
29
|
+
end
|
30
|
+
t = @operands[1]
|
31
|
+
unless t.is_a?(Factbase::Term)
|
32
|
+
raise ArgumentError,
|
33
|
+
"A term expected as second argument of 'assert', but '#{t}' provided"
|
34
|
+
end
|
35
|
+
result = t.evaluate(fact, maps, fb)
|
36
|
+
# Convert result to boolean-like evaluation
|
37
|
+
# Arrays are truthy if they contain at least one truthy element
|
38
|
+
truthy =
|
39
|
+
if result.is_a?(Array)
|
40
|
+
result.any? { |v| v && v != 0 }
|
41
|
+
else
|
42
|
+
result && result != 0
|
43
|
+
end
|
44
|
+
raise message unless truthy
|
45
|
+
true
|
46
|
+
end
|
22
47
|
end
|
data/lib/factbase/version.rb
CHANGED
@@ -33,4 +33,79 @@ class TestDebug < Factbase::Test
|
|
33
33
|
end
|
34
34
|
assert_match(/Too many \(\d+\) operands for 'traced' \(\d+ expected\)/, e.message)
|
35
35
|
end
|
36
|
+
|
37
|
+
def test_assert_with_true_condition
|
38
|
+
t = Factbase::Term.new(:assert, ['all must be positive', Factbase::Term.new(:gt, [:foo, 0])])
|
39
|
+
assert(t.evaluate(fact('foo' => 5), [], Factbase.new))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_assert_with_false_condition
|
43
|
+
t = Factbase::Term.new(:assert, ['all must be positive', Factbase::Term.new(:gt, [:foo, 0])])
|
44
|
+
e =
|
45
|
+
assert_raises(RuntimeError) do
|
46
|
+
t.evaluate(fact('foo' => -1), [], Factbase.new)
|
47
|
+
end
|
48
|
+
assert_equal("all must be positive at (assert 'all must be positive' (gt foo 0))", e.message)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_assert_with_zero_value
|
52
|
+
t = Factbase::Term.new(:assert, ['value must not be zero', Factbase::Term.new(:gt, [:foo, 0])])
|
53
|
+
e =
|
54
|
+
assert_raises(RuntimeError) do
|
55
|
+
t.evaluate(fact('foo' => 0), [], Factbase.new)
|
56
|
+
end
|
57
|
+
assert_equal("value must not be zero at (assert 'value must not be zero' (gt foo 0))", e.message)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_assert_with_array_true_condition
|
61
|
+
t = Factbase::Term.new(:assert, ['at least one positive', Factbase::Term.new(:gt, [:foo, 0])])
|
62
|
+
assert(t.evaluate(fact('foo' => [1, 2, 3]), [], Factbase.new))
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_assert_with_array_false_condition
|
66
|
+
t = Factbase::Term.new(:assert, ['at least one positive', Factbase::Term.new(:gt, [:foo, 0])])
|
67
|
+
e =
|
68
|
+
assert_raises(RuntimeError) do
|
69
|
+
t.evaluate(fact('foo' => [-1, -2, -3]), [], Factbase.new)
|
70
|
+
end
|
71
|
+
assert_equal("at least one positive at (assert 'at least one positive' (gt foo 0))", e.message)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_assert_with_mixed_array
|
75
|
+
t = Factbase::Term.new(:assert, ['at least one positive', Factbase::Term.new(:gt, [:foo, 0])])
|
76
|
+
assert(t.evaluate(fact('foo' => [-1, 0, 3]), [], Factbase.new))
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_assert_raises_when_message_not_string
|
80
|
+
e =
|
81
|
+
assert_raises(StandardError) do
|
82
|
+
Factbase::Term.new(:assert, [123, Factbase::Term.new(:gt, [:foo, 0])]).evaluate(fact, [], Factbase.new)
|
83
|
+
end
|
84
|
+
assert_match(/A string expected as first argument of 'assert', but '123' provided/, e.message)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_assert_raises_when_second_arg_not_term
|
88
|
+
e =
|
89
|
+
assert_raises(StandardError) do
|
90
|
+
Factbase::Term.new(:assert, %w[message not_a_term]).evaluate(fact, [], Factbase.new)
|
91
|
+
end
|
92
|
+
assert_match(/A term expected as second argument of 'assert', but 'not_a_term' provided/, e.message)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_assert_raises_when_too_few_args
|
96
|
+
e =
|
97
|
+
assert_raises(StandardError) do
|
98
|
+
Factbase::Term.new(:assert, ['message']).evaluate(fact, [], Factbase.new)
|
99
|
+
end
|
100
|
+
assert_match(/Too few \(\d+\) operands for 'assert' \(\d+ expected\)/, e.message)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_assert_raises_when_too_many_args
|
104
|
+
e =
|
105
|
+
assert_raises(StandardError) do
|
106
|
+
Factbase::Term.new(:assert, ['message', Factbase::Term.new(:gt, [:foo, 0]), 'extra']).evaluate(fact, [],
|
107
|
+
Factbase.new)
|
108
|
+
end
|
109
|
+
assert_match(/Too many \(\d+\) operands for 'assert' \(\d+ expected\)/, e.message)
|
110
|
+
end
|
36
111
|
end
|