diff --git a/Library/tests/Contracts/EnumsTests.cs b/Library/tests/Contracts/EnumsTests.cs index b2d1f7a..bcce0ba 100644 --- a/Library/tests/Contracts/EnumsTests.cs +++ b/Library/tests/Contracts/EnumsTests.cs @@ -74,4 +74,66 @@ public void Enums() Assert.IsTrue(newVal == expectedVal); } + + private enum ItemKind + { + Sword, + Shield, + Knife + } + + [Test] + public void TestEnumsWithValues() + { + string[] sourceCode = new string[] + { + "enum ItemKind { Sword=0, Shield=1, Knife=2}", + "contract test{", + $"global state: ItemKind;", + "constructor(owner:address) {", + "state = ItemKind.Sword;}", + "public getValue():ItemKind {", + "return state;}", + "public isSet(val:ItemKind):bool {", + "return state.isSet(val);}", + "}" + }; + + var parser = new TombLangCompiler(); + var contract = parser.Process(sourceCode).First(); + + var storage = new Dictionary(new ByteArrayComparer()); + + TestVM vm; + + var constructor = contract.abi.FindMethod(SmartContract.ConstructorName); + Assert.IsNotNull(constructor); + + var keys = PhantasmaKeys.Generate(); + + vm = new TestVM(contract, storage, constructor); + vm.Stack.Push(VMObject.FromObject(keys.Address)); + var result = vm.Execute(); + Assert.IsTrue(result == ExecutionState.Halt); + + Assert.IsTrue(storage.Count == 1); + + // call getVal + var getValue = contract.abi.FindMethod("getValue"); + Assert.IsNotNull(getValue); + + vm = new TestVM(contract, storage, getValue); + result = vm.Execute(); + Assert.IsTrue(result == ExecutionState.Halt); + + Assert.IsTrue(storage.Count == 1); + + Assert.IsTrue(vm.Stack.Count == 1); + + var obj = vm.Stack.Pop(); + var newVal = obj.AsEnum(); + var expectedVal = ItemKind.Sword; + + Assert.IsTrue(newVal == expectedVal); + } } \ No newline at end of file