Sha256: 426ad5559a95a98890e4343697d496e4655819e4bf87683480b83a40c0af82b3

Contents?: true

Size: 1.86 KB

Versions: 14

Compression:

Stored size: 1.86 KB

Contents

class OptionalArgsTest < Minitest::Test
  def test_overriding_optional_argument
    actual = Invokr.invoke(
      method: :optional_argument,
      on:     TestMethodBank,
      with:   { album: 'rift' },
    )

    assert_equal 'rift', actual
  end

  def test_default_optional_argument
    actual = Invokr.invoke(
      method: :optional_argument,
      on:     TestMethodBank,
      with:   {},
    )

    assert_equal 'junta', actual
  end

  def test_use_extra_args_as_hash_for_optional_argument
    hsh = { guitarist: 'trey' }

    actual = Invokr.invoke(
      method: :optional_argument,
      on:     TestMethodBank,
      with:   hsh,
    )

    assert_equal hsh, actual
  end

  def test_cant_use_extra_args_as_hash_to_override_optional_argument
    error = assert_raises Invokr::ExtraArgumentsError do
      Invokr.invoke(
        method: :optional_argument,
        on:     TestMethodBank,
        with:   { album: 'junta', guitarist: 'trey' },
      )
    end

    assert_equal(
      "unused argument(s) `guitarist' when invoking method `optional_argument' on #<TestMethodBank:0xdeadbeef>",
      error.message,
    )
  end

  def test_argument_after_optional_argument_raises_error
    error = assert_raises Invokr::OptionalPositionalArgumentError do
      Invokr.invoke(
        method: :double_optional_argument,
        on:     TestMethodBank,
        with:   {},
      )
    end

    expected_error_message = <<-MESSAGE
method `double_optional_argument' has optional positional argument `album2', after optional argument `album1'.

We cannot use this method because there's no way to supply an explicit value for `album2' without knowing the default value for `album1'. It's technically possible to overcome this with S-expression analysis, but a much simpler solution would be to use keyword arguments.
    MESSAGE

    assert_equal expected_error_message, error.message
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
invokr-0.9.7 test/optional_args_test.rb
invokr-0.9.6 test/optional_args_test.rb
invokr-0.9.5 test/optional_args_test.rb
invokr-0.9.4 test/optional_args_test.rb
invokr-0.9.3 test/optional_args_test.rb
invokr-0.9.2 test/optional_args_test.rb
invokr-0.9.1 test/optional_args_test.rb
invokr-0.9.0 test/optional_args_test.rb
invokr-0.1.0 test/optional_args_test.rb
invokr-0.0.6 test/optional_args_test.rb
invokr-0.0.5 test/optional_args_test.rb
invokr-0.0.4 test/optional_args_test.rb
invokr-0.0.2 test/optional_args_test.rb
invokr-0.0.1 test/optional_args_test.rb