OILS / opy / compiler2 / symbols_test.py View on Github | oilshell.org

68 lines, 46 significant
1#!/usr/bin/env python2
2"""
3symbols_test.py: Tests for symbols.py
4
5Copied out of symbols.py. You pass this some filenames.
6"""
7
8import sys
9import symtable
10
11from . import symbols
12from . import transformer
13
14
15def list_eq(l1, l2):
16 return sorted(l1) == sorted(l2)
17
18def get_names(syms):
19 return [s for s in [s.get_name() for s in syms.get_symbols()]
20 if not (s.startswith('_[') or s.startswith('.'))]
21
22
23if __name__ == "__main__":
24 # TODO: Pass symbols
25 transformer.Init(None)
26
27 for file in sys.argv[1:]:
28 print(file)
29 f = open(file)
30 buf = f.read()
31 f.close()
32 syms = symtable.symtable(buf, file, "exec")
33 mod_names = get_names(syms)
34
35 #tree = parseFile(file)
36 tree = None
37 s = symbols.SymbolVisitor()
38 s.Dispatch(tree)
39
40 # compare module-level symbols
41 names2 = s.scopes[tree].get_names()
42
43 if not list_eq(mod_names, names2):
44 print()
45 print("oops", file)
46 print(sorted(mod_names))
47 print(sorted(names2))
48 sys.exit(-1)
49
50 d = {}
51 d.update(s.scopes)
52 del d[tree]
53 scopes = d.values()
54 del d
55
56 for s in syms.get_symbols():
57 if s.is_namespace():
58 l = [sc for sc in scopes
59 if sc.name == s.get_name()]
60 if len(l) > 1:
61 print("skipping", s.get_name())
62 else:
63 if not list_eq(get_names(s.get_namespace()),
64 l[0].get_names()):
65 print(s.get_name())
66 print(sorted(get_names(s.get_namespace())))
67 print(sorted(l[0].get_names()))
68 sys.exit(-1)