Skip to content

Commit 617517f

Browse files
committed
Add initial Node and N-API testing infrastructure and tests.
1 parent f98829e commit 617517f

File tree

8 files changed

+190
-1
lines changed

8 files changed

+190
-1
lines changed

build/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -e
33
DIR=$( cd "$( dirname "$0" )" && pwd )
44
$DIR/build.sh test "$@"

tests2/Builtins.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <cstdint>
2+
3+
void ReturnsVoid () { }
4+
5+
std::nullptr_t ReturnsNullptr () { return nullptr; }
6+
std::nullptr_t PassAndReturnsNullptr (std::nullptr_t t) { return t; }
7+
8+
bool ReturnsBool () { return true; }
9+
bool PassAndReturnsBool (bool v) { return v; }
10+
11+
// Character types
12+
char ReturnsChar () { return 'a'; }
13+
signed char ReturnsSChar () { return 'a'; }
14+
unsigned char ReturnsUChar () { return 'a'; }
15+
char PassAndReturnsChar (char v) { return v; }
16+
signed char PassAndReturnsSChar (signed char v) { return v; }
17+
unsigned char PassAndReturnsUChar (unsigned char v) { return v; }
18+
19+
20+
wchar_t ReturnsWChar () { return 'a'; }
21+
#if __cplusplus > 201703L
22+
char8_t ReturnsChar8 () { return 'a'; }
23+
#endif
24+
char16_t ReturnsChar16 () { return 'a'; }
25+
char32_t ReturnsChar32 () { return 'a'; }
26+
27+
// Floating-point types
28+
float ReturnsFloat () { return 5.0; }
29+
double ReturnsDouble () { return -5.0; }
30+
long double ReturnsLongDouble () { return -5.0; }
31+
32+
float PassAndReturnsFloat (float v) { return v; }
33+
double PassAndReturnsDouble (double v) { return v; }
34+
long double PassAndReturnsLongDouble (long double v) { return v; }
35+
36+
// Integer types
37+
int8_t ReturnsInt8 () { return -5; }
38+
uint8_t ReturnsUInt8 () { return 5; }
39+
int16_t ReturnsInt16 () { return -5; }
40+
uint16_t ReturnsUInt16 () { return 5; }
41+
int32_t ReturnsInt32 () { return -5; }
42+
uint32_t ReturnsUInt32 () { return 5; }
43+
int64_t ReturnsInt64 () { return -5; }
44+
uint64_t ReturnsUInt64 () { return 5; }
45+
46+
int8_t PassAndReturnsInt8 (int8_t v) { return v; }
47+
uint8_t PassAndReturnsUInt8 (uint8_t v) { return v; }
48+
int16_t PassAndReturnsInt16 (int16_t v) { return v; }
49+
uint16_t PassAndReturnsUInt16 (uint16_t v) { return v; }
50+
int32_t PassAndReturnsInt32 (int32_t v) { return v; }
51+
uint32_t PassAndReturnsUInt32 (uint32_t v) { return v; }
52+
int64_t PassAndReturnsInt64 (int64_t v) { return v; }
53+
uint64_t PassAndReturnsUInt64 (uint64_t v) { return v; }

tests2/Enums.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum class Enum0
2+
{
3+
Item0,
4+
Item1,
5+
Item2 = 5
6+
};

tests2/Overloads.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int Overload(int, int) { return 1; }
2+
int Overload(int, float) { return 2; }
3+
int Overload(float, int) { return 3; }

tests2/napi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gen

tests2/napi/premake5.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
workspace "test"
2+
configurations {"Debug", "Release"}
3+
location "gen"
4+
symbols "On"
5+
optimize "Off"
6+
project "tests"
7+
kind "SharedLib"
8+
files {"gen/**.cpp"}
9+
includedirs {".."}
10+
targetprefix ""
11+
targetextension ".node"

tests2/napi/test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var test = require('./gen/bin/Debug/tests.node')
2+
const assert = require('assert').strict;
3+
4+
const eq = assert.strictEqual;
5+
const floateq = (actual, expected) => { assert(Math.abs(actual - expected) < Number.EPSILON) }
6+
7+
const ascii = v => v.charCodeAt(0)
8+
9+
function builtins()
10+
{
11+
eq(test.ReturnsVoid(), undefined)
12+
eq(test.ReturnsBool(), true)
13+
eq(test.PassAndReturnsBool(false), false)
14+
eq(test.ReturnsNullptr(), null)
15+
//eq(test.PassAndReturnsNullptr(null), null)
16+
eq(test.ReturnsNullptr(), null)
17+
18+
eq(test.ReturnsChar (), ascii('a'));
19+
eq(test.ReturnsSChar(), ascii('a'));
20+
eq(test.ReturnsUChar(), ascii('a'));
21+
22+
eq(test.PassAndReturnsChar (ascii('a')), ascii('a'));
23+
eq(test.PassAndReturnsSChar(ascii('b')), ascii('b'));
24+
eq(test.PassAndReturnsUChar(ascii('c')), ascii('c'));
25+
26+
eq(test.ReturnsFloat (), 5.0);
27+
eq(test.ReturnsDouble(), -5.0);
28+
//eq(test.ReturnsLongDouble(), -5.0);
29+
30+
//floateq(test.PassAndReturnsFloat (1.32), 1.32);
31+
floateq(test.PassAndReturnsDouble(1.32), 1.32);
32+
//float(test.PassAndReturnsLongDouble(1.32), 1.32);
33+
34+
eq(test.ReturnsInt8 (), -5);
35+
eq(test.ReturnsUInt8 (), 5);
36+
eq(test.ReturnsInt16 (), -5);
37+
eq(test.ReturnsUInt16(), 5);
38+
eq(test.ReturnsInt32 (), -5);
39+
eq(test.ReturnsUInt32(), 5);
40+
eq(test.ReturnsInt64 (), -5n);
41+
eq(test.ReturnsUInt64(), 5n);
42+
43+
const int8 = { min: -(2**7), max: (2**7) - 1 };
44+
eq(test.PassAndReturnsInt8(int8.min), int8.min);
45+
eq(test.PassAndReturnsInt8(int8.max), int8.max);
46+
47+
const uint8 = { min: 0, max: (2**8) - 1 };
48+
eq(test.PassAndReturnsUInt8(uint8.min), uint8.min);
49+
eq(test.PassAndReturnsUInt8(uint8.max), uint8.max);
50+
51+
const int16 = { min: -(2**15), max: (2**15) - 1 };
52+
eq(test.PassAndReturnsInt16(int16.min), int16.min);
53+
eq(test.PassAndReturnsInt16(int16.max), int16.max);
54+
55+
const uint16 = { min: 0, max: (2**16) - 1 };
56+
eq(test.PassAndReturnsUInt16(uint16.min), uint16.min);
57+
eq(test.PassAndReturnsUInt16(uint16.max), uint16.max);
58+
59+
const int32 = { min: -(2**31), max: (2**31) - 1 };
60+
eq(test.PassAndReturnsInt32(int32.min), int32.min);
61+
eq(test.PassAndReturnsInt32(int32.max), int32.max);
62+
63+
const uint32 = { min: 0, max: (2**32) - 1 };
64+
eq(test.PassAndReturnsUInt32(uint32.min), uint32.min);
65+
eq(test.PassAndReturnsUInt32(uint32.max), uint32.max);
66+
67+
const int64 = { min: BigInt(2**63) * -1n, max: BigInt(2**63) - 1n };
68+
eq(test.PassAndReturnsInt64(int64.min), int64.min);
69+
eq(test.PassAndReturnsInt64(int64.max), int64.max);
70+
71+
const uint64 = { min: BigInt(0), max: BigInt(2**64) - 1n };
72+
eq(test.PassAndReturnsUInt64(uint64.min), uint64.min);
73+
eq(test.PassAndReturnsUInt64(uint64.max), uint64.max);
74+
}
75+
76+
function enums()
77+
{
78+
eq(test.Enum0.Item0, 0);
79+
eq(test.Enum0.Item1, 1);
80+
eq(test.Enum0.Item2, 5);
81+
}
82+
83+
function overloads()
84+
{
85+
eq(test.Overload(1, 2), 1)
86+
eq(test.Overload(1, 2.032), 2);
87+
eq(test.Overload(1.23, 2), 3);
88+
}
89+
90+
builtins();
91+
enums();
92+
overloads();

tests2/napi/test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
dir=$(cd "$(dirname "$0")"; pwd)
4+
rootdir="$dir/../.."
5+
configuration=Release
6+
platform=x64
7+
8+
red=`tput setaf 1`
9+
green=`tput setaf 2`
10+
reset=`tput sgr0`
11+
12+
echo "${green}Generating bindings${reset}"
13+
dotnet $rootdir/bin/${configuration}_${platform}/CppSharp.CLI.dll \
14+
--gen=napi -I$dir/.. -o $dir/gen -m tests $dir/../*.h
15+
16+
echo "${green}Building generated binding files${reset}"
17+
premake=$rootdir/build/premake.sh
18+
$premake --file=$dir/premake5.lua gmake
19+
make -C $dir/gen
20+
echo
21+
22+
echo "${green}Executing JS tests with Node${reset}"
23+
node $dir/test.js

0 commit comments

Comments
 (0)