Ejemplo n.º 1
0
    def test_apply_change_add(self):
        # Setup: Create a script file with a selection of test text
        sf = self._get_test_script_file()

        # If: I apply a change that adds text
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 1,
                    'character': 1
                },
                'end': {
                    'line': 3,
                    'character': 1
                }
            },
            'text':
            '\r\npgsql\r\nis\r\nawesome\r\n'
        })
        sf.apply_change(params)

        # Then:
        # ... The text should have updated
        expected_result = ['abc', 'd', 'pgsql', 'is', 'awesome', 'lm']
        self.assertListEqual(sf.file_lines, expected_result)
Ejemplo n.º 2
0
    def test_apply_change_invalid_position(self):
        # Setup: Create a script file with a selection of test text
        sf = self._get_test_script_file()

        # If: I apply a change at an invalid part of the document
        # Then:
        # ... I should get an exception throws
        with self.assertRaises(ValueError):
            params = TextDocumentChangeEvent.from_dict({
                'range': {
                    'start': {
                        'line': 1,
                        'character': -1
                    },  # Invalid character
                    'end': {
                        'line': 3,
                        'character': 1
                    }
                },
                'text': ''
            })
            sf.apply_change(params)

        # ... The text should remain the same
        self.assertListEqual(sf.file_lines,
                             self._get_test_script_file().file_lines)
Ejemplo n.º 3
0
    def test_apply_change_remove_line(self):
        """Test removing an entire line from the script file by selecting the line"""
        # Set up the test file and parameters to remove a line from the file
        script_file = self._get_test_script_file()

        # If I remove a line from the file
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 1,
                    'character': 0
                },
                'end': {
                    'line': 2,
                    'character': 0
                }
            },
            'text': ''
        })
        script_file.apply_change(params)

        # Then the text should have updated without a validation error
        expected_result = ['abc', 'ghij', 'klm']
        self.assertListEqual(script_file.file_lines, expected_result)
Ejemplo n.º 4
0
    def test_apply_change_add_character(self):
        """Test applying a change by adding a single character to the end of an existing line"""
        # Set up the test file and parameters to add a character at the end of the first line
        script_file = self._get_test_script_file()

        # If I add a single character to the end of a line
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 0,
                    'character': 3
                },
                'end': {
                    'line': 0,
                    'character': 3
                }
            },
            'text': 'a'
        })
        script_file.apply_change(params)

        # Then the text should have updated without a validation error
        expected_result = ['abca', 'def', 'ghij', 'klm']
        self.assertListEqual(script_file.file_lines, expected_result)
Ejemplo n.º 5
0
    def test_apply_change_remove(self):
        # Setup: Create a script file with a selection of test text
        sf = self._get_test_script_file()

        # If: I apply a change that removes the text
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 1,
                    'character': 1
                },
                'end': {
                    'line': 3,
                    'character': 1
                }
            },
            'text': '1'
        })
        sf.apply_change(params)

        # Then:
        # ... The text should have updated
        expected_result = ['abc', 'd1lm']
        self.assertListEqual(sf.file_lines, expected_result)
Ejemplo n.º 6
0
    def test_apply_change_add_parentheses(self):
        """Test applying a change that simulates typing parentheses and putting text between them"""
        # Set up the test file and parameters to add a character at the end of the first line
        script_file = self._get_test_script_file()

        # If I add parentheses to the end of a line...
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 0,
                    'character': 3
                },
                'end': {
                    'line': 0,
                    'character': 3
                }
            },
            'text': '()'
        })
        script_file.apply_change(params)

        expected_result = ['abc()', 'def', 'ghij', 'klm']
        self.assertListEqual(script_file.file_lines, expected_result)

        # And then type inside the parentheses
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 0,
                    'character': 4
                },
                'end': {
                    'line': 0,
                    'character': 4
                }
            },
            'text': 'a'
        })
        script_file.apply_change(params)

        expected_result = ['abc(a)', 'def', 'ghij', 'klm']
        self.assertListEqual(script_file.file_lines, expected_result)

        # And then overwrite the last parenthesis
        params = TextDocumentChangeEvent.from_dict({
            'range': {
                'start': {
                    'line': 0,
                    'character': 5
                },
                'end': {
                    'line': 0,
                    'character': 6
                }
            },
            'text': ')'
        })
        script_file.apply_change(params)

        # Then the text should have updated without a validation error
        expected_result = ['abc(a)', 'def', 'ghij', 'klm']
        self.assertListEqual(script_file.file_lines, expected_result)