From cb14e48f1dcf3a26c288a6d12b9a23a8cfb30724 Mon Sep 17 00:00:00 2001 From: mfeckie Date: Sat, 6 Apr 2013 12:18:41 +0800 Subject: [PATCH] Add tests for keyword arguments in Ruby 2.0 --- src/about_methods.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/about_methods.rb b/src/about_methods.rb index b720010..eabe7aa 100644 --- a/src/about_methods.rb +++ b/src/about_methods.rb @@ -77,6 +77,36 @@ class AboutMethods < EdgeCase::Koan assert_equal __([:one]), method_with_var_args(:one) assert_equal __([:one, :two]), method_with_var_args(:one, :two) end + # ------------------------------------------------------------------ + + if ruby_version?('2.0') + def method_with_keyword_arguments(one: 1, two: 'two') + [one, two] + end + + def test_keyword_arguments + assert_equal __, method_with_keyword_arguments.class + assert_equal __, method_with_keyword_arguments + assert_equal __, method_with_keyword_arguments(one: 'one') + assert_equal __, method_with_keyword_arguments(two: 2) + end + + def method_with_keywork_arguments_with_mandatory_argument(one, two: 2, three: 3) + [one, two, three] + end + + def test_keyword_arguments_with_wrong_number_of_arguments + exception = assert_raise (__) do + method_with_keywork_arguments_with_mandatory_argument + end + assert_match(/__/, exception.message) + end + + # THINK ABOUT IT: + # + # Keyword arguments always have a default value, making them optional to the caller + + end # ------------------------------------------------------------------