diff --git a/exercises/change/.meta/generator/change_case.rb b/exercises/change/.meta/generator/change_case.rb index 2885e9b56d..99369d768c 100644 --- a/exercises/change/.meta/generator/change_case.rb +++ b/exercises/change/.meta/generator/change_case.rb @@ -3,7 +3,7 @@ class ChangeCase < Generator::ExerciseCase def workload if error_expected? - assert_raises(ArgumentError, subject_of_test) + handle_errors else assert_equal(expected, subject_of_test) end @@ -11,6 +11,15 @@ def workload private + def handle_errors + case test_name + when 'test_cannot_find_negative_change_values' + assert_raises('Change::NegativeTargetError', subject_of_test) + else + assert_raises('Change::ImpossibleCombinationError', subject_of_test) + end + end + def subject_of_test "Change.generate(#{coins}, #{target})" end diff --git a/exercises/change/.meta/solutions/change.rb b/exercises/change/.meta/solutions/change.rb index 93b712db3a..fead516e0c 100644 --- a/exercises/change/.meta/solutions/change.rb +++ b/exercises/change/.meta/solutions/change.rb @@ -1,6 +1,9 @@ class Change attr_reader :coins, :target + class NegativeTargetError < ArgumentError; end + class ImpossibleCombinationError < StandardError; end + def initialize(coins, target) @coins = coins.sort.reverse @target = target @@ -8,10 +11,11 @@ def initialize(coins, target) end def generate + raise NegativeTargetError if target.negative? return [] if target.zero? calculate_change(coins, [], target) - raise ArgumentError if total_change.none? + raise ImpossibleCombinationError if total_change.none? total_change.sort end diff --git a/exercises/change/change_test.rb b/exercises/change/change_test.rb index dbb5ca15be..4f25756eaf 100644 --- a/exercises/change/change_test.rb +++ b/exercises/change/change_test.rb @@ -45,21 +45,21 @@ def test_no_coins_make_0_change def test_error_testing_for_change_smaller_than_the_smallest_of_coins skip - assert_raises(ArgumentError) do + assert_raises(Change::ImpossibleCombinationError) do Change.generate([5, 10], 3) end end def test_error_if_no_combination_can_add_up_to_target skip - assert_raises(ArgumentError) do + assert_raises(Change::ImpossibleCombinationError) do Change.generate([5, 10], 94) end end def test_cannot_find_negative_change_values skip - assert_raises(ArgumentError) do + assert_raises(Change::NegativeTargetError) do Change.generate([1, 2, 5], -5) end end