|
1 |
| -// process.env.DEBUG = 1 |
| 1 | +/* eslint-env mocha */ |
2 | 2 | import { py, python, PyClass } from 'pythonia'
|
3 | 3 | import assert from 'assert'
|
4 | 4 | const f = await python('./pythonia/pyImp.py')
|
5 | 5 | const demo = await python('./pythonia/demo.py')
|
6 | 6 |
|
7 |
| -// async function it (what, fn) { |
8 |
| -// console.log('it', what) |
9 |
| -// await fn() |
10 |
| -// } |
11 |
| - |
12 |
| -await it('does function calls', async function () { |
13 |
| - console.log('ok', await f.add_inverse) |
14 |
| - assert.strictEqual(await f.add_inverse(3, 2), -5) |
15 |
| - const complex = await f.complex_num() |
16 |
| - console.log('complex', complex) |
17 |
| - console.log('real & complex', await complex.real, await complex.imag) |
18 |
| - console.log('FABC - this will fail', f.a.b.c) |
19 |
| -}) |
20 |
| - |
21 |
| -await it('declares classes', async function () { |
22 |
| - class MyClas extends PyClass { |
23 |
| - constructor () { |
24 |
| - super(demo.DemoClass, [4]) |
| 7 | +describe('Bridge from Node.js', function () { |
| 8 | + it('does function calls', async function () { |
| 9 | + console.log('ok', await f.add_inverse) |
| 10 | + assert.strictEqual(await f.add_inverse(3, 2), -5) |
| 11 | + const complex = await f.complex_num() |
| 12 | + console.log('complex', complex) |
| 13 | + console.log('real & complex', await complex.real, await complex.imag) |
| 14 | + console.log('FABC - this will fail', f.a.b.c) |
| 15 | + }) |
| 16 | + |
| 17 | + it('declares classes', async function () { |
| 18 | + class MyClas extends PyClass { |
| 19 | + constructor () { |
| 20 | + super(demo.DemoClass, [4]) |
| 21 | + } |
| 22 | + |
| 23 | + someMethod () { |
| 24 | + return 3 |
| 25 | + } |
25 | 26 | }
|
26 | 27 |
|
27 |
| - someMethod () { |
28 |
| - return 3 |
| 28 | + await f.some_event(async (message, method) => { |
| 29 | + // Call a Python function passed as a paramater |
| 30 | + assert.strictEqual(message, 'from python') |
| 31 | + assert.strictEqual(await method(), 3) |
| 32 | + }, await MyClas.init()) |
| 33 | + }) |
| 34 | + |
| 35 | + it('consumes classes', async function () { |
| 36 | + const { DemoClass, add } = demo |
| 37 | + const demoInst = await DemoClass(3) |
| 38 | + assert.ok(demoInst) |
| 39 | + console.log(demoInst) |
| 40 | + }) |
| 41 | + |
| 42 | + it('catches errors', async function () { |
| 43 | + try { |
| 44 | + await demo.throw() |
| 45 | + } catch (e) { |
| 46 | + console.log('OK!') |
29 | 47 | }
|
30 |
| - } |
31 |
| - |
32 |
| - await f.some_event(async (message, method) => { |
33 |
| - // Call a Python function passed as a paramater |
34 |
| - assert.strictEqual(message, 'from python') |
35 |
| - assert.strictEqual(await method(), 3) |
36 |
| - // console.log('Message', message, await method()) |
37 |
| - }, await MyClas.init()) |
38 |
| -}) |
39 |
| - |
40 |
| -await it('consumes classes', async function () { |
41 |
| - const { DemoClass, add } = demo |
42 |
| - const demoInst = await DemoClass(3) |
43 |
| - assert.ok(demoInst) |
44 |
| - console.log(demoInst) |
45 |
| -}) |
46 |
| - |
47 |
| -await it('catches errors', async function () { |
48 |
| - try { |
49 |
| - await demo.throw() |
50 |
| - } catch (e) { |
51 |
| - console.log('OK!') |
52 |
| - } |
53 |
| -}) |
54 |
| - |
55 |
| -await it('calls functions with special args', async function () { |
56 |
| - await demo.special$(1, 2, { kwarg1: 3, extra: 77, xx: Math.random() }) |
57 |
| -}) |
58 |
| - |
59 |
| -await it('can add Python numbers', async function () { |
60 |
| - const num = py`3j` |
61 |
| - const num2 = py`2j` |
62 |
| - console.log('3 + 3', await py`3+3 + ${num} + ${num2}`) |
63 |
| -}) |
64 |
| - |
65 |
| -await it('can set variables', async function () { |
66 |
| - f.x[2] = 4 |
67 |
| - console.log(await f.x) |
68 |
| - f.y.b = 'meow' |
69 |
| - console.log(await f.y) |
70 |
| - f.a.prop = 44 |
71 |
| - assert.strictEqual(await f.a.prop, 44) |
72 |
| - // console.log(await f.a.prop) |
73 |
| -}) |
74 |
| - |
75 |
| -await it('can return primitive values', async function () { |
76 |
| - const arr = await f.x.valueOf() |
77 |
| - console.log(arr, typeof arr) |
78 |
| - assert.strictEqual(arr.toString(), '1,2,4') |
79 |
| -}) |
80 |
| - |
81 |
| -await it('can iterate asynchronously', async function () { |
82 |
| - const array = await f.x |
83 |
| - let v = 0 |
84 |
| - for await (const entry of array) { |
85 |
| - console.log(entry) |
86 |
| - v += entry |
87 |
| - } |
88 |
| - assert.strictEqual(v, 7) |
89 |
| -}) |
90 |
| - |
91 |
| -await it('can iterate from Python', async function () { |
92 |
| - const a = await f.iter({ x: 1, y: 2, z: 3 }) |
93 |
| - const b = await f.iter([1, 2, 3]) |
94 |
| - assert.deepEqual(await a.valueOf(), ['x', 'y', 'z']) |
95 |
| - assert.deepEqual(await b.valueOf(), [1, 2, 3]) |
96 |
| -}) |
97 |
| - |
98 |
| -await it('can recieve big numbers', async function () { |
99 |
| - const bigNumber = await py`2**63` |
100 |
| - console.log(bigNumber) |
101 |
| - assert.ok(bigNumber > 2 ** 60) |
102 |
| -}) |
103 |
| - |
104 |
| -// process.exit(0) |
105 |
| - |
106 |
| -after(() => { |
107 |
| - python.exit() |
| 48 | + }) |
| 49 | + |
| 50 | + it('calls functions with special args', async function () { |
| 51 | + await demo.special$(1, 2, { kwarg1: 3, extra: 77, xx: Math.random() }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('can add Python numbers', async function () { |
| 55 | + const num = py`3j` |
| 56 | + const num2 = py`2j` |
| 57 | + console.log('3 + 3', await py`3+3 + ${num} + ${num2}`) |
| 58 | + }) |
| 59 | + |
| 60 | + it('can set variables', async function () { |
| 61 | + f.x[2] = 4 |
| 62 | + console.log(await f.x) |
| 63 | + f.y.b = 'meow' |
| 64 | + console.log(await f.y) |
| 65 | + f.a.prop = 44 |
| 66 | + assert.strictEqual(await f.a.prop, 44) |
| 67 | + }) |
| 68 | + |
| 69 | + it('can return primitive values', async function () { |
| 70 | + const arr = await f.x.valueOf() |
| 71 | + console.log(arr, typeof arr) |
| 72 | + assert.strictEqual(arr.toString(), '1,2,4') |
| 73 | + }) |
| 74 | + |
| 75 | + it('can iterate asynchronously', async function () { |
| 76 | + const array = await f.x |
| 77 | + let v = 0 |
| 78 | + for await (const entry of array) { |
| 79 | + console.log(entry) |
| 80 | + v += entry |
| 81 | + } |
| 82 | + assert.strictEqual(v, 7) |
| 83 | + }) |
| 84 | + |
| 85 | + it('can iterate from Python', async function () { |
| 86 | + const a = await f.iter({ x: 1, y: 2, z: 3 }) |
| 87 | + const b = await f.iter([1, 2, 3]) |
| 88 | + assert.deepEqual(await a.valueOf(), ['x', 'y', 'z']) |
| 89 | + assert.deepEqual(await b.valueOf(), [1, 2, 3]) |
| 90 | + }) |
| 91 | + |
| 92 | + it('can recieve big numbers', async function () { |
| 93 | + const bigNumber = await py`2**63` |
| 94 | + console.log(bigNumber) |
| 95 | + assert.ok(bigNumber > 2 ** 60) |
| 96 | + }) |
| 97 | + |
| 98 | + after(() => { |
| 99 | + python.exit() |
| 100 | + }) |
108 | 101 | })
|
0 commit comments