summaryrefslogtreecommitdiff
path: root/tests/test_praat.py
blob: cced8983e3a7cb10539ec666286c35a1f5d92e56 (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
208
209
210
211
212
213
# -*- coding: utf-8 -*-
"""
    Praat lexer tests
    ~~~~~~~~~~~~~~~~~

    :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import pytest

from pygments.token import Token
from pygments.lexers import PraatLexer


@pytest.fixture(scope='module')
def lexer():
    yield PraatLexer()


def test_numeric_assignment(lexer):
    fragment = 'var = -15e4\n'
    tokens = [
        (Token.Text, 'var'),
        (Token.Text, ' '),
        (Token.Operator, '='),
        (Token.Text, ' '),
        (Token.Operator, '-'),
        (Token.Literal.Number, '15e4'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def testStringAssignment(lexer):
    fragment = 'var$ = "foo"\n'
    tokens = [
        (Token.Text, 'var$'),
        (Token.Text, ' '),
        (Token.Operator, '='),
        (Token.Text, ' '),
        (Token.Literal.String, '"'),
        (Token.Literal.String, 'foo'),
        (Token.Literal.String, '"'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_string_escaped_quotes(lexer):
    fragment = '"it said ""foo"""\n'
    tokens = [
        (Token.Literal.String, '"'),
        (Token.Literal.String, 'it said '),
        (Token.Literal.String, '"'),
        (Token.Literal.String, '"'),
        (Token.Literal.String, 'foo'),
        (Token.Literal.String, '"'),
        (Token.Literal.String, '"'),
        (Token.Literal.String, '"'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_function_call(lexer):
    fragment = 'selected("Sound", i+(a*b))\n'
    tokens = [
        (Token.Name.Function, 'selected'),
        (Token.Punctuation, '('),
        (Token.Literal.String, '"'),
        (Token.Literal.String, 'Sound'),
        (Token.Literal.String, '"'),
        (Token.Punctuation, ','),
        (Token.Text, ' '),
        (Token.Text, 'i'),
        (Token.Operator, '+'),
        (Token.Text, '('),
        (Token.Text, 'a'),
        (Token.Operator, '*'),
        (Token.Text, 'b'),
        (Token.Text, ')'),
        (Token.Punctuation, ')'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_broken_unquoted_string(lexer):
    fragment = 'printline string\n... \'interpolated\' string\n'
    tokens = [
        (Token.Keyword, 'printline'),
        (Token.Text, ' '),
        (Token.Literal.String, 'string'),
        (Token.Text, '\n'),
        (Token.Punctuation, '...'),
        (Token.Text, ' '),
        (Token.Literal.String.Interpol, "'interpolated'"),
        (Token.Text, ' '),
        (Token.Literal.String, 'string'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_inline_if(lexer):
    fragment = 'var = if true == 1 then -1 else 0 fi'
    tokens = [
        (Token.Text, 'var'),
        (Token.Text, ' '),
        (Token.Operator, '='),
        (Token.Text, ' '),
        (Token.Keyword, 'if'),
        (Token.Text, ' '),
        (Token.Text, 'true'),
        (Token.Text, ' '),
        (Token.Operator, '=='),
        (Token.Text, ' '),
        (Token.Literal.Number, '1'),
        (Token.Text, ' '),
        (Token.Keyword, 'then'),
        (Token.Text, ' '),
        (Token.Operator, '-'),
        (Token.Literal.Number, '1'),
        (Token.Text, ' '),
        (Token.Keyword, 'else'),
        (Token.Text, ' '),
        (Token.Literal.Number, '0'),
        (Token.Text, ' '),
        (Token.Keyword, 'fi'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolation_boundary(lexer):
    fragment = '"\'" + "\'"'
    tokens = [
        (Token.Literal.String, '"'),
        (Token.Literal.String, "'"),
        (Token.Literal.String, '"'),
        (Token.Text, ' '),
        (Token.Operator, '+'),
        (Token.Text, ' '),
        (Token.Literal.String, '"'),
        (Token.Literal.String, "'"),
        (Token.Literal.String, '"'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_numeric_indexed(lexer):
    fragment = "'a[3]'"
    tokens = [
        (Token.Literal.String.Interpol, "'a[3]'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_numeric_hash(lexer):
    fragment = "'a[\"b\"]'"
    tokens = [
        (Token.Literal.String.Interpol, "'a[\"b\"]'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_string_indexed(lexer):
    fragment = "'a$[3]'"
    tokens = [
        (Token.Literal.String.Interpol, "'a$[3]'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_string_hash(lexer):
    fragment = "'a$[\"b\"]'"
    tokens = [
        (Token.Literal.String.Interpol, "'a$[\"b\"]'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_numeric_with_precision(lexer):
    fragment = "'a:3'"
    tokens = [
        (Token.Literal.String.Interpol, "'a:3'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_indexed_numeric_with_precision(lexer):
    fragment = "'a[3]:3'"
    tokens = [
        (Token.Literal.String.Interpol, "'a[3]:3'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_interpolated_local_numeric_with_precision(lexer):
    fragment = "'a.a:3'"
    tokens = [
        (Token.Literal.String.Interpol, "'a.a:3'"),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens