aboutsummaryrefslogtreecommitdiff
path: root/tests/test_streaming_lexer.py
blob: cd03513094faa94b3c66123ebd50bd790fad1213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import re
from typing import Optional, Tuple
from enum import Enum, auto
from dataclasses import dataclass

import pytest

from src.gpt_chat_cli.streaming_lexer import (
    MatchState,
    CodeFenceContext,
    _try_to_parse_code_fence,
    SinglePassStreamingLexer,
    Token,
    TokenType,
    TokenOrientation,
    make_text_token
)

def test_try_to_parse_code_fence():
    # Test valid cases
    valid_cases = [
        ("```python\nhe", CodeFenceContext(0, "python", 10)),
        ("  ```python\n", CodeFenceContext(2, "python", 12)),
        ("~~~python\n", CodeFenceContext(0, "python", 10)),
        ("   ~~~python\nmore", CodeFenceContext(3, "python", 13))
    ]


    for case, expected in valid_cases:
        result = _try_to_parse_code_fence(case)
        assert result[0] == MatchState.MATCH
        assert result[1] == expected

    # Test invalid cases
    invalid_cases = [
        "    ```python\n",
        "~``python\n",
        "```python ```\n",
        "~~~python ~~~\n",
    ]

    for case in invalid_cases:
        print(case)
        result = _try_to_parse_code_fence(case)
        assert result[0] == MatchState.MISMATCH

    # Test indeterminate case
    indeterminate_cases = [
        "```",
        "  ~~~",
    ]

    for case in indeterminate_cases:
        result = _try_to_parse_code_fence(case)
        assert result[0] == MatchState.INDETERMINATE

def _check_exact_lexing_matches( chunk_tokens, final_tokens ):

    lexer = SinglePassStreamingLexer()

    for ( chunk, expected_tokens ) in chunk_tokens:

        lexer.add_chunk( chunk )

        n_tokens_emitted = 0

        for i, token in enumerate(lexer.parse()):
            assert i < len(expected_tokens)
            assert expected_tokens[i] == token

            n_tokens_emitted += 1

        assert n_tokens_emitted == len(expected_tokens)

    lexer.finish()

    n_tokens_emitted = 0

    for i, token in enumerate(lexer.parse()):
        assert i < len(final_tokens)
        print(token)
        assert final_tokens[i] == token

        n_tokens_emitted += 1

    assert n_tokens_emitted == len(final_tokens)


def test_single_pass_lexing():

    cases = [
        ( 'Some text\n', [
            make_text_token( 'Some text\n' )
        ] ),
        ( 'More text\n', [
            make_text_token( 'More text\n' )
        ] ),
        ( '  Indented text\n', [
            make_text_token( '  Indented text\n' )
        ] ),
        ( '```python\n', [
            Token( TokenType.CODE_FENCE, TokenOrientation.BEGIN, 'python' )
        ] ),
        ( 'print("Hello")\n', [
            make_text_token( 'print("Hello")\n' )
        ] ),
        ( '```', [] ),
    ]

    final_tokens = [
        Token( TokenType.CODE_FENCE, TokenOrientation.END ),
        Token( TokenType.EOF, TokenOrientation.NONE ),
    ]

    _check_exact_lexing_matches( cases, final_tokens )

    cases = [
        ( '```java\nSome text\nMore ', [
            Token( TokenType.CODE_FENCE, TokenOrientation.BEGIN, 'java' ),
            make_text_token( 'Some text\n' ),
            make_text_token( 'More ' ),
        ] ),
        ( ' text\n```', [
            make_text_token( ' text\n' ),
        ] ),
        ( '\n', [
            Token( TokenType.CODE_FENCE, TokenOrientation.END )
        ]),
    ]

    final_tokens = [
        Token( TokenType.EOF, TokenOrientation.NONE ),
    ]

    _check_exact_lexing_matches( cases, final_tokens )

    cases = [
        ( '  ```java \n  Some text\n  More ', [
            Token( TokenType.CODE_FENCE, TokenOrientation.BEGIN, 'java' ),
            make_text_token( 'Some text\n' ),
            make_text_token( 'More ' ),
        ] ),
        ( '  text\n  ```', [
            make_text_token( '  text\n' ),
        ] ),
        ( '\n', [
            Token( TokenType.CODE_FENCE, TokenOrientation.END )
        ]),
    ]

    final_tokens = [
        Token( TokenType.EOF, TokenOrientation.NONE ),
    ]

    _check_exact_lexing_matches( cases, final_tokens )

    cases = [
        ( '  ``', []),
        ('` java \n  Some text\n  More ', [
            Token( TokenType.CODE_FENCE, TokenOrientation.BEGIN, 'java' ),
            make_text_token( 'Some text\n' ),
            make_text_token( 'More ' ),
        ] ),
        ( '  text\n  ```', [
            make_text_token( '  text\n' ),
        ] ),
        ( '\n', [
            Token( TokenType.CODE_FENCE, TokenOrientation.END )
        ]),
    ]

    final_tokens = [
        Token( TokenType.EOF, TokenOrientation.NONE ),
    ]

    _check_exact_lexing_matches( cases, final_tokens )

    # Ticks preceded by characters don't initiate a code block
    cases = [
        ( 'tick```java\nSome text\n', [
            make_text_token( 'tick```java\n' ),
            make_text_token( 'Some text\n' ),
        ] ),
    ]

    final_tokens = [
        Token( TokenType.EOF, TokenOrientation.NONE ),
    ]

    _check_exact_lexing_matches( cases, final_tokens )

    # Code blocks which are not terminated, terminate
    # at the end of the document
    cases = [
        ( '```java\nSome text\n', [
            Token( TokenType.CODE_FENCE, TokenOrientation.BEGIN, 'java' ),
            make_text_token( 'Some text\n' ),
        ] ),
    ]

    final_tokens = [
        Token( TokenType.CODE_FENCE, TokenOrientation.END ),
        Token( TokenType.EOF, TokenOrientation.NONE ),
    ]

    _check_exact_lexing_matches( cases, final_tokens )