Skip to content

Commit

Permalink
Add acceptance tests for keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek committed Sep 25, 2022
1 parent 26c4ae1 commit ddd44a6
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ Metrics/LineLength:
# It's not possible to set TargetRubyVersion to Ruby < v2.2
Gemspec/RequiredRubyVersion:
Enabled: false

Style/BracesAroundHashParameters:
Exclude:
# we specifically want to test hash parameters
- 'test/acceptance/keyword_arguments_test.rb'
120 changes: 120 additions & 0 deletions test/acceptance/keyword_arguments_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
require File.expand_path('../acceptance_test_helper', __FILE__)

class KeywordArgumentsTest < Mocha::TestCase
include AcceptanceTest

def setup
setup_acceptance_test
end

def teardown
teardown_acceptance_test
end

def test_should_match_hash_parameter_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(:key => 42)
mock.method({ :key => 42 })
end
assert_passed(test_result)
# with strict keyword matching:
# assert_failed(test_result)
end

def test_should_match_hash_parameter_with_splatted_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ :key => 42 })
mock.method({ :key => 42 })
end
assert_passed(test_result)
# with strict keyword matching:
# assert_failed(test_result)
end

def test_should_match_splatted_hash_parameter_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(:key => 42)
mock.method(**{ :key => 42 })
end
assert_passed(test_result)
end

def test_should_match_splatted_hash_parameter_with_splatted_hash
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ :key => 42 })
mock.method(**{ :key => 42 })
end
assert_passed(test_result)
end

def test_should_match_positional_and_keyword_args_with_last_positional_hash
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, { :key => 42 })
mock.method(1, :key => 42)
end
assert_passed(test_result)
# with strict keyword matching:
# assert_failed(test_result)
end

def test_should_match_last_positional_hash_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, :key => 42)
mock.method(1, { :key => 42 })
end
assert_passed(test_result)
# with strict keyword matching:
# assert_failed(test_result)
end

def test_should_match_positional_and_keyword_args_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, :key => 42)
mock.method(1, :key => 42)
end
assert_passed(test_result)
end

def test_should_match_hash_parameter_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(has_key(:key))
mock.method({ :key => 42 })
end
assert_passed(test_result)
end

def test_should_match_splatted_hash_parameter_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(has_key(:key))
mock.method(**{ :key => 42 })
end
assert_passed(test_result)
end

def test_should_match_positional_and_keyword_args_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, has_key(:key))
mock.method(1, :key => 42)
end
assert_passed(test_result)
end

def test_should_match_last_positional_hash_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, has_key(:key))
mock.method(1, { :key => 42 })
end
assert_passed(test_result)
end
end

0 comments on commit ddd44a6

Please sign in to comment.