Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions exercises/practice/wordy/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
class WordProblem
attr_reader :question

def initialize(question)
@question = question
end

def answer
if too_complicated?
fail ArgumentError.new("I don't understand the question")
end
raise ArgumentError, "I don't understand the question" if too_complicated?

unless @answer
if matches[2].nil?
@answer = n1
else
@answer = n1.send(operation(2), n2)
@answer = @answer.send(operation(5), n3) if chain?
@answer = @answer.send(operation(4), n3) if chain?
end

@answer
end

private

def too_complicated?
matches.nil?
end
Expand All @@ -29,7 +29,7 @@ def matches

def pattern
operations = '(plus|minus|multiplied by|divided by)'
/What is (-?\d+) #{operations} (-?\d+)( #{operations} (-?\d+))?\?/
/What is (-?\d+)(?: #{operations} (-?\d+))?(?: #{operations} (-?\d+))?\?/
end

def operation(index)
Expand All @@ -50,7 +50,7 @@ def n2
end

def n3
matches[6].to_i
matches[-1].to_i
end

def chain?
Expand Down
12 changes: 12 additions & 0 deletions exercises/practice/wordy/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@
[88bf4b28-0de3-4883-93c7-db1b14aa806e]
description = "just a number"

[18983214-1dfc-4ebd-ac77-c110dde699ce]
description = "just a zero"

[607c08ee-2241-4288-916d-dae5455c87e6]
description = "just a negative number"

[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
description = "addition"

[bb9f2082-171c-46ad-ad4e-c3f72087c1b5]
description = "addition with a left hand zero"

[6fa05f17-405a-4742-80ae-5d1a8edb0d5d]
description = "addition with a right hand zero"

[79e49e06-c5ae-40aa-a352-7a3a01f70015]
description = "more addition"

Expand Down
160 changes: 128 additions & 32 deletions exercises/practice/wordy/wordy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,199 @@
require_relative 'wordy'

class WordyTest < Minitest::Test
def test_addition
def test_just_a_number
# skip
problem = WordProblem.new("What is 1 plus 1?")
assert_equal(2, problem.answer)
actual = WordProblem.new("What is 5?").answer
expected = 5
assert_equal expected, actual
end

def test_just_a_zero
skip
actual = WordProblem.new("What is 0?").answer
expected = 0
assert_equal expected, actual
end

def test_just_a_negative_number
skip
actual = WordProblem.new("What is -123?").answer
expected = -123
assert_equal expected, actual
end

def test_addition
skip
actual = WordProblem.new("What is 1 plus 1?").answer
expected = 2
assert_equal expected, actual
end

def test_addition_with_a_left_hand_zero
skip
actual = WordProblem.new("What is 0 plus 2?").answer
expected = 2
assert_equal expected, actual
end

def test_addition_with_a_right_hand_zero
skip
actual = WordProblem.new("What is 3 plus 0?").answer
expected = 3
assert_equal expected, actual
end

def test_more_addition
skip
problem = WordProblem.new("What is 53 plus 2?")
assert_equal(55, problem.answer)
actual = WordProblem.new("What is 53 plus 2?").answer
expected = 55
assert_equal expected, actual
end

def test_addition_with_negative_numbers
skip
problem = WordProblem.new("What is -1 plus -10?")
assert_equal(-11, problem.answer)
actual = WordProblem.new("What is -1 plus -10?").answer
expected = -11
assert_equal expected, actual
end

def test_large_addition
skip
problem = WordProblem.new("What is 123 plus 45678?")
assert_equal(45_801, problem.answer)
actual = WordProblem.new("What is 123 plus 45678?").answer
expected = 45_801
assert_equal expected, actual
end

def test_subtraction
skip
problem = WordProblem.new("What is 4 minus -12?")
assert_equal(16, problem.answer)
actual = WordProblem.new("What is 4 minus -12?").answer
expected = 16
assert_equal expected, actual
end

def test_multiplication
skip
problem = WordProblem.new("What is -3 multiplied by 25?")
assert_equal(-75, problem.answer)
actual = WordProblem.new("What is -3 multiplied by 25?").answer
expected = -75
assert_equal expected, actual
end

def test_division
skip
problem = WordProblem.new("What is 33 divided by -3?")
assert_equal(-11, problem.answer)
actual = WordProblem.new("What is 33 divided by -3?").answer
expected = -11
assert_equal expected, actual
end

def test_multiple_additions
skip
problem = WordProblem.new("What is 1 plus 1 plus 1?")
assert_equal(3, problem.answer)
actual = WordProblem.new("What is 1 plus 1 plus 1?").answer
expected = 3
assert_equal expected, actual
end

def test_addition_and_subtraction
skip
problem = WordProblem.new("What is 1 plus 5 minus -2?")
assert_equal(8, problem.answer)
actual = WordProblem.new("What is 1 plus 5 minus -2?").answer
expected = 8
assert_equal expected, actual
end

def test_multiple_subtraction
skip
problem = WordProblem.new("What is 20 minus 4 minus 13?")
assert_equal(3, problem.answer)
actual = WordProblem.new("What is 20 minus 4 minus 13?").answer
expected = 3
assert_equal expected, actual
end

def test_subtraction_then_addition
skip
problem = WordProblem.new("What is 17 minus 6 plus 3?")
assert_equal(14, problem.answer)
actual = WordProblem.new("What is 17 minus 6 plus 3?").answer
expected = 14
assert_equal expected, actual
end

def test_multiple_multiplication
skip
problem = WordProblem.new("What is 2 multiplied by -2 multiplied by 3?")
assert_equal(-12, problem.answer)
actual = WordProblem.new("What is 2 multiplied by -2 multiplied by 3?").answer
expected = -12
assert_equal expected, actual
end

def test_addition_and_multiplication
skip
problem = WordProblem.new("What is -3 plus 7 multiplied by -2?")
message = "You should ignore order of precedence. -3 + 7 * -2 = -8, not #{problem.answer}"
assert_equal(-8, problem.answer, message)
actual = WordProblem.new("What is -3 plus 7 multiplied by -2?").answer
expected = -8
assert_equal expected, actual
end

def test_multiple_division
skip
problem = WordProblem.new("What is -12 divided by 2 divided by -3?")
assert_equal(2, problem.answer)
actual = WordProblem.new("What is -12 divided by 2 divided by -3?").answer
expected = 2
assert_equal expected, actual
end

def test_unknown_operation
skip
problem = WordProblem.new("What is 52 cubed?")
assert_raises(ArgumentError) do
problem = WordProblem.new("What is 52 cubed?")
problem.answer
end
end

def test_non_math_question
skip
problem = WordProblem.new("Who is the President of the United States?")
assert_raises(ArgumentError) do
problem = WordProblem.new("Who is the President of the United States?")
problem.answer
end
end

def test_reject_problem_missing_an_operand
skip
assert_raises(ArgumentError) do
problem = WordProblem.new("What is 1 plus?")
problem.answer
end
end

def test_reject_problem_with_no_operands_or_operators
skip
assert_raises(ArgumentError) do
problem = WordProblem.new("What is?")
problem.answer
end
end

def test_reject_two_operations_in_a_row
skip
assert_raises(ArgumentError) do
problem = WordProblem.new("What is 1 plus plus 2?")
problem.answer
end
end

def test_reject_two_numbers_in_a_row
skip
assert_raises(ArgumentError) do
problem = WordProblem.new("What is 1 plus 2 1?")
problem.answer
end
end

def test_reject_postfix_notation
skip
assert_raises(ArgumentError) do
problem = WordProblem.new("What is 1 2 plus?")
problem.answer
end
end

def test_reject_prefix_notation
skip
assert_raises(ArgumentError) do
problem = WordProblem.new("What is plus 1 2?")
problem.answer
end
end
Expand Down