rubocop --safe-auto-correct lib spec
This commit is contained in:
parent
57e4fe24a8
commit
d64d598098
|
@ -1,3 +1,3 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic'
|
||||
require "dry/logic"
|
||||
|
|
|
@ -7,5 +7,5 @@ module Dry
|
|||
end
|
||||
end
|
||||
|
||||
require 'dry/logic/rule/predicate'
|
||||
require 'dry/logic/operations'
|
||||
require "dry/logic/rule/predicate"
|
||||
require "dry/logic/operations"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/equalizer'
|
||||
require "dry/equalizer"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/and'
|
||||
require 'dry/logic/operations/or'
|
||||
require 'dry/logic/operations/xor'
|
||||
require 'dry/logic/operations/implication'
|
||||
require 'dry/logic/operations/negation'
|
||||
require "dry/logic/operations/and"
|
||||
require "dry/logic/operations/or"
|
||||
require "dry/logic/operations/xor"
|
||||
require "dry/logic/operations/implication"
|
||||
require "dry/logic/operations/negation"
|
||||
|
||||
require 'dry/logic/operations/key'
|
||||
require 'dry/logic/operations/attr'
|
||||
require 'dry/logic/operations/each'
|
||||
require 'dry/logic/operations/set'
|
||||
require 'dry/logic/operations/check'
|
||||
require "dry/logic/operations/key"
|
||||
require "dry/logic/operations/attr"
|
||||
require "dry/logic/operations/each"
|
||||
require "dry/logic/operations/set"
|
||||
require "dry/logic/operations/check"
|
||||
|
||||
require 'dry/logic/operators'
|
||||
require "dry/logic/operators"
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/core/constants'
|
||||
require 'dry/equalizer'
|
||||
require 'dry/logic/operators'
|
||||
require "dry/core/constants"
|
||||
require "dry/equalizer"
|
||||
require "dry/logic/operators"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/binary'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/binary"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
@ -17,7 +17,7 @@ module Dry
|
|||
def type
|
||||
:and
|
||||
end
|
||||
alias operator type
|
||||
alias_method :operator, :type
|
||||
|
||||
def call(input)
|
||||
left_result = left.(input)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/key'
|
||||
require "dry/logic/operations/key"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/abstract'
|
||||
require "dry/logic/operations/abstract"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/unary'
|
||||
require 'dry/logic/evaluator'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/unary"
|
||||
require "dry/logic/evaluator"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/unary'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/unary"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/binary'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/binary"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/unary'
|
||||
require 'dry/logic/evaluator'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/unary"
|
||||
require "dry/logic/evaluator"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/unary'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/unary"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/binary'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/binary"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/abstract'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/abstract"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
@ -29,7 +29,7 @@ module Dry
|
|||
end
|
||||
|
||||
def to_s
|
||||
"#{type}(#{rules.map(&:to_s).join(', ')})"
|
||||
"#{type}(#{rules.map(&:to_s).join(", ")})"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/abstract'
|
||||
require "dry/logic/operations/abstract"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/operations/binary'
|
||||
require 'dry/logic/result'
|
||||
require "dry/logic/operations/binary"
|
||||
require "dry/logic/result"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -6,22 +6,22 @@ module Dry
|
|||
def and(other)
|
||||
Operations::And.new(self, other)
|
||||
end
|
||||
alias & and
|
||||
alias_method :&, :and
|
||||
|
||||
def or(other)
|
||||
Operations::Or.new(self, other)
|
||||
end
|
||||
alias | or
|
||||
alias_method :|, :or
|
||||
|
||||
def xor(other)
|
||||
Operations::Xor.new(self, other)
|
||||
end
|
||||
alias ^ xor
|
||||
alias_method :^, :xor
|
||||
|
||||
def then(other)
|
||||
Operations::Implication.new(self, other)
|
||||
end
|
||||
alias > then
|
||||
alias_method :>, :then
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'bigdecimal'
|
||||
require 'bigdecimal/util'
|
||||
require 'date'
|
||||
require "bigdecimal"
|
||||
require "bigdecimal/util"
|
||||
require "date"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
@ -13,7 +13,7 @@ module Dry
|
|||
end
|
||||
|
||||
def type?(type, input)
|
||||
input.kind_of?(type)
|
||||
input.is_a?(type)
|
||||
end
|
||||
|
||||
def nil?(input)
|
||||
|
@ -147,12 +147,12 @@ module Dry
|
|||
end
|
||||
|
||||
def inclusion?(list, input)
|
||||
::Kernel.warn 'inclusion is deprecated - use included_in instead.'
|
||||
::Kernel.warn "inclusion is deprecated - use included_in instead."
|
||||
included_in?(list, input)
|
||||
end
|
||||
|
||||
def exclusion?(list, input)
|
||||
::Kernel.warn 'exclusion is deprecated - use excluded_from instead.'
|
||||
::Kernel.warn "exclusion is deprecated - use excluded_from instead."
|
||||
excluded_from?(list, input)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/core/constants'
|
||||
require "dry/core/constants"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
@ -69,7 +69,7 @@ module Dry
|
|||
if args.empty?
|
||||
name.to_s
|
||||
else
|
||||
"#{name}(#{args.map(&:last).map(&:inspect).join(', ')})"
|
||||
"#{name}(#{args.map(&:last).map(&:inspect).join(", ")})"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'concurrent/map'
|
||||
require 'dry/core/constants'
|
||||
require 'dry/equalizer'
|
||||
require 'dry/logic/operations'
|
||||
require 'dry/logic/result'
|
||||
require 'dry/logic/rule/interface'
|
||||
require "concurrent/map"
|
||||
require "dry/core/constants"
|
||||
require "dry/equalizer"
|
||||
require "dry/logic/operations"
|
||||
require "dry/logic/result"
|
||||
require "dry/logic/rule/interface"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
@ -38,13 +38,13 @@ module Dry
|
|||
base.interfaces.fetch_or_store([arity, curried]) do
|
||||
interface = Interface.new(arity, curried)
|
||||
klass = Class.new(base) { include interface }
|
||||
base.const_set("#{base.name.split('::').last}#{interface.name}", klass)
|
||||
base.const_set("#{base.name.split("::").last}#{interface.name}", klass)
|
||||
klass
|
||||
end
|
||||
end
|
||||
|
||||
def self.build(predicate, args: EMPTY_ARRAY, arity: predicate.arity, **options)
|
||||
specialize(arity, args.size).new(predicate, { args: args, arity: arity, **options })
|
||||
specialize(arity, args.size).new(predicate, {args: args, arity: arity, **options})
|
||||
end
|
||||
|
||||
def initialize(predicate, options = EMPTY_HASH)
|
||||
|
|
|
@ -49,9 +49,9 @@ module Dry
|
|||
|
||||
def name
|
||||
if constant?
|
||||
'Constant'
|
||||
"Constant"
|
||||
else
|
||||
arity_str = variable_arity? ? 'VariableArity' : "#{arity}Arity"
|
||||
arity_str = variable_arity? ? "VariableArity" : "#{arity}Arity"
|
||||
curried_str = curried? ? "#{curried}Curried" : EMPTY_STRING
|
||||
|
||||
"#{arity_str}#{curried_str}"
|
||||
|
@ -61,9 +61,9 @@ module Dry
|
|||
def define_constructor
|
||||
assignment =
|
||||
if curried.equal?(1)
|
||||
'@arg0 = @args[0]'
|
||||
"@arg0 = @args[0]"
|
||||
else
|
||||
"#{curried_args.join(', ')} = @args"
|
||||
"#{curried_args.join(", ")} = @args"
|
||||
end
|
||||
|
||||
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
||||
|
@ -94,9 +94,9 @@ module Dry
|
|||
def define_splat_application
|
||||
application =
|
||||
if curried?
|
||||
"@predicate[#{curried_args.join(', ')}, *input]"
|
||||
"@predicate[#{curried_args.join(", ")}, *input]"
|
||||
else
|
||||
'@predicate[*input]'
|
||||
"@predicate[*input]"
|
||||
end
|
||||
|
||||
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
||||
|
@ -115,8 +115,8 @@ module Dry
|
|||
end
|
||||
|
||||
def define_fixed_application
|
||||
parameters = unapplied_args.join(', ')
|
||||
application = "@predicate[#{(curried_args + unapplied_args).join(', ')}]"
|
||||
parameters = unapplied_args.join(", ")
|
||||
application = "@predicate[#{(curried_args + unapplied_args).join(", ")}]"
|
||||
|
||||
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
||||
def call(#{parameters})
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/rule'
|
||||
require "dry/logic/rule"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
@ -19,9 +19,9 @@ module Dry
|
|||
|
||||
def to_s
|
||||
if args.size > 0
|
||||
"#{name}(#{args.map(&:inspect).join(', ')})"
|
||||
"#{name}(#{args.map(&:inspect).join(", ")})"
|
||||
else
|
||||
"#{name}"
|
||||
name.to_s
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/core/constants'
|
||||
require "dry/core/constants"
|
||||
|
||||
require 'dry/logic/rule'
|
||||
require 'dry/logic/rule/predicate'
|
||||
require "dry/logic/rule"
|
||||
require "dry/logic/rule/predicate"
|
||||
|
||||
module Dry
|
||||
module Logic
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
module Dry
|
||||
module Logic
|
||||
VERSION = '1.0.7'
|
||||
VERSION = "1.0.7"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,61 +1,61 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Result do
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
describe '#to_s' do
|
||||
shared_examples_for 'string representation' do
|
||||
it 'returns string representation' do
|
||||
describe "#to_s" do
|
||||
shared_examples_for "string representation" do
|
||||
it "returns string representation" do
|
||||
expect(rule.(input).to_s).to eql(output)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a predicate' do
|
||||
context "with a predicate" do
|
||||
let(:rule) { Rule::Predicate.build(gt?, args: [18]) }
|
||||
let(:input) { 17 }
|
||||
let(:output) { 'gt?(18, 17)' }
|
||||
let(:output) { "gt?(18, 17)" }
|
||||
|
||||
it_behaves_like 'string representation'
|
||||
it_behaves_like "string representation"
|
||||
end
|
||||
|
||||
context 'with AND operation' do
|
||||
context "with AND operation" do
|
||||
let(:rule) { Rule::Predicate.build(array?).and(Rule::Predicate.build(empty?)) }
|
||||
let(:input) { '' }
|
||||
let(:input) { "" }
|
||||
let(:output) { 'array?("") AND empty?("")' }
|
||||
|
||||
it_behaves_like 'string representation'
|
||||
it_behaves_like "string representation"
|
||||
end
|
||||
|
||||
context 'with OR operation' do
|
||||
context "with OR operation" do
|
||||
let(:rule) { Rule::Predicate.build(array?).or(Rule::Predicate.build(empty?)) }
|
||||
let(:input) { 123 }
|
||||
let(:output) { 'array?(123) OR empty?(123)' }
|
||||
let(:output) { "array?(123) OR empty?(123)" }
|
||||
|
||||
it_behaves_like 'string representation'
|
||||
it_behaves_like "string representation"
|
||||
end
|
||||
|
||||
context 'with XOR operation' do
|
||||
context "with XOR operation" do
|
||||
let(:rule) { Rule::Predicate.build(array?).xor(Rule::Predicate.build(empty?)) }
|
||||
let(:input) { [] }
|
||||
let(:output) { 'array?([]) XOR empty?([])' }
|
||||
let(:output) { "array?([]) XOR empty?([])" }
|
||||
|
||||
it_behaves_like 'string representation'
|
||||
it_behaves_like "string representation"
|
||||
end
|
||||
|
||||
context 'with THEN operation' do
|
||||
context "with THEN operation" do
|
||||
let(:rule) { Rule::Predicate.build(array?).then(Rule::Predicate.build(empty?)) }
|
||||
let(:input) { [1, 2, 3] }
|
||||
let(:output) { 'empty?([1, 2, 3])' }
|
||||
let(:output) { "empty?([1, 2, 3])" }
|
||||
|
||||
it_behaves_like 'string representation'
|
||||
it_behaves_like "string representation"
|
||||
end
|
||||
|
||||
context 'with NOT operation' do
|
||||
context "with NOT operation" do
|
||||
let(:rule) { Operations::Negation.new(Rule::Predicate.build(array?)) }
|
||||
let(:input) { 'foo' }
|
||||
let(:input) { "foo" }
|
||||
let(:output) { 'not(array?("foo"))' }
|
||||
|
||||
it_behaves_like 'string representation'
|
||||
it_behaves_like "string representation"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry-logic'
|
||||
require "dry-logic"
|
||||
|
||||
RSpec.describe 'Rules' do
|
||||
specify 'defining an anonymous rule with an arbitrary predicate' do
|
||||
RSpec.describe "Rules" do
|
||||
specify "defining an anonymous rule with an arbitrary predicate" do
|
||||
rule = Dry::Logic.Rule { |value| value.is_a?(Integer) }
|
||||
|
||||
expect(rule.(1)).to be_success
|
||||
expect(rule[1]).to be(true)
|
||||
end
|
||||
|
||||
specify 'defining a conjunction' do
|
||||
specify "defining a conjunction" do
|
||||
rule = Dry::Logic.Rule(&:even?) & Dry::Logic.Rule { |v| v > 4 }
|
||||
|
||||
expect(rule.(3)).to be_failure
|
||||
|
@ -19,7 +19,7 @@ RSpec.describe 'Rules' do
|
|||
expect(rule.(6)).to be_success
|
||||
end
|
||||
|
||||
specify 'defining a disjunction' do
|
||||
specify "defining a disjunction" do
|
||||
rule = Dry::Logic.Rule { |v| v < 4 } | Dry::Logic.Rule { |v| v > 6 }
|
||||
|
||||
expect(rule.(5)).to be_failure
|
||||
|
@ -27,29 +27,29 @@ RSpec.describe 'Rules' do
|
|||
expect(rule.(7)).to be_success
|
||||
end
|
||||
|
||||
specify 'defining an implication' do
|
||||
specify "defining an implication" do
|
||||
rule = Dry::Logic.Rule(&:empty?) > Dry::Logic.Rule { |v| v.is_a?(Array) }
|
||||
|
||||
expect(rule.('foo')).to be_success
|
||||
expect(rule.("foo")).to be_success
|
||||
expect(rule.([1, 2])).to be_success
|
||||
expect(rule.([])).to be_success
|
||||
expect(rule.('')).to be_failure
|
||||
expect(rule.("")).to be_failure
|
||||
end
|
||||
|
||||
specify 'defining an exclusive disjunction' do
|
||||
specify "defining an exclusive disjunction" do
|
||||
rule = Dry::Logic.Rule(&:empty?) ^ Dry::Logic.Rule { |v| v.is_a?(Array) }
|
||||
|
||||
expect(rule.('foo')).to be_failure
|
||||
expect(rule.("foo")).to be_failure
|
||||
expect(rule.([])).to be_failure
|
||||
expect(rule.([1, 2])).to be_success
|
||||
expect(rule.('')).to be_success
|
||||
expect(rule.("")).to be_success
|
||||
end
|
||||
|
||||
specify 'defining a rule with options' do
|
||||
specify "defining a rule with options" do
|
||||
rule = Dry::Logic::Rule(id: :empty?) { |value| value.empty? }
|
||||
|
||||
expect(rule.('foo')).to be_failure
|
||||
expect(rule.('')).to be_success
|
||||
expect(rule.ast('foo')).to eql([:predicate, [:empty?, [[:value, 'foo']]]])
|
||||
expect(rule.("foo")).to be_failure
|
||||
expect(rule.("")).to be_success
|
||||
expect(rule.ast("foo")).to eql([:predicate, [:empty?, [[:value, "foo"]]]])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.shared_examples 'predicates' do
|
||||
RSpec.shared_examples "predicates" do
|
||||
let(:nil?) { Dry::Logic::Predicates[:nil?] }
|
||||
|
||||
let(:array?) { Dry::Logic::Predicates[:array?] }
|
||||
|
@ -38,7 +38,7 @@ RSpec.shared_examples 'predicates' do
|
|||
let(:equal?) { Dry::Logic::Predicates[:equal?] }
|
||||
end
|
||||
|
||||
RSpec.shared_examples 'a passing predicate' do
|
||||
RSpec.shared_examples "a passing predicate" do
|
||||
let(:predicate) { Dry::Logic::Predicates[predicate_name] }
|
||||
|
||||
it do
|
||||
|
@ -48,7 +48,7 @@ RSpec.shared_examples 'a passing predicate' do
|
|||
end
|
||||
end
|
||||
|
||||
RSpec.shared_examples 'a failing predicate' do
|
||||
RSpec.shared_examples "a failing predicate" do
|
||||
let(:predicate) { Dry::Logic::Predicates[predicate_name] }
|
||||
|
||||
it do
|
||||
|
|
|
@ -6,26 +6,26 @@ shared_examples_for Dry::Logic::Rule do
|
|||
let(:rule_type) { described_class }
|
||||
let(:predicate_name) { :good? }
|
||||
|
||||
describe '#arity' do
|
||||
it 'returns its predicate arity' do
|
||||
describe "#arity" do
|
||||
it "returns its predicate arity" do
|
||||
rule = rule_type.build(predicate)
|
||||
|
||||
expect(rule.arity).to be(2)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#parameters' do
|
||||
it 'returns a list of args with their names' do
|
||||
describe "#parameters" do
|
||||
it "returns a list of args with their names" do
|
||||
rule = rule_type.build(-> foo, bar { true }, args: [312])
|
||||
|
||||
expect(rule.parameters).to eql([[:req, :foo], [:req, :bar]])
|
||||
expect(rule.parameters).to eql([%i[req foo], %i[req bar]])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#call' do
|
||||
describe "#call" do
|
||||
let(:arity) { 1 }
|
||||
|
||||
it 'returns success for valid input' do
|
||||
it "returns success for valid input" do
|
||||
rule = rule_type.build(predicate)
|
||||
|
||||
expect(predicate).to receive(:[]).with(2).and_return(true)
|
||||
|
@ -33,7 +33,7 @@ shared_examples_for Dry::Logic::Rule do
|
|||
expect(rule.(2)).to be_success
|
||||
end
|
||||
|
||||
it 'returns failure for invalid input' do
|
||||
it "returns failure for invalid input" do
|
||||
rule = rule_type.build(predicate)
|
||||
|
||||
expect(predicate).to receive(:[]).with(2).and_return(false)
|
||||
|
@ -42,10 +42,10 @@ shared_examples_for Dry::Logic::Rule do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#[]' do
|
||||
describe "#[]" do
|
||||
let(:arity) { 1 }
|
||||
|
||||
it 'delegates to its predicate' do
|
||||
it "delegates to its predicate" do
|
||||
rule = rule_type.build(predicate)
|
||||
|
||||
expect(predicate).to receive(:[]).with(2).and_return(true)
|
||||
|
@ -53,8 +53,8 @@ shared_examples_for Dry::Logic::Rule do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#curry' do
|
||||
it 'returns a curried rule' do
|
||||
describe "#curry" do
|
||||
it "returns a curried rule" do
|
||||
rule = rule_type.build(predicate).curry(3)
|
||||
|
||||
expect(predicate).to receive(:[]).with(3, 2).and_return(true)
|
||||
|
@ -63,11 +63,11 @@ shared_examples_for Dry::Logic::Rule do
|
|||
expect(rule.(2)).to be_success
|
||||
end
|
||||
|
||||
it 'raises argument error when arity does not match' do
|
||||
it "raises argument error when arity does not match" do
|
||||
expect(predicate).to receive(:arity).and_return(2)
|
||||
|
||||
expect { rule_type.build(predicate).curry(3, 2, 1) }.to raise_error(
|
||||
ArgumentError, 'wrong number of arguments (3 for 2)'
|
||||
ArgumentError, "wrong number of arguments (3 for 2)"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'support/coverage'
|
||||
require_relative 'support/warnings'
|
||||
require_relative "support/coverage"
|
||||
require_relative "support/warnings"
|
||||
|
||||
begin
|
||||
require 'pry-byebug'
|
||||
require "pry-byebug"
|
||||
rescue LoadError; end
|
||||
require 'dry-logic'
|
||||
require 'dry/core/constants'
|
||||
require 'pathname'
|
||||
require "dry-logic"
|
||||
require "dry/core/constants"
|
||||
require "pathname"
|
||||
|
||||
SPEC_ROOT = Pathname(__dir__)
|
||||
|
||||
Dir[SPEC_ROOT.join('shared/**/*.rb')].each(&method(:require))
|
||||
Dir[SPEC_ROOT.join('support/**/*.rb')].each(&method(:require))
|
||||
Dir[SPEC_ROOT.join("shared/**/*.rb")].each(&method(:require))
|
||||
Dir[SPEC_ROOT.join("support/**/*.rb")].each(&method(:require))
|
||||
|
||||
include Dry::Logic
|
||||
include Dry::Core::Constants
|
||||
|
|
|
@ -3,31 +3,31 @@
|
|||
RSpec.describe Operations::And do
|
||||
subject(:operation) { Operations::And.new(left, right) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:left) { Rule::Predicate.build(int?) }
|
||||
let(:right) { Rule::Predicate.build(gt?).curry(18) }
|
||||
|
||||
describe '#call' do
|
||||
it 'calls left and right' do
|
||||
describe "#call" do
|
||||
it "calls left and right" do
|
||||
expect(operation.(18)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:and, [[:predicate, [:int?, [[:input, Undefined]]]], [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns result ast' do
|
||||
expect(operation.('18').to_ast).to eql(
|
||||
[:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]
|
||||
it "returns result ast" do
|
||||
expect(operation.("18").to_ast).to eql(
|
||||
[:and, [[:predicate, [:int?, [[:input, "18"]]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, "18"]]]]]]]
|
||||
)
|
||||
|
||||
expect(operation.with(hints: false).('18').to_ast).to eql(
|
||||
[:predicate, [:int?, [[:input, '18']]]]
|
||||
expect(operation.with(hints: false).("18").to_ast).to eql(
|
||||
[:predicate, [:int?, [[:input, "18"]]]]
|
||||
)
|
||||
|
||||
expect(operation.(18).to_ast).to eql(
|
||||
|
@ -35,9 +35,9 @@ RSpec.describe Operations::And do
|
|||
)
|
||||
end
|
||||
|
||||
it 'returns failure result ast' do
|
||||
expect(operation.with(id: :age).('18').to_ast).to eql(
|
||||
[:failure, [:age, [:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]]]
|
||||
it "returns failure result ast" do
|
||||
expect(operation.with(id: :age).("18").to_ast).to eql(
|
||||
[:failure, [:age, [:and, [[:predicate, [:int?, [[:input, "18"]]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, "18"]]]]]]]]]
|
||||
)
|
||||
|
||||
expect(operation.with(id: :age).(18).to_ast).to eql(
|
||||
|
@ -46,25 +46,25 @@ RSpec.describe Operations::And do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#and' do
|
||||
describe "#and" do
|
||||
let(:other) { Rule::Predicate.build(lt?).curry(30) }
|
||||
|
||||
it 'creates and with the other' do
|
||||
it "creates and with the other" do
|
||||
expect(operation.and(other).(31)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#or' do
|
||||
describe "#or" do
|
||||
let(:other) { Rule::Predicate.build(lt?).curry(14) }
|
||||
|
||||
it 'creates or with the other' do
|
||||
it "creates or with the other" do
|
||||
expect(operation.or(other).(13)).to be_success
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('int? AND gt?(18)')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("int? AND gt?(18)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,26 +3,26 @@
|
|||
RSpec.describe Operations::Attr do
|
||||
subject(:operation) { Operations::Attr.new(Rule::Predicate.build(str?), name: :name) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:model) { Struct.new(:name) }
|
||||
|
||||
describe '#call' do
|
||||
it 'applies predicate to the value' do
|
||||
expect(operation.(model.new('Jane'))).to be_success
|
||||
describe "#call" do
|
||||
it "applies predicate to the value" do
|
||||
expect(operation.(model.new("Jane"))).to be_success
|
||||
expect(operation.(model.new(nil))).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#and' do
|
||||
describe "#and" do
|
||||
let(:other) { Operations::Attr.new(Rule::Predicate.build(min_size?).curry(3), name: :name) }
|
||||
|
||||
it 'returns and where value is passed to the right' do
|
||||
it "returns and where value is passed to the right" do
|
||||
present_and_string = operation.and(other)
|
||||
|
||||
expect(present_and_string.(model.new('Jane'))).to be_success
|
||||
expect(present_and_string.(model.new("Jane"))).to be_success
|
||||
|
||||
expect(present_and_string.(model.new('Ja'))).to be_failure
|
||||
expect(present_and_string.(model.new("Ja"))).to be_failure
|
||||
expect(present_and_string.(model.new(1))).to be_failure
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,32 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Operations::Check do
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
describe '#call' do
|
||||
context 'with 1-level nesting' do
|
||||
describe "#call" do
|
||||
context "with 1-level nesting" do
|
||||
subject(:operation) do
|
||||
Operations::Check.new(Rule::Predicate.build(eql?).curry(1), id: :compare, keys: [:num])
|
||||
end
|
||||
|
||||
it 'applies predicate to args extracted from the input' do
|
||||
it "applies predicate to args extracted from the input" do
|
||||
expect(operation.(num: 1)).to be_success
|
||||
expect(operation.(num: 2)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
context 'with 2-levels nesting' do
|
||||
context "with 2-levels nesting" do
|
||||
subject(:operation) do
|
||||
Operations::Check.new(Rule::Predicate.build(eql?), id: :compare, keys: [[:nums, :left], [:nums, :right]])
|
||||
end
|
||||
|
||||
it 'applies predicate to args extracted from the input' do
|
||||
expect(operation.(nums: { left: 1, right: 1 })).to be_success
|
||||
expect(operation.(nums: { left: 1, right: 2 })).to be_failure
|
||||
it "applies predicate to args extracted from the input" do
|
||||
expect(operation.(nums: {left: 1, right: 1})).to be_success
|
||||
expect(operation.(nums: {left: 1, right: 2})).to be_failure
|
||||
end
|
||||
|
||||
it 'curries args properly' do
|
||||
result = operation.(nums: { left: 1, right: 2 })
|
||||
it "curries args properly" do
|
||||
result = operation.(nums: {left: 1, right: 2})
|
||||
|
||||
expect(result.to_ast).to eql(
|
||||
[:failure, [:compare, [:check, [
|
||||
|
@ -37,12 +37,12 @@ RSpec.describe Operations::Check do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
describe "#to_ast" do
|
||||
subject(:operation) do
|
||||
Operations::Check.new(Rule::Predicate.build(str?), keys: [:email])
|
||||
end
|
||||
|
||||
it 'returns ast' do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:check, [[:email], [:predicate, [:str?, [[:input, Undefined]]]]]]
|
||||
)
|
||||
|
|
|
@ -3,25 +3,25 @@
|
|||
RSpec.describe Operations::Each do
|
||||
subject(:operation) { Operations::Each.new(is_string) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:is_string) { Rule::Predicate.build(str?) }
|
||||
|
||||
describe '#call' do
|
||||
it 'applies its rules to all elements in the input' do
|
||||
expect(operation.(['Address'])).to be_success
|
||||
describe "#call" do
|
||||
it "applies its rules to all elements in the input" do
|
||||
expect(operation.(["Address"])).to be_success
|
||||
|
||||
expect(operation.([nil, 'Address'])).to be_failure
|
||||
expect(operation.([:Address, 'Address'])).to be_failure
|
||||
expect(operation.([nil, "Address"])).to be_failure
|
||||
expect(operation.([:Address, "Address"])).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql([:each, [:predicate, [:str?, [[:input, Undefined]]]]])
|
||||
end
|
||||
|
||||
it 'returns result ast' do
|
||||
it "returns result ast" do
|
||||
expect(operation.([nil, 12, nil]).to_ast).to eql(
|
||||
[:set, [
|
||||
[:key, [0, [:predicate, [:str?, [[:input, nil]]]]]],
|
||||
|
@ -31,8 +31,8 @@ RSpec.describe Operations::Each do
|
|||
)
|
||||
end
|
||||
|
||||
it 'returns failure result ast' do
|
||||
expect(operation.with(id: :tags).([nil, 'red', 12]).to_ast).to eql(
|
||||
it "returns failure result ast" do
|
||||
expect(operation.with(id: :tags).([nil, "red", 12]).to_ast).to eql(
|
||||
[:failure, [:tags, [:set, [
|
||||
[:key, [0, [:predicate, [:str?, [[:input, nil]]]]]],
|
||||
[:key, [2, [:predicate, [:str?, [[:input, 12]]]]]]
|
||||
|
@ -41,9 +41,9 @@ RSpec.describe Operations::Each do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('each(str?)')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("each(str?)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,30 +3,30 @@
|
|||
RSpec.describe Operations::Implication do
|
||||
subject(:operation) { Operations::Implication.new(left, right) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:left) { Rule::Predicate.build(int?) }
|
||||
let(:right) { Rule::Predicate.build(gt?).curry(18) }
|
||||
|
||||
describe '#call' do
|
||||
it 'calls left and right' do
|
||||
expect(operation.('19')).to be_success
|
||||
describe "#call" do
|
||||
it "calls left and right" do
|
||||
expect(operation.("19")).to be_success
|
||||
expect(operation.(19)).to be_success
|
||||
expect(operation.(18)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:implication, [[:predicate, [:int?, [[:input, Undefined]]]], [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('int? THEN gt?(18)')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("int? THEN gt?(18)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,19 +3,19 @@
|
|||
RSpec.describe Operations::Key do
|
||||
subject(:operation) { Operations::Key.new(predicate, name: :user) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:predicate) do
|
||||
Rule::Predicate.build(key?).curry(:age)
|
||||
end
|
||||
|
||||
describe '#call' do
|
||||
context 'with a plain predicate' do
|
||||
it 'returns a success for valid input' do
|
||||
expect(operation.(user: { age: 18 })).to be_success
|
||||
describe "#call" do
|
||||
context "with a plain predicate" do
|
||||
it "returns a success for valid input" do
|
||||
expect(operation.(user: {age: 18})).to be_success
|
||||
end
|
||||
|
||||
it 'returns a failure for invalid input' do
|
||||
it "returns a failure for invalid input" do
|
||||
result = operation.(user: {})
|
||||
|
||||
expect(result).to be_failure
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Operations::Key do
|
|||
end
|
||||
end
|
||||
|
||||
context 'with a set rule as predicate' do
|
||||
context "with a set rule as predicate" do
|
||||
subject(:operation) do
|
||||
Operations::Key.new(predicate, name: :address)
|
||||
end
|
||||
|
@ -36,26 +36,26 @@ RSpec.describe Operations::Key do
|
|||
Operations::Set.new(Rule::Predicate.build(key?).curry(:city), Rule::Predicate.build(key?).curry(:zipcode))
|
||||
end
|
||||
|
||||
it 'applies set rule to the value that passes' do
|
||||
result = operation.(address: { city: 'NYC', zipcode: '123' })
|
||||
it "applies set rule to the value that passes" do
|
||||
result = operation.(address: {city: "NYC", zipcode: "123"})
|
||||
|
||||
expect(result).to be_success
|
||||
end
|
||||
|
||||
it 'applies set rule to the value that fails' do
|
||||
result = operation.(address: { city: 'NYC' })
|
||||
it "applies set rule to the value that fails" do
|
||||
result = operation.(address: {city: "NYC"})
|
||||
|
||||
expect(result).to be_failure
|
||||
|
||||
expect(result.to_ast).to eql(
|
||||
[:failure, [:address, [:key, [:address, [:set, [
|
||||
[:predicate, [:key?, [[:name, :zipcode], [:input, { city: 'NYC' }]]]]
|
||||
[:predicate, [:key?, [[:name, :zipcode], [:input, {city: "NYC"}]]]]
|
||||
]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with an each rule as predicate' do
|
||||
context "with an each rule as predicate" do
|
||||
subject(:operation) do
|
||||
Operations::Key.new(predicate, name: :nums)
|
||||
end
|
||||
|
@ -64,14 +64,14 @@ RSpec.describe Operations::Key do
|
|||
Operations::Each.new(Rule::Predicate.build(str?))
|
||||
end
|
||||
|
||||
it 'applies each rule to the value that passses' do
|
||||
result = operation.(nums: %w(1 2 3))
|
||||
it "applies each rule to the value that passses" do
|
||||
result = operation.(nums: %w[1 2 3])
|
||||
|
||||
expect(result).to be_success
|
||||
end
|
||||
|
||||
it 'applies each rule to the value that fails' do
|
||||
failure = operation.(nums: [1, '3', 3])
|
||||
it "applies each rule to the value that fails" do
|
||||
failure = operation.(nums: [1, "3", 3])
|
||||
|
||||
expect(failure).to be_failure
|
||||
|
||||
|
@ -85,29 +85,29 @@ RSpec.describe Operations::Key do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, Undefined]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#ast' do
|
||||
it 'returns ast without the input' do
|
||||
describe "#ast" do
|
||||
it "returns ast without the input" do
|
||||
expect(operation.ast).to eql(
|
||||
[:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, Undefined]]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns ast with the input' do
|
||||
expect(operation.ast(user: 'jane')).to eql(
|
||||
[:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, 'jane']]]]]]
|
||||
it "returns ast with the input" do
|
||||
expect(operation.ast(user: "jane")).to eql(
|
||||
[:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, "jane"]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#and' do
|
||||
describe "#and" do
|
||||
subject(:operation) do
|
||||
Operations::Key.new(Rule::Predicate.build(str?), name: [:user, :name])
|
||||
end
|
||||
|
@ -116,19 +116,19 @@ RSpec.describe Operations::Key do
|
|||
Operations::Key.new(Rule::Predicate.build(filled?), name: [:user, :name])
|
||||
end
|
||||
|
||||
it 'returns and rule where value is passed to the right' do
|
||||
it "returns and rule where value is passed to the right" do
|
||||
present_and_string = operation.and(other)
|
||||
|
||||
expect(present_and_string.(user: { name: 'Jane' })).to be_success
|
||||
expect(present_and_string.(user: {name: "Jane"})).to be_success
|
||||
|
||||
expect(present_and_string.(user: {})).to be_failure
|
||||
expect(present_and_string.(user: { name: 1 })).to be_failure
|
||||
expect(present_and_string.(user: {name: 1})).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('key[user](key?(:age))')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("key[user](key?(:age))")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,49 +3,49 @@
|
|||
RSpec.describe Operations::Negation do
|
||||
subject(:operation) { Operations::Negation.new(is_int) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:is_int) { Rule::Predicate.build(int?) }
|
||||
|
||||
describe '#call' do
|
||||
it 'negates its rule' do
|
||||
expect(operation.('19')).to be_success
|
||||
describe "#call" do
|
||||
it "negates its rule" do
|
||||
expect(operation.("19")).to be_success
|
||||
expect(operation.(17)).to be_failure
|
||||
end
|
||||
|
||||
context 'double negation' do
|
||||
context "double negation" do
|
||||
subject(:double_negation) { Operations::Negation.new(operation) }
|
||||
|
||||
it 'works as rule' do
|
||||
expect(double_negation.('19')).to be_failure
|
||||
it "works as rule" do
|
||||
expect(double_negation.("19")).to be_failure
|
||||
expect(double_negation.(17)).to be_success
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:not, [:predicate, [:int?, [[:input, Undefined]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns result ast' do
|
||||
it "returns result ast" do
|
||||
expect(operation.(17).to_ast).to eql(
|
||||
[:not, [:predicate, [:int?, [[:input, 17]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns result ast with an :id' do
|
||||
it "returns result ast with an :id" do
|
||||
expect(operation.with(id: :age).(17).to_ast).to eql(
|
||||
[:failure, [:age, [:not, [:predicate, [:int?, [[:input, 17]]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('not(int?)')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("not(int?)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
RSpec.describe Operations::Or do
|
||||
subject(:operation) { Operations::Or.new(left, right) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:left) { Rule::Predicate.build(nil?) }
|
||||
let(:right) { Rule::Predicate.build(gt?).curry(18) }
|
||||
|
@ -12,16 +12,16 @@ RSpec.describe Operations::Or do
|
|||
Rule::Predicate.build(int?) & Rule::Predicate.build(lt?).curry(14)
|
||||
end
|
||||
|
||||
describe '#call' do
|
||||
it 'calls left and right' do
|
||||
describe "#call" do
|
||||
it "calls left and right" do
|
||||
expect(operation.(nil)).to be_success
|
||||
expect(operation.(19)).to be_success
|
||||
expect(operation.(18)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:or, [
|
||||
[:predicate, [:nil?, [[:input, Undefined]]]],
|
||||
|
@ -30,7 +30,7 @@ RSpec.describe Operations::Or do
|
|||
)
|
||||
end
|
||||
|
||||
it 'returns result ast' do
|
||||
it "returns result ast" do
|
||||
expect(operation.(17).to_ast).to eql(
|
||||
[:or, [
|
||||
[:predicate, [:nil?, [[:input, 17]]]],
|
||||
|
@ -39,7 +39,7 @@ RSpec.describe Operations::Or do
|
|||
)
|
||||
end
|
||||
|
||||
it 'returns failure result ast' do
|
||||
it "returns failure result ast" do
|
||||
expect(operation.with(id: :age).(17).to_ast).to eql(
|
||||
[:failure, [:age, [:or, [
|
||||
[:predicate, [:nil?, [[:input, 17]]]],
|
||||
|
@ -49,8 +49,8 @@ RSpec.describe Operations::Or do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#and' do
|
||||
it 'creates and with the other' do
|
||||
describe "#and" do
|
||||
it "creates and with the other" do
|
||||
expect(operation.and(other).(nil)).to be_failure
|
||||
expect(operation.and(other).(19)).to be_failure
|
||||
expect(operation.and(other).(13)).to be_failure
|
||||
|
@ -58,8 +58,8 @@ RSpec.describe Operations::Or do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#or' do
|
||||
it 'creates or with the other' do
|
||||
describe "#or" do
|
||||
it "creates or with the other" do
|
||||
expect(operation.or(other).(nil)).to be_success
|
||||
expect(operation.or(other).(19)).to be_success
|
||||
expect(operation.or(other).(13)).to be_success
|
||||
|
@ -67,9 +67,9 @@ RSpec.describe Operations::Or do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('nil? OR gt?(18)')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("nil? OR gt?(18)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,41 +3,41 @@
|
|||
RSpec.describe Operations::Set do
|
||||
subject(:operation) { Operations::Set.new(is_int, gt_18) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:is_int) { Rule::Predicate.build(int?) }
|
||||
let(:gt_18) { Rule::Predicate.build(gt?, args: [18]) }
|
||||
|
||||
describe '#call' do
|
||||
it 'applies all its rules to the input' do
|
||||
describe "#call" do
|
||||
it "applies all its rules to the input" do
|
||||
expect(operation.(19)).to be_success
|
||||
expect(operation.(17)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:set, [[:predicate, [:int?, [[:input, Undefined]]]], [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns result ast' do
|
||||
it "returns result ast" do
|
||||
expect(operation.(17).to_ast).to eql(
|
||||
[:set, [[:predicate, [:gt?, [[:num, 18], [:input, 17]]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns result ast with an :id' do
|
||||
it "returns result ast with an :id" do
|
||||
expect(operation.with(id: :age).(17).to_ast).to eql(
|
||||
[:failure, [:age, [:set, [[:predicate, [:gt?, [[:num, 18], [:input, 17]]]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('set(int?, gt?(18))')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("set(int?, gt?(18))")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
RSpec.describe Operations::Xor do
|
||||
subject(:operation) { Operations::Xor.new(left, right) }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
let(:left) { Rule::Predicate.build(array?) }
|
||||
let(:right) { Rule::Predicate.build(empty?) }
|
||||
|
@ -12,52 +12,52 @@ RSpec.describe Operations::Xor do
|
|||
Rule::Predicate.build(str?)
|
||||
end
|
||||
|
||||
describe '#call' do
|
||||
it 'calls left and right' do
|
||||
describe "#call" do
|
||||
it "calls left and right" do
|
||||
expect(operation.(nil)).to be_success
|
||||
expect(operation.('')).to be_success
|
||||
expect(operation.("")).to be_success
|
||||
expect(operation.([])).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
it 'returns ast' do
|
||||
describe "#to_ast" do
|
||||
it "returns ast" do
|
||||
expect(operation.to_ast).to eql(
|
||||
[:xor, [[:predicate, [:array?, [[:input, Undefined]]]], [:predicate, [:empty?, [[:input, Undefined]]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns result ast' do
|
||||
it "returns result ast" do
|
||||
expect(operation.([]).to_ast).to eql(
|
||||
[:xor, [[:predicate, [:array?, [[:input, []]]]], [:predicate, [:empty?, [[:input, []]]]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns failure result ast' do
|
||||
it "returns failure result ast" do
|
||||
expect(operation.with(id: :name).([]).to_ast).to eql(
|
||||
[:failure, [:name, [:xor, [[:predicate, [:array?, [[:input, []]]]], [:predicate, [:empty?, [[:input, []]]]]]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#and' do
|
||||
it 'creates conjunction with the other' do
|
||||
describe "#and" do
|
||||
it "creates conjunction with the other" do
|
||||
expect(operation.and(other).(nil)).to be_failure
|
||||
expect(operation.and(other).(19)).to be_failure
|
||||
expect(operation.and(other).('')).to be_success
|
||||
expect(operation.and(other).("")).to be_success
|
||||
end
|
||||
end
|
||||
|
||||
describe '#or' do
|
||||
it 'creates disjunction with the other' do
|
||||
describe "#or" do
|
||||
it "creates disjunction with the other" do
|
||||
expect(operation.or(other).([])).to be_failure
|
||||
expect(operation.or(other).('')).to be_success
|
||||
expect(operation.or(other).("")).to be_success
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(operation.to_s).to eql('array? XOR empty?')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(operation.to_s).to eql("array? XOR empty?")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#array?' do
|
||||
describe "#array?" do
|
||||
let(:predicate_name) { :array? }
|
||||
|
||||
context 'when value is an array' do
|
||||
context "when value is an array" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[[]],
|
||||
[['other', 'array']],
|
||||
[[123, 'really', :blah]],
|
||||
[Array.new],
|
||||
[%w[other array]],
|
||||
[[123, "really", :blah]],
|
||||
[[]],
|
||||
[[nil]],
|
||||
[[false]],
|
||||
[[true]]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is not an array' do
|
||||
context "when value is not an array" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[{}],
|
||||
[nil],
|
||||
[:symbol],
|
||||
|
@ -33,11 +33,11 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[1],
|
||||
[1.0],
|
||||
[true],
|
||||
[Hash.new]
|
||||
[{}]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#attr?' do
|
||||
describe "#attr?" do
|
||||
let(:predicate_name) { :attr? }
|
||||
|
||||
context 'when value responds to the attr name' do
|
||||
context "when value responds to the attr name" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[:name, Struct.new(:name).new('John')],
|
||||
[:name, Struct.new(:name).new("John")],
|
||||
[:age, Struct.new(:age).new(18)]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value does not respond to the attr name' do
|
||||
context "with value does not respond to the attr name" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[:name, Struct.new(:age).new(18)],
|
||||
[:age, Struct.new(:name).new('Jill')]
|
||||
[:age, Struct.new(:name).new("Jill")]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#bool?' do
|
||||
describe "#bool?" do
|
||||
let(:predicate_name) { :bool? }
|
||||
|
||||
context 'when value is a boolean' do
|
||||
context "when value is a boolean" do
|
||||
let(:arguments_list) do
|
||||
[[true], [false]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is not a bool' do
|
||||
context "when value is not a bool" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -25,12 +25,12 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[String],
|
||||
[1],
|
||||
[0],
|
||||
['true'],
|
||||
['false']
|
||||
["true"],
|
||||
["false"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,47 +1,47 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#bytesize?' do
|
||||
describe "#bytesize?" do
|
||||
let(:predicate_name) { :bytesize? }
|
||||
|
||||
context 'when value size is equal to n' do
|
||||
context "when value size is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[4, 'こa'],
|
||||
[1..8, 'こa']
|
||||
[4, "こa"],
|
||||
[1..8, "こa"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value size is greater than n' do
|
||||
context "when value size is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[3, 'こa'],
|
||||
[1..3, 'こa']
|
||||
[3, "こa"],
|
||||
[1..3, "こa"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with value size is less than n' do
|
||||
context "with value size is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[5, 'こa'],
|
||||
[5..10, 'こa']
|
||||
[5, "こa"],
|
||||
[5..10, "こa"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with an unsupported size' do
|
||||
it 'raises an error' do
|
||||
expect { Predicates[:bytesize?].call('oops', 1) }.to raise_error(ArgumentError, /oops/)
|
||||
context "with an unsupported size" do
|
||||
it "raises an error" do
|
||||
expect { Predicates[:bytesize?].call("oops", 1) }.to raise_error(ArgumentError, /oops/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#case?' do
|
||||
describe "#case?" do
|
||||
let(:predicate_name) { :case? }
|
||||
|
||||
context 'when the value matches the pattern' do
|
||||
context "when the value matches the pattern" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[11, 11],
|
||||
[:odd?.to_proc, 11],
|
||||
[/\Af/, 'foo'],
|
||||
[/\Af/, "foo"],
|
||||
[Integer, 11]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context "when the value doesn't match the pattern" do
|
||||
|
@ -24,12 +24,12 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[
|
||||
[13, 14],
|
||||
[:odd?.to_proc, 12],
|
||||
[/\Af/, 'bar'],
|
||||
[/\Af/, "bar"],
|
||||
[String, 11]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#date?' do
|
||||
describe "#date?" do
|
||||
let(:predicate_name) { :date? }
|
||||
|
||||
context 'when value is a date' do
|
||||
context "when value is a date" do
|
||||
let(:arguments_list) do
|
||||
[[Date.today]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not an integer' do
|
||||
context "with value is not an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#date_time?' do
|
||||
describe "#date_time?" do
|
||||
let(:predicate_name) { :date_time? }
|
||||
|
||||
context 'when value is a datetime' do
|
||||
context "when value is a datetime" do
|
||||
let(:arguments_list) do
|
||||
[[DateTime.now]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not an integer' do
|
||||
context "with value is not an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#decimal?' do
|
||||
describe "#decimal?" do
|
||||
let(:predicate_name) { :decimal? }
|
||||
|
||||
context 'when value is a decimal' do
|
||||
context "when value is a decimal" do
|
||||
let(:arguments_list) do
|
||||
[[1.2.to_d]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not an integer' do
|
||||
context "with value is not an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -28,7 +28,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,40 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#empty?' do
|
||||
describe "#empty?" do
|
||||
let(:predicate_name) { :empty? }
|
||||
|
||||
context 'when value is empty' do
|
||||
context "when value is empty" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not empty' do
|
||||
context "with value is not empty" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
['Jill'],
|
||||
["Jill"],
|
||||
[[1, 2, 3]],
|
||||
[{ name: 'John' }],
|
||||
[{name: "John"}],
|
||||
[true],
|
||||
[false],
|
||||
['1'],
|
||||
['0'],
|
||||
["1"],
|
||||
["0"],
|
||||
[:symbol],
|
||||
[String]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates, '#eql?' do
|
||||
RSpec.describe Dry::Logic::Predicates, "#eql?" do
|
||||
let(:predicate_name) { :eql? }
|
||||
|
||||
context 'when value is equal to the arg' do
|
||||
context "when value is equal to the arg" do
|
||||
let(:arguments_list) do
|
||||
[['Foo', 'Foo']]
|
||||
[%w[Foo Foo]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not equal to the arg' do
|
||||
context "with value is not equal to the arg" do
|
||||
let(:arguments_list) do
|
||||
[['Bar', 'Foo']]
|
||||
[%w[Bar Foo]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#even?' do
|
||||
describe "#even?" do
|
||||
let(:predicate_name) { :even? }
|
||||
|
||||
context 'when value is an odd int' do
|
||||
context "when value is an odd int" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13],
|
||||
|
@ -15,10 +15,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with value is an even int' do
|
||||
context "with value is an even int" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[0],
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#excluded_from?' do
|
||||
describe "#excluded_from?" do
|
||||
let(:predicate_name) { :excluded_from? }
|
||||
|
||||
context 'when value is not present in list' do
|
||||
context "when value is not present in list" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[['Jill', 'John'], 'Jack'],
|
||||
[%w[Jill John], "Jack"],
|
||||
[1..2, 0],
|
||||
[1..2, 3],
|
||||
[[nil, false], true]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is present in list' do
|
||||
context "with value is present in list" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[['Jill', 'John'], 'Jill'],
|
||||
[['Jill', 'John'], 'John'],
|
||||
[%w[Jill John], "Jill"],
|
||||
[%w[Jill John], "John"],
|
||||
[1..2, 1],
|
||||
[1..2, 2],
|
||||
[[nil, false], nil],
|
||||
|
@ -31,7 +31,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,58 +1,58 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#excludes?' do
|
||||
describe "#excludes?" do
|
||||
let(:predicate_name) { :excludes? }
|
||||
|
||||
context 'with input excludes value' do
|
||||
context "with input excludes value" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
['Jack', ['Jill', 'John']],
|
||||
["Jack", %w[Jill John]],
|
||||
[0, 1..2],
|
||||
[3, 1..2],
|
||||
['foo', 'Hello World'],
|
||||
[:foo, { bar: 0 }],
|
||||
["foo", "Hello World"],
|
||||
[:foo, {bar: 0}],
|
||||
[true, [nil, false]]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with input of invalid type' do
|
||||
context "with input of invalid type" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[2, 1],
|
||||
[1, nil],
|
||||
['foo', 1],
|
||||
[1, 'foo'],
|
||||
[1..2, 'foo'],
|
||||
['foo', 1..2],
|
||||
[:key, 'foo']
|
||||
["foo", 1],
|
||||
[1, "foo"],
|
||||
[1..2, "foo"],
|
||||
["foo", 1..2],
|
||||
[:key, "foo"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when input includes value' do
|
||||
context "when input includes value" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
['Jill', ['Jill', 'John']],
|
||||
['John', ['Jill', 'John']],
|
||||
["Jill", %w[Jill John]],
|
||||
["John", %w[Jill John]],
|
||||
[1, 1..2],
|
||||
[2, 1..2],
|
||||
['Hello', 'Hello World'],
|
||||
['World', 'Hello World'],
|
||||
[:bar, { bar: 0 }],
|
||||
["Hello", "Hello World"],
|
||||
["World", "Hello World"],
|
||||
[:bar, {bar: 0}],
|
||||
[nil, [nil, false]],
|
||||
[false, [nil, false]]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#false?' do
|
||||
describe "#false?" do
|
||||
let(:predicate_name) { :false? }
|
||||
|
||||
context 'when value is false' do
|
||||
context "when value is false" do
|
||||
let(:arguments_list) do
|
||||
[[false]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is not false' do
|
||||
context "when value is not false" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[true],
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -26,12 +26,12 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[String],
|
||||
[1],
|
||||
[0],
|
||||
['true'],
|
||||
['false']
|
||||
["true"],
|
||||
["false"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,40 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#filled?' do
|
||||
describe "#filled?" do
|
||||
let(:predicate_name) { :filled? }
|
||||
|
||||
context 'when value is filled' do
|
||||
context "when value is filled" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
['Jill'],
|
||||
["Jill"],
|
||||
[[1, 2, 3]],
|
||||
[{ name: 'John' }],
|
||||
[{name: "John"}],
|
||||
[true],
|
||||
[false],
|
||||
['1'],
|
||||
['0'],
|
||||
["1"],
|
||||
["0"],
|
||||
[:symbol],
|
||||
[String]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not filled' do
|
||||
context "with value is not filled" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#float?' do
|
||||
describe "#float?" do
|
||||
let(:predicate_name) { :float? }
|
||||
|
||||
context 'when value is a float' do
|
||||
context "when value is a float" do
|
||||
let(:arguments_list) do
|
||||
[[1.0]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not an integer' do
|
||||
context "with value is not an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates, '#format?' do
|
||||
RSpec.describe Dry::Logic::Predicates, "#format?" do
|
||||
let(:predicate_name) { :format? }
|
||||
|
||||
context 'when value matches provided regexp' do
|
||||
context "when value matches provided regexp" do
|
||||
let(:arguments_list) do
|
||||
[[/^F/, 'Foo']]
|
||||
[[/^F/, "Foo"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value does not match provided regexp' do
|
||||
context "when value does not match provided regexp" do
|
||||
let(:arguments_list) do
|
||||
[[/^F/, 'Bar']]
|
||||
[[/^F/, "Bar"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'when input is nil' do
|
||||
context "when input is nil" do
|
||||
let(:arguments_list) do
|
||||
[[/^F/, nil]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#gt?' do
|
||||
describe "#gt?" do
|
||||
let(:predicate_name) { :gt? }
|
||||
|
||||
context 'when value is greater than n' do
|
||||
context "when value is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 14],
|
||||
|
@ -14,10 +14,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is equal to n' do
|
||||
context "when value is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 13],
|
||||
|
@ -25,10 +25,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with value is less than n' do
|
||||
context "with value is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 12],
|
||||
|
@ -36,7 +36,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#gteq?' do
|
||||
describe "#gteq?" do
|
||||
let(:predicate_name) { :gteq? }
|
||||
|
||||
context 'when value is greater than n' do
|
||||
context "when value is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 14],
|
||||
|
@ -14,10 +14,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is equal to n' do
|
||||
context "when value is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 13],
|
||||
|
@ -25,10 +25,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is less than n' do
|
||||
context "with value is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 12],
|
||||
|
@ -36,7 +36,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,27 +1,27 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#hash?' do
|
||||
describe "#hash?" do
|
||||
let(:predicate_name) { :hash? }
|
||||
|
||||
context 'when value is a hash' do
|
||||
context "when value is a hash" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[{}],
|
||||
[foo: :bar],
|
||||
[Hash.new]
|
||||
[{}]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is not a hash' do
|
||||
context "when value is not a hash" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[nil],
|
||||
[:symbol],
|
||||
|
@ -29,12 +29,12 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[1],
|
||||
[1.0],
|
||||
[true],
|
||||
[Array.new],
|
||||
[[]],
|
||||
[Hash]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#included_in?' do
|
||||
describe "#included_in?" do
|
||||
let(:predicate_name) { :included_in? }
|
||||
|
||||
context 'when value is present in list' do
|
||||
context "when value is present in list" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[['Jill', 'John'], 'Jill'],
|
||||
[['Jill', 'John'], 'John'],
|
||||
[%w[Jill John], "Jill"],
|
||||
[%w[Jill John], "John"],
|
||||
[1..2, 1],
|
||||
[1..2, 2],
|
||||
[[nil, false], nil],
|
||||
|
@ -18,20 +18,20 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not present in list' do
|
||||
context "with value is not present in list" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[['Jill', 'John'], 'Jack'],
|
||||
[%w[Jill John], "Jack"],
|
||||
[1..2, 0],
|
||||
[1..2, 3],
|
||||
[[nil, false], true]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates, '#is?' do
|
||||
RSpec.describe Dry::Logic::Predicates, "#is?" do
|
||||
let(:predicate_name) { :is? }
|
||||
let(:one) { Object.new }
|
||||
let(:two) { Object.new }
|
||||
|
||||
context 'when value is equal to the arg' do
|
||||
context "when value is equal to the arg" do
|
||||
let(:arguments_list) do
|
||||
[[one, one], [:one, :one]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not equal to the arg' do
|
||||
context "with value is not equal to the arg" do
|
||||
let(:arguments_list) do
|
||||
[[one, two], [{}, {}]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#int?' do
|
||||
describe "#int?" do
|
||||
let(:predicate_name) { :int? }
|
||||
|
||||
context 'when value is an integer' do
|
||||
context "when value is an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[1],
|
||||
|
@ -15,13 +15,13 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not an integer' do
|
||||
context "with value is not an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -30,7 +30,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#key?' do
|
||||
describe "#key?" do
|
||||
let(:predicate_name) { :key? }
|
||||
|
||||
context 'when key is present in value' do
|
||||
context "when key is present in value" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[:name, { name: 'John' }],
|
||||
[:age, { age: 18 }]
|
||||
[:name, {name: "John"}],
|
||||
[:age, {age: 18}]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with key is not present in value' do
|
||||
context "with key is not present in value" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[:name, { age: 18 }],
|
||||
[:age, { name: 'Jill' }]
|
||||
[:name, {age: 18}],
|
||||
[:age, {name: "Jill"}]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#lt?' do
|
||||
describe "#lt?" do
|
||||
let(:predicate_name) { :lt? }
|
||||
|
||||
context 'when value is less than n' do
|
||||
context "when value is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 12],
|
||||
[13.37, 13.36],
|
||||
[13.37, 13.36]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is equal to n' do
|
||||
context "when value is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 13],
|
||||
|
@ -25,10 +25,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with value is greater than n' do
|
||||
context "with value is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 14],
|
||||
|
@ -36,7 +36,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#lteq?' do
|
||||
describe "#lteq?" do
|
||||
let(:predicate_name) { :lteq? }
|
||||
|
||||
context 'when value is less than n' do
|
||||
context "when value is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 12],
|
||||
|
@ -14,10 +14,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is equal to n' do
|
||||
context "when value is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 13],
|
||||
|
@ -25,10 +25,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is greater than n' do
|
||||
context "with value is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13, 14],
|
||||
|
@ -36,7 +36,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,39 +1,39 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#max_bytesize?' do
|
||||
describe "#max_bytesize?" do
|
||||
let(:predicate_name) { :max_bytesize? }
|
||||
|
||||
context 'when value size is less than n' do
|
||||
context "when value size is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[5, 'こa']
|
||||
[5, "こa"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value size is equal to n' do
|
||||
context "when value size is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[5, 'こab']
|
||||
[5, "こab"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value size is greater than n' do
|
||||
context "with value size is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[5, 'こabc']
|
||||
[5, "こabc"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,51 +1,51 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#max_size?' do
|
||||
describe "#max_size?" do
|
||||
let(:predicate_name) { :max_size? }
|
||||
|
||||
context 'when value size is less than n' do
|
||||
context "when value size is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[3, [1, 2]],
|
||||
[5, 'Jill'],
|
||||
[3, { 1 => 'st', 2 => 'nd' }],
|
||||
[5, "Jill"],
|
||||
[3, {1 => "st", 2 => "nd"}],
|
||||
[9, 1],
|
||||
[6, 1..5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value size is equal to n' do
|
||||
context "when value size is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[2, [1, 2]],
|
||||
[4, 'Jill'],
|
||||
[2, { 1 => 'st', 2 => 'nd' }],
|
||||
[4, "Jill"],
|
||||
[2, {1 => "st", 2 => "nd"}],
|
||||
[8, 1],
|
||||
[5, 1..5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value size is greater than n' do
|
||||
context "with value size is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[1, [1, 2]],
|
||||
[3, 'Jill'],
|
||||
[1, { 1 => 'st', 2 => 'nd' }],
|
||||
[3, "Jill"],
|
||||
[1, {1 => "st", 2 => "nd"}],
|
||||
[7, 1],
|
||||
[4, 1..5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,39 +1,39 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#min_bytesize?' do
|
||||
describe "#min_bytesize?" do
|
||||
let(:predicate_name) { :min_bytesize? }
|
||||
|
||||
context 'when value size is greater than n' do
|
||||
context "when value size is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[3, 'こa']
|
||||
[3, "こa"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value size is equal to n' do
|
||||
context "when value size is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[5, 'こab']
|
||||
[5, "こab"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value size is less than n' do
|
||||
context "with value size is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[5, 'こ']
|
||||
[5, "こ"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,51 +1,51 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#min_size?' do
|
||||
describe "#min_size?" do
|
||||
let(:predicate_name) { :min_size? }
|
||||
|
||||
context 'when value size is greater than n' do
|
||||
context "when value size is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[1, [1, 2]],
|
||||
[3, 'Jill'],
|
||||
[1, { 1 => 'st', 2 => 'nd' }],
|
||||
[3, "Jill"],
|
||||
[1, {1 => "st", 2 => "nd"}],
|
||||
[7, 1],
|
||||
[4, 1..5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value size is equal to n' do
|
||||
context "when value size is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[2, [1, 2]],
|
||||
[4, 'Jill'],
|
||||
[2, { 1 => 'st', 2 => 'nd' }],
|
||||
[4, "Jill"],
|
||||
[2, {1 => "st", 2 => "nd"}],
|
||||
[8, 1],
|
||||
[5, 1..5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value size is less than n' do
|
||||
context "with value size is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[3, [1, 2]],
|
||||
[5, 'Jill'],
|
||||
[3, { 1 => 'st', 2 => 'nd' }],
|
||||
[5, "Jill"],
|
||||
[3, {1 => "st", 2 => "nd"}],
|
||||
[9, 1],
|
||||
[6, 1..5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#nil?' do
|
||||
describe "#nil?" do
|
||||
let(:predicate_name) { :nil? }
|
||||
|
||||
context 'when value is nil' do
|
||||
context "when value is nil" do
|
||||
let(:arguments_list) { [[nil]] }
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value is not nil' do
|
||||
context "when value is not nil" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[true],
|
||||
[false],
|
||||
[0],
|
||||
|
@ -24,7 +24,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[String]
|
||||
]
|
||||
end
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates, '#not_eql?' do
|
||||
RSpec.describe Dry::Logic::Predicates, "#not_eql?" do
|
||||
let(:predicate_name) { :not_eql? }
|
||||
|
||||
context 'when value is equal to the arg' do
|
||||
context "when value is equal to the arg" do
|
||||
let(:arguments_list) do
|
||||
[['Foo', 'Foo']]
|
||||
[%w[Foo Foo]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not equal to the arg' do
|
||||
context "with value is not equal to the arg" do
|
||||
let(:arguments_list) do
|
||||
[['Bar', 'Foo']]
|
||||
[%w[Bar Foo]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,39 +1,39 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#number?' do
|
||||
describe "#number?" do
|
||||
let(:predicate_name) { :number? }
|
||||
|
||||
context 'when value is numerical' do
|
||||
context "when value is numerical" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
['34'],
|
||||
['1.000004'],
|
||||
['0'],
|
||||
["34"],
|
||||
["1.000004"],
|
||||
["0"],
|
||||
[4],
|
||||
['-15.24'],
|
||||
["-15.24"],
|
||||
[-3.5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not numerical' do
|
||||
context "with value is not numerical" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
['-14px'],
|
||||
['10,150.00'],
|
||||
[""],
|
||||
["-14px"],
|
||||
["10,150.00"],
|
||||
[nil],
|
||||
[:symbol],
|
||||
[String]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#odd?' do
|
||||
describe "#odd?" do
|
||||
let(:predicate_name) { :odd? }
|
||||
|
||||
context 'when value is an odd int' do
|
||||
context "when value is an odd int" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[13],
|
||||
|
@ -15,10 +15,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is an even int' do
|
||||
context "with value is an even int" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[0],
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#respond_to?' do
|
||||
describe "#respond_to?" do
|
||||
let(:predicate_name) { :respond_to? }
|
||||
|
||||
context 'when value responds to method' do
|
||||
context "when value responds to method" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[:method, Object],
|
||||
|
@ -14,10 +14,10 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value does not respond to method' do
|
||||
context "when value does not respond to method" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[:foo, Object],
|
||||
|
@ -25,7 +25,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,56 +1,56 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#size?' do
|
||||
describe "#size?" do
|
||||
let(:predicate_name) { :size? }
|
||||
|
||||
context 'when value size is equal to n' do
|
||||
context "when value size is equal to n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[[8], 2],
|
||||
[4, 'Jill'],
|
||||
[2, { 1 => 'st', 2 => 'nd' }],
|
||||
[4, "Jill"],
|
||||
[2, {1 => "st", 2 => "nd"}],
|
||||
[8, 8],
|
||||
[1..8, 5]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'when value size is greater than n' do
|
||||
context "when value size is greater than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[[1, 2], 3],
|
||||
[5, 'Jill'],
|
||||
[3, { 1 => 'st', 2 => 'nd' }],
|
||||
[5, "Jill"],
|
||||
[3, {1 => "st", 2 => "nd"}],
|
||||
[1, 9],
|
||||
[1..5, 6]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with value size is less than n' do
|
||||
context "with value size is less than n" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[[1, 2], 1],
|
||||
[3, 'Jill'],
|
||||
[1, { 1 => 'st', 2 => 'nd' }],
|
||||
[3, "Jill"],
|
||||
[1, {1 => "st", 2 => "nd"}],
|
||||
[1, 7],
|
||||
[1..5, 4]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
|
||||
context 'with an unsupported size' do
|
||||
it 'raises an error' do
|
||||
expect { Predicates[:size?].call('oops', 1) }.to raise_error(ArgumentError, /oops/)
|
||||
context "with an unsupported size" do
|
||||
it "raises an error" do
|
||||
expect { Predicates[:size?].call("oops", 1) }.to raise_error(ArgumentError, /oops/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#str?' do
|
||||
describe "#str?" do
|
||||
let(:predicate_name) { :str? }
|
||||
|
||||
context 'when value is a string' do
|
||||
context "when value is a string" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
['John']
|
||||
[""],
|
||||
["John"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a string' do
|
||||
context "with value is not a string" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[[]],
|
||||
|
@ -28,7 +28,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#time?' do
|
||||
describe "#time?" do
|
||||
let(:predicate_name) { :time? }
|
||||
|
||||
context 'when value is a time' do
|
||||
context "when value is a time" do
|
||||
let(:arguments_list) do
|
||||
[[Time.now]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not an integer' do
|
||||
context "with value is not an integer" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -27,7 +27,7 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#true?' do
|
||||
describe "#true?" do
|
||||
let(:predicate_name) { :true? }
|
||||
|
||||
context 'when value is true' do
|
||||
context "when value is true" do
|
||||
let(:arguments_list) do
|
||||
[[true]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not true' do
|
||||
context "with value is not true" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[false],
|
||||
[''],
|
||||
[""],
|
||||
[[]],
|
||||
[{}],
|
||||
[nil],
|
||||
|
@ -26,12 +26,12 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[String],
|
||||
[1],
|
||||
[0],
|
||||
['true'],
|
||||
['false']
|
||||
["true"],
|
||||
["false"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#type?' do
|
||||
describe "#type?" do
|
||||
let(:predicate_name) { :type? }
|
||||
|
||||
context 'when value has a correct type' do
|
||||
context "when value has a correct type" do
|
||||
let(:arguments_list) do
|
||||
[[TrueClass, true]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not true' do
|
||||
context "with value is not true" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[TrueClass, false],
|
||||
[TrueClass, ''],
|
||||
[TrueClass, ""],
|
||||
[TrueClass, []],
|
||||
[TrueClass, {}],
|
||||
[TrueClass, nil],
|
||||
|
@ -26,12 +26,12 @@ RSpec.describe Dry::Logic::Predicates do
|
|||
[TrueClass, String],
|
||||
[TrueClass, 1],
|
||||
[TrueClass, 0],
|
||||
[TrueClass, 'true'],
|
||||
[TrueClass, 'false']
|
||||
[TrueClass, "true"],
|
||||
[TrueClass, "false"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#uri?' do
|
||||
describe "#uri?" do
|
||||
let(:predicate_name) { :uri? }
|
||||
|
||||
context 'when value is a valid URI' do
|
||||
context "when value is a valid URI" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
[nil, 'https://github.com/dry-rb/dry-logic'], # without schemes param
|
||||
['https', 'https://github.com/dry-rb/dry-logic'], # with scheme param
|
||||
[['http', 'https'], 'https://github.com/dry-rb/dry-logic'], # with schemes array
|
||||
['mailto', 'mailto:myemail@host.com'], # with mailto format
|
||||
['urn', "urn:isbn:0451450523"] # with URN format
|
||||
[nil, "https://github.com/dry-rb/dry-logic"], # without schemes param
|
||||
["https", "https://github.com/dry-rb/dry-logic"], # with scheme param
|
||||
[%w[http https], "https://github.com/dry-rb/dry-logic"], # with schemes array
|
||||
["mailto", "mailto:myemail@host.com"], # with mailto format
|
||||
["urn", "urn:isbn:0451450523"] # with URN format
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a valid URI' do
|
||||
context "with value is not a valid URI" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
['http', 'mailto:myemail@host.com'], # scheme not allowed
|
||||
[['http', 'https'], 'ftp:://myftp.com'], # scheme not allowed
|
||||
['', 'not-a-uri-at-all']
|
||||
["http", "mailto:myemail@host.com"], # scheme not allowed
|
||||
[%w[http https], "ftp:://myftp.com"], # scheme not allowed
|
||||
["", "not-a-uri-at-all"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#uuid_v1?' do
|
||||
describe "#uuid_v1?" do
|
||||
let(:predicate_name) { :uuid_v1? }
|
||||
|
||||
context 'when value is a valid V1 UUID' do
|
||||
context "when value is a valid V1 UUID" do
|
||||
let(:arguments_list) do
|
||||
[['f2d26c57-e07c-1416-a749-57e937930e04']]
|
||||
[["f2d26c57-e07c-1416-a749-57e937930e04"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a valid V1 UUID' do
|
||||
context "with value is not a valid V1 UUID" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
["not-a-uuid-at-all\nf2d26c57-e07c-1416-a749-57e937930e04"], # V4 with invalid prefix
|
||||
["f2d26c57-e07c-1416-a749-57e937930e04\nnot-a-uuid-at-all"], # V4 with invalid suffix
|
||||
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 1)
|
||||
['20633928-6a07-41e9-a923-1681be663d3e'], # UUID V4
|
||||
['not-a-uuid-at-all']
|
||||
["f2d26c57-e07c-3416-a749-57e937930e04"], # wrong version number (3, not 1)
|
||||
["20633928-6a07-41e9-a923-1681be663d3e"], # UUID V4
|
||||
["not-a-uuid-at-all"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#uuid_v2?' do
|
||||
describe "#uuid_v2?" do
|
||||
let(:predicate_name) { :uuid_v2? }
|
||||
|
||||
context 'when value is a valid V1 UUID' do
|
||||
context "when value is a valid V1 UUID" do
|
||||
let(:arguments_list) do
|
||||
[['f2d26c57-e07c-2416-a749-57e937930e04']]
|
||||
[["f2d26c57-e07c-2416-a749-57e937930e04"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a valid V4 UUID' do
|
||||
context "with value is not a valid V4 UUID" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
["not-a-uuid-at-all\nf2d26c57-e07c-2416-a749-57e937930e04"], # V2 with invalid prefix
|
||||
["f2d26c57-e07c-2416-a749-57e937930e04\nnot-a-uuid-at-all"], # V2 with invalid suffix
|
||||
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 2)
|
||||
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
|
||||
['not-a-uuid-at-all']
|
||||
["f2d26c57-e07c-3416-a749-57e937930e04"], # wrong version number (3, not 2)
|
||||
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
|
||||
["not-a-uuid-at-all"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#uuid_v3?' do
|
||||
describe "#uuid_v3?" do
|
||||
let(:predicate_name) { :uuid_v3? }
|
||||
|
||||
context 'when value is a valid V3 UUID' do
|
||||
context "when value is a valid V3 UUID" do
|
||||
let(:arguments_list) do
|
||||
[['f2d26c57-e07c-3416-a749-57e937930e04']]
|
||||
[["f2d26c57-e07c-3416-a749-57e937930e04"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a valid V4 UUID' do
|
||||
context "with value is not a valid V4 UUID" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
["not-a-uuid-at-all\nf2d26c57-e07c-3416-a749-57e937930e04"], # V3 with invalid prefix
|
||||
["f2d26c57-e07c-3416-a749-57e937930e04\nnot-a-uuid-at-all"], # V3 with invalid suffix
|
||||
['f2d26c57-e07c-4416-a749-57e937930e04'], # wrong version number (4, not 3)
|
||||
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
|
||||
['not-a-uuid-at-all']
|
||||
["f2d26c57-e07c-4416-a749-57e937930e04"], # wrong version number (4, not 3)
|
||||
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
|
||||
["not-a-uuid-at-all"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#uuid_v4?' do
|
||||
describe "#uuid_v4?" do
|
||||
let(:predicate_name) { :uuid_v4? }
|
||||
|
||||
context 'when value is a valid V4 UUID' do
|
||||
context "when value is a valid V4 UUID" do
|
||||
let(:arguments_list) do
|
||||
[['f2d26c57-e07c-4416-a749-57e937930e04']]
|
||||
[["f2d26c57-e07c-4416-a749-57e937930e04"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a valid V4 UUID' do
|
||||
context "with value is not a valid V4 UUID" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
["not-a-uuid-at-all\nf2d26c57-e07c-4416-a749-57e937930e04"], # V4 with invalid prefix
|
||||
["f2d26c57-e07c-4416-a749-57e937930e04\nnot-a-uuid-at-all"], # V4 with invalid suffix
|
||||
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 4)
|
||||
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
|
||||
['not-a-uuid-at-all']
|
||||
["f2d26c57-e07c-3416-a749-57e937930e04"], # wrong version number (3, not 4)
|
||||
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
|
||||
["not-a-uuid-at-all"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Dry::Logic::Predicates do
|
||||
describe '#uuid_v5?' do
|
||||
describe "#uuid_v5?" do
|
||||
let(:predicate_name) { :uuid_v5? }
|
||||
|
||||
context 'when value is a valid V5 UUID' do
|
||||
context "when value is a valid V5 UUID" do
|
||||
let(:arguments_list) do
|
||||
[['f2d26c57-e07c-5416-a749-57e937930e04']]
|
||||
[["f2d26c57-e07c-5416-a749-57e937930e04"]]
|
||||
end
|
||||
|
||||
it_behaves_like 'a passing predicate'
|
||||
it_behaves_like "a passing predicate"
|
||||
end
|
||||
|
||||
context 'with value is not a valid V4 UUID' do
|
||||
context "with value is not a valid V4 UUID" do
|
||||
let(:arguments_list) do
|
||||
[
|
||||
["not-a-uuid-at-all\nf2d26c57-e07c-5416-a749-57e937930e04"], # V4 with invalid prefix
|
||||
["f2d26c57-e07c-5416-a749-57e937930e04\nnot-a-uuid-at-all"], # V4 with invalid suffix
|
||||
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 5)
|
||||
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
|
||||
['not-a-uuid-at-all']
|
||||
["f2d26c57-e07c-3416-a749-57e937930e04"], # wrong version number (3, not 5)
|
||||
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
|
||||
["not-a-uuid-at-all"]
|
||||
]
|
||||
end
|
||||
|
||||
it_behaves_like 'a failing predicate'
|
||||
it_behaves_like "a failing predicate"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/predicates'
|
||||
require "dry/logic/predicates"
|
||||
|
||||
RSpec.describe Predicates do
|
||||
it 'can be included in another module' do
|
||||
it "can be included in another module" do
|
||||
mod = Module.new { include Predicates }
|
||||
|
||||
expect(mod[:key?]).to be_a(Method)
|
||||
end
|
||||
|
||||
describe '.predicate' do
|
||||
it 'defines a predicate method' do
|
||||
describe ".predicate" do
|
||||
it "defines a predicate method" do
|
||||
mod = Module.new {
|
||||
include Predicates
|
||||
|
||||
|
@ -19,7 +19,7 @@ RSpec.describe Predicates do
|
|||
end
|
||||
}
|
||||
|
||||
expect(mod.test?('arg')).to be(true)
|
||||
expect(mod.test?("arg")).to be(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,51 +5,57 @@ RSpec.describe Rule::Predicate do
|
|||
|
||||
let(:predicate) { str? }
|
||||
|
||||
include_context 'predicates'
|
||||
include_context "predicates"
|
||||
|
||||
it_behaves_like Rule
|
||||
|
||||
describe '#name' do
|
||||
it 'returns predicate identifier' do
|
||||
describe "#name" do
|
||||
it "returns predicate identifier" do
|
||||
expect(rule.name).to be(:str?)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_ast' do
|
||||
context 'without a result' do
|
||||
it 'returns rule ast' do
|
||||
describe "#to_ast" do
|
||||
context "without a result" do
|
||||
it "returns rule ast" do
|
||||
expect(rule.to_ast).to eql([:predicate, [:str?, [[:input, Undefined]]]])
|
||||
end
|
||||
|
||||
it 'returns :failure with an id' do
|
||||
it "returns :failure with an id" do
|
||||
email = rule.with(id: :email)
|
||||
|
||||
expect(email.(11).to_ast).to eql([:failure, [:email, [:predicate, [:str?, [[:input, 11]]]]]])
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a result' do
|
||||
it 'returns success' do
|
||||
expect(rule.('foo')).to be_success
|
||||
context "with a result" do
|
||||
it "returns success" do
|
||||
expect(rule.("foo")).to be_success
|
||||
end
|
||||
|
||||
it 'returns failure ast' do
|
||||
it "returns failure ast" do
|
||||
expect(rule.(5).to_ast).to eql([:predicate, [:str?, [[:input, 5]]]])
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a zero-arity predicate' do
|
||||
let(:predicate) { Module.new { def self.test?; true; end }.method(:test?) }
|
||||
context "with a zero-arity predicate" do
|
||||
let(:predicate) {
|
||||
Module.new {
|
||||
def self.test?
|
||||
true
|
||||
end
|
||||
} .method(:test?)
|
||||
}
|
||||
|
||||
it 'returns ast' do
|
||||
it "returns ast" do
|
||||
expect(rule.to_ast).to eql([:predicate, [:test?, []]])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'returns string representation' do
|
||||
expect(rule.curry('foo').to_s).to eql('str?("foo")')
|
||||
describe "#to_s" do
|
||||
it "returns string representation" do
|
||||
expect(rule.curry("foo").to_s).to eql('str?("foo")')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'dry/logic/rule_compiler'
|
||||
require "dry/logic/rule_compiler"
|
||||
|
||||
RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
||||
RSpec.describe Dry::Logic::RuleCompiler, "#call" do
|
||||
subject(:compiler) { RuleCompiler.new(predicates) }
|
||||
|
||||
let(:predicates) {
|
||||
{ key?: predicate,
|
||||
attr?: predicate,
|
||||
filled?: predicate,
|
||||
gt?: predicate,
|
||||
one: predicate }
|
||||
{key?: predicate,
|
||||
attr?: predicate,
|
||||
filled?: predicate,
|
||||
gt?: predicate,
|
||||
one: predicate}
|
||||
}
|
||||
|
||||
let(:predicate) { double(:predicate, name: :test?, arity: 2).as_null_object }
|
||||
|
@ -26,7 +26,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
let(:set_op) { Operations::Set.new(rule) }
|
||||
let(:each_op) { Operations::Each.new(rule) }
|
||||
|
||||
it 'compiles key rules' do
|
||||
it "compiles key rules" do
|
||||
ast = [[:key, [:email, [:predicate, [:filled?, [[:input, Undefined]]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
@ -34,7 +34,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([key_op])
|
||||
end
|
||||
|
||||
it 'compiles attr rules' do
|
||||
it "compiles attr rules" do
|
||||
ast = [[:attr, [:email, [:predicate, [:filled?, [[:input, Undefined]]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
@ -42,7 +42,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([attr_op])
|
||||
end
|
||||
|
||||
it 'compiles check rules' do
|
||||
it "compiles check rules" do
|
||||
ast = [[:check, [[:email], [:predicate, [:filled?, [[:input, Undefined]]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
@ -50,7 +50,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([check_op])
|
||||
end
|
||||
|
||||
it 'compiles attr rules' do
|
||||
it "compiles attr rules" do
|
||||
ast = [[:attr, [:email, [:predicate, [:filled?, [[:input, Undefined]]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
@ -58,7 +58,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([attr_op])
|
||||
end
|
||||
|
||||
it 'compiles negated rules' do
|
||||
it "compiles negated rules" do
|
||||
ast = [[:not, [:key, [:email, [:predicate, [:filled?, [[:input, Undefined]]]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
@ -66,7 +66,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([not_key_op])
|
||||
end
|
||||
|
||||
it 'compiles and rules' do
|
||||
it "compiles and rules" do
|
||||
ast = [
|
||||
[
|
||||
:and, [
|
||||
|
@ -81,7 +81,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([and_op])
|
||||
end
|
||||
|
||||
it 'compiles or rules' do
|
||||
it "compiles or rules" do
|
||||
ast = [
|
||||
[
|
||||
:or, [
|
||||
|
@ -96,7 +96,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([or_op])
|
||||
end
|
||||
|
||||
it 'compiles exclusive or rules' do
|
||||
it "compiles exclusive or rules" do
|
||||
ast = [
|
||||
[
|
||||
:xor, [
|
||||
|
@ -111,7 +111,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([xor_op])
|
||||
end
|
||||
|
||||
it 'compiles set rules' do
|
||||
it "compiles set rules" do
|
||||
ast = [[:set, [[:predicate, [:filled?, [[:input, nil]]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
@ -119,7 +119,7 @@ RSpec.describe Dry::Logic::RuleCompiler, '#call' do
|
|||
expect(rules).to eql([set_op])
|
||||
end
|
||||
|
||||
it 'compiles each rules' do
|
||||
it "compiles each rules" do
|
||||
ast = [[:each, [:predicate, [:filled?, [[:input, nil]]]]]]
|
||||
|
||||
rules = compiler.(ast)
|
||||
|
|
|
@ -11,7 +11,7 @@ RSpec.describe Dry::Logic::Rule do
|
|||
define_method(:class, Kernel.instance_method(:class))
|
||||
|
||||
def method_missing(m, *)
|
||||
if m.to_s.end_with?('?')
|
||||
if m.to_s.end_with?("?")
|
||||
self.class.new
|
||||
else
|
||||
super
|
||||
|
@ -34,119 +34,137 @@ RSpec.describe Dry::Logic::Rule do
|
|||
|
||||
it_behaves_like Dry::Logic::Rule
|
||||
|
||||
describe '.new' do
|
||||
it 'accepts an :id' do
|
||||
describe ".new" do
|
||||
it "accepts an :id" do
|
||||
expect(Rule.build(predicate, id: :check_num).id).to be(:check_num)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with a function returning truthy value' do
|
||||
it 'is successful for valid input' do
|
||||
expect(Rule.build(-> val { val }).('true')).to be_success
|
||||
describe "with a function returning truthy value" do
|
||||
it "is successful for valid input" do
|
||||
expect(Rule.build(-> val { val }).("true")).to be_success
|
||||
end
|
||||
|
||||
it 'is not successful for invalid input' do
|
||||
it "is not successful for invalid input" do
|
||||
expect(Rule.build(-> val { val }).(nil)).to be_failure
|
||||
end
|
||||
end
|
||||
|
||||
describe '#ast' do
|
||||
it 'returns predicate node with :id' do
|
||||
expect(Rule.build(-> value { true }).with(id: :email?).ast('oops')).to eql(
|
||||
[:predicate, [:email?, [[:value, 'oops']]]]
|
||||
describe "#ast" do
|
||||
it "returns predicate node with :id" do
|
||||
expect(Rule.build(-> value { true }).with(id: :email?).ast("oops")).to eql(
|
||||
[:predicate, [:email?, [[:value, "oops"]]]]
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns predicate node with undefined args' do
|
||||
it "returns predicate node with undefined args" do
|
||||
expect(Rule.build(-> value { true }).with(id: :email?).ast).to eql(
|
||||
[:predicate, [:email?, [[:value, Undefined]]]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#type' do
|
||||
it 'returns rule type' do
|
||||
describe "#type" do
|
||||
it "returns rule type" do
|
||||
expect(rule.type).to be(:rule)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#bind' do
|
||||
describe "#bind" do
|
||||
let(:bound) { rule.with(id: :bound).bind(object) }
|
||||
|
||||
context 'with an unbound method' do
|
||||
context "with an unbound method" do
|
||||
let(:predicate) { klass.instance_method(:test?) }
|
||||
let(:klass) { Class.new { def test?; true; end } }
|
||||
let(:klass) {
|
||||
Class.new {
|
||||
def test?
|
||||
true
|
||||
end
|
||||
}
|
||||
}
|
||||
let(:object) { klass.new }
|
||||
|
||||
it 'returns a new rule with its predicate bound to a specific object' do
|
||||
it "returns a new rule with its predicate bound to a specific object" do
|
||||
expect(bound.()).to be_success
|
||||
end
|
||||
|
||||
it 'carries id' do
|
||||
it "carries id" do
|
||||
expect(bound.id).to be(:bound)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with an arbitrary block' do
|
||||
context "with an arbitrary block" do
|
||||
let(:predicate) { -> value { value == expected } }
|
||||
let(:object) { Class.new { def expected; 'test'; end }.new }
|
||||
let(:object) {
|
||||
Class.new {
|
||||
def expected
|
||||
"test"
|
||||
end
|
||||
} .new
|
||||
}
|
||||
|
||||
it 'returns a new with its predicate executed in the context of the provided object' do
|
||||
expect(bound.('test')).to be_success
|
||||
expect(bound.('oops')).to be_failure
|
||||
it "returns a new with its predicate executed in the context of the provided object" do
|
||||
expect(bound.("test")).to be_success
|
||||
expect(bound.("oops")).to be_failure
|
||||
end
|
||||
|
||||
it 'carries id' do
|
||||
it "carries id" do
|
||||
expect(bound.id).to be(:bound)
|
||||
end
|
||||
|
||||
it 'stores arity' do
|
||||
it "stores arity" do
|
||||
expect(bound.options[:arity]).to be(rule.arity)
|
||||
end
|
||||
|
||||
it 'stores parameters' do
|
||||
it "stores parameters" do
|
||||
expect(bound.options[:parameters]).to eql(rule.parameters)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a schema instance' do
|
||||
context "with a schema instance" do
|
||||
let(:object) { schema }
|
||||
let(:predicate) { schema }
|
||||
|
||||
it 'returns a new with its predicate executed in the context of the provided object' do
|
||||
it "returns a new with its predicate executed in the context of the provided object" do
|
||||
expect(bound.(true)).to be_success
|
||||
expect(bound.(false)).to be_failure
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#eval_args' do
|
||||
context 'with an unbound method' do
|
||||
let(:options) { { args: [1, klass.instance_method(:num), :foo], arity: 3 } }
|
||||
let(:klass) { Class.new { def num; 7; end } }
|
||||
describe "#eval_args" do
|
||||
context "with an unbound method" do
|
||||
let(:options) { {args: [1, klass.instance_method(:num), :foo], arity: 3} }
|
||||
let(:klass) {
|
||||
Class.new {
|
||||
def num
|
||||
7
|
||||
end
|
||||
}
|
||||
}
|
||||
let(:object) { klass.new }
|
||||
|
||||
it 'evaluates args in the context of the provided object' do
|
||||
it "evaluates args in the context of the provided object" do
|
||||
expect(rule.eval_args(object).args).to eql([1, 7, :foo])
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a schema instance' do
|
||||
let(:options) { { args: [1, schema, :foo], arity: 3 } }
|
||||
context "with a schema instance" do
|
||||
let(:options) { {args: [1, schema, :foo], arity: 3} }
|
||||
let(:object) { Object.new }
|
||||
|
||||
it 'returns a new with its predicate executed in the context of the provided object' do
|
||||
it "returns a new with its predicate executed in the context of the provided object" do
|
||||
expect(rule.eval_args(object).args).to eql([1, schema, :foo])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'arity specialization' do
|
||||
describe '0-arity rule' do
|
||||
let(:options) { { args: [1], arity: 1 } }
|
||||
describe "arity specialization" do
|
||||
describe "0-arity rule" do
|
||||
let(:options) { {args: [1], arity: 1} }
|
||||
let(:predicate) { :odd?.to_proc }
|
||||
|
||||
it 'generates interface with the right arity' do
|
||||
it "generates interface with the right arity" do
|
||||
expect(rule.method(:call).arity).to be_zero
|
||||
expect(rule.method(:[]).arity).to be_zero
|
||||
expect(rule[]).to be(true)
|
||||
|
@ -154,11 +172,11 @@ RSpec.describe Dry::Logic::Rule do
|
|||
end
|
||||
end
|
||||
|
||||
describe '1-arity rule' do
|
||||
let(:options) { { args: [1], arity: 2 } }
|
||||
describe "1-arity rule" do
|
||||
let(:options) { {args: [1], arity: 2} }
|
||||
let(:predicate) { -> a, b { a + b } }
|
||||
|
||||
it 'generates interface with the right arity' do
|
||||
it "generates interface with the right arity" do
|
||||
expect(rule.method(:call).arity).to be(1)
|
||||
expect(rule.method(:[]).arity).to be(1)
|
||||
expect(rule[10]).to be(11)
|
||||
|
@ -166,12 +184,12 @@ RSpec.describe Dry::Logic::Rule do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'currying' do
|
||||
let(:options) { { args: [], arity: 2 } }
|
||||
describe "currying" do
|
||||
let(:options) { {args: [], arity: 2} }
|
||||
let(:predicate) { -> a, b { a + b } }
|
||||
let(:rule) { super().curry(1) }
|
||||
|
||||
it 'generates correct arity on currying' do
|
||||
it "generates correct arity on currying" do
|
||||
expect(rule.method(:call).arity).to be(1)
|
||||
expect(rule.method(:[]).arity).to be(1)
|
||||
expect(rule[10]).to be(11)
|
||||
|
@ -179,32 +197,32 @@ RSpec.describe Dry::Logic::Rule do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'arbitrary arity' do
|
||||
describe "arbitrary arity" do
|
||||
let(:arity) { rand(1..20) }
|
||||
let(:curried) { rand(arity) }
|
||||
|
||||
let(:options) { { args: [1] * curried, arity: arity } }
|
||||
let(:options) { {args: [1] * curried, arity: arity} }
|
||||
let(:predicate) { double(:predicate) }
|
||||
|
||||
it 'generates correct arity' do
|
||||
it "generates correct arity" do
|
||||
expect(rule.method(:call).arity).to be(arity - curried)
|
||||
expect(rule.method(:[]).arity).to be(arity - curried)
|
||||
end
|
||||
end
|
||||
|
||||
describe '-1 arity' do
|
||||
let(:options) { { args: [], arity: -1 } }
|
||||
describe "-1 arity" do
|
||||
let(:options) { {args: [], arity: -1} }
|
||||
|
||||
it 'accepts variable number of arguments' do
|
||||
it "accepts variable number of arguments" do
|
||||
expect(rule.method(:call).arity).to be(-1)
|
||||
expect(rule.method(:[]).arity).to be(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'constants' do
|
||||
let(:options) { { args: [], arity: 0 } }
|
||||
describe "constants" do
|
||||
let(:options) { {args: [], arity: 0} }
|
||||
|
||||
it 'accepts variable number of arguments' do
|
||||
it "accepts variable number of arguments" do
|
||||
expect(rule.method(:call).arity).to be(-1)
|
||||
expect(rule.method(:[]).arity).to be(-1)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue