forked from cory/tildefriends
146 lines
3.5 KiB
C
146 lines
3.5 KiB
C
|
#include "tests.h"
|
||
|
|
||
|
#include "ssb.h"
|
||
|
|
||
|
#include <assert.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
static void _test_nop(const char* exe_path)
|
||
|
{
|
||
|
FILE* file = fopen("out/test.js", "w");
|
||
|
fprintf(file, "print('hi');");
|
||
|
fclose(file);
|
||
|
|
||
|
char command[256];
|
||
|
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||
|
printf("%s\n", command);
|
||
|
int result = system(command);
|
||
|
assert(WIFEXITED(result));
|
||
|
assert(WEXITSTATUS(result) == 0);
|
||
|
}
|
||
|
|
||
|
static void _test_child(const char* exe_path)
|
||
|
{
|
||
|
FILE* file = fopen("out/test.js", "w");
|
||
|
fprintf(file,
|
||
|
"var task = new Task();\n"
|
||
|
"task.onExit = function() {\n"
|
||
|
" print('child exited');\n"
|
||
|
"};\n"
|
||
|
"task.activate();\n"
|
||
|
"task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n"
|
||
|
" print('child started');\n"
|
||
|
"});");
|
||
|
fclose(file);
|
||
|
|
||
|
file = fopen("out/child.js", "w");
|
||
|
fprintf(file,
|
||
|
"print('I am the child process.');\n"
|
||
|
"exit(0);\n");
|
||
|
fclose(file);
|
||
|
|
||
|
char command[256];
|
||
|
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||
|
printf("%s\n", command);
|
||
|
int result = system(command);
|
||
|
assert(WIFEXITED(result));
|
||
|
assert(WEXITSTATUS(result) == 0);
|
||
|
|
||
|
unlink("out/test.js");
|
||
|
unlink("out/child.js");
|
||
|
}
|
||
|
|
||
|
static void _test_promise(const char* exe_path)
|
||
|
{
|
||
|
FILE* file = fopen("out/test.js", "w");
|
||
|
fprintf(file,
|
||
|
"var task = new Task();\n"
|
||
|
"task.activate();\n"
|
||
|
"task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n"
|
||
|
" task.getExports().then(function(exports) {\n"
|
||
|
" return exports.add(1, 1);\n"
|
||
|
" }).then(function(sum) {\n"
|
||
|
" if (sum == 2) {\n"
|
||
|
" exit(0);\n"
|
||
|
" } else {\n"
|
||
|
" exit(1);\n"
|
||
|
" }\n"
|
||
|
" });\n"
|
||
|
"});\n");
|
||
|
fclose(file);
|
||
|
|
||
|
file = fopen("out/child.js", "w");
|
||
|
fprintf(file,
|
||
|
"exports = {\n"
|
||
|
" add: function(left, right) {\n"
|
||
|
" return left + right;\n"
|
||
|
" }\n"
|
||
|
"}\n");
|
||
|
fclose(file);
|
||
|
|
||
|
char command[256];
|
||
|
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||
|
printf("%s\n", command);
|
||
|
int result = system(command);
|
||
|
assert(WIFEXITED(result));
|
||
|
assert(WEXITSTATUS(result) == 0);
|
||
|
|
||
|
unlink("out/test.js");
|
||
|
unlink("out/child.js");
|
||
|
}
|
||
|
|
||
|
static void _test_promise_remote_throw(const char* exe_path)
|
||
|
{
|
||
|
FILE* file = fopen("out/test.js", "w");
|
||
|
fprintf(file,
|
||
|
"var task = new Task();\n"
|
||
|
"task.activate();\n"
|
||
|
"task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n"
|
||
|
" task.getExports().then(function(exp) {\n"
|
||
|
" return exp.add(1, 1);\n"
|
||
|
" }).then(function(sum) {\n"
|
||
|
" exit(1);\n"
|
||
|
" }).catch(function(error) {\n"
|
||
|
" print('Caught: ' + error.message);\n"
|
||
|
" if (error.stack) {\n"
|
||
|
" print('stack: ' + error.stack);\n"
|
||
|
" }\n"
|
||
|
" exit(0);\n"
|
||
|
" });\n"
|
||
|
"}).catch(function(e) {\n"
|
||
|
" print('caught', e.message);\n"
|
||
|
"});\n");
|
||
|
fclose(file);
|
||
|
|
||
|
file = fopen("out/child.js", "w");
|
||
|
fprintf(file,
|
||
|
"exports = {\n"
|
||
|
" add: function(left, right) {\n"
|
||
|
" throw new Error('fail');\n"
|
||
|
" }\n"
|
||
|
"}\n");
|
||
|
fclose(file);
|
||
|
|
||
|
char command[256];
|
||
|
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||
|
printf("%s\n", command);
|
||
|
int result = system(command);
|
||
|
assert(WIFEXITED(result));
|
||
|
assert(WEXITSTATUS(result) == 0);
|
||
|
|
||
|
unlink("out/test.js");
|
||
|
unlink("out/child.js");
|
||
|
}
|
||
|
|
||
|
void tf_tests(const char* exe_path)
|
||
|
{
|
||
|
tf_ssb_test();
|
||
|
_test_nop(exe_path);
|
||
|
_test_child(exe_path);
|
||
|
_test_promise(exe_path);
|
||
|
_test_promise_remote_throw(exe_path);
|
||
|
printf("Tests completed.\n");
|
||
|
}
|