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 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 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 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_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 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_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 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_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_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 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_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_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 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_SuccessCase(self): # Arrange txt = 'abc123 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.Success(('123', ' lorem ipsum')) # Assert self.assertEqual(result, expected)
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 test_NoMatch(self): # Arrange txt = 'aabbaccca1234' parser1 = bpr.LParser('x') parser2 = bpr.LParser('y') # Act parser = cmb.many(parser1, parser2) result = parser(txt) expected = res.Success(('', 'aabbaccca1234')) # Assert self.assertEqual(result, expected)
def test_WhenFirstLettersAreCorrect_ReturnSuccess(self): # Arrange txt = 'lorem ipsum' parser1 = bpr.LParser('l') parser2 = bpr.LParser('o') parser3 = bpr.LParser('r') parser = cmb.and_then(parser1, parser2, parser3) # Act result = parser(txt) expected = res.Success(('lor', 'em ipsum')) # Assert self.assertEqual(result, expected)
def internal(txt): res1 = parsers[0](txt) if res1.isFailure: return res1 char1, remaining1 = res1.value res2 = parsers[1](remaining1) if isinstance(res2, res.Failure): return res2 char2, remaining2 = res2.value return res.Success((char1 + char2, remaining2))
def test_SuccessCase(self): # Arrange txt = 'aabbaccca1234' parser1 = bpr.LParser('a') parser2 = bpr.LParser('b') parser3 = bpr.LParser('c') # Act parser = cmb.many1(parser1, parser2, parser3) result = parser(txt) expected = res.Success(('aabbaccca', '1234')) # 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_SuccessCase(self): # Arrange txt = 'ABCabc123 lorem ipsum' parser_l = stp.parse_uppercases() parser_m = stp.parse_lowercases() parser_r = stp.parse_digits() parser = cmb.betweenparsers(parser_l, parser_m, parser_r) # Act result = parser(txt) expected = res.Success(('abc', ' lorem ipsum')) # Assert self.assertEqual(result, expected)
def test_strip(self): # Arrange txt1 = ' \n\tLorem \n\tIpsum' txt2 = ' 12312 \nXYZ' txt3 = '11Lorem111Ipsum' exp1 = res.Success(('Lorem', 'Ipsum')) exp2 = res.Success(('12312', 'XYZ')) exp3 = res.Success(('Lorem', 'Ipsum')) parser1 = stp.strip(stp.parse_string("Lorem")) parser2 = stp.strip(stp.parse_digits(), ignored_lst=[' ', '\n']) parser3 = stp.strip(stp.parse_string("Lorem"), ignored_lst=['1']) # Act res1 = parser1(txt1) res2 = parser2(txt2) res3 = parser3(txt3) # Assert self.assertEqual(res1, exp1) self.assertEqual(res2, exp2) self.assertEqual(res3, exp3) self.assertEqual(parser1.label, "Lorem") self.assertEqual(parser2.label, "[0-9]+") self.assertEqual(parser3.label, "Lorem")
def test_ParserParseEverything_ReturnsSuccess(self): # Arrange txt = 'aabbaccca' parser_a = bpr.LParser('a') parser_b = bpr.LParser('b') parser_c = bpr.LParser('c') parser_d = bpr.LParser('d') # Act parser = cmb.many1(parser_a, parser_b, parser_c, parser_d) result = parser(txt) expected = res.Success(('aabbaccca', '')) # Assert self.assertEqual(result, expected)
def test_liftn_WhenOneArgumentFunctionIsGiven_liftIsReproduced(self): # Arrange def double(x): return 2 * x lifted_fn = res.Result.liftn(double) lifted_f = res.Result.lift(double) arg = res.Success(5) # Act result_f = lifted_f(arg) result_fn = lifted_fn(arg) # Assert self.assertEqual(result_f, result_fn)