|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import unittest |
3 | 6 |
|
4 | | -from java2python.config import Config |
5 | 7 | from java2python.compiler import buildAST |
6 | 8 | from java2python.lang import tokens |
7 | 9 | from java2python.lang.selector import Token, Type |
| 10 | +from java2python.lib import colortools |
8 | 11 |
|
9 | 12 |
|
10 | | -if __name__ == '__main__': |
11 | | - import sys, os |
| 13 | +def setUpModule(): |
12 | 14 | fn = os.path.join(os.path.dirname(__file__), 'Selector1.java') |
13 | | - source = open(fn).read() |
14 | | - tree = buildAST(source, Config(())) |
15 | | - tree.dump(sys.stdout) |
16 | | - |
17 | | - selectors = [ |
18 | | -# Type('EXPR'), |
19 | | -# Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'), |
20 | | -# Type('CLASS')[2], |
21 | | -# Type('METHOD_CALL') & Type('IDENT'), |
22 | | -# Type('IDENT') + Type('IDENT'), |
23 | | - Type('CLASS') > Type('IDENT'), |
24 | | - Type('IDENT', 'foo'), |
25 | | - Token(text='foo'), |
26 | | - Token(type='BLOCK_SCOPE', line=2) & Token(type='IDENT', text='foo') |
27 | | - ] |
28 | | - |
29 | | - for index, selector in enumerate(selectors): |
30 | | - print 'Selector Test {0}:\n ==== {1}'.format(index, selector) |
31 | | - for node in selector.walk(tree): |
32 | | - name = str(node) |
33 | | - ntype = tokens.map[node.type] |
34 | | - args = (name, '') if name == ntype else (ntype, name) |
35 | | - print '{0}---- match: {1} {2}'.format(*(' '*8, )+args) |
36 | | - print '{0}---- token: {1}'.format(' '*8, node.dumps() ) |
37 | | - print |
| 15 | + SelectorTest.tree = buildAST(open(fn).read()) |
| 16 | + SelectorTest.tree.dump(sys.stdout) |
| 17 | + |
| 18 | + |
| 19 | +class SelectorTest(unittest.TestCase): |
| 20 | + def walk(self, selector): |
| 21 | + return list(selector.walk(self.tree)) |
| 22 | + |
| 23 | + def assertNodes(self, nodes, length): |
| 24 | + self.assertTrue(nodes) |
| 25 | + self.assertEqual(len(nodes), length) |
| 26 | + |
| 27 | + def shortDescription(self): |
| 28 | + fs = 'Description: {}\nSelector: {}\n' |
| 29 | + args = (colortools.cyan(self.description), colortools.yellow(self.selector)) |
| 30 | + return fs.format(*args) |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def make(cls, count): |
| 34 | + def t(self): |
| 35 | + nodes = self.walk(self.selector) |
| 36 | + self.assertNodes(nodes, count) |
| 37 | + return t |
| 38 | + |
| 39 | + |
| 40 | +class TestIdentChildOfClass(SelectorTest): |
| 41 | + description = 'select one IDENT node that is a child of a CLASS node' |
| 42 | + selector = Type('CLASS') > Type('IDENT') |
| 43 | + test = SelectorTest.make(1) |
| 44 | + |
| 45 | + |
| 46 | +class TestIdentWithText(SelectorTest): |
| 47 | + description = 'select two IDENT nodes with text "foo"' |
| 48 | + selector = Type('IDENT', 'foo') |
| 49 | + test = SelectorTest.make(2) |
| 50 | + |
| 51 | + |
| 52 | +class TestTokensWithText(SelectorTest): |
| 53 | + description = 'select two nodes with text "foo"' |
| 54 | + selector = Token(text='foo') |
| 55 | + test = SelectorTest.make(2) |
| 56 | + |
| 57 | + |
| 58 | +class TestTokenCallableCombo(SelectorTest): |
| 59 | + description = 'select BLOCK_SCOPE on line 7' |
| 60 | + selector = Token(type=lambda t: t.type == tokens.BLOCK_SCOPE, line=7) |
| 61 | + test = SelectorTest.make(1) |
| 62 | + |
| 63 | + |
| 64 | +class TestTokenMultipleCallables(SelectorTest): |
| 65 | + description = 'select BLOCK_SCOPE on line 2 or 7' |
| 66 | + selector = Token(type=lambda t: t.type == tokens.BLOCK_SCOPE, line=lambda t:t.line in (2, 7)) |
| 67 | + test = SelectorTest.make(2) |
| 68 | + |
| 69 | + |
| 70 | +class TestTokenChildCallable(SelectorTest): |
| 71 | + description = 'select BLOCK_SCOPE with one child IDENT starting with "f"' |
| 72 | + selector = Token(type=lambda t: t.type == tokens.BLOCK_SCOPE) & Token(type='IDENT', text=lambda tok:tok.text.startswith('f')) |
| 73 | + test = SelectorTest.make(1) |
| 74 | + |
| 75 | + |
| 76 | +class TestNthChildWithExtraChecks(SelectorTest): |
| 77 | + description = 'select two children of FORMAL_PARAM_STD_DECL at index 2 ' |
| 78 | + selector = Type('FORMAL_PARAM_STD_DECL')[2] |
| 79 | + |
| 80 | + def test(self): |
| 81 | + nodes = self.walk(self.selector) |
| 82 | + self.assertNodes(nodes, 2) |
| 83 | + self.assertEquals(nodes[0].type, tokens.IDENT) |
| 84 | + self.assertEquals(nodes[1].type, tokens.IDENT) |
| 85 | + self.assertEquals(nodes[0].text, 'x') |
| 86 | + self.assertEquals(nodes[1].text, 'y') |
| 87 | + |
| 88 | + |
| 89 | +class TestDirectChildren(SelectorTest): |
| 90 | + description = 'select two TYPE nodes that are children of a VAR_DECLARATION node' |
| 91 | + selector = Type('VAR_DECLARATION') > Type('TYPE') |
| 92 | + test = SelectorTest.make(2) |
| 93 | + |
| 94 | + |
| 95 | +class TestSimpleSiblings(SelectorTest): |
| 96 | + description = 'select three IDENT nodes that are siblings of a MODIFIER_LIST' |
| 97 | + selector = Type('MODIFIER_LIST') + Type('IDENT') |
| 98 | + test = SelectorTest.make(3) |
| 99 | + |
| 100 | + |
| 101 | +class TestClassIdent(SelectorTest): |
| 102 | + description = 'select one IDENT node that is a child of a CLASS node' |
| 103 | + selector = Type('CLASS') > Type('IDENT') |
| 104 | + test = SelectorTest.make(1) |
0 commit comments