Sha256: de7d513c83a96a3721f80d0e1aec0cd95d49bf2534b5d56fab053e90f4b963a8

Contents?: true

Size: 1.6 KB

Versions: 34

Compression:

Stored size: 1.6 KB

Contents

# TODO: this should be loaded from rubyspec instead

describe "An instance method with keyword arguments" do
  context "when there is a single keyword argument" do
    before do
      def foo(a: 1)
        a
      end
    end

    it "evaluates to the default when a value isn't provided" do
      foo.should == 1
    end

    it "evaluates to the provided value" do
      foo(a: 20).should == 20
      foo(a: nil).should be_nil
    end

    it "raises an argument error when an unknown keyword argument is provided" do
      lambda { foo(b: 20) }.should raise_error(ArgumentError)
    end

    it "raises an argument error when a non-keyword argument is provided" do
      lambda { foo(1) }.should raise_error(ArgumentError)
    end
  end

  it "treats a sole hash argument correctly" do
    def foo(a, b: 10)
      [a, b]
    end
    foo(b: "b").should == [{:b => "b"}, 10]
    foo("a", b: "b").should == ["a", "b"]
  end

  it "correctly distinguishes between optional and keyword arguments" do
    def foo(a = true, b: 10)
      [a, b]
    end
    foo(b: 42).should == [true, 42]
    foo(false, b: 42).should == [false, 42]
  end

  it "correctly distinguishes between rest and keyword arguments" do
    def foo(*a, b: 10)
      [a, b]
    end
    foo(1, 2, 3, 4).should == [[1, 2, 3, 4], 10]
    foo(1, 2, 3, 4, b: 42).should == [[1, 2, 3, 4], 42]
    foo(b: 42).should == [[], 42]
  end

  it "should allow keyword rest arguments" do
    def foo(a: 1, **b)
      [a, b]
    end
    foo(b: 2, c: 3, d: 4).should == [1, {:b => 2, :c => 3, :d => 4}]
    foo(a: 4, b: 2).should == [4, {:b => 2}]
    foo.should == [1, {}]
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
opal-0.9.0.beta2 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.9.0.beta1 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.1 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.1.rc1 spec/opal/core/language/versions/def_2_0_spec.rb
opal-wedge-0.9.0.dev spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.0 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.0.rc3 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.0.rc2 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.0.rc1 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.8.0.beta1 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.7.2 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.7.1 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.7.0 spec/opal/core/language/versions/def_2_0_spec.rb
opal-0.7.0.rc1 spec/opal/core/language/versions/def_2_0_spec.rb