-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-napi.js
61 lines (56 loc) · 2.78 KB
/
test-napi.js
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
const assert = require('assert');
const eqq = assert.strictEqual;
const {ErlangNode,tuple,charlist,atom,Tuple,Atom} = require('./index');
const spawn = require('child_process').spawn;
const proc = spawn('elixir', ['--no-halt','--name','napi@127.0.0.1','--cookie',
'abc123','-e','IO.puts :ready'])
let done = false;
proc.on('error', e => {throw new Error(e.toString())});
proc.stderr.on('data', data => console.log(data.toString()));
proc.on('close', _ => assert(done, "Process closed before completion"));
proc.stdout.on('data', data => {
const str = data.toString().trim();
if(str !== 'ready') return;
const enode = new ErlangNode("mynodename", "abc123", "napi@127.0.0.1");
try {
eqq(enode.rpc("Elixir.Enum", "max", [5,4,9,2]), 9);
eqq(enode.rpc("Elixir.Enum", "member?", [5,4,9,2], 2), true);
eqq(enode.rpc("Elixir.Enum", "member?", [5,4,9,2], 242), false);
eqq(enode.rpc("Elixir.Kernel", "+", 4, 5.9), 9.9);
eqq(enode.rpc("Elixir.IO", "inspect", charlist("I am a charlist")), "I am a charlist");
eqq(enode.rpc("Elixir.IO", "inspect", "Hello!"), "Hello!");
eqq(enode.rpc("Elixir.IO", "inspect", "⋄~♥~⋄"), "⋄~♥~⋄");
let nested = ["a", "b", ["c", "d", "e", ["f", "g"], "h", "i"], "j"];
assert.deepStrictEqual(enode.rpc("Elixir.IO", "inspect", nested), nested);
eqq(enode.rpc("Elixir.String", "upcase", "guy fieri"), "GUY FIERI");
let tupe = enode.rpc("Elixir.IO", "inspect", tuple(1, 2, 3.506, atom("I AM ATOM")));
eqq(tupe.length, 4);
eqq(tupe[2],3.506);
const arr = [1,2,3.5,"abc",[4,5,"heyyy"]];
assert.deepEqual(enode.rpc("Elixir.IO", "inspect", arr), arr);
tupe = enode.rpc("Elixir.IO", "inspect", tuple(1,2,3,['a','b','c',tuple("hello",atom("world"))]));
eqq(tupe[3][2], 'c');
eqq(tupe[3][3][0], 'hello');
assert.deepEqual(enode.rpc("Elixir.IO","inspect", []), []);
eqq(enode.rpc("Elixir.Enum", "at", [5], 10), null);
eqq(enode.rpc("Elixir.Enum", "at", [5], 0), 5);
eqq(enode.rpc("Elixir.Enum", "at", [], 10), null);
assert.deepEqual(enode.rpc("Elixir.IO", "inspect", {x: 4, y: -24}), {x: 4, y: -24});
assert.deepEqual(enode.rpc("Elixir.IO", "inspect", {}), {});
eqq(enode.ex.List.first([6,3,2]), 6);
eqq(enode.ex.Enum.at([4,5,2], 1), 5);
eqq(enode.ex.String.pad_leading("a", 4, "0"), "000a");
function badenode(){ enode.ex.GenServer.call(atom("SomeFakeProcessName"), atom("fakeaction"))};
function enoent(){ enode.ex.System.cmd("missing command", ["arg1"]) };
assert.throws(enoent, /badrpc\(enoent\)/)
assert.throws(badenode, /badrpc\(noproc\)/);
assert.throws(enode.ex.FakeModule.fake_function);
enode.ex.IO.ANSI.format(["Hello", atom("red"), "World"], true);
//enode.ex.System.stop()
done = true;
console.log("All tests passed. Closing node.");
} finally {
proc.kill()
enode.disconnect();
}
})