summaryrefslogtreecommitdiff
path: root/tests/test_r.py
blob: ffd3f576c1b518e7cab98265ff46e91f28c9dcc1 (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
"""
    R Tests
    ~~~~~~~

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

import pytest

from pygments.lexers import SLexer
from pygments.token import Token, Name, Punctuation


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


def test_call(lexer):
    fragment = 'f(1, a)\n'
    tokens = [
        (Name.Function, 'f'),
        (Punctuation, '('),
        (Token.Literal.Number, '1'),
        (Punctuation, ','),
        (Token.Text, ' '),
        (Token.Name, 'a'),
        (Punctuation, ')'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_name1(lexer):
    fragment = '._a_2.c'
    tokens = [
        (Name, '._a_2.c'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_name2(lexer):
    # Invalid names are valid if backticks are used
    fragment = '`.1 blah`'
    tokens = [
        (Name, '`.1 blah`'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_name3(lexer):
    # Internal backticks can be escaped
    fragment = '`.1 \\` blah`'
    tokens = [
        (Name, '`.1 \\` blah`'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_custom_operator(lexer):
    fragment = '7 % and % 8'
    tokens = [
        (Token.Literal.Number, '7'),
        (Token.Text, ' '),
        (Token.Operator, '% and %'),
        (Token.Text, ' '),
        (Token.Literal.Number, '8'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_indexing(lexer):
    fragment = 'a[1]'
    tokens = [
        (Token.Name, 'a'),
        (Token.Punctuation, '['),
        (Token.Literal.Number, '1'),
        (Token.Punctuation, ']'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_dot_name(lexer):
    fragment = '. <- 1'
    tokens = [
        (Token.Name, '.'),
        (Token.Text, ' '),
        (Token.Operator, '<-'),
        (Token.Text, ' '),
        (Token.Literal.Number, '1'),
        (Token.Text, '\n')
    ]
    assert list(lexer.get_tokens(fragment)) == tokens


def test_dot_indexing(lexer):
    fragment = '.[1]'
    tokens = [
        (Token.Name, '.'),
        (Token.Punctuation, '['),
        (Token.Literal.Number, '1'),
        (Token.Punctuation, ']'),
        (Token.Text, '\n'),
    ]
    assert list(lexer.get_tokens(fragment)) == tokens