96 naRef argA = ctx.requireArg<naRef>(0);
97 naRef argB = ctx.requireArg<naRef>(1);
98 auto msg = ctx.getArg<std::string>(2,
"assert_equal failed");
102 std::string aStr = ctx.from_nasal<std::string>(argA);
103 std::string bStr = ctx.from_nasal<std::string>(argB);
104 msg +=
"; expected:" + aStr +
", actual:" + bStr;
107 static_activeTest->failureFileName = ctx.from_nasal<std::string>(naGetSourceFile(ctx.c_ctx(), 0));
109 ctx.runtimeError(msg.c_str());
117 double argA = ctx.requireArg<
double>(0);
118 double argB = ctx.requireArg<
double>(1);
119 double tolerance = ctx.requireArg<
double>(2);
121 auto msg = ctx.getArg<std::string>(3,
"assert_doubles_equal failed");
123 const bool same = fabs(argA - argB) < tolerance;
125 msg +=
"; expected:" + std::to_string(argA) +
", actual:" + std::to_string(argB);
128 static_activeTest->failureFileName = ctx.from_nasal<std::string>(naGetSourceFile(ctx.c_ctx(), 0));
130 ctx.runtimeError(msg.c_str());
210 const char* buf,
int len,
214 naRef srcfile = naNewString(ctx);
215 naStr_fromdata(srcfile, (
char*)filename, strlen(filename));
216 naRef code = naParseCode(ctx, srcfile, 1, (
char*)buf, len, &errLine);
218 std::ostringstream errorMessageStream;
219 errorMessageStream <<
"Nasal Test parse error: " << naGetError(ctx) <<
220 " in "<< filename <<
", line " << errLine;
221 errors = errorMessageStream.str();
222 SG_LOG(SG_NASAL, SG_DEV_ALERT, errors);
227 return naBindFunction(ctx, code,
nasalSys->nasalGlobals());
234 naContext ctx = naNewContext();
236 sg_ifstream file_in(path);
237 const auto source = file_in.read_all();
240 std::string fileName = path.utf8Str();
243 source.size(), errors);
251 auto localNS =
nasalSys->getGlobals().createHash(
"_test_" + path.utf8Str());
252 nasalSys->callWithContext(ctx, code, 0, 0, localNS.get_naRef());
255 auto setUpFunc = localNS.get(
"setUp");
256 auto tearDown = localNS.get(
"tearDown");
258 for (
const auto value : localNS) {
259 if (value.getKey().find(
"test_") == 0) {
262 if (naIsFunc(setUpFunc)) {
263 nasalSys->callWithContext(ctx, setUpFunc, 0,
nullptr ,localNS.get_naRef());
266 const auto testName = value.getKey();
267 auto testFunc = value.getValue<naRef>();
268 if (!naIsFunc(testFunc)) {
269 SG_LOG(SG_NAVAID, SG_DEV_WARN,
"Skipping non-function test member:" << testName);
273 nasalSys->callWithContext(ctx, testFunc, 0,
nullptr, localNS.get_naRef());
277 SG_LOG(SG_NASAL, SG_ALERT, testName <<
": Test passed");
280 if (naIsFunc(tearDown)) {
281 nasalSys->callWithContext(ctx, tearDown, 0,
nullptr ,localNS.get_naRef());
int nasalStructEqual(naContext ctx, naRef a, naRef b)
@breif wrapper for naEqual which recursively checks vec/hash equality Probably not very performant.