1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
# frozen_string_literal: true
RSpec.describe Dry::Logic::Rule do
subject(:rule) { described_class.build(predicate, **options) }
let(:predicate) { -> { true } }
let(:options) { {} }
let(:schema) do
Class.new do
define_method(:class, Kernel.instance_method(:class))
def method_missing(m, *)
if m.to_s.end_with?("?")
self.class.new
else
super
end
end
def to_proc
-> value { value }
end
def arity
1
end
def parameters
[[:req, :value]]
end
end.new
end
it_behaves_like described_class
describe ".new" do
it "accepts an :id" do
expect(described_class.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(described_class.build(-> val { val }).("true")).to be_success
end
it "is not successful for invalid input" do
expect(described_class.build(-> val { val }).(nil)).to be_failure
end
end
describe "#ast" do
it "returns predicate node with :id" do
expect(described_class.build(-> value { true }).with(id: :email?).ast("oops")).to eql(
[:predicate, [:email?, [[:value, "oops"]]]]
)
end
it "returns predicate node with undefined args" do
expect(described_class.build(-> value { true }).with(id: :email?).ast).to eql(
[:predicate, [:email?, [[:value, undefined]]]]
)
end
end
describe "#type" do
it "returns rule type" do
expect(rule.type).to be(:rule)
end
end
describe "#bind" do
let(:bound) { rule.with(id: :bound).bind(object) }
context "with an unbound method" do
let(:predicate) { klass.instance_method(:test?) }
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
expect(bound.()).to be_success
end
it "carries id" do
expect(bound.id).to be(:bound)
end
end
context "with an arbitrary block" do
let(:predicate) { -> value { value == expected } }
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
end
it "carries id" do
expect(bound.id).to be(:bound)
end
it "stores arity" do
expect(bound.options[:arity]).to be(rule.arity)
end
it "stores parameters" do
expect(bound.options[:parameters]).to eql(rule.parameters)
end
end
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
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
}
}
let(:object) { klass.new }
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} }
let(:object) { Object.new }
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} }
let(:predicate) { :odd?.to_proc }
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)
expect(rule.()).to be_success
end
end
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
expect(rule.method(:call).arity).to be(1)
expect(rule.method(:[]).arity).to be(1)
expect(rule[10]).to be(11)
expect(rule.(1)).to be_success
end
end
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
expect(rule.method(:call).arity).to be(1)
expect(rule.method(:[]).arity).to be(1)
expect(rule[10]).to be(11)
expect(rule.(1)).to be_success
end
end
describe "arbitrary arity" do
let(:arity) { rand(1..20) }
let(:curried) { rand(arity) }
let(:options) { {args: [1] * curried, arity: arity} }
let(:predicate) { double(:predicate) }
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} }
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 "-2 arity" do
let(:options) { {args: [], arity: -2} }
it "accepts variable number of arguments" do
expect(rule.method(:call).arity).to be(-2)
expect(rule.method(:[]).arity).to be(-2)
end
context "curried 1" do
let(:options) { {args: [1], arity: -2} }
it "doesn't have required arguments" do
expect(rule.method(:call).arity).to be(-1)
expect(rule.method(:[]).arity).to be(-1)
end
end
context "curried 2" do
let(:options) { {args: [1, 2], arity: -2} }
it "doesn't have required arguments" do
expect(rule.method(:call).arity).to be(-1)
expect(rule.method(:[]).arity).to be(-1)
end
end
end
describe "constants" do
let(:options) { {args: [], arity: 0} }
it "accepts variable number of arguments" do
expect(rule.method(:call).arity).to be(-1)
expect(rule.method(:[]).arity).to be(-1)
end
end
end
end
|