summaryrefslogtreecommitdiff
path: root/tests/test_kotlin.py
blob: 417d0d9f99fcb4c9216b5724e8806e15805a697f (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
# -*- coding: utf-8 -*-
"""
    Basic JavaLexer Test
    ~~~~~~~~~~~~~~~~~~~~

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

import unittest

from pygments.token import Text, Name, Operator, Keyword, Number, Punctuation, String
from pygments.lexers import KotlinLexer

class KotlinTest(unittest.TestCase):

    def setUp(self):
        self.lexer = KotlinLexer()
        self.maxDiff = None
    
    def testCanCopeWithBackTickNamesInFunctions(self):
        fragment = u'fun `wo bble`'
        tokens = [
            (Keyword, u'fun'),
            (Text, u' '),
            (Name.Function, u'`wo bble`'),
            (Text, u'\n')
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

    def testCanCopeWithCommasAndDashesInBackTickNames(self):
        fragment = u'fun `wo,-bble`'
        tokens = [
            (Keyword, u'fun'),
            (Text, u' '),
            (Name.Function, u'`wo,-bble`'),
            (Text, u'\n')
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
    
    def testCanCopeWithDestructuring(self):
        fragment = u'val (a, b) = '
        tokens = [
            (Keyword, u'val'),
            (Text, u' '),
            (Punctuation, u'('),
            (Name.Property, u'a'),
            (Punctuation, u','),
            (Text, u' '),
            (Name.Property, u'b'),
            (Punctuation, u')'),
            (Text, u' '),
            (Punctuation, u'='),
            (Text, u' '),
            (Text, u'\n')
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
    
    def testCanCopeGenericsInDestructuring(self):
        fragment = u'val (a: List<Something>, b: Set<Wobble>) ='
        tokens = [
            (Keyword, u'val'),
            (Text, u' '),
            (Punctuation, u'('),
            (Name.Property, u'a'),
            (Punctuation, u':'),
            (Text, u' '),
            (Name.Property, u'List'),
            (Punctuation, u'<'),
            (Name, u'Something'),
            (Punctuation, u'>'),
            (Punctuation, u','),
            (Text, u' '),
            (Name.Property, u'b'),
            (Punctuation, u':'),
            (Text, u' '),
            (Name.Property, u'Set'),
            (Punctuation, u'<'),
            (Name, u'Wobble'),
            (Punctuation, u'>'),
            (Punctuation, u')'),
            (Text, u' '),
            (Punctuation, u'='),
            (Text, u'\n')
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

    def testCanCopeWithGenerics(self):
        fragment = u'inline fun <reified T : ContractState> VaultService.queryBy(): Vault.Page<T> {'
        tokens = [
            (Keyword, u'inline fun'),
            (Text, u' '),
            (Punctuation, u'<'),
            (Keyword, u'reified'),
            (Text, u' '),
            (Name, u'T'),
            (Text, u' '),
            (Punctuation, u':'),
            (Text, u' '),
            (Name, u'ContractState'),
            (Punctuation, u'>'),
            (Text, u' '),
            (Name.Class, u'VaultService'),
            (Punctuation, u'.'),
            (Name.Function, u'queryBy'),
            (Punctuation, u'('),
            (Punctuation, u')'),
            (Punctuation, u':'),
            (Text, u' '),
            (Name, u'Vault'),
            (Punctuation, u'.'),
            (Name, u'Page'),
            (Punctuation, u'<'),
            (Name, u'T'),
            (Punctuation, u'>'),
            (Text, u' '),
            (Punctuation, u'{'),
            (Text, u'\n')
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

    def testShouldCopeWithMultilineComments(self):
        fragment = u'"""\nthis\nis\na\ncomment"""'
        tokens = [
            (String, u'"""\nthis\nis\na\ncomment"""'), 
            (Text, u'\n')
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

if __name__ == '__main__':
    unittest.main()