Sha256: 6d16b15b255d4e4685b337aa6d6cc4d933aed5afa53da8be7373c5b51684139b

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require "dry-types"

describe "type constraint" do
  context "by dry-type" do
    before do
      module Test::Types
        include Dry::Types.module
      end

      class Test::Foo
        extend Dry::Initializer::Mixin
        param :foo, type: Test::Types::Strict::String, optional: true
      end
    end

    context "in case of mismatch" do
      subject { Test::Foo.new 1 }

      it "raises TypeError" do
        expect { subject }.to raise_error TypeError, /1/
      end
    end

    context "in case of match" do
      subject { Test::Foo.new "foo" }

      it "completes the initialization" do
        expect { subject }.not_to raise_error
      end
    end

    context "if optional value not set" do
      subject { Test::Foo.new }

      it "completes the initialization" do
        expect { subject }.not_to raise_error
      end
    end
  end

  context "by invalid constraint" do
    it "raises TypeError" do
      expect {
        class Test::Foo
          extend Dry::Initializer::Mixin
          param :foo, type: String
        end
      }.to raise_error(TypeError)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-0.8.0 spec/type_constraint_spec.rb
dry-initializer-0.7.0 spec/dry/type_constraint_spec.rb