class StringCalculatorTest(unittest.TestCase): def setUp(self): self.sc = StringCalculator() def test_empty(self): self.assertEqual(0, self.sc.Add("")) def test_three(self): self.assertEqual(8, self.sc.Add("1,2,5")) def test_threeNL(self): self.assertEqual(6, self.sc.Add("1\n,2,3")) self.assertEqual(7, self.sc.Add("1,\n2,4")) def test_delim(self): self.assertEqual(8, self.sc.Add("//;\n1;3;4")) self.assertEqual(6, self.sc.Add("//$\n1$2$3")) self.assertEqual(13, self.sc.Add("//@\n2@3@8")) def test_noNegative(self): with self.assertRaises(NegativeException) as neContext: self.sc.Add("1,-2,3,-4") ne = neContext.exception self.assertTrue(0 < ne.message.find("-2")) self.assertTrue(0 < ne.message.find("-4")) def test_tooLarge(self): self.assertEqual(1002, self.sc.Add("2,1000")) self.assertEqual(2, self.sc.Add("2,1001")) def test_longDelimTest(self): self.assertEquals(6, self.sc.Add("//***\n1***2***3")) def test_multiDelimTest(self): self.assertEquals(6, self.sc.Add("//$,@\n1$2@3"))
def test_negative_numbers_throw_exception(self): calc = StringCalculator() try: calc.add("-1,-2") self.fail() except NegativeException as ex: self.assertEqual(ex.args[0], "Negatives not allowed: -1,-2")
class TestKata(unittest.TestCase): def setUp(self): self.calc = StringCalculator() def test_no_operands(self): self.assertEqual(self.calc.Add(""), 0) def test_n_operands(self): self.assertEqual(self.calc.Add("1"), 1) self.assertEqual(self.calc.Add("2"), 2) self.assertEqual(self.calc.Add("1,2"), 3) self.assertEqual(self.calc.Add("1,2,3"), 6) def test_delimiter_newline(self): self.assertEqual(self.calc.Add("1\n2"), 3) self.assertEqual(self.calc.Add("1\n2,3"), 6) def test_delimiter_choice(self): self.assertEqual(self.calc.Add("//;\n1;2,3\n4"), 10) def test_double_digits(self): self.assertEqual(self.calc.Add("12,3,4"), 19) def test_double_digits2(self): self.assertEqual(self.calc.Add("12,24,36"), 72)
def test_add_with_empty(self): """should return 0 for empty string""" # Given input = "" stringcalculator = StringCalculator() # When result = stringcalculator.add(input) # Then self.assertEqual(result, 0)
def test_add_with_space_as_separator(self): """should return_sum when space is separator(""" # Given input = "3 4" stringcalculator = StringCalculator() # When result = stringcalculator.add(input) # Then self.assertEqual(result, 7)
def test_add_with_two_number(self): """should return sum of numbers with two numbers""" # Given input = "3,4" stringcalculator = StringCalculator() # When result = stringcalculator.add(input) # Then self.assertEqual(result, 7)
def test_add_with_one_number(self): """should return the number with only one number""" # Given input = "3" stringcalculator = StringCalculator() # When result = stringcalculator.add(input) # Then self.assertEqual(result, 3)
def parse_args(self): parser = argparse.ArgumentParser( description='StringCalculator (stringcal "12,4,5")') parser.add_argument('stringcal') parser.add_argument('string_sum', type=str) args = parser.parse_args() calc = StringCalculator() #result = cal.Add(operands=args.string_sum) result = calc.Add(args.string_sum) print(result) return result
class StringCalculatorTest(unittest.TestCase): def setUp(self): self.stringCalculator = StringCalculator() def testSumEmptyString(self): self.assertEqual("0",self.stringCalculator.add("")) def testSumOneValue(self): self.assertEqual("1",self.stringCalculator.add("1")) def testSumTwoValues(self): self.assertEqual("3",self.stringCalculator.add("1,2")) def testSum15Values(self): self.assertEqual(str(16*7+8),self.stringCalculator.add(self.string15())) def testSumTwoNewLinedString(self): self.assertEqual("3",self.stringCalculator.add("1\n2")) def testDefaultDelimiterSemiColom(self): self.assertEqual("3",self.stringCalculator.add("//;\n1;2")) def testNegativeValue(self): self.assertRaises(self.stringCalculator.NegativeNumbers,self.stringCalculator.add,"-1,2") def testANegativeValueMessage(self): self.assertRaisesRegexp(self.stringCalculator.NegativeNumbers,"^-1$",self.stringCalculator.add,"-1,2") def testNegativeValuesMessage(self): self.assertRaisesRegexp(self.stringCalculator.NegativeNumbers,"^-1,-2$",self.stringCalculator.add,"-1,-2,3") def testValuesOver1000(self): self.assertEqual("1003",self.stringCalculator.add("1,2,1000,1001")) def testMultipleCharDelimiter(self): self.assertEqual("3",self.stringCalculator.add("//[**]\n1**2")) def testMultipleCharMultipleDelimiter(self): self.assertEqual("6",self.stringCalculator.add("//[**][perra]\n1**2perra3")) def tearDown(self): pass def string15(self): string15 = "" for number in range (0,16): string15 += str(number) string15 += "," string15 = string15.strip(",") return string15
class StringCalculatorTest(unittest.TestCase): # tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests. def test_start_here(self): self.calculator = StringCalculator() result = self.calculator.generate() self.assertEqual(result, '')
class TestStringCalculator(unittest.TestCase): def setUp(self): self.calc = StringCalculator() def testVacio(self): self.assertEqual(0, self.calc.Add("")) def testUnNumero(self): self.assertEqual(1, self.calc.Add("1")) self.assertEqual(2, self.calc.Add("2")) def testDosNumeros(self): self.assertEqual(3, self.calc.Add("1,2")) self.assertEqual(30, self.calc.Add("10,20")) def testVariosNumeros(self): self.assertEqual(6, self.calc.Add("1,2,3")) self.assertEqual(60, self.calc.Add("10,20,30")) def testSeparadorSaltoLinea(self): self.assertEqual(6, self.calc.Add("1\n2,3")) def testNoSeAdmiteSeparadorDoble(self): self.assertEqual(6, self.calc.Add("1\n,,3"))
def test_support_custom_delimiter(self): calc = StringCalculator() result = calc.add("//;\n1;2") self.assertEqual(3, result)
def test_support_multiple_custom_delimiter(self): calc = StringCalculator() result = calc.add("//[*][%]\n1*2%3") self.assertEqual(6, result)
def test_string_supports_infite_number_of_numbers(self): calc = StringCalculator() result = calc.add("1,2,3") self.assertEqual(6, result)
def test_support_newline_as_delimiter(self): calc = StringCalculator() result = calc.add("1\n2,3") self.assertEqual(6, result)
def test_string_one_returns_one(self): calc = StringCalculator() result = calc.add("1") self.assertEqual(1, result)
def test_string_one_comma_two_returns_three(self): calc = StringCalculator() result = calc.add("1,2") self.assertEqual(3, result)
def test_add_empty_str(self): test_data = "" correct_result = 0 test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)
def test_empty(self): result = StringCalculator().add("") self.assertEqual(result, 0)
def setUp(self): self.stringCalculator = StringCalculator()
def test_add_number_greater_1000(self): test_data = "10\n30\n15,1001" correct_result = 55 test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)
def test_delimiters(self): result = StringCalculator().add("//###\n1###2###10") self.assertEqual(result, 13)
def test_empty_string_returns_zero(self): calc = StringCalculator() result = calc.add("") self.assertEqual(0, result)
def test_add_one_argument(self): test_data = "1" correct_result = 1 test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)
def test_support_any_lenght_custom_delimiter(self): calc = StringCalculator() result = calc.add("//[***]\n1***2***3") self.assertEqual(6, result)
def test_add_with_long_separator(self): test_data = "//###\n1###2" correct_result = 3 test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)
def test_add_two_number_with_comma(self): test_data = "10,30" correct_result = 40 test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)
def test_sum(self): result = StringCalculator().add("12 34 20 4") self.assertEqual(result, 70)
def test_numbers_above_1000_are_ignored(self): calc = StringCalculator() result = calc.add("2,1001") self.assertEqual(2, result)
def test_add_negative_number(self): test_data = "-10\n30\n-15" correct_result = "отрицательные числа запрещены: -10,-15" test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)
class Test(unittest.TestCase): __stringCalculator = StringCalculator() def testAddEmpty(self): self.assertEquals(self.__stringCalculator.add(""), 0, "Se ha intentado sumar vacio, y no ha dado 0") def testAddSingleNumber(self): self.assertEquals( self.__stringCalculator.add("1"), 1, "Se ha intentado sumar 1+vacio (un numero) y no ha dado 1") def testAddOtherSingleNumber(self): self.assertEquals( self.__stringCalculator.add("2"), 2, "Se ha intentado sumar 2+vacio (un numero) y no ha dado 2") def testAddTwoNumbers(self): self.assertEquals( self.__stringCalculator.add("1,2"), 3, "Se ha intentado sumar 1+2 (dos numeros) y no ha dado 3") def testAddMoreThanTwoNumbers(self): self.assertEquals( self.__stringCalculator.add("1,2,3,4,5"), 15, "Se ha intentado sumar 1+2+3+4+5 (mas de dos numeros) y no ha dado 15" ) def testAddTwoNumbersWithBreakLine(self): self.assertEquals( self.__stringCalculator.add("1\n2"), 3, "Se ha intentado sumar 1+2 con el delimitador de salto de linea y no ha dado 3" ) def testAddNumbersWithBreakLineAndComma(self): self.assertEquals( self.__stringCalculator.add("1\n2,3"), 6, "Se ha intentado sumar 1+2+3 con el delimitador de salto de linea y la coma y no ha dado 6" ) def testAddNumbersWithSpecifiedDelimiter(self): self.assertEquals( self.__stringCalculator.add("//;\n1;2"), 3, "Se ha intentado sumar 1+2 con el delimitador especificado (;) y no ha dado 3" ) def testAddNegativeNumber(self): try: self.__stringCalculator.add("-1") self.fail( "Se ha intentado sumar -1+vacio y no se ha devuelto ninguna excepcion" ) except Exception as exc: self.assertEquals( exc.message, "Negatives not allowed -1", "Se ha intentado sumar -1+vacio y no ha devuelvo una excepcion con el texto \"negatives not allowed -1\"" ) def testAddMultipleNegativeNumber(self): try: self.__stringCalculator.add("-1,-2") self.fail( "Se ha intentado sumar -1+-2+vacio y no se ha devuelto ninguna excepcion" ) except Exception as exc: self.assertEquals( exc.message, "Negatives not allowed -1,-2", "Se ha intentado sumar -1+-2+vacio y no ha devuelvo una excepcion con el texto \"negatives not allowed -1\"" ) def testAddNumbersBiggerThanThousand(self): self.assertEquals( self.__stringCalculator.add("1,1005"), 1, "Se ha intentado sumar 1+1005 (1 + un numero mayor que 1000) y no ha dado 1" )
def test_add_three_number_with_enter(self): test_data = "10\n30\n15" correct_result = 55 test_result = StringCalculator().add(test_data) self.assertEqual(correct_result, test_result)