Exemple #1
0
    def test_get_token_with_comma(self):
        """
        Test the get_token() method, which turns a token string into a Token object
        """
        text = "|Room >> Kitchen, LivingRoom, Bathroom"
        lexer = Lexer(text)

        # Check that the token is a PREXEL marker
        self.assertEqual(lexer.get_token().type, Token.START_MARKER)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is a inheritance marker
        self.assertEqual(lexer.get_token().type, Token.INHERITANCE)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is a comma
        self.assertEqual(lexer.get_token().type, Token.COMMA)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is a comma
        self.assertEqual(lexer.get_token().type, Token.COMMA)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)
Exemple #2
0
    def test_step(self):
        """
        Test the step() method, which steps forward in the string one character.
        """
        text = "|Kitchen get_cabinet()"
        lexer = Lexer(text)

        for _ in range(5):
            lexer.step()

        self.assertEqual(lexer.current, "h")
Exemple #3
0
    def test_step_end_of_text(self):
        """
        Test the step() method going all the way to the end of the string.
        """
        text = "|Kitchen"
        lexer = Lexer(text)

        for _ in range(8):
            lexer.step()

        self.assertEqual(lexer.current, None)
Exemple #4
0
    def test_get_token_ignore_reserved_characters(self):
        """
        Test the get_token() method with reserved characters
        """
        text = "|Room >> Kitchen|, arrange_kitchen()||,"
        lexer = Lexer(text)

        self.assertEqual(lexer.get_token().type, Token.START_MARKER)
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)
        self.assertEqual(lexer.get_token().type, Token.INHERITANCE)

        # "Kitchen"
        token = lexer.get_token()
        self.assertEqual(token.type, Token.CLASS_NAME)
        self.assertEqual(token.value, "Kitchen")

        # Secondary START_MARKER tokens are ignored, so COMMA token is next
        self.assertEqual(lexer.get_token().type, Token.COMMA)

        # "arrange_kitchen()"
        token = lexer.get_token()
        self.assertEqual(token.type, Token.METHOD)
        self.assertEqual(token.value, "arrange_kitchen()")

        # Secondary START_MARKER tokens are ignored, so COMMA token is next
        self.assertEqual(lexer.get_token().type, Token.COMMA)
Exemple #5
0
    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")
Exemple #6
0
    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()"])
Exemple #7
0
    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)
Exemple #8
0
    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)
            
Exemple #9
0
    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, "|")
Exemple #10
0
    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.")
Exemple #11
0
    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")
Exemple #12
0
    def test_generate_token_string(self):
        """
        Test the generate_token_string() method, which creates strings that will be turned into Token object
        """
        text = "|Kitchen color square_feet show_kitchen()"
        lexer = Lexer(text)

        # Skip "|"
        lexer.step()
        self.assertEqual(lexer.generate_token_string(), "Kitchen")

        # Skip whitespace between "Kitchen" and "color"
        lexer.skip_whitespace()
        self.assertEqual(lexer.generate_token_string(), "color")
Exemple #13
0
    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"])
Exemple #14
0
    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)
Exemple #15
0
    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()"])
Exemple #16
0
    def test_get_token_skip_extra_prexel_markets(self):
        """
        Test the get_token() method with extra PREXEL markers
        """
        text = "|Room >> Kitchen \n|arrange_kitchen()\n|place_floor_cabinet()"
        lexer = Lexer(text)

        lexer.get_token()  # PREXEL marker
        lexer.get_token()  # "Room"
        lexer.get_token()  # ">>"
        lexer.get_token()  # "Kitchen"

        # Skip extra "|" tokens
        self.assertEqual(lexer.get_token().type, Token.METHOD)
        self.assertEqual(lexer.get_token().type, Token.METHOD)
Exemple #17
0
    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")
Exemple #18
0
    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 \">>\"")
Exemple #19
0
    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))
Exemple #20
0
    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")
Exemple #21
0
    def test_get_token(self):
        """
        Test the get_token() method, which turns a token string into a Token object
        """
        text = "|Kitchen color square_feet show_kitchen()"
        lexer = Lexer(text)

        # Check that the token is a PREXEL marker
        self.assertEqual(lexer.get_token().type, Token.START_MARKER)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is a field
        self.assertEqual(lexer.get_token().type, Token.FIELD)

        # Check that the token is a field
        self.assertEqual(lexer.get_token().type, Token.FIELD)

        # Check that the token is a method
        self.assertEqual(lexer.get_token().type, Token.METHOD)
Exemple #22
0
    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")
Exemple #23
0
    def test_get_token_with_inheritance(self):
        """
        Test the get_token() method with an inheritance Token
        """
        text = "|Room >> Kitchen"
        lexer = Lexer(text)

        # Check that the token is a PREXEL marker
        self.assertEqual(lexer.get_token().type, Token.START_MARKER)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is an inheritance token
        self.assertEqual(lexer.get_token().type, Token.INHERITANCE)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)
Exemple #24
0
    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")
Exemple #25
0
    def test_get_token_with_aggregation_error(self):
        """
        Test the get_token() method with an aggregation Token that is not correct
        """
        text = "|Airplane length <> Wing"
        lexer = Lexer(text)

        # Check that the token is a prexel marker
        self.assertEqual(lexer.get_token().type, Token.START_MARKER)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is a field
        self.assertEqual(lexer.get_token().type, Token.FIELD)

        # Check that token is the "Wing" values and not "<>"
        # which should have been skipped
        token = lexer.get_token()
        self.assertEqual(token.type, Token.CLASS_NAME)
        self.assertEqual(token.value, "Wing")
Exemple #26
0
    def test_get_token_with_aggregation(self):
        """
        Test the get_token() method with an aggregation Token
        """
        text = "|Airplane <>-wings--> Wing"
        lexer = Lexer(text)

        # Check that the token is a PREXEL marker
        self.assertEqual(lexer.get_token().type, Token.START_MARKER)

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)

        # Check that the token is an aggregation
        token = lexer.get_token()
        self.assertEqual(token.type, Token.AGGREGATION)
        self.assertEqual(token.value["left_multi"], "")
        self.assertEqual(token.value["name"], "wings")
        self.assertEqual(token.value["right_multi"], "")

        # Check that the token is a class name
        self.assertEqual(lexer.get_token().type, Token.CLASS_NAME)
Exemple #27
0
    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)
Exemple #28
0
    def test_skip_whitespace(self):
        """
        Test the skip_whitespace() method, which will advance the self.current pointer past
        all whitespace character until the next non-whitespace character.
        """
        text = "|Kitchen    color \n\n size"
        lexer = Lexer(text)

        # Skip "|"
        lexer.step()

        # Get first token, e.g., "Kitchen"
        lexer.generate_token_string()

        # Skip the following whitespace
        lexer.skip_whitespace()

        # Assert correct character at current pointer
        self.assertEqual(lexer.current, "c")

        # Get second token, e.g., "color"
        lexer.generate_token_string()

        # Skip the newline character
        lexer.skip_whitespace()

        # Assert correct character at current pointer
        self.assertEqual(lexer.current, "s")