OILS / frontend / id_kind_def_test.py View on Github | oilshell.org

97 lines, 59 significant
1#!/usr/bin/env python2
2# Copyright 2016 Andy Chu. All rights reserved.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8"""
9id_kind_test.py: Tests for id_kind_def.py
10"""
11from __future__ import print_function
12
13import unittest
14
15from _devbuild.gen.id_kind_asdl import Id, Kind
16from frontend import consts
17from frontend.lexer import DummyToken as Tok
18from frontend.lexer_def import ID_SPEC
19
20_kind_sizes = ID_SPEC.kind_sizes
21
22
23class TokensTest(unittest.TestCase):
24
25 def testId(self):
26 #print(dir(Id))
27 print(Id.Op_Newline)
28 print(Id.Undefined_Tok)
29
30 def testTokens(self):
31 print(Id.Op_Newline)
32 print(Tok(Id.Op_Newline, '\n'))
33
34 print(Id.Op_Newline)
35
36 print(Kind.Eof)
37 print(Kind.Left)
38
39 print('--')
40 num_kinds = 0
41 for name in dir(Kind):
42 if name[0].isupper():
43 kind = getattr(Kind, name)
44 print('%-20s %s' % (name, kind))
45 num_kinds += 1
46
47 print()
48 print('Number of Kinds:', num_kinds)
49 print()
50
51 for name in dir(Id):
52 if name[0].isupper():
53 id_ = getattr(Id, name)
54 print('%-30s %s' % (name, id_))
55
56 # 309 out of 256 tokens now
57 print()
58 print('Number of IDs:', len(ID_SPEC.id_str2int))
59
60 t = Tok(Id.Arith_Plus, '+')
61 self.assertEqual(Kind.Arith, consts.GetKind(t.id))
62 t = Tok(Id.Arith_CaretEqual, '^=')
63 self.assertEqual(Kind.Arith, consts.GetKind(t.id))
64 t = Tok(Id.Arith_RBrace, '}')
65 self.assertEqual(Kind.Arith, consts.GetKind(t.id))
66
67 t = Tok(Id.BoolBinary_GlobDEqual, '==')
68 self.assertEqual(Kind.BoolBinary, consts.GetKind(t.id))
69
70 t = Tok(Id.BoolBinary_Equal, '=')
71 self.assertEqual(Kind.BoolBinary, consts.GetKind(t.id))
72
73 def testLexerPairs(self):
74
75 def MakeLookup(p):
76 return dict((pat, tok) for _, pat, tok in p)
77
78 lookup = MakeLookup(ID_SPEC.LexerPairs(Kind.BoolUnary))
79 #print(lookup)
80 self.assertEqual(Id.BoolUnary_e, lookup['-e'])
81 self.assertEqual(Id.BoolUnary_z, lookup['-z'])
82
83 lookup2 = MakeLookup(ID_SPEC.LexerPairs(Kind.BoolBinary))
84 self.assertEqual(Id.BoolBinary_eq, lookup2['-eq'])
85 #print(lookup2)
86
87 def testPrintStats(self):
88 print('---')
89 k = _kind_sizes
90 print('STATS: %d tokens in %d groups: %s' % (sum(k), len(k), k))
91 # Thinking about switching
92 big = [i for i in k if i > 8]
93 print('%d BIG groups: %s' % (len(big), sorted(big)))
94
95
96if __name__ == '__main__':
97 unittest.main()