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_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_unit(self): # Arrange expected = res.Failure("error") # Act result = res.Failure.unit("error") # Assert self.assertEqual(result, expected)
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_eq(self): # Arrange s1 = res.Success(10) s2 = res.Success(10) s3 = res.Success(11) f1 = res.Failure(10) f2 = res.Failure(10) f3 = res.Failure(11) # Act # Assert self.assertTrue(s1 == s2) self.assertFalse(s1 == s3) self.assertTrue(f1 == f2) self.assertFalse(f1 == f3) self.assertFalse(s1 == f1) self.assertFalse(s1 == f3)
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 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_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): 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 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_FailureCase(self): # Arrange txt = 'Lorem' parser_1 = bpr.LParser('1') # Act parser = cmb.until(parser_1) result = parser(txt) expected = res.Failure('No more input') # Assert self.assertEqual(result, expected)
def test_Failure(self): # Arrange txt = 'aabbaccca1234' parser1 = bpr.LParser('x') parser2 = bpr.LParser('y') # Act parser = cmb.many1(parser1, parser2) result = parser(txt) expected = res.Failure('error') # Assert self.assertEqual(result, expected)
def test_FailureCase(self): # Arrange txt = '123abc lorem ipsum' parser_l = stp.parse_lowercases() parser_r = stp.parse_digits() parser = cmb.rightparser(parser_l, parser_r) # Act result = parser(txt) expected = res.Failure('error') # Assert self.assertEqual(result, expected)
def test_FailureCase(self): # Arrange txt = 'ABCabc123 lorem ipsum' parser_l = stp.parse_uppercases() parser_m = stp.parse_digits() parser_r = stp.parse_lowercases() parser = cmb.betweenparsers(parser_l, parser_m, parser_r) # Act result = parser(txt) expected = res.Failure('error') # 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_WhenFirstLettersAreIncorrect_ReturnFailure(self): # Arrange txt = 'lorem ipsum' parser1 = bpr.LParser('l') parser2 = bpr.LParser('x') parser3 = bpr.LParser('r') parser = cmb.and_then(parser1, parser2, parser3) # Act result = parser(txt) expected = res.Failure('error') # Assert self.assertEqual(result, expected)
def test_FailureCase(self): # Arrange txt = 'Lorem' parser1 = bpr.LParser('l') parser2 = bpr.LParser('x') parser3 = bpr.LParser('i') # Act parser = cmb.or_else(parser1, parser2, parser3) result = parser(txt) expected = res.Failure('error') # Assert self.assertEqual(result, expected)
def internal(txt): if txt == "": return res.Success.unit(('', '')) ret = '' fst_element = item(txt) if fst_element.isSuccess: parsed, remaining = fst_element.value rest_of_elements = sep_items(remaining) if rest_of_elements.isSuccess: parsed_rest, remaining = rest_of_elements.value return res.Success.unit((parsed + parsed_rest, remaining)) return res.Failure('error')
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_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_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 internal(input_txt): ret = "" txt = input_txt anyp = parse_any() checker = parser(txt) while checker.isFailure: parsed, txt = anyp(txt).value ret = ret + parsed checker = parser(txt) if txt == "": break if checker.isSuccess: return res.Success.unit((ret, txt)) return res.Failure("No more input")
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_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')
def test_parse_string(self): # Arrange txt1 = 'lorem ipsum dolor sit amet' txt2 = 'ipsum dolor sit amet' txt3 = 'dolor sit amet' parser_succ = stp.parse_one_of_strings('lorem', 'ipsum', 'dolor') parser_fail = stp.parse_one_of_strings('xxx', label='FAIL') # Act result_succ1 = parser_succ(txt1) result_succ2 = parser_succ(txt2) result_fail = parser_fail(txt3) expected_succ1 = res.Success(('lorem', ' ipsum dolor sit amet')) expected_succ2 = res.Success(('ipsum', ' dolor sit amet')) 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_succ.label, '(lorem|ipsum|dolor)') self.assertEqual(parser_fail.label, 'FAIL')
def _inner(self, txt): if txt == "": return res.Failure("No more input") if txt.startswith(self._val): return res.Success((self._val, txt[1:])) return res.Failure("error")
def internal(txt): if txt == "": return res.Failure("No more input") return res.Success.unit((txt[0], txt[1:]))
def test_isFailureisSuccess(self): fail = res.Failure("error") self.assertFalse(fail.isSuccess) self.assertTrue(fail.isFailure)