0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038
0039 #include <atmi.h>
0040 #include <ubf.h>
0041 #include <ndebug.h>
0042 #include <test.fd.h>
0043 #include <ndrstandard.h>
0044 #include "test027.h"
0045 #include <pscript.h>
0046 #include <stdarg.h> /* va_list, va_start, va_arg, va_end */
0047 #include <cgreen/cgreen.h>
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 void printfunc(HPSCRIPTVM v,const PSChar *s,...)
0058 {
0059 va_list vl;
0060 va_start(vl, s);
0061 vfprintf(stdout, s, vl);
0062 va_end(vl);
0063 }
0064
0065
0066 void errorfunc(HPSCRIPTVM v,const PSChar *s,...)
0067 {
0068 va_list vl;
0069 va_start(vl, s);
0070 vfprintf(stderr, s, vl);
0071 va_end(vl);
0072 }
0073
0074
0075 Ensure(basic_script_func_call)
0076 {
0077 HPSCRIPTVM v;
0078 v = ps_open(1024);
0079
0080 const PSChar *err;
0081 ps_setprintfunc(v,printfunc,errorfunc);
0082
0083 char *script = "function testHello(name){return name+\" Hello World\";}";
0084 const PSChar *s;
0085
0086 if (PS_FAILED(ps_compilebuffer(v, script, strlen(script), "hello.psc", PSTrue)))
0087 {
0088 printf("Failed to compile...\n");
0089
0090 if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0091 {
0092 printf(_SC("Error [%s]\n"),err);
0093 assert_true(0);
0094 return;
0095 }
0096 }
0097 else
0098 {
0099 ps_pushroottable(v);
0100 }
0101
0102
0103 if (PS_FAILED(ps_call(v,1,PSTrue, PSTrue)))
0104 {
0105 printf("Failed to load...\n");
0106 ps_getlasterror(v);
0107 if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0108 {
0109 printf(_SC("Error [%s]\n"),err);
0110 assert_true(0);
0111 return;
0112 }
0113 }
0114
0115
0116 ps_pushroottable(v);
0117 ps_pushstring(v,"testHello",-1);
0118 ps_get(v,-2);
0119
0120 ps_pushroottable(v);
0121 ps_pushstring(v, "Jimbo", -1);
0122
0123
0124
0125
0126 if (PS_FAILED(ps_call(v,2,PSTrue, PSTrue)))
0127 {
0128 printf("Failed to call...\n");
0129 ps_getlasterror(v);
0130 if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0131 {
0132 printf(_SC("Error [%s]\n"),err);
0133 assert_true(0);
0134 return;
0135 }
0136 }
0137
0138 ps_getstring(v,-1,&s);
0139 printf("Got result: [%s]\n", s);
0140 assert_string_equal(s, "Jimbo Hello World");
0141
0142 ps_pop(v,3);
0143
0144 ps_close(v);
0145 }
0146
0147
0148 TestSuite *pscrit_test_all(void)
0149 {
0150 TestSuite *suite = create_test_suite();
0151 add_test(suite, basic_script_func_call);
0152
0153 return suite;
0154 }
0155
0156
0157
0158
0159 int main(int argc, char** argv)
0160 {
0161
0162 int ret;
0163 TestSuite *suite = create_test_suite();
0164
0165 add_suite(suite, pscrit_test_all());
0166
0167
0168 if (argc > 1) {
0169 ret=run_single_test(suite,argv[1],create_text_reporter());
0170 }
0171 else
0172 {
0173 ret=run_test_suite(suite, create_text_reporter());
0174 }
0175
0176 destroy_test_suite(suite);
0177
0178 return ret;
0179 }
0180
0181