def test_parse_with_incorrect_input_file(self): mock_open = mock.mock_open(read_data=self.INCORRECT_INPUT_FILE) with mock.patch('main.open', mock_open, create=True): with self.assertRaises(Exception) as e: parser = Parser() parser.parse('') error_msg = e.exception self.assertEquals(str(error_msg), Parser.INCORRECT_FORMAT_OF_FILE_ERROR_MSG)
def test_parse_with_incorrect_number_of_queries(self): mock_open = mock.mock_open(read_data=self.INCORRECT_NUMBER_OF_QUERIES_INPUT_FILE) with mock.patch('main.open', mock_open, create=True): with self.assertRaises(Exception) as e: parser = Parser() parser.parse('') error_msg = e.exception self.assertEquals(str(error_msg), Parser.INCORRECT_NUMBER_OF_QUERIES_ERROR_MSG)
class TestParser(unittest.TestCase): def setUp(self): self.parser = Parser() def test_start_parsing(self): self.assertTrue(True) def test_get_response_fail(self): self.assertTrue(True==self.parser._original(get_response('com', None))) def test_register_session(self): self.assertTrue(True) def test_find_value(self): self.assertTrue(True) def test_get_article(self): self.assertTrue(True) def test_get_post_time(self): self.assertTrue(True) def test_parse_page(self): self.assertTrue(True)
def test_parse_correct_input_file(self): mock_open = mock.mock_open(read_data=self.CORRECT_INPUT_FILE) with mock.patch('main.open', mock_open, create=True): parser = Parser() parser.parse('') self.assertEqual(parser.cases_list.__len__(), 1) self.assertEqual(parser.cases_list[0].city.dx, 4) self.assertEqual(parser.cases_list[0].city.dy, 4) self.assertEquals(parser.cases_list[0].coffee_shops.__len__(), 5) self.assertIn((1, 1), parser.cases_list[0].coffee_shops) self.assertIn((1, 2), parser.cases_list[0].coffee_shops) self.assertIn((3, 3), parser.cases_list[0].coffee_shops) self.assertIn((4, 4), parser.cases_list[0].coffee_shops) self.assertIn((2, 4), parser.cases_list[0].coffee_shops) self.assertEquals(parser.cases_list[0].queries.__len__(), 3) self.assertIn(1, parser.cases_list[0].queries) self.assertIn(2, parser.cases_list[0].queries) self.assertIn(4, parser.cases_list[0].queries)
def runtests(amount=1): for i in range(amount): methods = ['*', '/', '+', '-'] x = random.randint(-100, 100) m = random.choice(methods) y = random.randint(-100, 100) if y == 0: y = 1 test = f'{x} {m} {y}' print(f'Running test on {test}') myres = Parser().parse(Lexer().lex(test)).calculate().to_actual_type() actualres = eval(test) print(myres) if myres != actualres: raise Exception(f'Results differ, {myres} {actualres}')
class TestParser(unittest.TestCase): def setUp(self): self.parse = Parser() def test_input_file(self): file_text = "./example.json.txt" with open(file_text) as f: self.parse.file_input(f) expected = '[{"id":"test","score":12,"ip":"1.2.3.4","message":"Hi"},' \ '{"id":"test","score":5,"ip":"1.2.3.5"},' \ '{"id":"test","score":17,"ip":"1.2.3.4"},' \ '{"id":"test2","score":9,"ip":"1.2.3.4"}]' json_expectation = json.loads(expected) self.assertEqual(json_expectation, self.parse.object_list) def test_data_in(self): string_input = '{"test": "id"}' result = self.parse.data_in(string_input) self.assertEqual(201, result[0]) def test_data_in_not_json(self): not_json = "[}" result = self.parse.data_in(not_json) # self.assertRaises(JSONDecodeError, "ValueError not raised.") self.assertRaises(ValueError) def test_result_out(self): self.parse.data_in( '{"id":"test","score":12,"ip":"1.2.3.4","message":"Hi"}') self.parse.data_in('{"id":"test","score":5,"ip":"1.2.3.5"}') expected = "test\n{'1.2.3.4': 1, '1.2.3.5': 1}\nscore: 17\n" self.assertEqual(expected, str(self.parse)) def test_merge_identifiers(self): obj_list = [ dict(id="test", score=12, ip="1.2.3.4", message="Hi"), dict(id="test", score=12, ip="1.2.3.4", message="Hi"), dict(id="test2", score=24, ip="1.2.3.5") ] results = self.parse.merge_identifiers(obj_list) expected = { 'test': { 'ips': { '1.2.3.4': 2 }, 'score': 24 }, 'test2': { 'ips': { '1.2.3.5': 1 }, 'score': 24 } } self.assertEqual(expected, results, "Merge identifiers not equal did something change?") def test_merge_ip_addresses(self): obj_dictionary = ['1.2.3.4', '1.2.3.5'] results = self.parse.merge_ip_addresses(obj_dictionary) expected = {'1.2.3.4': 1, '1.2.3.5': 1} self.assertEqual(expected, results) def add_to_output(self): json_test = dict(id="test", score=12, ip="1.2.3.4", message="Hi") parse = Parser() parse.add_to_output(json_test) self.assertEqual(parse.object_list[-1], json_test) def tearDown(self) -> None: ...
def test_sample_1(self): regular = 'ab+c.aba.*.bac.+.+*' k = 3 x = Parser(regular, k) assert [x.get_answer(i) for i in range(k)] == [0, 4, 2]
from main import Lexer, Parser #shows lexer working ''' lexer = Lexer() lexed = lexer.lex("2.2 + 2") print(lexed.as_raw()) print(lexed.as_tuple()) print(lexed.as_list()) print(lexed.as_dict()) ''' #calculator using lexer and parser while True: a = input("Calc:") b = Parser().parse(Lexer().lex(a)) print(b.as_raw()) print(b.calculate().token) #New func that returns formatted string ''' while True: a=input("Lex:") b = Lexer().lex(a) print(b.as_strf('Value: %v Type: %t') '''
def test_sample_2(self): regular = 'acb..bab.c.*.ab.ba.+.+*a.' k = 3 x = Parser(regular, k) assert [x.get_answer(i) for i in range(k)] == ['INF', 1, 'INF']
def setUp(self): self.parser = Parser()
from main import encrypt, Lexer, Parser, File answer = input(""" Hey Pr0, What you wanna do(?) Press 1 for Encrypting text Press 2 for Decrypting text \n """) if answer == "1": text = input("Tell me what you wanna encrypt: ") new_l = Lexer(encrypt(text)) new_l.create_encrypted() new_l.create_schema() print(new_l.generate_files()) elif answer == "2": enc = input("Enter the exact path of the encrypted file: ") sch = input("Enter the exact path of the schema file: ") dec = Parser(File(enc).read(), File(sch).read()) print("Here goes the decrypted string" + "\n" + "==========================HALO10=============================" + "\n" + dec.decrypt()) else: print("Simp.. that's an incorrect try")
lexed = lexer.lex("2.2 + 2") print(lexed.as_raw()) print(lexed.as_tuple()) print(lexed.as_list()) print(lexed.as_dict()) ''' #reads from file and prints line by line results ''' a=Lexer().lex_lbl('example.txt') b = Parser().parse_lbl(a) print(b.ln_res()) ''' #calculator using lexer and parser while True: userinput = input("Calc:") parseres = Parser().parse(Lexer().lex(userinput)) calcres = parseres.calculate() if calcres.type != 'RES_SETVAR': print(calcres.token) #Func that returns formatted string ''' while True: a=input("Lex:") b = Lexer().lex(a) print(b.as_strf('Value: %v Type: %t Pos: (%s,%e)')) '''
def products(self): xml = minidom.parse("RatesHistory.xml") return Parser(xml).parse_products()
def main(): with open('app\\tests\\test.do', 'r') as f: content = f.read() tokens = Lexer(content).tokenize() Parser(tokens).parse()
""" Example batch script. This one want as input a directory that has txt and fact files. """ import os, sys, glob from main import Parser INDIR = sys.argv[1] OUTDIR = 'data/tmp' for text_file in glob.glob("%s/*.txt" % INDIR): basename = os.path.basename(text_file) fact_file = text_file[:-3] + 'fact' sect_file = os.path.join(OUTDIR, basename[:-3] + 'sect') if os.path.exists(fact_file): #if not basename == 'USPP021257P2.txt': # continue print basename parser = Parser() parser.collection = 'LEXISNEXIS' parser.process_file(text_file, fact_file, sect_file, verbose=False)
def test_fake_regular(self): regular = '*' k = 4 x = Parser(regular, k) with pytest.raises(ParserException): x.get_answer(3)
def test_7(self): regular = 'aa.bbb..+*' k = 10 x = Parser(regular, k) assert [x.get_answer(i) for i in range(k)] == [0, 11, 2, 3, 4, 5, 6, 7, 8, 9]
def test_6(self): regular = 'ab.' k = 4 x = Parser(regular, k) assert [x.get_answer(i) for i in range(k)] == ['INF', 'INF', 2, 'INF']
def test_3(self): regular = 'aba.*.a.*ab1+..' k = 4 x = Parser(regular, k) assert [x.get_answer(i) for i in range(k)] == [4, 1, 2, 3]
from main import Lexer, Parser #shows lexer working ''' lexer = Lexer() lexed = lexer.lex("2.2 + 2") print(lexed.as_raw()) print(lexed.as_tuple()) print(lexed.as_list()) print(lexed.as_dict()) ''' #calculator using lexer and parser ''' while True: a=input("Calc:") print(Parser().parse(Lexer().lex(a)).calculate().token) ''' while True: a=input("Calc:") print(Parser().parse(Lexer().lex(a)).calculate().token)
def add_to_output(self): json_test = dict(id="test", score=12, ip="1.2.3.4", message="Hi") parse = Parser() parse.add_to_output(json_test) self.assertEqual(parse.object_list[-1], json_test)
def test_parser_get_ground_size_and_robot_first_pos(): p = Parser("commands.txt") ground_size = p.get_ground_size() assert (10,10) == ground_size robot_first_pos = p.get_robot_first_pos() assert ("N", 2, 5) == robot_first_pos
import sys from main import Lexer, Parser from utils import VERSION ##try: ## tmp = sys.argv[1] ##except: ## raise Exception("This program is a CLI application only. Please specify args.") from None ##my_text = " ".join(sys.argv[1:]) ##l = Lexer().lex(my_text) ##parseres = Parser().parse(l) ##calcres = parseres.calculate() ##print(calcres.token,end="") if "pythonw.exe" not in sys.executable: print(f"CSLang version {VERSION}.") while True: text = input(">>>") if text == "" or text.lower() == "exit": break l = Lexer().lex(text) parseres = Parser().parse(l) calcres = parseres.calculate() print(calcres.token) else: raise Exception("This program is a CLI application only.") from None