Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/about_pattern_matching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_pattern_may_not_match
end
rescue Exception => ex
# What exception has been caught?
assert_equal __, ex.class
assert_equal __(NoMatchingPatternError), ex.class
end
end

Expand All @@ -22,7 +22,7 @@ def test_we_can_use_else
:no_match
end

assert_equal __, result
assert_equal __(:no_match), result
end

# ------------------------------------------------------------------
Expand All @@ -41,10 +41,10 @@ def value_pattern(variable)
end

def test_value_pattern
assert_equal __, value_pattern(0)
assert_equal __, value_pattern(5)
assert_equal __, value_pattern(100)
assert_equal __, value_pattern('Not a Number!')
assert_equal __(:match_exact_value), value_pattern(0)
assert_equal __(:match_in_range), value_pattern(5)
assert_equal __(:match_with_class), value_pattern(100)
assert_equal __(:no_match), value_pattern('Not a Number!')
end

# ------------------------------------------------------------------
Expand All @@ -60,7 +60,7 @@ def variable_pattern_with_binding(variable)
end

def test_variable_pattern_with_binding
assert_equal __, variable_pattern_with_binding(1)
assert_equal __(0), variable_pattern_with_binding(1)
end

# ------------------------------------------------------------------
Expand All @@ -77,7 +77,7 @@ def variable_pattern_with_pin(variable)
end

def test_variable_pattern_with_pin
assert_equal __, variable_pattern_with_pin(1)
assert_equal __(:no_match), variable_pattern_with_pin(1)
end

# ------------------------------------------------------------------
Expand All @@ -94,8 +94,8 @@ def pattern_with_dropping(variable)
end

def test_pattern_with_dropping
assert_equal __, pattern_with_dropping(['I will not be checked', 2])
assert_equal __, pattern_with_dropping(['I will not be checked', 'But I will!'])
assert_equal __(:match), pattern_with_dropping(['I will not be checked', 2])
assert_equal __(:no_match), pattern_with_dropping(['I will not be checked', 'But I will!'])
end

# ------------------------------------------------------------------
Expand All @@ -112,10 +112,10 @@ def alternative_pattern(variable)
end

def test_alternative_pattern
assert_equal __, alternative_pattern(0)
assert_equal __, alternative_pattern(false)
assert_equal __, alternative_pattern(nil)
assert_equal __, alternative_pattern(4)
assert_equal __(:match), alternative_pattern(0)
assert_equal __(:match), alternative_pattern(false)
assert_equal __(:match), alternative_pattern(nil)
assert_equal __(:no_match), alternative_pattern(4)
end

# ------------------------------------------------------------------
Expand All @@ -135,7 +135,7 @@ def as_pattern
end

def test_as_pattern
assert_equal __, as_pattern
assert_equal __('I was petrified'), as_pattern
end

# ------------------------------------------------------------------
Expand Down Expand Up @@ -163,8 +163,8 @@ def array_pattern(deconstructible)
end

def test_array_pattern
assert_equal __, array_pattern(Deconstructible.new('abcd'))
assert_equal __, array_pattern(Deconstructible.new('123'))
assert_equal __(['b', 'c']), array_pattern(Deconstructible.new('abcd'))
assert_equal __(:no_match), array_pattern(Deconstructible.new('123'))
end

# ------------------------------------------------------------------
Expand Down Expand Up @@ -193,8 +193,8 @@ def hash_pattern(deconstructible_as_hash)
end

def test_hash_pattern
assert_equal __, hash_pattern(LetterAccountant.new('aaabbc'))
assert_equal __, hash_pattern(LetterAccountant.new('xyz'))
assert_equal __([3, 2]), hash_pattern(LetterAccountant.new('aaabbc'))
assert_equal __([0, 0]), hash_pattern(LetterAccountant.new('xyz'))
end

# we can write it even shorter
Expand All @@ -208,8 +208,8 @@ def hash_pattern_with_sugar(deconstructible_as_hash)
end

def test_hash_pattern_with_sugar
assert_equal __, hash_pattern_with_sugar(LetterAccountant.new('aaabbc'))
assert_equal __, hash_pattern_with_sugar(LetterAccountant.new('xyz'))
assert_equal __([3, 2]), hash_pattern_with_sugar(LetterAccountant.new('aaabbc'))
assert_equal __([0, 0]), hash_pattern_with_sugar(LetterAccountant.new('xyz'))
end

end
end