def test_evaluate_aggregation_first_with_inheritance(self): text = """ |Room size >> Kitchen <>-cupboard--> Cupboard |color |show_kitchen() """ lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.evaluate() # Main class diagram main = diagram.main self.assertEqual(main.name, "Kitchen") self.assertEqual(main.methods, ["show_kitchen()"]) self.assertEqual(main.fields, ["cupboard", "color"]) # Parent class diagram parent = diagram.parent self.assertEqual(parent.name, "Room") self.assertEqual(parent.fields, ["size"]) # Aggregation diagram aggregation = diagram.aggregation self.assertIsInstance(aggregation, AggregationDiagramPart) self.assertEqual(aggregation.name, "cupboard") self.assertEqual(aggregation.left_multiplicity, "") self.assertEqual(aggregation.right_multiplicity, "") # Aggregated class diagram aggregated = diagram.aggregated self.assertIsInstance(aggregated, ClassDiagramPart) self.assertEqual(aggregated.name, "Cupboard")
def run(self, edit): # Get the current selection or the line where the cursor is line = self.view.line(self.view.sel()[0]) easy_entry = self.view.substr(line) # Parse and interpret the tokens and create a diagram object try: lexer = Lexer(easy_entry) interpreter = Interpreter(lexer) diagram = interpreter.evaluate() except InterpreterException as e: self.view.show_popup("Invalid PREXEL syntax - {}".format(e), sublime.HIDE_ON_MOUSE_MOVE_AWAY) else: # Cache some values that are needed by other methods # self.edit = edit self.diagram = diagram self.easy_entry = easy_entry self.line = line # Show popup menu to determine what to generate self.view.show_popup_menu([ "Generate UML", "Generate Source", "Generate Both UML and Source" ], self.on_done)
def test_evaluate_advanced(self): text = "|Room size >> Kitchen color square_feet show_kitchen() " \ "<>*-cupboards--1> Cupboard open()" lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.evaluate() # Main class diagram main = diagram.main self.assertEqual(main.name, "Kitchen") self.assertEqual(main.methods, ["show_kitchen()"]) self.assertEqual(main.fields, ["color", "square_feet", "cupboards"]) # Inheritance diagram inheritance = diagram.inheritance self.assertIsInstance(inheritance, InheritanceDiagramPart) # Inherited class diagram parent = diagram.parent self.assertEqual(parent.name, "Room") self.assertIsInstance(parent, ClassDiagramPart) # Aggregation diagram aggregation = diagram.aggregation self.assertIsInstance(aggregation, AggregationDiagramPart) self.assertEqual(aggregation.left_multiplicity, "*") self.assertEqual(aggregation.right_multiplicity, "1") # Aggregated class diagram aggregated = diagram.aggregated self.assertEqual(aggregated.name, "Cupboard") self.assertEqual(aggregated.methods, ["open()"])
def test_full(self): text = "|Room >> Kitchen color square_feet show_kitchen() " \ "<>*-cupboards--1> Cupboard open()" lexer = Lexer(text) # Interpret the tokens and create a diagram object try: interpreter = Interpreter(lexer) diagram = interpreter.evaluate() except InterpreterException as e: print(e) # Pretty-print encode diagram for output to the view pretty_print = PrettyPrintEncoder() result = pretty_print.generate(diagram) print(result) # Source-code encode diagram for files source_code = SourceCodeEncoder() classes = source_code.generate(diagram) for class_item in classes: print(class_item)
def test_class_name(self): """ Test the class_name() method, which processes a CLASS_NAME token. """ text = "|Airplane <>-wings--> Wing" lexer = Lexer(text) interpreter = Interpreter(lexer) interpreter.start_marker() name = interpreter.class_name() self.assertEqual(name, "Airplane")
def test_evaluate_error(self): text = "|Kitchen color square_feet show_kitchen() <>-cupboards-->" lexer = Lexer(text) interpreter = Interpreter(lexer) # Test error message with self.assertRaises(InterpreterException) as context: interpreter.evaluate() self.assertEqual(context.exception.args[0], "There is no class name following the aggregation.")
def test_start_marker(self): """ Test the start_marker() method, which processes a START_MARKER token. """ text = "|Airplane <>-wings--> Wing" lexer = Lexer(text) interpreter = Interpreter(lexer) interpreter.start_marker() self.assertEqual(interpreter.current_token.value, "Airplane") self.assertEqual(interpreter.current_token.type, Token.CLASS_NAME)
def test_evaluate(self): text = "|Kitchen color square_feet show_kitchen()" lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.evaluate() main = diagram.main self.assertIsInstance(main, ClassDiagramPart) self.assertEqual(main.name, "Kitchen") self.assertEqual(main.methods, ["show_kitchen()"]) self.assertEqual(main.fields, ["color", "square_feet"])
def test_init(self): """ Test the __init__() method. """ text = "|Airplane <>-wings--> Wing" lexer = Lexer(text) interpreter = Interpreter(lexer) self.assertEqual(interpreter.current_token.type, Token.START_MARKER) self.assertEqual(interpreter.current_token.value, "|")
def test_evaluate_aggregation_first(self): text = "|TaskList <>-tasks----*> Task \n |get_the_tasks()" lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.evaluate() # Main class diagram main = diagram.main self.assertEqual(main.name, "TaskList") self.assertEqual(main.methods, ["get_the_tasks()"]) # Aggregation diagram aggregation = diagram.aggregation self.assertIsInstance(aggregation, AggregationDiagramPart) self.assertEqual(aggregation.left_multiplicity, "") self.assertEqual(aggregation.right_multiplicity, "*") # Aggregated class diagram aggregated = diagram.aggregated self.assertIsInstance(aggregated, ClassDiagramPart) self.assertEqual(aggregated.name, "Task")
def test_class_body(self): """ Test the class_body() method, which processes FIELD and METHOD tokens """ text = "|Airplane size color take_off()" lexer = Lexer(text) interpreter = Interpreter(lexer) interpreter.start_marker() class_diagram = ClassDiagramPart() class_diagram.name = interpreter.class_name() interpreter.class_body(class_diagram) self.assertEqual(class_diagram.fields, ["size", "color"]) self.assertEqual(class_diagram.methods, ["take_off()"])
def test_aggregation_multi_line(self): """ Test the aggregation() method using multi-line syntax """ text = """ |Kitchen <>-cupboard--> Cupboard |size |color""" lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.diagram class_diagram_part = ClassDiagramPart() interpreter.start_marker() class_diagram_part.name = interpreter.class_name() diagram.main = class_diagram_part interpreter.aggregation(include_following_tokens=False) interpreter.class_body(diagram.main) self.assertEqual(diagram.aggregation.name, "cupboard") self.assertEqual(diagram.main.fields, ["cupboard", "size", "color"]) self.assertEqual(diagram.aggregated.name, "Cupboard")
def test_inheritance_with_error(self): """ Test the inheritance() method, with an improper syntax """ text = "|Kitchen >>" lexer = Lexer(text) interpreter = Interpreter(lexer) class_diagram_part = ClassDiagramPart() interpreter.start_marker() class_diagram_part.name = interpreter.class_name() # Should raise a InterpreterException with self.assertRaises(InterpreterException) as context: interpreter.inheritance() self.assertEqual(context.exception.args[0], "Missing child class after \">>\"")
def test_aggregation_with_missing_aggregation_name(self): """ Test the aggregation() method, """ text = "|Kitchen <>---> Cupboard" lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.diagram class_diagram_part = ClassDiagramPart() interpreter.start_marker() class_diagram_part.name = interpreter.class_name() diagram.main = class_diagram_part interpreter.aggregation() self.assertEqual(diagram.aggregation.name, "cupboard") self.assertEqual(diagram.main.fields, ["cupboard"]) self.assertEqual(diagram.aggregated.name, "Cupboard")
def test_inheritance(self): """ Test the inheritance() method, which processes an inheritance relationship """ text = "|Room height width >> Kitchen" lexer = Lexer(text) interpreter = Interpreter(lexer) diagram = interpreter.diagram class_diagram_part = ClassDiagramPart() interpreter.start_marker() class_diagram_part.name = interpreter.class_name() interpreter.class_body(class_diagram_part) if interpreter.inheritance(): diagram.parent = class_diagram_part else: diagram.main = class_diagram_part self.assertEqual(diagram.parent.name, "Room") self.assertEqual(diagram.parent.fields, ["height", "width"]) self.assertEqual(diagram.main.name, "Kitchen")
def test_process_token(self): """ Test the process_token() method which processes one token at a time. Also includes a test to confirm an error message is given if an improper token is given. """ text = "|Airplane <>-wings--> Wing" lexer = Lexer(text) interpreter = Interpreter(lexer) interpreter.process_token(Token.START_MARKER) # Assert the current token is a CLASS_NAME self.assertEqual(interpreter.current_token.type, Token.CLASS_NAME) # Test error message is raised when the incorrect token processed with self.assertRaises(InterpreterException) as context: interpreter.process_token(Token.FIELD) self.assertTrue('Invalid Syntax' in str(context.exception))
def run(self, edit): # Get the current selection or the line where the cursor is line = self.view.line(self.view.sel()[0]) easy_entry3 = self.view.substr(line) List1 = [] List2 = [] List3 = [] List4 = [] pattern1 = re.compile('>>') pattern2 = re.compile('<>') #print(text) #text2 = text.splitlines() text2 = easy_entry3.splitlines() #text2 = list(filter(None, text2)) print(text2) text2 = [x for x in text2 if '|' in x] print(text2) text2 = ''.join(text2) print(text2) text2 = text2.split('|') text2 = list(filter(None, text2)) for i in text2: if bool(pattern1.search(i)) == True or bool( pattern2.search(i)) == True: List1 = i.split(" ") else: text3 = i text4 = i.split(" ") List2.append(text4) List4.append(text3) #print(text4) print(List1) print(List2) print(List3) print(List4) for i in List2: List3.append(i[0]) print(List3) #print(List4) #for i in List3: if not List1: for i in List3: for j in List4: if i in j: # print(j) List1.append(j) #print(List1) else: for i in List3: cmp = List1.index(i) List1[cmp] = List4[List3.index(i)] easy_entry2 = ' '.join(List1) easy_entry = '|' + easy_entry2 print(easy_entry) # Parse and interpret the tokens and create a diagram object try: lexer = Lexer(easy_entry) interpreter = Interpreter(lexer) diagram = interpreter.evaluate() except InterpreterException as e: self.view.show_popup("Invalid PREXEL syntax - {}".format(e), sublime.HIDE_ON_MOUSE_MOVE_AWAY) else: # Cache some values that are needed by other methods # self.edit = edit self.diagram = diagram self.easy_entry = easy_entry self.line = line # Show popup menu to determine what to generate self.view.show_popup_menu([ "Generate UML", "Generate Source", "Generate Both UML and Source" ], self.on_done)