Sha256: b822f7124917e2fd99b1dc106fb35b1a37c517c7d87c4bbdde427d9897dc657e

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require "spec_helper"

RSpec.describe SubclassMustImplement do

  class BaseFoo
    include SubclassMustImplement

    subclass_must_implement :foo, :bar, :baz
  end

  class Foo < BaseFoo
    def foo
      :foo
    end
  end

  class BaseBar
    extend SubclassMustImplement

    subclass_must_implement :version, :sub_version, err_message: "Version expected!!!"
  end

  class Bar < BaseBar
    def version
      SubclassMustImplement.version
    end
  end

  let(:foo) { Foo.new }
  let(:bar) { Bar.new }

  context "module included" do
    it "will raise a NotImplementedError if any required method is not implemented in the subclass" do
      expect { foo.bar }.to raise_error(NotImplementedError, "`bar` must be implemented in a subclass.")
      expect { foo.baz }.to raise_error(NotImplementedError, "`baz` must be implemented in a subclass.")
    end

    it "will not raise an error if a requred method is implemented by the subclass" do
      expect(foo.foo).to eq(:foo)
    end
  end

  context "module extended" do
    it "will raise a NotImplementedError if any required method is not implemented in the subclass" do
      expect { bar.sub_version }.to raise_error(NotImplementedError, "Version expected!!!")
    end

    it "will not raise an error if a requred method is implemented by the subclass" do
      expect(bar.version).to eq(described_class.version)
    end
  end

  context "matcher" do
    it { expect(BaseFoo    ).to require_subclass_to_implement(:foo) }
    it { expect(BaseFoo.new).to require_subclass_to_implement(:bar) }
    it { expect(BaseFoo).not_to require_subclass_to_implement(:qux) }

    it do
      expect(BaseBar).to require_subclass_to_implement(:version).with_error_message("Version expected!!!")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
subclass_must_implement-0.0.2 spec/subclass_must_implement_spec.rb