Skip to content

Commit

Permalink
Update EnumsTests.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
TeknoPT committed Sep 21, 2023
1 parent 3040712 commit f7105b9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Library/tests/Contracts/EnumsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[], byte[]>(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<ItemKind>();
var expectedVal = ItemKind.Sword;

Assert.IsTrue(newVal == expectedVal);
}
}

0 comments on commit f7105b9

Please sign in to comment.