Skip to content

Commit 05a8677

Browse files
Add 'assert' function, for testing.
1 parent 20c3cf0 commit 05a8677

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

run_tests.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
using namespace std;
4646

4747
/**
48-
* Generic
48+
* Generic JSON format logger.
49+
* It is used to generate call log.
4950
*/
5051
class JsonLogger
5152
{
@@ -86,6 +87,27 @@ class JsonLogger
8687
bool m_first;
8788
};
8889

90+
91+
/**
92+
* Assertion function exported to tests
93+
* @param pScope
94+
* @return
95+
*/
96+
Ref<JSValue> assertFunction(FunctionScope* pScope)
97+
{
98+
auto value = pScope->getParam("value");
99+
100+
if (!value->toBoolean())
101+
{
102+
auto text = pScope->getParam("text")->toString();
103+
104+
error("Assertion failed: %s", text.c_str());
105+
}
106+
107+
return undefined();
108+
}
109+
110+
89111
JsonLogger* s_curFunctionLogger = NULL;
90112

91113
bool run_test(const std::string& szFile, const string &testDir, const string& resultsDir)
@@ -107,12 +129,13 @@ bool run_test(const std::string& szFile, const string &testDir, const string& re
107129
Ref<IScope> globals = ObjectScope::create(globalsObj);
108130

109131
globals->set("result", jsInt(0));
132+
addNative("function assert(value, text)", assertFunction, globals);
110133
try
111134
{
112135
//This code is copied from 'evaluate', to log the intermediate results
113136
//generated from each state
114137
CScriptToken token (script.c_str());
115-
StatementList statements;
138+
AstNodeList statements;
116139

117140
//Parsing loop
118141
token = token.next();
@@ -144,7 +167,7 @@ bool run_test(const std::string& szFile, const string &testDir, const string& re
144167
s_curFunctionLogger->log(entry->getJSON(0));
145168
return undefined();
146169
};
147-
addNative("function callLogger(x)", logFn, globals);
170+
//addNative("function callLogger(x)", logFn, globals);
148171

149172
//Execution
150173
mvmExecute(code, globals);

0 commit comments

Comments
 (0)