Sha256: 197d0efcbf54d9a419b0ca0d1dbe65a22e9f177c69e8c2937e9a67045a9acd9a

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

class GenericExample
  Contract C::Args[String], C::KeywordArgs[ repeat: C::Maybe[C::Num] ] => C::ArrayOf[String]
  def splat_then_optional_named(*vals, repeat: 2)
    vals.map { |v| v * repeat }
  end

  Contract C::KeywordArgs[ foo: C::Nat ] => nil
  def nat_test_with_kwarg(foo: 10)
  end

  Contract C::KeywordArgs[name: C::Optional[String]], C::Func[String => String] => String
  def keyword_args_hello(name: "Adit", &block)
    "Hey, #{yield name}!"
  end
end

RSpec.describe "Contracts:" do
  before :all do
    @o = GenericExample.new
  end

  describe "Optional named arguments" do
    it "should work with optional named argument unfilled after splat" do
      expect { @o.splat_then_optional_named("hello", "world") }.to_not raise_error
    end

    it "should work with optional named argument filled after splat" do
      expect { @o.splat_then_optional_named("hello", "world", repeat: 3) }.to_not raise_error
    end
  end

  describe "Nat:" do
    it "should pass for keyword args with correct arg given" do
      expect { @o.nat_test_with_kwarg(foo: 10) }.to_not raise_error
    end

    it "should fail with a ContractError for wrong keyword args input" do
      expect { @o.nat_test_with_kwarg(foo: -10) }.to raise_error(ContractError)
    end

    it "should fail with a ContractError for no input" do
      expect { @o.nat_test_with_kwarg }.to raise_error(ContractError)
    end
  end

  describe "keyword args with defaults, with a block" do
    it "should work when both keyword args and a block is given" do
      expect(@o.keyword_args_hello(name: "maggie", &:upcase)).to eq("Hey, MAGGIE!")
    end

    it "should work even when keyword args aren't given" do
      expect(@o.keyword_args_hello(&:upcase)).to eq("Hey, ADIT!")
    end
  end
end

Version data entries

16 entries across 16 versions & 5 rubygems

Version Path
entitlements-app-1.2.0 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
contracts-0.17.2 spec/ruby_version_specific/contracts_spec_2.0.rb
contracts-0.17.1 spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-1.1.0 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-1.0.0 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-0.3.4 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-0.3.1 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/spec/ruby_version_specific/contracts_spec_2.0.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-0.3.0 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-0.2.1 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-0.2.1 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-0.2.0 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
entitlements-app-0.2.0 lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/contracts-0.17/spec/ruby_version_specific/contracts_spec_2.0.rb
contracts-0.17 spec/ruby_version_specific/contracts_spec_2.0.rb