dry-logic 1.0.5 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +122 -26
- data/LICENSE +1 -1
- data/README.md +12 -14
- data/dry-logic.gemspec +26 -20
- data/lib/dry-logic.rb +1 -1
- data/lib/dry/logic.rb +2 -2
- data/lib/dry/logic/evaluator.rb +1 -1
- data/lib/dry/logic/operations.rb +11 -11
- data/lib/dry/logic/operations/abstract.rb +3 -3
- data/lib/dry/logic/operations/and.rb +3 -3
- data/lib/dry/logic/operations/attr.rb +1 -1
- data/lib/dry/logic/operations/binary.rb +4 -3
- data/lib/dry/logic/operations/check.rb +4 -4
- data/lib/dry/logic/operations/each.rb +2 -2
- data/lib/dry/logic/operations/implication.rb +2 -2
- data/lib/dry/logic/operations/key.rb +3 -3
- data/lib/dry/logic/operations/negation.rb +2 -2
- data/lib/dry/logic/operations/or.rb +2 -2
- data/lib/dry/logic/operations/set.rb +3 -3
- data/lib/dry/logic/operations/unary.rb +1 -1
- data/lib/dry/logic/operations/xor.rb +2 -2
- data/lib/dry/logic/operators.rb +4 -4
- data/lib/dry/logic/predicates.rb +32 -7
- data/lib/dry/logic/result.rb +2 -2
- data/lib/dry/logic/rule.rb +8 -8
- data/lib/dry/logic/rule/interface.rb +32 -37
- data/lib/dry/logic/rule/predicate.rb +3 -3
- data/lib/dry/logic/rule_compiler.rb +3 -3
- data/lib/dry/logic/version.rb +1 -1
- metadata +17 -166
- data/.codeclimate.yml +0 -12
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
- data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -34
- data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
- data/.github/workflows/ci.yml +0 -70
- data/.github/workflows/docsite.yml +0 -34
- data/.github/workflows/sync_configs.yml +0 -34
- data/.gitignore +0 -9
- data/.rspec +0 -4
- data/.rubocop.yml +0 -89
- data/CODE_OF_CONDUCT.md +0 -13
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -16
- data/Rakefile +0 -14
- data/benchmarks/rule_application.rb +0 -30
- data/benchmarks/setup.rb +0 -13
- data/bin/console +0 -11
- data/docsite/source/index.html.md +0 -54
- data/docsite/source/operations.html.md +0 -62
- data/docsite/source/predicates.html.md +0 -102
- data/examples/basic.rb +0 -16
- data/spec/integration/result_spec.rb +0 -61
- data/spec/integration/rule_spec.rb +0 -55
- data/spec/shared/predicates.rb +0 -59
- data/spec/shared/rule.rb +0 -74
- data/spec/spec_helper.rb +0 -30
- data/spec/support/mutant.rb +0 -11
- data/spec/unit/operations/and_spec.rb +0 -70
- data/spec/unit/operations/attr_spec.rb +0 -29
- data/spec/unit/operations/check_spec.rb +0 -51
- data/spec/unit/operations/each_spec.rb +0 -49
- data/spec/unit/operations/implication_spec.rb +0 -32
- data/spec/unit/operations/key_spec.rb +0 -135
- data/spec/unit/operations/negation_spec.rb +0 -51
- data/spec/unit/operations/or_spec.rb +0 -75
- data/spec/unit/operations/set_spec.rb +0 -43
- data/spec/unit/operations/xor_spec.rb +0 -63
- data/spec/unit/predicates/array_spec.rb +0 -43
- data/spec/unit/predicates/attr_spec.rb +0 -31
- data/spec/unit/predicates/bool_spec.rb +0 -36
- data/spec/unit/predicates/bytesize_spec.rb +0 -48
- data/spec/unit/predicates/case_spec.rb +0 -35
- data/spec/unit/predicates/date_spec.rb +0 -33
- data/spec/unit/predicates/date_time_spec.rb +0 -33
- data/spec/unit/predicates/decimal_spec.rb +0 -34
- data/spec/unit/predicates/empty_spec.rb +0 -40
- data/spec/unit/predicates/eql_spec.rb +0 -23
- data/spec/unit/predicates/even_spec.rb +0 -33
- data/spec/unit/predicates/excluded_from_spec.rb +0 -37
- data/spec/unit/predicates/excludes_spec.rb +0 -58
- data/spec/unit/predicates/false_spec.rb +0 -37
- data/spec/unit/predicates/filled_spec.rb +0 -40
- data/spec/unit/predicates/float_spec.rb +0 -33
- data/spec/unit/predicates/format_spec.rb +0 -31
- data/spec/unit/predicates/gt_spec.rb +0 -42
- data/spec/unit/predicates/gteq_spec.rb +0 -42
- data/spec/unit/predicates/hash_spec.rb +0 -40
- data/spec/unit/predicates/included_in_spec.rb +0 -37
- data/spec/unit/predicates/includes_spec.rb +0 -24
- data/spec/unit/predicates/int_spec.rb +0 -36
- data/spec/unit/predicates/key_spec.rb +0 -31
- data/spec/unit/predicates/lt_spec.rb +0 -42
- data/spec/unit/predicates/lteq_spec.rb +0 -42
- data/spec/unit/predicates/max_bytesize_spec.rb +0 -39
- data/spec/unit/predicates/max_size_spec.rb +0 -51
- data/spec/unit/predicates/min_bytesize_spec.rb +0 -39
- data/spec/unit/predicates/min_size_spec.rb +0 -51
- data/spec/unit/predicates/none_spec.rb +0 -30
- data/spec/unit/predicates/not_eql_spec.rb +0 -23
- data/spec/unit/predicates/number_spec.rb +0 -39
- data/spec/unit/predicates/odd_spec.rb +0 -33
- data/spec/unit/predicates/respond_to_spec.rb +0 -31
- data/spec/unit/predicates/size_spec.rb +0 -57
- data/spec/unit/predicates/str_spec.rb +0 -34
- data/spec/unit/predicates/time_spec.rb +0 -33
- data/spec/unit/predicates/true_spec.rb +0 -37
- data/spec/unit/predicates/type_spec.rb +0 -37
- data/spec/unit/predicates/uuid_v4_spec.rb +0 -29
- data/spec/unit/predicates_spec.rb +0 -25
- data/spec/unit/rule/predicate_spec.rb +0 -55
- data/spec/unit/rule_compiler_spec.rb +0 -129
- data/spec/unit/rule_spec.rb +0 -213
data/examples/basic.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic'
|
4
|
-
require 'dry/logic/predicates'
|
5
|
-
|
6
|
-
include Dry::Logic
|
7
|
-
|
8
|
-
user_present = Rule::Predicate.build(Predicates[:key?]).curry(:user)
|
9
|
-
|
10
|
-
has_min_age = Operations::Key.new(Rule::Predicate.build(Predicates[:gt?]).curry(18), name: [:user, :age])
|
11
|
-
|
12
|
-
user_rule = user_present & has_min_age
|
13
|
-
|
14
|
-
puts user_rule.(user: { age: 19 }).success?
|
15
|
-
|
16
|
-
puts user_rule.(user: { age: 18 }).success?
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Result do
|
4
|
-
include_context 'predicates'
|
5
|
-
|
6
|
-
describe '#to_s' do
|
7
|
-
shared_examples_for 'string representation' do
|
8
|
-
it 'returns string representation' do
|
9
|
-
expect(rule.(input).to_s).to eql(output)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context 'with a predicate' do
|
14
|
-
let(:rule) { Rule::Predicate.build(gt?, args: [18]) }
|
15
|
-
let(:input) { 17 }
|
16
|
-
let(:output) { 'gt?(18, 17)' }
|
17
|
-
|
18
|
-
it_behaves_like 'string representation'
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'with AND operation' do
|
22
|
-
let(:rule) { Rule::Predicate.build(array?).and(Rule::Predicate.build(empty?)) }
|
23
|
-
let(:input) { '' }
|
24
|
-
let(:output) { 'array?("") AND empty?("")' }
|
25
|
-
|
26
|
-
it_behaves_like 'string representation'
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'with OR operation' do
|
30
|
-
let(:rule) { Rule::Predicate.build(array?).or(Rule::Predicate.build(empty?)) }
|
31
|
-
let(:input) { 123 }
|
32
|
-
let(:output) { 'array?(123) OR empty?(123)' }
|
33
|
-
|
34
|
-
it_behaves_like 'string representation'
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'with XOR operation' do
|
38
|
-
let(:rule) { Rule::Predicate.build(array?).xor(Rule::Predicate.build(empty?)) }
|
39
|
-
let(:input) { [] }
|
40
|
-
let(:output) { 'array?([]) XOR empty?([])' }
|
41
|
-
|
42
|
-
it_behaves_like 'string representation'
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'with THEN operation' do
|
46
|
-
let(:rule) { Rule::Predicate.build(array?).then(Rule::Predicate.build(empty?)) }
|
47
|
-
let(:input) { [1, 2, 3] }
|
48
|
-
let(:output) { 'empty?([1, 2, 3])' }
|
49
|
-
|
50
|
-
it_behaves_like 'string representation'
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'with NOT operation' do
|
54
|
-
let(:rule) { Operations::Negation.new(Rule::Predicate.build(array?)) }
|
55
|
-
let(:input) { 'foo' }
|
56
|
-
let(:output) { 'not(array?("foo"))' }
|
57
|
-
|
58
|
-
it_behaves_like 'string representation'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry-logic'
|
4
|
-
|
5
|
-
RSpec.describe 'Rules' do
|
6
|
-
specify 'defining an anonymous rule with an arbitrary predicate' do
|
7
|
-
rule = Dry::Logic.Rule { |value| value.is_a?(Integer) }
|
8
|
-
|
9
|
-
expect(rule.(1)).to be_success
|
10
|
-
expect(rule[1]).to be(true)
|
11
|
-
end
|
12
|
-
|
13
|
-
specify 'defining a conjunction' do
|
14
|
-
rule = Dry::Logic.Rule(&:even?) & Dry::Logic.Rule { |v| v > 4 }
|
15
|
-
|
16
|
-
expect(rule.(3)).to be_failure
|
17
|
-
expect(rule.(4)).to be_failure
|
18
|
-
expect(rule.(5)).to be_failure
|
19
|
-
expect(rule.(6)).to be_success
|
20
|
-
end
|
21
|
-
|
22
|
-
specify 'defining a disjunction' do
|
23
|
-
rule = Dry::Logic.Rule { |v| v < 4 } | Dry::Logic.Rule { |v| v > 6 }
|
24
|
-
|
25
|
-
expect(rule.(5)).to be_failure
|
26
|
-
expect(rule.(3)).to be_success
|
27
|
-
expect(rule.(7)).to be_success
|
28
|
-
end
|
29
|
-
|
30
|
-
specify 'defining an implication' do
|
31
|
-
rule = Dry::Logic.Rule(&:empty?) > Dry::Logic.Rule { |v| v.is_a?(Array) }
|
32
|
-
|
33
|
-
expect(rule.('foo')).to be_success
|
34
|
-
expect(rule.([1, 2])).to be_success
|
35
|
-
expect(rule.([])).to be_success
|
36
|
-
expect(rule.('')).to be_failure
|
37
|
-
end
|
38
|
-
|
39
|
-
specify 'defining an exclusive disjunction' do
|
40
|
-
rule = Dry::Logic.Rule(&:empty?) ^ Dry::Logic.Rule { |v| v.is_a?(Array) }
|
41
|
-
|
42
|
-
expect(rule.('foo')).to be_failure
|
43
|
-
expect(rule.([])).to be_failure
|
44
|
-
expect(rule.([1, 2])).to be_success
|
45
|
-
expect(rule.('')).to be_success
|
46
|
-
end
|
47
|
-
|
48
|
-
specify 'defining a rule with options' do
|
49
|
-
rule = Dry::Logic::Rule(id: :empty?) { |value| value.empty? }
|
50
|
-
|
51
|
-
expect(rule.('foo')).to be_failure
|
52
|
-
expect(rule.('')).to be_success
|
53
|
-
expect(rule.ast('foo')).to eql([:predicate, [:empty?, [[:value, 'foo']]]])
|
54
|
-
end
|
55
|
-
end
|
data/spec/shared/predicates.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.shared_examples 'predicates' do
|
6
|
-
let(:nil?) { Dry::Logic::Predicates[:nil?] }
|
7
|
-
|
8
|
-
let(:array?) { Dry::Logic::Predicates[:array?] }
|
9
|
-
|
10
|
-
let(:empty?) { Dry::Logic::Predicates[:empty?] }
|
11
|
-
|
12
|
-
let(:str?) { Dry::Logic::Predicates[:str?] }
|
13
|
-
|
14
|
-
let(:true?) { Dry::Logic::Predicates[:true?] }
|
15
|
-
|
16
|
-
let(:hash?) { Dry::Logic::Predicates[:hash?] }
|
17
|
-
|
18
|
-
let(:int?) { Dry::Logic::Predicates[:int?] }
|
19
|
-
|
20
|
-
let(:filled?) { Dry::Logic::Predicates[:filled?] }
|
21
|
-
|
22
|
-
let(:min_size?) { Dry::Logic::Predicates[:min_size?] }
|
23
|
-
|
24
|
-
let(:lt?) { Dry::Logic::Predicates[:lt?] }
|
25
|
-
|
26
|
-
let(:gt?) { Dry::Logic::Predicates[:gt?] }
|
27
|
-
|
28
|
-
let(:key?) { Dry::Logic::Predicates[:key?] }
|
29
|
-
|
30
|
-
let(:attr?) { Dry::Logic::Predicates[:attr?] }
|
31
|
-
|
32
|
-
let(:eql?) { Dry::Logic::Predicates[:eql?] }
|
33
|
-
|
34
|
-
let(:size?) { Dry::Logic::Predicates[:size?] }
|
35
|
-
|
36
|
-
let(:case?) { Dry::Logic::Predicates[:case?] }
|
37
|
-
|
38
|
-
let(:equal?) { Dry::Logic::Predicates[:equal?] }
|
39
|
-
end
|
40
|
-
|
41
|
-
RSpec.shared_examples 'a passing predicate' do
|
42
|
-
let(:predicate) { Dry::Logic::Predicates[predicate_name] }
|
43
|
-
|
44
|
-
it do
|
45
|
-
arguments_list.each do |args|
|
46
|
-
expect(predicate.call(*args)).to be(true)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
RSpec.shared_examples 'a failing predicate' do
|
52
|
-
let(:predicate) { Dry::Logic::Predicates[predicate_name] }
|
53
|
-
|
54
|
-
it do
|
55
|
-
arguments_list.each do |args|
|
56
|
-
expect(predicate.call(*args)).to be(false)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
data/spec/shared/rule.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
shared_examples_for Dry::Logic::Rule do
|
4
|
-
let(:arity) { 2 }
|
5
|
-
let(:predicate) { double(:predicate, arity: arity, name: predicate_name) }
|
6
|
-
let(:rule_type) { described_class }
|
7
|
-
let(:predicate_name) { :good? }
|
8
|
-
|
9
|
-
describe '#arity' do
|
10
|
-
it 'returns its predicate arity' do
|
11
|
-
rule = rule_type.build(predicate)
|
12
|
-
|
13
|
-
expect(rule.arity).to be(2)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#parameters' do
|
18
|
-
it 'returns a list of args with their names' do
|
19
|
-
rule = rule_type.build(-> foo, bar { true }, args: [312])
|
20
|
-
|
21
|
-
expect(rule.parameters).to eql([[:req, :foo], [:req, :bar]])
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#call' do
|
26
|
-
let(:arity) { 1 }
|
27
|
-
|
28
|
-
it 'returns success for valid input' do
|
29
|
-
rule = rule_type.build(predicate)
|
30
|
-
|
31
|
-
expect(predicate).to receive(:[]).with(2).and_return(true)
|
32
|
-
|
33
|
-
expect(rule.(2)).to be_success
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'returns failure for invalid input' do
|
37
|
-
rule = rule_type.build(predicate)
|
38
|
-
|
39
|
-
expect(predicate).to receive(:[]).with(2).and_return(false)
|
40
|
-
|
41
|
-
expect(rule.(2)).to be_failure
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe '#[]' do
|
46
|
-
let(:arity) { 1 }
|
47
|
-
|
48
|
-
it 'delegates to its predicate' do
|
49
|
-
rule = rule_type.build(predicate)
|
50
|
-
|
51
|
-
expect(predicate).to receive(:[]).with(2).and_return(true)
|
52
|
-
expect(rule[2]).to be(true)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe '#curry' do
|
57
|
-
it 'returns a curried rule' do
|
58
|
-
rule = rule_type.build(predicate).curry(3)
|
59
|
-
|
60
|
-
expect(predicate).to receive(:[]).with(3, 2).and_return(true)
|
61
|
-
expect(rule.args).to eql([3])
|
62
|
-
|
63
|
-
expect(rule.(2)).to be_success
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'raises argument error when arity does not match' do
|
67
|
-
expect(predicate).to receive(:arity).and_return(2)
|
68
|
-
|
69
|
-
expect { rule_type.build(predicate).curry(3, 2, 1) }.to raise_error(
|
70
|
-
ArgumentError, 'wrong number of arguments (3 for 2)'
|
71
|
-
)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
if ENV['COVERAGE'] == 'true'
|
4
|
-
require 'simplecov'
|
5
|
-
SimpleCov.start do
|
6
|
-
add_filter '/spec/'
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
begin
|
11
|
-
require 'pry-byebug'
|
12
|
-
rescue LoadError; end
|
13
|
-
|
14
|
-
require 'dry-logic'
|
15
|
-
require 'dry/core/constants'
|
16
|
-
require 'pathname'
|
17
|
-
|
18
|
-
SPEC_ROOT = Pathname(__dir__)
|
19
|
-
|
20
|
-
Dir[SPEC_ROOT.join('shared/**/*.rb')].each(&method(:require))
|
21
|
-
Dir[SPEC_ROOT.join('support/**/*.rb')].each(&method(:require))
|
22
|
-
|
23
|
-
include Dry::Logic
|
24
|
-
include Dry::Core::Constants
|
25
|
-
|
26
|
-
RSpec.configure do |config|
|
27
|
-
config.disable_monkey_patching!
|
28
|
-
|
29
|
-
config.warnings = true
|
30
|
-
end
|
data/spec/support/mutant.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Operations::And do
|
4
|
-
subject(:operation) { Operations::And.new(left, right) }
|
5
|
-
|
6
|
-
include_context 'predicates'
|
7
|
-
|
8
|
-
let(:left) { Rule::Predicate.build(int?) }
|
9
|
-
let(:right) { Rule::Predicate.build(gt?).curry(18) }
|
10
|
-
|
11
|
-
describe '#call' do
|
12
|
-
it 'calls left and right' do
|
13
|
-
expect(operation.(18)).to be_failure
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#to_ast' do
|
18
|
-
it 'returns ast' do
|
19
|
-
expect(operation.to_ast).to eql(
|
20
|
-
[:and, [[:predicate, [:int?, [[:input, Undefined]]]], [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]
|
21
|
-
)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'returns result ast' do
|
25
|
-
expect(operation.('18').to_ast).to eql(
|
26
|
-
[:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]
|
27
|
-
)
|
28
|
-
|
29
|
-
expect(operation.with(hints: false).('18').to_ast).to eql(
|
30
|
-
[:predicate, [:int?, [[:input, '18']]]]
|
31
|
-
)
|
32
|
-
|
33
|
-
expect(operation.(18).to_ast).to eql(
|
34
|
-
[:predicate, [:gt?, [[:num, 18], [:input, 18]]]]
|
35
|
-
)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'returns failure result ast' do
|
39
|
-
expect(operation.with(id: :age).('18').to_ast).to eql(
|
40
|
-
[:failure, [:age, [:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]]]
|
41
|
-
)
|
42
|
-
|
43
|
-
expect(operation.with(id: :age).(18).to_ast).to eql(
|
44
|
-
[:failure, [:age, [:predicate, [:gt?, [[:num, 18], [:input, 18]]]]]]
|
45
|
-
)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe '#and' do
|
50
|
-
let(:other) { Rule::Predicate.build(lt?).curry(30) }
|
51
|
-
|
52
|
-
it 'creates and with the other' do
|
53
|
-
expect(operation.and(other).(31)).to be_failure
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe '#or' do
|
58
|
-
let(:other) { Rule::Predicate.build(lt?).curry(14) }
|
59
|
-
|
60
|
-
it 'creates or with the other' do
|
61
|
-
expect(operation.or(other).(13)).to be_success
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
describe '#to_s' do
|
66
|
-
it 'returns string representation' do
|
67
|
-
expect(operation.to_s).to eql('int? AND gt?(18)')
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Operations::Attr do
|
4
|
-
subject(:operation) { Operations::Attr.new(Rule::Predicate.build(str?), name: :name) }
|
5
|
-
|
6
|
-
include_context 'predicates'
|
7
|
-
|
8
|
-
let(:model) { Struct.new(:name) }
|
9
|
-
|
10
|
-
describe '#call' do
|
11
|
-
it 'applies predicate to the value' do
|
12
|
-
expect(operation.(model.new('Jane'))).to be_success
|
13
|
-
expect(operation.(model.new(nil))).to be_failure
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#and' do
|
18
|
-
let(:other) { Operations::Attr.new(Rule::Predicate.build(min_size?).curry(3), name: :name) }
|
19
|
-
|
20
|
-
it 'returns and where value is passed to the right' do
|
21
|
-
present_and_string = operation.and(other)
|
22
|
-
|
23
|
-
expect(present_and_string.(model.new('Jane'))).to be_success
|
24
|
-
|
25
|
-
expect(present_and_string.(model.new('Ja'))).to be_failure
|
26
|
-
expect(present_and_string.(model.new(1))).to be_failure
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Operations::Check do
|
4
|
-
include_context 'predicates'
|
5
|
-
|
6
|
-
describe '#call' do
|
7
|
-
context 'with 1-level nesting' do
|
8
|
-
subject(:operation) do
|
9
|
-
Operations::Check.new(Rule::Predicate.build(eql?).curry(1), id: :compare, keys: [:num])
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'applies predicate to args extracted from the input' do
|
13
|
-
expect(operation.(num: 1)).to be_success
|
14
|
-
expect(operation.(num: 2)).to be_failure
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'with 2-levels nesting' do
|
19
|
-
subject(:operation) do
|
20
|
-
Operations::Check.new(Rule::Predicate.build(eql?), id: :compare, keys: [[:nums, :left], [:nums, :right]])
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'applies predicate to args extracted from the input' do
|
24
|
-
expect(operation.(nums: { left: 1, right: 1 })).to be_success
|
25
|
-
expect(operation.(nums: { left: 1, right: 2 })).to be_failure
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'curries args properly' do
|
29
|
-
result = operation.(nums: { left: 1, right: 2 })
|
30
|
-
|
31
|
-
expect(result.to_ast).to eql(
|
32
|
-
[:failure, [:compare, [:check, [
|
33
|
-
[[:nums, :left], [:nums, :right]], [:predicate, [:eql?, [[:left, 1], [:right, 2]]]]]
|
34
|
-
]]]
|
35
|
-
)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe '#to_ast' do
|
41
|
-
subject(:operation) do
|
42
|
-
Operations::Check.new(Rule::Predicate.build(str?), keys: [:email])
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'returns ast' do
|
46
|
-
expect(operation.to_ast).to eql(
|
47
|
-
[:check, [[:email], [:predicate, [:str?, [[:input, Undefined]]]]]]
|
48
|
-
)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|