def step(context, negatives): negs = map(int,negatives.split(",")) try: string_calculator.add(context.input) assert False, 'the previous line should have thrown an error' except ValueError as e: unfound = [n for n in negs if str(n) not in e.message] assert len(unfound) == 0, 'expected negative values {negs} to appear in message "{msg}"'.format(negs=unfound, msg=e.message)
def test_new_line_separator(self): self.assertEqual(string_calculator.add('1\n2,3'), 6, 'add(1\\n2,3) == 6') self.assertEqual(string_calculator.add('1\n2,3\n5'), 11, 'add(1\\n2,3\\n5) == 11')
def test_delimiters_any_length(): """it should ignore numbers bigger than 1000""" assert string_calculator.add('//[***]\n1***2***3') == 6
def test_different_delimiters(): """it should be able handle different delimiters""" assert string_calculator.add('//;\n1;2') == 3
def test_add_bigger_than_thousand(): assert add("//;\n1000,1;2") == 3
def test_add_positive_integers(): assert add("1") == 1 assert add("1,2") == 3 assert add("1,3,5,9") == 18
def test_numbers_bigger_than(self): self.assertEqual(2, calc.add("//;\n1001;2")) self.assertEqual(1002, calc.add("//;\n1000;2")) self.assertEqual(2, calc.add("//;\n2;1001")) self.assertEqual(6, calc.add("//;\n2;1001;1;3"))
def test_newline_between_numbers_input(self): self.assertEqual(6, calc.add("1\n2,3")) self.assertEqual(6, calc.add("1,2\n3")) self.assertEqual(12, calc.add("1,2\n3\n1,2,3"))
def test_if_given_a_two_returns_a_two(self): expected = "2" actual = add("2") self.assertEqual(expected, actual)
def test_add_one_number(self): self.assertEqual(string_calculator.add('1'), 1, 'add("1") == 1') self.assertEqual(string_calculator.add('6'), 6, 'add("6") == 6')
def step(context, sum): expected_sum = int(sum) actual_sum = string_calculator.add(context.input) assert actual_sum == expected_sum, 'expected {exp}, got {act}'.format(exp=expected_sum, act=actual_sum)
def test_add_empty_string(self): self.assertEqual(string_calculator.add(''), 0, 'add("") == 0')
def test_bigger_than_1000_numbers(self): self.assertEqual(string_calculator.add('1\n2,3,1001'), 6, 'add(1\\n2,3,1001) == 6') self.assertEqual(string_calculator.add('//:\n1:2:3:2000'), 6, 'add("//:\\n1:2:3:2000") == 6')
def test_custom_delimiters(self): self.assertEqual(string_calculator.add('//;\n1;2'), 3, 'add("//;\\n1;2") == 3') self.assertEqual(string_calculator.add('//:\n1:2:3'), 6, 'add("//:\\n1:2:3") == 6')
def test_add(): assert string_calculator.add("") == 0 assert string_calculator.add("1") == 1 assert string_calculator.add("2") == 2 assert string_calculator.add("1,2") == 3 assert string_calculator.add("2,3") == 5 assert string_calculator.add("1,2,3,4") == 10 assert string_calculator.add("1,4,5,6") == 16 assert string_calculator.add("1\n2") == 3 assert string_calculator.add("3\n4") == 7 assert string_calculator.add("//:\n1:2:3") == 6 assert string_calculator.add("3\n4") == 6 assert string_calculator.add("//---\n1---2---3") == 6 assert string_calculator.add("//-!:o-\n1-1;o-2-!;o-3") == 6 assert string_calculator.add("//-!o-\n15-!o-12-!o-3") == 30
def test_if_given_a_four_returns_a_four(self): expected = "4" actual = add("4") self.assertEqual(expected, actual)
def test_two_string_input(self): self.assertEqual(3, calc.add("1,2")) self.assertEqual(6, calc.add("2,4"))
def test_adds_one_and_one_and_returns_two(self): expected = "2" actual = add("1,1") self.assertEqual(expected,actual)
def test_with_negative_number(self): with self.assertRaises(Exception) as context: calc.add("//;\n-1") self.assertTrue("Negatives not allowed: -1" in str(context.exception))
def test_adds_two_strings_returns_sum(self): expected = "807" actual = add("747,60") self.assertEqual(expected,actual)
def test_empty_string_input(self): self.assertEqual(0, calc.add("")) self.assertEqual(0, calc.add(" "))
def test_adds_three_strings_returns_sum(self): expected = "809" actual = add("747,60,2") self.assertEqual(expected,actual)
def test_add_with_delimiters(): assert add("//;\n1;2") == 3 assert add("//***\n1***2***3") == 6 assert add("//[\*][%]\n1\*2%3") == 6 assert add("//***//[\*][%]\n1\*2%3\n1***2***3") == 12
def test_can_use_newlines_to_delimit(self): expected = "2" actual = add("1\n1") self.assertEqual(expected,actual)
def test_add_multiple_CSVs(): """it should an unknown amount of numbers in a comma separated string""" assert string_calculator.add('1, 2, 3') == 6
def test_can_use_custom_chars_to_delimit(self): expected = "2" actual = add("\\@\n1@1") self.assertEqual(expected,actual)
def test_bigger_than_1000(): """it should ignore numbers bigger than 1000""" assert string_calculator.add('//;\n1000,1;2') == 3
def test_using_negatives_returns_exception_string(self): expected = "negatives not allowed: -2" actual = add("-2") self.assertEqual(expected,actual)
def test_add_two_CSVs(): """it should add 2 numbers in a comma separated string""" assert string_calculator.add('1, 2') == 3
def test_negatives_and_positive_mix_returns_negatives(self): expected = "negatives not allowed: -2" actual = add("1,-2") self.assertEqual(expected,actual)
def test_add_one(): #Question 1: One number assert add("1") == 1
def test_numbers_larger_than_1000_are_ignored(self): expected = "2" actual = add("2,1001") self.assertEqual(expected,actual)
def test_one_string_input(self): self.assertEqual(1, calc.add("1")) self.assertEqual(2, calc.add("2"))
def test_when_add_is_called_logger_is_called_with_add_result(self, log_function): add("1,1") string_calculator.log.assert_called_with("2")
def test_unknown_amount_input(self): self.assertEqual(6, calc.add("1,2,3")) self.assertEqual(4, calc.add("1,1,1,1")) self.assertEqual(6, calc.add("1,1,1,1,2"))
def test_returns_zero_for_an_empty_string(self): expected = "0" actual = add("") self.assertEqual(expected, actual)
def test_different_delimiters(self): self.assertEqual(3, calc.add("//;\n1;2")) self.assertEqual(6, calc.add("//;\n1;2\n3")) self.assertEqual(6, calc.add("//;1;2\n3")) self.assertEqual(6, calc.add("//;1\n2\n3"))
def test_two_numbers(self): self.assertEqual(3, add("1,2"))
def test_with_several_negative_numbers(self): with self.assertRaises(Exception) as context: calc.add("//;\n-1;-2") self.assertTrue( "Negatives not allowed: [-1, -2]" in str(context.exception))
def test_three_numbers(self): self.assertEqual(5, add("1,2,2"))
def test_any_length_delimiter(self): self.assertEqual(6, calc.add("//[***]\n1***2***3")) self.assertEqual(10, calc.add("//[+]\n1+2+3+4")) self.assertEqual(10, calc.add("//[..]\n1..2\n3..4"))
def test_new_line_delimiter(self): self.assertEqual(6, add("1\n2,3"))
def test_multiple_delimiters(self): self.assertEqual(6, calc.add("//[*][%]\n1*2%3")) self.assertEqual(10, calc.add("//[a][x][..]\n1a2..3x4"))
def test_support_different_delimiter(self): self.assertEqual(3, add("//;\n1;2"))
def test_add_with_new_lines(): assert add("1\n2,3") == 6
def test_catch_negative_numbers(self): with self.assertRaises(ValueError) as context: add("-1") self.assertEqual("Negative numbers not allowed: -1", context.exception.message)
def test_add_negative_integers(): with pytest.raises(Exception) as error: assert add("-1,-2,3,4") assert str(error.value) == 'ERROR: negatives not allowed -1,-2'
def test_numbers_bigger_than_1000_not_allowed(self): self.assertEqual(2, add("2,1001"))
def test_add_empty_string(): assert add("") == 0
def test_bigger_delimiters(self): self.assertEqual(6, add("//[***]\n1***2***3"))
def test_new_line_as_delimiter(): """it should be able to use new lines as delimiters""" assert string_calculator.add('1\n2,3') == 6
def test_multiple_delimiters(self): self.assertEqual(6, add("//[*][%]\n1*2%3"))
def test_negative_numbers_exception(): """it should raise an exception containing all negative numbers if any are found""" with pytest.raises(Exception, match = r'negatives not allowed \[-1, -2, -3\]'): string_calculator.add('-1, -2, -3, 1, 2, 3')
def test_empty_string(self): self.assertEqual(0, add(""))
def test_empty_string(): """it should return 0 if the string is empty""" assert string_calculator.add('') == 0
def test_one_number(self): self.assertEqual(1, add("1"))
def test_multiple_delimiters(): """it should be able handle multiple delimiters""" assert string_calculator.add('//[*][%]\n1*2%3') == 6
def test_add_more_characters(): #Question 9: Test multiple delimiters with length longer than one character assert add("//[*][%]\n1*2%+???++@aqw^^^\\^3") == 6
def test_add_numbers_step4(numbers, output): result = add(numbers) assert result == output
def test_add_unknown_amount_of_numbers(self): self.assertEqual(string_calculator.add('1,3,5,8'), 17, 'add("1,3,5,8") == 17') self.assertEqual(string_calculator.add('2,3,4,5,6'), 20, 'add("2,3,4,5,6") == 20')