forked from team-discovery-channel/compose
-
Notifications
You must be signed in to change notification settings - Fork 1
/
python.test.ts
152 lines (131 loc) · 4.4 KB
/
python.test.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Based on code from https://github.com/jasonrute/modulize
* */
import {python} from '../../src/api/python';
import {Composable, Language} from '../../src/api/language';
import * as utils from '../../src/api/python.utils';
import {example, desiredOutput} from "./pytest"
const language = python;
const name = "python"
const ext = ".py"
function getAllFuncs(obj :{}) :string[] {
/*
* source: https://stackoverflow.com/
questions/31054910/get-functions-methods-of-a-class
* author: Muhammad Umer
* modifications: removed filtered return, made tslint pass.
*/
let props :string[] = [];
do {
props = props.concat(Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj)
} while (obj);
return props;
}
test(`does the ${name} object have properties`, ()=>{
const properties :string[] = Object.keys(language);
expect(properties.length).not.toEqual(0);
})
test(`is ${name} a language`,()=>{
expect(language).toBeInstanceOf(Language);
})
test(`${name} process lines returns a string with 4 less leading spaces`, ()=>{
expect(language.processLine(" test")).toEqual("test")
})
test(`is ${name} composable`,()=>{
const isComposable = ()=>{
const isA:Composable = language;
}
expect(isComposable).not.toThrow();
})
test('test 1 for get_modules_from_import, import x format', ()=>{
const im0 = "import foo"
expect(utils.getModulesFromImport(im0)[0]).toEqual("foo")
})
test('test 2 for get_modules_from_import, from x format', ()=>{
const im1 = "from foo import bar"
expect(utils.getModulesFromImport(im1)[0]).toEqual("foo")
})
test('test 3 for get_modules_from_import, import x.y format', ()=>{
const im2 = "import foo.bar"
expect(utils.getModulesFromImport(im2)[0]).toEqual("foo.bar")
})
test("test for parseImportsStructure", ()=>{
const expected = {'foo/__init__.py': [], 'foo/bar.py':[] , '__main__.py': ['foo', 'foo.bar']}
const parsed = utils.parseImportStructure(example, "__main__.py", "")
expect(parsed).toEqual(expected)
})
test("test for fileToModule", ()=>{
const file1 = utils.fileToModule("__main__.py", "__main__.py", "")
const file2 = utils.fileToModule("foo/bar.py", "__main__.py", "")
const file3 = utils.fileToModule("foo/__init__.py", "__main__.py", "")
expect(file1).toEqual(["main", "__main__"])
expect(file2).toEqual(["module", "foo.bar"])
expect(file3).toEqual(["package", "foo"])
})
test("test for block, main file", ()=>{
const expected = `\n
def __main__():
#Begin __main__.py
import foo.bar
import os
fb = foo.bar.bar_func(foo.foo_var)
print(fb) # foo bar
#End __main__.py
__main__()
`
const filename = "__main__.py"
const filetext = example[filename].join("\n")
const mod = utils.fileToModule(filename, filename, "")
const deps = utils.parseImportStructure(example, filename, "")
const block = utils.block(filename, mod, filetext, deps[filename])
expect(block).toEqual(expected)
})
test("test for block, block file", ()=>{
const expected = `
@modulize('foo.bar')
def _bar(__name__):
#Begin foo/bar.py
def bar_func(x):
return x + ' bar2'
#End foo/bar.py
return locals()
`
const filename = "foo/bar.py"
const filetext = example[filename].join("\n")
const mod = utils.fileToModule(filename, "__main__.py", "")
const deps = utils.parseImportStructure(example, filename, "")
const block = utils.block(filename, mod, filetext, deps[filename])
expect(block).toEqual(expected)
})
test("test for block, block file with dependancies", ()=>{
const expected = `
@modulize('foo.bar', dependencies= ("foo"))
def _bar(__name__):
#Begin foo/bar.py
def bar_func(x):
return x + ' bar2'
#End foo/bar.py
return locals()
`
const filename = "foo/bar.py"
const filetext = example[filename].join("\n")
const mod = utils.fileToModule(filename, "__main__.py", "")
const deps = ['foo']
const block = utils.block(filename, mod, filetext, deps)
expect(block).toEqual(expected)
})
test('test for python combined', ()=>{
expect(python.compose("__main__.py", example)).toEqual(desiredOutput)
})
test('test for python combined with base dir', ()=>{
try{
expect(python.compose("my_dir/__main__.py", example))
}
catch(e){}
})
test(`${name} object name() returns correct name`,()=>{
expect(language.getName()).toEqual(name)
})
test(`${name} object isValidExt() returns true for ${ext}`,()=>{
expect(language.isValidExt(ext)).toBe(true)
})