Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 10 additions & 120 deletions Test.hx
Original file line number Diff line number Diff line change
@@ -1,131 +1,21 @@
package;

import hscript.AbstractHScriptClass;
import utest.Runner;
import utest.ui.Report;

import cases.ClassFeaturesCase;
import cases.LanguageFeaturesCase;

class Test
{
static function main()
{
// TODO: Make proper unit tests, this stinks

var parser = new hscript.SuperParser();
var interp = new hscript.SuperInterp();

var script:String = "
package hscript;

import haxe.ds.StringMap;
import haxe.io.Path;

class TestClass extends TestClass
{
public static var hscriptStaticVar:Float = 2.7;

static public function myStaticFunc(add:Float, mult:Float, div:Float = 1.3)
{
trace(hscriptStaticVar + add);
trace(hscriptStaticVar / div);
staticPrivateFunc();
return hscriptStaticVar * mult;
}




public var hscriptVar:String = 'Hello World!';

public function new(num:Int, str:String)
{
trace('instanced!');
trace(super);
super();
trace(num);
trace(str);
trace(super);
}

public function hscriptFunc(add:Float)
{
trace('hscript function called');
trace(super.myVar + add);
}

private function privateFunc()
{
trace('private func');
trace(super.myFunc('bruh', 555555.5555));
}

private static function staticPrivateFunc()
{
mult = 10; // going to ignore this, should not happen;
trace('called the private function from static class');
}
}
";
trace("created interp and parser");

var program = parser.parseString(script, "TestScript.hx", 0 );
trace("parsed script");
parser.resumeErrors = true;
var declarations = parser.parseModule(script, "TestScript.hx", 0 );
parser.resumeErrors = false;
trace("parsed modules");

trace("executing");

interp.execute(program);

trace("registering structures");
var runner = new Runner();

interp.registerStructures(declarations);
runner.addCase(new LanguageFeaturesCase());
runner.addCase(new ClassFeaturesCase());

trace("getting class");

var cls:AbstractHScriptClass = hscript.SuperInterp.resolveClass("hscript.TestClass");
trace("got the class");

//try { trace(cls.hscriptStaticVar); } catch(e:Dynamic) { trace(e); } // works
//try { trace(cls.myStaticFunc(5, 3)); } catch(e:Dynamic) { trace(e); } // works, need to test to see if function arguments remain in the local variables list
//try { trace(cls.staticPrivateFunc()); } catch(e:Dynamic) { trace(e); } // works
// dont need to test for class extensions because static classes do not need to extend other classes

var inst:AbstractHScriptClass = cls.createInstance(85, "hi");

try { trace(inst.hscriptVar); } catch(e:Dynamic) { trace(e); }
try { trace(inst.hscriptFunc(2.5)); } catch(e:Dynamic) { trace(e); }
try { trace(inst.privateFunc()); } catch(e:Dynamic) { trace(e); }
try { trace(inst.myVar); } catch(e:Dynamic) { trace(e); }
try { trace(inst.myFunc(123,456)); } catch(e:Dynamic) { trace(e); }
try { trace(inst.super.myFunc(123,456)); } catch(e:Dynamic) { trace(e); }
try { inst.myVar += 8; } catch(e:Dynamic) { trace(e); }
try { trace(inst.myVar); } catch(e:Dynamic) { trace(e); }
try { trace(inst.myPrivateFunc(1)); } catch(e:Dynamic) { trace(e); }

trace("DONE!");
}
}

class TestClass
{
public static var staticVar:Float = 9.30;

public var myVar:Int = 1;

public function new()
{
trace("CONSTRUCTOR CALLED!!!");
}

public function myFunc(arg1:Dynamic, arg2:Dynamic, arg3:Dynamic)
{
trace('my function was called with args $arg1 $arg2 and $arg3');
return 5;
}

private function myPrivateFunc()
{
trace('my private function was called');
return 'hello!';
Report.create(runner);
runner.run();
}
}
10 changes: 1 addition & 9 deletions build.hxml
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
-cp src
-m Test
-lib hscript

-D no-deprecation-warnings
-D no-invalid-expression-warnings
-D hscriptPos

--macro addGlobalMetadata('hscript', '@:build(hscript.macros.ParserMacro.buildAll())', false, true, false)
test/build-base.hxml
--interp
2 changes: 1 addition & 1 deletion hscript/SuperInterp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class SuperInterp extends ReInterp
switch( #if hscriptPos x #else e #end )
{
case EIdent(id):
var val = super.expr(e);
var val:Dynamic = super.expr(e);
if (blacklist.contains(val))
error(ECustom('Blacklisted expression $val referenced'));
case _:
Expand Down
15 changes: 10 additions & 5 deletions hscript/macros/ParserMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ class ParserMacro

function makeEnumField(name, kind):Field
{
return {
for (f in fields)
if (f.name == name)
return null;

var field:Field = {
name: name,
doc: null,
meta: [],
access: [],
kind: kind,
pos: Context.currentPos()
}
};
fields.push(field);
return field;
}
fields.push(
makeEnumField("TApostr", FVar(null, null))
);

makeEnumField("TApostr", FVar(null, null));
case _:
}

Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Build.*
7 changes: 7 additions & 0 deletions test/build-base.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extraParams.hxml
-m Test
-lib utest
-cp test/source

-D no-deprecation-warnings
-D no-invalid-expression-warnings
2 changes: 2 additions & 0 deletions test/build-hl.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/build-base.hxml
--hl test/Build.hl
2 changes: 2 additions & 0 deletions test/build-interp.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/build-base.hxml
--interp
2 changes: 2 additions & 0 deletions test/build-js.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/build-base.hxml
--js test/Build.js
2 changes: 2 additions & 0 deletions test/build-neko.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/build-base.hxml
--neko test/Build.n
39 changes: 39 additions & 0 deletions test/source/Shared.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import haxe.io.Path;
import sys.io.File;
import hscript.*;

function evalExpr(expr:String, ?vars: Map<String, Dynamic>, ?params:Array<Dynamic>):Dynamic
{
var parser = new SuperParser();
var interp = new SuperInterp();

var program = parser.parseString(expr, '<eval>', 0);

if (params != null)
interp.variables.set("params", params);

if (vars != null)
for (key => value in vars)
interp.variables.set(key, value);

return interp.execute(program);
}

function evalFile(file:String, ?vars:Map<String, Dynamic>):Dynamic
{
var expr = File.getContent(file);
var fname = Path.withoutDirectory(file);

var parser = new SuperParser();
var interp = new SuperInterp();

var program = parser.parseString(expr, fname, 0);
var modules = parser.parseModule(expr, fname, 0);

if (vars != null)
for (key => value in vars)
interp.variables.set(key, value);

interp.registerStructures(modules, file);
return interp.execute(program);
}
10 changes: 10 additions & 0 deletions test/source/cases/ClassFeaturesCase.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cases;

import utest.ITest;
import utest.Assert;
import hscript.*;

class ClassFeaturesCase implements ITest
{
public function new() {}
}
Loading