dry-schema/spec/integration/schema/predicates/respond_to_spec.rb

378 lines
9.0 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "Predicates: Respond To" do
context "with required" do
subject(:schema) do
Dry::Schema.define { required(:foo) { respond_to?(:bar) } }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is not successful" do
expect(result).to be_failing ["is missing", "must respond to bar"]
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
context "with optional" do
subject(:schema) do
Dry::Schema.define { optional(:foo) { respond_to?(:bar) } }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is successful" do
expect(result).to be_successful
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
context "as macro" do
context "with required" do
context "with value" do
subject(:schema) do
Dry::Schema.define { required(:foo).value(interface?: :bar) }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is not successful" do
expect(result).to be_failing ["is missing", "must respond to bar"]
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
context "with filled" do
subject(:schema) do
Dry::Schema.define { required(:foo).filled(interface?: :bar) }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is not successful" do
expect(result).to be_failing ["is missing", "must respond to bar"]
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is not successful" do
expect(result).to be_failing ["must be filled", "must respond to bar"]
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must be filled", "must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
context "with maybe" do
subject(:schema) do
Dry::Schema.define { required(:foo).maybe(interface?: :bar) }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is not successful" do
expect(result).to be_failing ["is missing", "must respond to bar"]
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is successful" do
expect(result).to be_successful
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
end
context "with optional" do
context "with value" do
subject(:schema) do
Dry::Schema.define { optional(:foo).value(interface?: :bar) }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is successful" do
expect(result).to be_successful
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
context "with filled" do
subject(:schema) do
Dry::Schema.define { optional(:foo).filled(interface?: :bar) }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is successful" do
expect(result).to be_successful
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is not successful" do
expect(result).to be_failing ["must be filled", "must respond to bar"]
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must be filled", "must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
context "with maybe" do
subject(:schema) do
Dry::Schema.define { optional(:foo).maybe(interface?: :bar) }
end
context "with valid input" do
let(:input) { {foo: double(bar: 23)} }
it "is successful" do
expect(result).to be_successful
end
end
context "with missing input" do
let(:input) { {} }
it "is successful" do
expect(result).to be_successful
end
end
context "with nil input" do
let(:input) { {foo: nil} }
it "is successful" do
expect(result).to be_successful
end
end
context "with blank input" do
let(:input) { {foo: ""} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
context "with invalid input" do
let(:input) { {foo: double(baz: 23)} }
it "is not successful" do
expect(result).to be_failing ["must respond to bar"]
end
end
end
end
end
end