File: test_cli_usage_lexer.py

package info (click to toggle)
tmuxp 1.64.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: python: 17,788; sh: 22; makefile: 6
file content (358 lines) | stat: -rw-r--r-- 10,151 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""Tests for cli_usage_lexer Pygments extension."""

from __future__ import annotations

import typing as t

import pytest
from cli_usage_lexer import (
    CLIUsageLexer,
    tokenize_usage,
)

# --- Helper to extract token type names ---


def get_tokens(text: str) -> list[tuple[str, str]]:
    """Get tokens as (type_name, value) tuples."""
    lexer = CLIUsageLexer()
    return [
        (str(tok_type), tok_value) for tok_type, tok_value in lexer.get_tokens(text)
    ]


# --- Token type fixtures ---


class TokenTypeFixture(t.NamedTuple):
    """Test fixture for verifying specific token types."""

    test_id: str
    input_text: str
    expected_token_type: str
    expected_value: str


TOKEN_TYPE_FIXTURES: list[TokenTypeFixture] = [
    TokenTypeFixture(
        test_id="usage_heading",
        input_text="usage:",
        expected_token_type="Token.Generic.Heading",
        expected_value="usage:",
    ),
    TokenTypeFixture(
        test_id="short_option",
        input_text="-h",
        expected_token_type="Token.Name.Attribute",
        expected_value="-h",
    ),
    TokenTypeFixture(
        test_id="long_option",
        input_text="--verbose",
        expected_token_type="Token.Name.Tag",
        expected_value="--verbose",
    ),
    TokenTypeFixture(
        test_id="long_option_with_dashes",
        input_text="--no-color",
        expected_token_type="Token.Name.Tag",
        expected_value="--no-color",
    ),
    TokenTypeFixture(
        test_id="uppercase_metavar",
        input_text="COMMAND",
        expected_token_type="Token.Name.Constant",
        expected_value="COMMAND",
    ),
    TokenTypeFixture(
        test_id="uppercase_metavar_with_underscore",
        input_text="FILE_PATH",
        expected_token_type="Token.Name.Constant",
        expected_value="FILE_PATH",
    ),
    TokenTypeFixture(
        test_id="positional_arg",
        input_text="repo-name",
        expected_token_type="Token.Name.Label",
        expected_value="repo-name",
    ),
    TokenTypeFixture(
        test_id="command_name",
        input_text="vcspull",
        expected_token_type="Token.Name.Label",
        expected_value="vcspull",
    ),
    TokenTypeFixture(
        test_id="open_bracket",
        input_text="[",
        expected_token_type="Token.Punctuation",
        expected_value="[",
    ),
    TokenTypeFixture(
        test_id="close_bracket",
        input_text="]",
        expected_token_type="Token.Punctuation",
        expected_value="]",
    ),
    TokenTypeFixture(
        test_id="pipe_operator",
        input_text="|",
        expected_token_type="Token.Operator",
        expected_value="|",
    ),
]


@pytest.mark.parametrize(
    TokenTypeFixture._fields,
    TOKEN_TYPE_FIXTURES,
    ids=[f.test_id for f in TOKEN_TYPE_FIXTURES],
)
def test_token_type(
    test_id: str,
    input_text: str,
    expected_token_type: str,
    expected_value: str,
) -> None:
    """Test individual token type detection."""
    tokens = get_tokens(input_text)
    # Find the expected token (skip whitespace)
    non_ws_tokens = [(t, v) for t, v in tokens if "Whitespace" not in t and v.strip()]
    assert len(non_ws_tokens) >= 1, f"No non-whitespace tokens found for '{input_text}'"
    token_type, token_value = non_ws_tokens[0]
    assert token_type == expected_token_type, (
        f"Expected {expected_token_type}, got {token_type}"
    )
    assert token_value == expected_value


# --- Short option with value fixtures ---


class ShortOptionValueFixture(t.NamedTuple):
    """Test fixture for short options with values."""

    test_id: str
    input_text: str
    option: str
    value: str


SHORT_OPTION_VALUE_FIXTURES: list[ShortOptionValueFixture] = [
    ShortOptionValueFixture(
        test_id="lowercase_value",
        input_text="-c config-path",
        option="-c",
        value="config-path",
    ),
    ShortOptionValueFixture(
        test_id="uppercase_value",
        input_text="-d DIRECTORY",
        option="-d",
        value="DIRECTORY",
    ),
    ShortOptionValueFixture(
        test_id="simple_value",
        input_text="-r name",
        option="-r",
        value="name",
    ),
]


@pytest.mark.parametrize(
    ShortOptionValueFixture._fields,
    SHORT_OPTION_VALUE_FIXTURES,
    ids=[f.test_id for f in SHORT_OPTION_VALUE_FIXTURES],
)
def test_short_option_with_value(
    test_id: str,
    input_text: str,
    option: str,
    value: str,
) -> None:
    """Test short option followed by value tokenization."""
    tokens = get_tokens(input_text)
    non_ws_tokens = [(t, v) for t, v in tokens if "Whitespace" not in t]

    assert len(non_ws_tokens) >= 2
    assert non_ws_tokens[0] == ("Token.Name.Attribute", option)
    # Value could be Name.Variable or Name.Constant depending on case
    assert non_ws_tokens[1][1] == value


# --- Long option with value fixtures ---


class LongOptionValueFixture(t.NamedTuple):
    """Test fixture for long options with = values."""

    test_id: str
    input_text: str
    option: str
    value: str


LONG_OPTION_VALUE_FIXTURES: list[LongOptionValueFixture] = [
    LongOptionValueFixture(
        test_id="uppercase_value",
        input_text="--config=FILE",
        option="--config",
        value="FILE",
    ),
    LongOptionValueFixture(
        test_id="lowercase_value",
        input_text="--output=path",
        option="--output",
        value="path",
    ),
]


@pytest.mark.parametrize(
    LongOptionValueFixture._fields,
    LONG_OPTION_VALUE_FIXTURES,
    ids=[f.test_id for f in LONG_OPTION_VALUE_FIXTURES],
)
def test_long_option_with_value(
    test_id: str,
    input_text: str,
    option: str,
    value: str,
) -> None:
    """Test long option with = value tokenization."""
    tokens = get_tokens(input_text)
    non_ws_tokens = [(t, v) for t, v in tokens if "Whitespace" not in t]

    assert len(non_ws_tokens) >= 3
    assert non_ws_tokens[0] == ("Token.Name.Tag", option)
    assert non_ws_tokens[1] == ("Token.Operator", "=")
    assert non_ws_tokens[2][1] == value


# --- Full usage string fixtures ---


class UsageStringFixture(t.NamedTuple):
    """Test fixture for full usage string tokenization."""

    test_id: str
    input_text: str
    expected_contains: list[tuple[str, str]]


USAGE_STRING_FIXTURES: list[UsageStringFixture] = [
    UsageStringFixture(
        test_id="simple_usage",
        input_text="usage: cmd [-h]",
        expected_contains=[
            ("Token.Generic.Heading", "usage:"),
            ("Token.Name.Label", "cmd"),
            ("Token.Punctuation", "["),
            ("Token.Name.Attribute", "-h"),
            ("Token.Punctuation", "]"),
        ],
    ),
    UsageStringFixture(
        test_id="mutually_exclusive",
        input_text="[--json | --ndjson | --table]",
        expected_contains=[
            ("Token.Name.Tag", "--json"),
            ("Token.Operator", "|"),
            ("Token.Name.Tag", "--ndjson"),
            ("Token.Operator", "|"),
            ("Token.Name.Tag", "--table"),
        ],
    ),
    UsageStringFixture(
        test_id="subcommand",
        input_text="usage: vcspull sync",
        expected_contains=[
            ("Token.Generic.Heading", "usage:"),
            ("Token.Name.Label", "vcspull"),
            ("Token.Name.Label", "sync"),
        ],
    ),
    UsageStringFixture(
        test_id="positional_args",
        input_text="[repo-name] [path]",
        expected_contains=[
            ("Token.Punctuation", "["),
            ("Token.Name.Label", "repo-name"),
            ("Token.Punctuation", "]"),
            ("Token.Punctuation", "["),
            ("Token.Name.Label", "path"),
            ("Token.Punctuation", "]"),
        ],
    ),
]


@pytest.mark.parametrize(
    UsageStringFixture._fields,
    USAGE_STRING_FIXTURES,
    ids=[f.test_id for f in USAGE_STRING_FIXTURES],
)
def test_usage_string(
    test_id: str,
    input_text: str,
    expected_contains: list[tuple[str, str]],
) -> None:
    """Test full usage string tokenization contains expected tokens."""
    tokens = get_tokens(input_text)
    for expected_type, expected_value in expected_contains:
        assert (expected_type, expected_value) in tokens, (
            f"Expected ({expected_type}, {expected_value!r}) not found in tokens"
        )


# --- Real vcspull usage output test ---


def test_vcspull_sync_usage() -> None:
    """Test real vcspull sync usage output tokenization."""
    usage_text = """\
usage: vcspull sync [-h] [-c CONFIG] [-d DIRECTORY]
                    [--json | --ndjson | --table] [--color {auto,always,never}]
                    [--no-progress] [--verbose]
                    [repo-name] [path]"""

    tokens = get_tokens(usage_text)

    # Check key elements are present
    # Note: DIRECTORY after -d is Name.Variable (option value), not Name.Constant
    expected = [
        ("Token.Generic.Heading", "usage:"),
        ("Token.Name.Label", "vcspull"),
        ("Token.Name.Label", "sync"),
        ("Token.Name.Attribute", "-h"),
        ("Token.Name.Attribute", "-c"),
        ("Token.Name.Variable", "CONFIG"),  # Option value, not standalone metavar
        ("Token.Name.Attribute", "-d"),
        ("Token.Name.Variable", "DIRECTORY"),  # Option value, not standalone metavar
        ("Token.Name.Tag", "--json"),
        ("Token.Name.Tag", "--ndjson"),
        ("Token.Name.Tag", "--table"),
        ("Token.Name.Tag", "--color"),
        ("Token.Name.Tag", "--no-progress"),
        ("Token.Name.Tag", "--verbose"),
        ("Token.Name.Label", "repo-name"),
        ("Token.Name.Label", "path"),
    ]

    for expected_type, expected_value in expected:
        assert (expected_type, expected_value) in tokens, (
            f"Expected ({expected_type}, {expected_value!r}) not in tokens"
        )


# --- tokenize_usage helper function test ---


def test_tokenize_usage_helper() -> None:
    """Test the tokenize_usage helper function."""
    result = tokenize_usage("usage: cmd [-h]")

    assert result[0] == ("Token.Generic.Heading", "usage:")
    assert ("Token.Name.Label", "cmd") in result
    assert ("Token.Name.Attribute", "-h") in result