spec/unit/assertion/base_spec.rb in assertion-0.2.0 vs spec/unit/assertion/base_spec.rb in assertion-0.2.1

- old
+ new

@@ -3,10 +3,22 @@ describe Assertion::Base do let(:klass) { Class.new(described_class) } before { allow(klass).to receive(:name) { "Test" } } + it "implements DSL::Caller" do + expect(klass).to be_kind_of Assertion::DSL::Caller + end + + it "implements DSL::Attributes" do + expect(klass).to be_kind_of Assertion::DSL::Attributes + end + + it "implements DSL::Inversion" do + expect(klass).to be_kind_of Assertion::DSL::Inversion + end + describe ".new" do let(:klass) { Class.new(described_class) { attribute :foo, :bar } } context "with attributes" do @@ -35,161 +47,42 @@ end # context end # describe .new - describe ".attributes" do + describe "#message" do - subject { klass.attributes } - it { is_expected.to eql [] } + let(:klass) { Class.new(described_class) { attribute :foo } } + let(:assertion) { klass.new(foo: :FOO) } - end # describe .attributes + shared_examples "translating" do |as: nil| - describe ".attribute" do + let(:translator) { Assertion::Translator.new(klass) } + let(:attributes) { assertion.attributes } - shared_examples "defining attributes" do - - it "registers attributes" do - expect { subject }.to change { klass.attributes }.to [:foo, :bar] - end - - it "declares attributes" do + it "uses attributes in a translation" do + expect(I18n).to receive :translate do |_, args| + expect(args.merge(attributes)).to eql args + end subject - assertion = klass.new(foo: :FOO, bar: :BAR, baz: :BAZ) - expect(assertion.attributes).to eql(foo: :FOO, bar: :BAR) - expect(assertion.foo).to eql :FOO - expect(assertion.bar).to eql :BAR end - end # shared examples - - shared_examples "raising NameError" do |with: nil| - - it "fails" do - expect { subject }.to raise_error do |exception| - expect(exception).to be_kind_of NameError - expect(exception.message).to eql "#{klass}##{with} is already defined" - end + it "returns a translation" do + expect(subject).to eql translator.call(as, attributes) end end # shared examples - context "with a single name" do - - subject do - klass.attribute :foo - klass.attribute "bar" - end - it_behaves_like "defining attributes" - - end # context - - context "with a list of names" do - - subject { klass.attribute :foo, :bar } - it_behaves_like "defining attributes" - - end # context - - context "with an array of names" do - - subject { klass.attribute %w(foo bar) } - it_behaves_like "defining attributes" - - end # context - - context ":check" do - - subject { klass.attribute :check } - it_behaves_like "raising NameError", with: :check - - end # context - - context ":call" do - - subject { klass.attribute :call } - it_behaves_like "raising NameError", with: :call - - end # context - - end # describe .attribute - - describe ".not" do - - subject { klass.not } - - it "creates the iverter for the current class" do - expect(subject).to be_kind_of Assertion::Inverter - expect(subject.source).to eql klass + it_behaves_like "translating", as: true do + subject { assertion.message(true) } end - end # describe .not - - describe ".[]" do - - let(:params) { { foo: :FOO } } - let(:state) { double } - let(:assertion) { double call: state } - - context "with params" do - - subject { klass[params] } - - it "checks the assertion for given attributes" do - allow(klass).to receive(:new).with(params) { assertion } - expect(subject).to eql state - end - - end # context - - context "without params" do - - subject { klass[] } - - it "checks the assertion" do - allow(klass).to receive(:new) { assertion } - expect(subject).to eql state - end - - end # context - - end # describe .[] - - describe ".translator" do - - subject { klass.translator } - - it { is_expected.to be_kind_of Assertion::Translator } - - it "refers to the current class" do - expect(subject.assertion).to eql klass + it_behaves_like "translating", as: false do + subject { assertion.message(false) } end - end # describe .translator - - describe "#attributes" do - - let(:attrs) { { foo: :FOO, bar: :BAR } } - let(:klass) { Class.new(described_class) { attribute :foo, :bar } } - let(:assertion) { klass.new attrs } - - subject { assertion.attributes } - it { is_expected.to eql attrs } - - end # describe #attributes - - describe "#message" do - - let(:state) { double } - let(:attrs) { { foo: :FOO, bar: :BAR } } - let(:klass) { Class.new(described_class) { attribute :foo, :bar } } - let(:assertion) { klass.new attrs } - let(:translator) { double call: nil } - - it "calls a translator with state and attributes" do - allow(klass).to receive(:translator) { translator } - expect(translator).to receive(:call).with(state, attrs) - assertion.message(state) + it_behaves_like "translating", as: false do + subject { assertion.message } end end # describe #message describe "#call" do