Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EnumsTests.cs - Added one more test #40

Merged
merged 4 commits into from
Sep 22, 2023
Merged
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
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);
}
}
102 changes: 102 additions & 0 deletions Library/tests/Contracts/EventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Numerics;
using Phantasma.Core.Cryptography;
using Phantasma.Core.Domain.Execution.Enums;
using Phantasma.Core.Domain.VM;
using Phantasma.Core.Utils;
using Phantasma.Tomb.Compilers;

namespace TOMBLib.Tests.Contracts;

public class EventTests
{
[Test]
public void TestEvent()
{
var sourceCode =
@"
contract test {
import Runtime;

event MyPayment:number = ""{address} paid {data}""; // here we use a short-form description

public paySomething(from:address, x:number)
{
local price: number = 10;
local thisAddr:address = $THIS_ADDRESS;
emit MyPayment(from, price);
}
}
";

var parser = new TombLangCompiler();
var contract = parser.Process(sourceCode).First();

var storage = new Dictionary<byte[], byte[]>(new ByteArrayComparer());

var payStuff = contract.abi.FindMethod("paySomething");
Assert.IsNotNull(payStuff);

// Test for the 1st case
var vm = new TestVM(contract, storage, payStuff);
var keys = PhantasmaKeys.Generate();
vm.Stack.Push(VMObject.FromObject((BigInteger)10));
vm.Stack.Push(VMObject.FromObject(keys.Address));
var result = vm.Execute();
Assert.IsTrue(result == ExecutionState.Halt);

Assert.IsTrue(vm.Stack.Count == 1);
}


[Test]
public void TestEventWithDescription()
{
var sourceCode =
@"
description payment_event {

code(from:address, amount:number): string {
local result:string = """";
result += from;
result += "" paid "";
result += amount;
return result;
}
}

contract test {
import Runtime;

event MyPayment:number = payment_event; // here we use a short-form declaration


public paySomething(from:address, x:number)
{
local price: number = 10;
local thisAddr:address = $THIS_ADDRESS;
emit MyPayment(from, price);
}
}
";


var parser = new TombLangCompiler();
var contract = parser.Process(sourceCode).First();

var storage = new Dictionary<byte[], byte[]>(new ByteArrayComparer());

var payStuff = contract.abi.FindMethod("paySomething");
Assert.IsNotNull(payStuff);

// Test for the 1st case
var vm = new TestVM(contract, storage, payStuff);
var keys = PhantasmaKeys.Generate();
vm.Stack.Push(VMObject.FromObject((BigInteger)10));
vm.Stack.Push(VMObject.FromObject(keys.Address));
var result = vm.Execute();
Assert.IsTrue(result == ExecutionState.Halt);

Assert.IsTrue(vm.Stack.Count == 1);

}
}
48 changes: 48 additions & 0 deletions Library/tests/Contracts/MethodTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using NUnit.Framework;
using Phantasma.Core.Cryptography;
using Phantasma.Core.Domain;
Expand Down Expand Up @@ -135,4 +136,51 @@ public fetch(val:number) : number

Assert.IsTrue(result == 11);
}

[Test]
public void TestContractLengthViaParamNameWithNumber()
{
var sourceCode =
@"
contract test {
import Call;

private countLetters(x1:string) : number
{
if ( x1.length() == 0 ) {
return 0;
}

return x1.length();
}

public fetch(val:string) : number
{
return this.countLetters(val);
}
}";

var parser = new TombLangCompiler();
var contract = parser.Process(sourceCode).First();

var storage = new Dictionary<byte[], byte[]>(new ByteArrayComparer());

TestVM vm;

var keys = PhantasmaKeys.Generate();

// call fetch
var fetch = contract.abi.FindMethod("fetch");
Assert.IsNotNull(fetch);

vm = new TestVM(contract, storage, fetch);
vm.Stack.Push(VMObject.FromObject("helloworld"));
var state = vm.Execute();
Assert.IsTrue(state == ExecutionState.Halt);
var result = vm.Stack.Pop().AsNumber();
BigInteger expected = "helloworld".Length;


Assert.AreEqual(expected, result);
}
}
7 changes: 4 additions & 3 deletions Library/tests/Contracts/StructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ contract test{
public testMyComplexStruct (name:string, age:number, _myEnum: MyEnum) : MyComplexStruct {
local myStruct : MyLocalStruct = Struct.MyLocalStruct(name, age);
local myStructWithEnum : MyStructWithEnum = Struct.MyStructWithEnum(name, age, _myEnum, myStruct);
local myComplextStruct : MyComplexStruct = Struct.MyComplexStruct(name, age, myStruct, _myEnum, myStructWithEnum);
local myComplextStruct : MyComplexStruct = Struct.MyComplexStruct(name, age, myStruct, MyEnum.Second, myStructWithEnum);
return myComplextStruct;
}
}";
Expand Down Expand Up @@ -168,7 +168,7 @@ public testMyComplexStruct (name:string, age:number, _myEnum: MyEnum) : MyComple
var returnObject = obj.AsStruct<MyComplexStruct>();
Assert.AreEqual(myStruct.name,returnObject.name );
Assert.AreEqual(myStruct.age, (BigInteger)10);
Assert.AreEqual(MyEnum.First, returnObject.myEnum);
Assert.AreEqual(MyEnum.Second, returnObject.myEnum);
Assert.AreEqual(myStruct.name,returnObject.myStructWithEnum.name );
Assert.AreEqual(myStruct.age, returnObject.myStructWithEnum.age);
Assert.AreEqual(MyEnum.First, returnObject.myStructWithEnum.myEnum);
Expand Down Expand Up @@ -215,6 +215,7 @@ public testMyComplexStruct (myComplexStruct:MyComplexStruct) : MyComplexStruct {
myComplexStruct.age = 20;
myComplexStruct.myStructWithEnum.name = ""Something else"";
myComplexStruct.myStructWithEnum.localStruct.age = 30;
myComplexStruct.myEnum = MyEnum.Second;
return myComplextStruct;
}
}";
Expand Down Expand Up @@ -249,7 +250,7 @@ public testMyComplexStruct (myComplexStruct:MyComplexStruct) : MyComplexStruct {
var myResultStruct = obj.AsStruct<MyComplexStruct>();
Assert.AreEqual(myComplexStructStruct.name,myResultStruct.name );
Assert.AreEqual((BigInteger)20, myResultStruct.age);
Assert.AreEqual(myComplexStructStruct.myEnum, myResultStruct.myEnum);
Assert.AreEqual(MyEnum.Second, myResultStruct.myEnum);
Assert.AreEqual("Something else",myResultStruct.myStructWithEnum.name );
Assert.AreEqual(myComplexStructStruct.myStructWithEnum.age, myResultStruct.myStructWithEnum.age);
Assert.AreEqual(myComplexStructStruct.myStructWithEnum.myEnum, myResultStruct.myStructWithEnum.myEnum);
Expand Down
Loading