From f7105b90bf7c52e423ca6c2b607d1c740cc47be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Novo?= <34069419+TeknoPT@users.noreply.github.com> Date: Thu, 21 Sep 2023 16:55:20 +0100 Subject: [PATCH 1/4] Update EnumsTests.cs --- Library/tests/Contracts/EnumsTests.cs | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) 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 From 3fa6f0f378fcb53e47ac537b66a11e4a3ea65bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Novo?= <34069419+TeknoPT@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:03:04 +0100 Subject: [PATCH 2/4] Update StructTests.cs --- Library/tests/Contracts/StructTests.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/tests/Contracts/StructTests.cs b/Library/tests/Contracts/StructTests.cs index bc7739a..1840787 100644 --- a/Library/tests/Contracts/StructTests.cs +++ b/Library/tests/Contracts/StructTests.cs @@ -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; } }"; @@ -168,7 +168,7 @@ public testMyComplexStruct (name:string, age:number, _myEnum: MyEnum) : MyComple var returnObject = obj.AsStruct(); 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); @@ -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; } }"; @@ -249,7 +250,7 @@ public testMyComplexStruct (myComplexStruct:MyComplexStruct) : MyComplexStruct { var myResultStruct = obj.AsStruct(); 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); From 90ce27f1203d96d471b3dd5752d5173472169c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Novo?= <34069419+TeknoPT@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:16:45 +0100 Subject: [PATCH 3/4] Create EventTests.cs --- Library/tests/Contracts/EventTests.cs | 102 ++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Library/tests/Contracts/EventTests.cs diff --git a/Library/tests/Contracts/EventTests.cs b/Library/tests/Contracts/EventTests.cs new file mode 100644 index 0000000..47b04b6 --- /dev/null +++ b/Library/tests/Contracts/EventTests.cs @@ -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(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(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); + + } +} \ No newline at end of file From 3d10a299d07f6ac8d247d525d8a50efafaa6c9fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Novo?= <34069419+TeknoPT@users.noreply.github.com> Date: Fri, 22 Sep 2023 14:21:34 +0100 Subject: [PATCH 4/4] Update MethodTests.cs --- Library/tests/Contracts/MethodTests.cs | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Library/tests/Contracts/MethodTests.cs b/Library/tests/Contracts/MethodTests.cs index 197daf9..5d6d416 100644 --- a/Library/tests/Contracts/MethodTests.cs +++ b/Library/tests/Contracts/MethodTests.cs @@ -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; @@ -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(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); + } } \ No newline at end of file