Beispiel #1
0
        public void StaticClassTest()
        {
            string code = @"
static class Log {
	entryCount = 0;
	text = """";

	addRecord(record) {
		entryCount++;
		text += record + ""\n"";
	}
}

Log.addRecord(""Log 1"");
Log.addRecord(""Log 2"");

var size = Log.entryCount;
var log = Log.text;
";

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(2), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("size"));
            Assert.Equal(new DefaultStringValue <GroupState>("Log 1\nLog 2\n"), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("log"));
        }
Beispiel #2
0
        public void NamespaceTest()
        {
            string code = @"
namespace Geometry {
	class Line {
		a;
		b;
		() {
			a = new Point();
			b = new Point();
			Shapes.count++;
		}
	}

	class Point {
		x;
		y;
		() {
			x = 0;
			y = 0;
			Shapes.count++;
		}
	}

	static class Shapes {
		count = 0;
	}
}

using Geometry.Line;
using Geometry.Shapes;

var line = new Line();
line.a.{
	x = 10,
	y = 20
};
line.b.{
	x = 70,
	y = 80
};
var aX = line.a.x;
var aY = line.a.y;
var bX = line.b.x;
var bY = line.b.y;
var count = Shapes.count;
";

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(10), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("aX"));
            Assert.Equal(new DefaultIntValue <GroupState>(20), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("aY"));
            Assert.Equal(new DefaultIntValue <GroupState>(70), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("bX"));
            Assert.Equal(new DefaultIntValue <GroupState>(80), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("bY"));
            Assert.Equal(new DefaultIntValue <GroupState>(3), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("count"));
        }
Beispiel #3
0
        public void NativeTest()
        {
            string code = @"
			var value1 = $System.Math.Max(3, 5);
			var value2 = $System.Math.PI;
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(5), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value1"));
            Assert.Equal(new DefaultDoubleValue <GroupState>(Math.PI), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value2"));
        }
Beispiel #4
0
        public void DecrementTest()
        {
            string code = @"
			var number1 = 10;
			var number2 = 10;
			var number3 = number1-- - --number2;
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(9), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("number1"));
            Assert.Equal(new DefaultIntValue <GroupState>(9), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("number2"));
            Assert.Equal(new DefaultIntValue <GroupState>(1), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("number3"));
        }
Beispiel #5
0
        public void ArraySetterTest()
        {
            string code = @"
var property = new {
	45,
	200000
};
var number = property[0];
var value = property[1];
";

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(45), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("number"));
            Assert.Equal(new DefaultIntValue <GroupState>(200000), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value"));
        }
Beispiel #6
0
        public void MapSetterTest()
        {
            string code = @"
var property = new [].{
	""number"" = 45,
	""value"" = 200000
};
var number = property.number;
var value = property.value;
";

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(45), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("number"));
            Assert.Equal(new DefaultIntValue <GroupState>(200000), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value"));
        }
Beispiel #7
0
        public void PlusAssignmentTest()
        {
            string code = @"
			var number = 10;
			number += 20;
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(30), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("number"));
        }
Beispiel #8
0
        public void ForLoopTest()
        {
            string code = @"
			var value = 0;
			for (var i = 1, i < 10, i++) {
				value += i;
			}
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(45), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value"));
        }
Beispiel #9
0
        public void WhileLoopTest()
        {
            string code = @"
			var value = 1;
			while (value < 10) {
				value++;
			}
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(10), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value"));
        }
Beispiel #10
0
        public void ForEachLoopTest()
        {
            string code = @"
			var array = new { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
			var value = 0;
			foreach (var num of array) {
				value += num;
			}
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(45), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value"));
        }
Beispiel #11
0
        public void ContinueWhileLoopTest()
        {
            string code = @"
			var value = 1;
			var i = 1;
			while (i < 10) {
				i++;
				if (i == 5) {
					continue;
				}
				value++;
			}
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(9), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("value"));
        }
Beispiel #12
0
        public void SubClassOverrideTest()
        {
            string code = @"
class C1 {
	size;
	setC1() {
		setC4();
	}
	setC4() {
		size = 40;
	}
}

class C2 : C1 {
}

class C3 : C2 {
	size;
	setC4() {
		size = 50;
	}

	getC3() {
		return size;
	}
}

var c3 = new C3();
c3.setC1();
var size = c3.getC3();
";

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(50), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("size"));
        }
Beispiel #13
0
        public void HostedClassTest()
        {
            string pointCode = @"
namespace Geometry {

	class Point {
		x;
		y;
		() {
			x = 0;
			y = 0;
			Shapes.count++;
		}
	}

	static class Shapes {
		count = 0;
	}
}
";
            string lineCode  = @"
using Geometry.Point;
using Geometry.Shapes;

namespace Geometry {
	class Line {
		a;
		b;
		() {
			a = new Point();
			b = new Point();
			Shapes.count++;
		}
	}
}

using Geometry.Line;

var line = new Line();
line.a.{
	x = 10,
	y = 20
};
line.b.{
	x = 70,
	y = 80
};
var aX = line.a.x;
var aY = line.a.y;
var bX = line.b.x;
var bY = line.b.y;
var count = Shapes.count;
";

            InstructionExecutor <GroupState> executor = new InstructionExecutor <GroupState>()
            {
                ExternalDynamicPointers = NativeLocator.GetNativePointer,
                ValueProvider           = new ValueProvider()
            };

            Run(pointCode, executor);

            ExecutionState <GroupState> executionState = Run(lineCode, executor);

            Assert.Equal(new DefaultIntValue <GroupState>(10), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("aX"));
            Assert.Equal(new DefaultIntValue <GroupState>(20), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("aY"));
            Assert.Equal(new DefaultIntValue <GroupState>(70), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("bX"));
            Assert.Equal(new DefaultIntValue <GroupState>(80), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("bY"));
        }
Beispiel #14
0
        public void SubClassTest()
        {
            string code = @"
class Property {
	address;
	value;
	size;

	(address) {
		self.address = address;
		value = 0;
		size = 0;
	}
	
	getValueRatio() {
		if (size > 0) {
			return value / size;
		}
		return 0;
	}

	getType() => ""Unknown"";
}

class House : Property {
	floors;

	(address) {
		self.address = address;
		value = 0;
		size = 0;
		floors = 1;
	}

	getType() => ""House"";	
	
	getSizeRatio() {
		if (getSize() > 0) {
			return getSize() / floors;
		}
		return 0;
	}

	getSize() => size;
}

var property = new Property(""45 Grove St"").{
	value = 15000,
	size = 2000
};

var house = new House(""40 Grove St"").{
	value = 20000,
	size = 1000,
	floors = 2
};
var houseValue = house.value;
var propertyValue = property.value;
var type = house.getType();
var sizeRatio = house.getSizeRatio();
var valueRatio = house.getValueRatio();
";

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultIntValue <GroupState>(20000), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("houseValue"));
            Assert.Equal(new DefaultIntValue <GroupState>(15000), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("propertyValue"));
            Assert.Equal(new DefaultStringValue <GroupState>("House"), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("type"));
            Assert.Equal(new DefaultIntValue <GroupState>(500), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("sizeRatio"));
            Assert.Equal(new DefaultIntValue <GroupState>(20), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("valueRatio"));
        }
Beispiel #15
0
        public void ArrayTest()
        {
            string code = @"
			var array = new [];
			array[0] = 20;
			array[1] = ""world"";
			array[2] = true;
			array[3] = ""hello"";
			array[""ab""] = array[1];
			array[1] = 10.5;
			var combo = """" + array[0] + array[1] + array[2] + array[3] + array[""ab""];
			"            ;

            ExecutionState <GroupState> executionState = Run(code);

            Assert.Equal(new DefaultStringValue <GroupState>("2010.5truehelloworld"), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("combo"));
        }