def test_CorrectlyHandlesDifferentNumberOfArguments(self): # Arrange txt = 'aabbacccadd1234' parser_a = bpr.LParser('a') parser_b = bpr.LParser('b') parser_c = bpr.LParser('c') parser_d = bpr.LParser('d') # Act parser1 = cmb.many1(parser_a) parser2 = cmb.many1(parser_a, parser_b) parser3 = cmb.many1(parser_a, parser_b, parser_c) parser4 = cmb.many1(parser_a, parser_b, parser_c, parser_d) result1 = parser1(txt) result2 = parser2(txt) result3 = parser3(txt) result4 = parser4(txt) expected1 = res.Success(('aa', 'bbacccadd1234')) expected2 = res.Success(('aabba', 'cccadd1234')) expected3 = res.Success(('aabbaccca', 'dd1234')) expected4 = res.Success(('aabbacccadd', '1234')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2) self.assertEqual(result3, expected3) self.assertEqual(result4, expected4)
def test_CorrectlyHandlesDifferentNumberOfArguments(self): # Arrange txt = 'lorem ipsum' parser_l = bpr.LParser('l') parser_o = bpr.LParser('o') parser_r = bpr.LParser('r') parser_e = bpr.LParser('e') parser1 = cmb.and_then(parser_l) parser2 = cmb.and_then(parser_l, parser_o) parser3 = cmb.and_then(parser_l, parser_o, parser_r) parser4 = cmb.and_then(parser_l, parser_o, parser_r, parser_e) # Act result1 = parser1(txt) result2 = parser2(txt) result3 = parser3(txt) result4 = parser4(txt) expected1 = res.Success(('l', 'orem ipsum')) expected2 = res.Success(('lo', 'rem ipsum')) expected3 = res.Success(('lor', 'em ipsum')) expected4 = res.Success(('lore', 'm ipsum')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2) self.assertEqual(result3, expected3) self.assertEqual(result4, expected4)
def test_CorrectlyHandlesDifferentNumberOfArguments(self): # Arrange txt1 = 'lorem ipsum dolor sit amet' txt2 = 'ipsum dolor sit amet' txt3 = 'dolor sit amet' txt4 = 'sit amet' parser_l = bpr.LParser('l') parser_o = bpr.LParser('i') parser_r = bpr.LParser('d') parser_e = bpr.LParser('s') parser = cmb.or_else(parser_l, parser_o, parser_r, parser_e) # Act result1 = parser(txt1) result2 = parser(txt2) result3 = parser(txt3) result4 = parser(txt4) expected1 = res.Success(('l', 'orem ipsum dolor sit amet')) expected2 = res.Success(('i', 'psum dolor sit amet')) expected3 = res.Success(('d', 'olor sit amet')) expected4 = res.Success(('s', 'it amet')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2) self.assertEqual(result3, expected3) self.assertEqual(result4, expected4)
def internal(txt): fst_result = parser(txt) if fst_result.isFailure: return res.Success(("", txt)) if fst_result.isSuccess: parsed, remaining = fst_result.value snd_result = parser(remaining) if snd_result.isFailure: return fst_result return res.Failure('error')
def test_lift_WhenLiftedFunctionCalledOnFailure_FailureIsReturn(self): # Arrange def triple(x): return 3 * x lifted_f = res.Result.lift(triple) arg = res.Failure("error") expected = res.Failure("error") # Act result = lifted_f(arg) # Assert self.assertEqual(result, expected)
def test_lift_WhenLiftedFunctionRaisesException_FailureIsReturn(self): # Arrange def triple(x): return 3 / x lifted_f = res.Result.lift(triple) arg = res.Success(0) expected = res.Failure("division by zero") # Act result = lifted_f(arg) # Assert self.assertEqual(result, expected)
def test_lift_WhenLiftedFunctionCalledOnSuccess_SuccessIsReturn(self): # Arrange def triple(x): return 3 * x lifted_f = res.Result.lift(triple) arg = res.Success(5) expected = res.Success(15) # Act result = lifted_f(arg) # Assert self.assertEqual(result, expected)
def test_SuccessCase(self): # Arrange txt1 = '2345' txt2 = '12345' parser = bpr.LParser('1') # Act parser = cmb.opt(parser) result1 = parser(txt1) result2 = parser(txt2) expected1 = res.Success(('', '2345')) expected2 = res.Success(('1', '2345')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def test_liftn_WhenLiftedFunctionCalledOnFailure_FailureIsReturn(self): # Arrange def sumThree(x, y, z): return x + y + z lifted_f = res.Result.liftn(sumThree) arg1 = res.Success(1) arg2 = res.Failure(4) arg3 = res.Success(3) # Act result = lifted_f(arg1, arg2, arg3) expected = tuple([arg1, arg2, arg3]) # Assert self.assertEqual(result, expected)
def test_liftn_WhenInproperNumberOfArgumentsGiven_FailureIsReturn(self): # Arrange def sumThree(x, y, z): return x + y + z lifted_f = res.Result.liftn(sumThree) arg1 = res.Success(1) arg2 = res.Success(2) # Act result = lifted_f(arg1, arg2) expected = res.Failure( r"sumThree() missing 1 required positional argument: 'z'") # Assert self.assertEqual(result, expected)
def test_liftn_WhenLiftedFunctionRaisesException_FailureIsReturn(self): # Arrange def sumThree(x, y, z): return x + y / z lifted_f = res.Result.liftn(sumThree) arg1 = res.Success(1) arg2 = res.Success(2) arg3 = res.Success(0) # Act result = lifted_f(arg1, arg2, arg3) expected = res.Failure("division by zero") # Assert self.assertEqual(result, expected)
def test_FailureCase(self): # Arrange txt1 = 'ABC' txt2 = '' sep = bpr.LParser(",") item = stp.parse_lowercases() parser = cmb.sep_by1(item, sep) # Act result1 = parser(txt1) expected1 = res.Failure('error') result2 = parser(txt2) expected2 = res.Failure('error') # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def test_parse_string(self): # Arrange txt = 'lorem ipsum' parser_succ = stp.parse_string('lorem') parser_fail = stp.parse_string('ipsum', label='FAIL') # Act result_succ = parser_succ(txt) result_fail = parser_fail(txt) expected_succ = res.Success(('lorem', ' ipsum')) expected_fail = res.Failure('error') # Assert self.assertEqual(result_succ, expected_succ) self.assertEqual(result_fail, expected_fail) self.assertEqual(parser_succ.label, 'lorem') self.assertEqual(parser_fail.label, 'FAIL')
def test_unit(self): # Arrange expected = res.Failure("error") # Act result = res.Failure.unit("error") # Assert self.assertEqual(result, expected)
def test_unit(self): # Arrange expected = res.Success(123) # Act result = res.Success.unit(123) # Assert self.assertEqual(result, expected)
def internal(txt): result = parser(txt) if result.isFailure: return result _, remaining = ignored_parser(result.value[1]).value return res.Success((result.value[0], remaining))
def test_SuccessCase(self): # Arrange txt1 = 'Lorem1' txt2 = 'Lorem#ipsum' parser_1 = bpr.LParser('1') parser_sharp = bpr.LParser('#') # Act parser = cmb.until(parser_1, parser_sharp) result1 = parser(txt1) result2 = parser(txt2) expected1 = res.Success(('Lorem', '1')) expected2 = res.Success(('Lorem', '#ipsum')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def internal(txt): if txt == "": return res.Failure.unit('error') fst_res = item(txt) if fst_res.isFailure: return res.Failure('error') parsed, remaining = fst_res.value snd_res = sep(remaining) if snd_res.isFailure: return res.Success((parsed, remaining)) result = parser(txt) if result.isSuccess: parsed, remaining = result.value return res.Success.unit((parsed, remaining)) return res.Failure('error')
def test_SuccessCase(self): # Arrange txt1 = 'Lorem' txt2 = 'ipsum' parser1 = bpr.LParser('L') parser2 = bpr.LParser('x') parser3 = bpr.LParser('i') # Act parser = cmb.or_else(parser1, parser2, parser3) result1 = parser(txt1) result2 = parser(txt2) expected1 = res.Success(('L', 'orem')) expected2 = res.Success(('i', 'psum')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def test_CorrectlyHandlesDifferentNumberOfArguments_Failure(self): # Arrange txt1 = '11789' txt2 = '123789' parser_1 = bpr.LParser('1') parser_2 = bpr.LParser('2') parser_3 = bpr.LParser('3') parser_4 = bpr.LParser('4') # Act parser = cmb.opt(parser_1, parser_2, parser_3, parser_4) result1 = parser(txt1) result2 = parser(txt2) expected1 = res.Failure('error') expected2 = res.Failure('error') # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def test_SuccessCase(self): # Arrange txt1 = '1' txt2 = 'a' txt3 = '$' parser = cmb.parse_any() # Act result1 = parser(txt1) result2 = parser(txt2) result3 = parser(txt3) expected1 = res.Success((txt1, '')) expected2 = res.Success((txt2, '')) expected3 = res.Success((txt3, '')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2) self.assertEqual(result3, expected3)
def test_CorrectlyHandlesDifferentNumberOfArguments_Success(self): # Arrange txt1 = '1789' txt2 = '789' parser_1 = bpr.LParser('1') parser_2 = bpr.LParser('2') parser_3 = bpr.LParser('3') parser_4 = bpr.LParser('4') # Act parser = cmb.opt(parser_1, parser_2, parser_3, parser_4) result1 = parser(txt1) result2 = parser(txt2) expected1 = res.Success(('1', '789')) expected2 = res.Success(('', '789')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def internal(txt): fst_digit = zero(txt) if fst_digit.isSuccess: snd = digits_09(fst_digit.value[1]) if snd.isSuccess: return res.Failure('error') else: return fst_digit return nonzero(txt)
def test_WhenFirstLetterIsIncorrect_ReturnsFailure(self): # Arrange txt = 'Lorem Ipsum' parser = bp.LParser('X') # Act result = parser(txt) expected = res.Failure('error') # Assert self.assertEqual(result, expected)
def test_WhenFirstLetterIsCorrect_ReturnsSuccess(self): # Arrange txt = 'Lorem ipsum' parser = bp.LParser('L') # Act result = parser(txt) expected = res.Success(('L', 'orem ipsum')) # Assert self.assertEqual(result, expected)
def test_FailureCase(self): # Arrange txt = '' parser = cmb.parse_any() # Act result = parser(txt) expected = res.Failure('No more input') # Assert self.assertEqual(result, expected)
def internal(txt): res1 = parsers[0](txt) if res1.isSuccess: return res1 res2 = parsers[1](txt) if res2.isSuccess: return res2 return res.Failure('error')
def test_Failure(self): # Arrange txt = '112345' parser = bpr.LParser('1') # Act parser = cmb.opt(parser) result = parser(txt) expected = res.Failure('error') # Assert self.assertEqual(result, expected)
def test_SuccessCase(self): # Arrange txt1 = 'abc,def,ghi' txt2 = 'abc' txt3 = 'abc,def,ghi2132' sep = bpr.LParser(",") item = stp.parse_lowercases() parser = cmb.sep_by1(item, sep) # Act result1 = parser(txt1) expected1 = res.Success(('abc,def,ghi', '')) result2 = parser(txt2) expected2 = res.Success(('abc', '')) result3 = parser(txt3) expected3 = res.Success(('abc,def,ghi', '2132')) # Assert self.assertEqual(result1, expected1) self.assertEqual(result2, expected2) self.assertEqual(result3, expected3)
def test_parse_character(self): # Arrange txt1 = 'aaa' txt2 = 'bbb' txt3 = 'ccc' parser = stp.parse_character('a', 'b') parser_label = stp.parse_character('ab', label='spam') # Act result_succ1 = parser(txt1) result_succ2 = parser_label(txt2) result_fail = parser(txt3) expected_succ1 = res.Success(('a', 'aa')) expected_succ2 = res.Success(('b', 'bb')) expected_fail = res.Failure('error') # Assert self.assertEqual(result_succ1, expected_succ1) self.assertEqual(result_succ2, expected_succ2) self.assertEqual(result_fail, expected_fail) self.assertEqual(parser.label, '[ab]') self.assertEqual(parser_label.label, 'spam')