summaryrefslogtreecommitdiff
path: root/tests/test_data.py
blob: 20e74be08c45d79729bd28fe5a383cb6dcfc117b (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
# -*- coding: utf-8 -*-
"""
    Data Tests
    ~~~~~~~~~~

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

import unittest

from pygments.lexers import JsonLexer, JsonBareObjectLexer, YamlLexer
from pygments.token import Token


class JsonTest(unittest.TestCase):
    def setUp(self):
        self.lexer = JsonLexer()

    def testBasic(self):
        fragment = u'{"foo": "bar", "foo2": [1, 2, 3]}\n'
        tokens = [
            (Token.Punctuation, u'{'),
            (Token.Name.Tag, u'"foo"'),
            (Token.Punctuation, u':'),
            (Token.Text, u' '),
            (Token.Literal.String.Double, u'"bar"'),
            (Token.Punctuation, u','),
            (Token.Text, u' '),
            (Token.Name.Tag, u'"foo2"'),
            (Token.Punctuation, u':'),
            (Token.Text, u' '),
            (Token.Punctuation, u'['),
            (Token.Literal.Number.Integer, u'1'),
            (Token.Punctuation, u','),
            (Token.Text, u' '),
            (Token.Literal.Number.Integer, u'2'),
            (Token.Punctuation, u','),
            (Token.Text, u' '),
            (Token.Literal.Number.Integer, u'3'),
            (Token.Punctuation, u']'),
            (Token.Punctuation, u'}'),
            (Token.Text, u'\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

class JsonBareObjectTest(unittest.TestCase):
    def setUp(self):
        self.lexer = JsonBareObjectLexer()

    def testBasic(self):
        # This is the same as testBasic for JsonLexer above, except the
        # enclosing curly braces are removed.
        fragment = u'"foo": "bar", "foo2": [1, 2, 3]\n'
        tokens = [
            (Token.Name.Tag, u'"foo"'),
            (Token.Punctuation, u':'),
            (Token.Text, u' '),
            (Token.Literal.String.Double, u'"bar"'),
            (Token.Punctuation, u','),
            (Token.Text, u' '),
            (Token.Name.Tag, u'"foo2"'),
            (Token.Punctuation, u':'),
            (Token.Text, u' '),
            (Token.Punctuation, u'['),
            (Token.Literal.Number.Integer, u'1'),
            (Token.Punctuation, u','),
            (Token.Text, u' '),
            (Token.Literal.Number.Integer, u'2'),
            (Token.Punctuation, u','),
            (Token.Text, u' '),
            (Token.Literal.Number.Integer, u'3'),
            (Token.Punctuation, u']'),
            (Token.Text, u'\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

    def testClosingCurly(self):
        # This can be an Error token, but should not be a can't-pop-from-stack
        # exception.
        fragment = '}"a"\n'
        tokens = [
            (Token.Error, '}'),
            (Token.Name.Tag, '"a"'),
            (Token.Text, '\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

    def testClosingCurlyInValue(self):
        fragment = '"": ""}\n'
        tokens = [
            (Token.Name.Tag, '""'),
            (Token.Punctuation, ':'),
            (Token.Text, ' '),
            (Token.Literal.String.Double, '""'),
            (Token.Error, '}'),
            (Token.Text, '\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

class YamlTest(unittest.TestCase):
    def setUp(self):
        self.lexer = YamlLexer()

    def testColonInComment(self):
        # Bug #1528: This previously parsed 'token # innocent' as a tag
        fragment = u'here: token # innocent: comment\n'
        tokens = [
            (Token.Name.Tag, u'here'),
            (Token.Punctuation, u':'),
            (Token.Text, u' '),
            (Token.Literal.Scalar.Plain, u'token'),
            (Token.Text, u' '),
            (Token.Comment.Single, u'# innocent: comment'),
            (Token.Text, u'\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))