Skip to content

Commit 0f7b438

Browse files
committed
Refactor tests for new callback cases.
1 parent f8c739c commit 0f7b438

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

source/ports/node_port/test/index.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,33 @@ describe('metacall', () => {
133133
// Receiving undefined from a function that returns nothing in Python
134134
assert.strictEqual(f.function_pass(), undefined);
135135

136+
// Opaque pointer for class instances
137+
assert.strictEqual(f.function_capsule_method(f.function_capsule_new_class()), 'hello world');
138+
139+
// Opaque pointer for class instances with callback
140+
assert.strictEqual(f.function_capsule_cb((klass) => f.function_capsule_method(klass)), 'hello world');
141+
136142
// Double recursion
137-
/*
138143
const sum = (value, f) => value <= 0 ? 0 : value + f(value - 1, sum);
139144
assert.strictEqual(sum(5, f.function_sum), 15);
145+
assert.strictEqual(sum(5, f.function_sum), 15); // Check for function lifetime
140146
assert.strictEqual(f.function_sum(5, sum), 15);
141-
*/
147+
assert.strictEqual(f.function_sum(5, sum), 15); // Check for function lifetime
142148

143149
// Factorial composition (@trgwii)
144-
/*
145-
const fact = f.function_factorial(c => v => v <= 0 ? 1 : v);
146-
assert.strictEqual(fact(0), 1);
147-
assert.strictEqual(fact(1), 1);
148-
assert.strictEqual(fact(2), 2);
149-
assert.strictEqual(fact(3), 6);
150-
151-
const js_factorial = f.function_chain((x) => (n) => n == 0 ? 1 : n * x(x)(n - 1));
152-
assert.notStrictEqual(js_factorial, undefined);
153-
assert.strictEqual(js_factorial(5), 120);
154-
155-
const py_factorial = f.function_chain(f.function_factorial);
156-
assert.notStrictEqual(py_factorial, undefined);
157-
assert.strictEqual(py_factorial(5), 120);
158-
*/
159-
160-
// Opaque pointer for class instances
161-
assert.strictEqual(f.function_capsule_method(f.function_capsule_new_class()), 'hello world');
162-
163-
// Opaque pointer for class instances with callback
164-
assert.strictEqual(f.function_capsule_cb((klass) => f.function_capsule_method(klass)), 'hello world');
150+
// const fact = f.function_factorial(c => v => v <= 0 ? 1 : v);
151+
// assert.strictEqual(fact(1), 1);
152+
// assert.strictEqual(fact(2), 2);
153+
// assert.strictEqual(fact(3), 6);
154+
// assert.strictEqual(fact(50000), 2499950000);
155+
156+
// const js_factorial = f.function_chain((x) => (n) => n == 0 ? 1 : n * x(x)(n - 1));
157+
// assert.notStrictEqual(js_factorial, undefined);
158+
// assert.strictEqual(js_factorial(5), 120);
159+
160+
// const py_factorial = f.function_chain(f.function_factorial);
161+
// assert.notStrictEqual(py_factorial, undefined);
162+
// assert.strictEqual(py_factorial(5), 120);
165163
});
166164
});
167165
});

source/scripts/python/function/source/function.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ def function_currying_more(y):
2828
def function_pass():
2929
pass;
3030

31-
def function_sum(value, f):
32-
return 0 if value <= 0 else value + f(value - 1, function_sum);
33-
34-
def function_chain(x):
35-
return lambda n: x(x)(n);
36-
37-
def function_factorial(x):
38-
return lambda n: 1 if n <= 0 else n * x(x)(n - 1);
39-
4031
class MyClass:
4132
def f(self):
4233
return 'hello world';
@@ -50,3 +41,12 @@ def function_capsule_method(klass):
5041

5142
def function_capsule_cb(cb):
5243
return cb(MyClass());
44+
45+
def function_sum(value, f):
46+
return 0 if value <= 0 else value + f(value - 1, function_sum);
47+
48+
def function_chain(x):
49+
return lambda n: x(x)(n);
50+
51+
def function_factorial(x):
52+
return lambda n: 1 if n <= 0 else n * x(x)(n - 1);

0 commit comments

Comments
 (0)