diff --git a/src/Neo.SmartContract.Framework/ByteString.Extension.cs b/src/Neo.SmartContract.Framework/ByteString.Extension.cs new file mode 100644 index 000000000..eeae195d4 --- /dev/null +++ b/src/Neo.SmartContract.Framework/ByteString.Extension.cs @@ -0,0 +1,109 @@ +using Neo.SmartContract.Framework.Native; +namespace Neo.SmartContract.Framework; + +public static class ByteStringExtension +{ + /// + /// Denotes whether provided character is a number. + /// + /// Input to check + /// True if is number + public static bool IsNumber(this ByteString byteString) + { + foreach (var value in byteString) + { + if (value is < 48 or > 57) + return false; + } + return true; + } + + /// + /// Denotes whether provided character is a lowercase letter. + /// + /// Input to check + /// True if is Alpha character + public static bool IsLowerAlphabet(this ByteString byteString) + { + foreach (var value in byteString) + { + if (value is < 97 or > 122) + return false; + } + return true; + } + + /// + /// Denotes whether provided character is a lowercase letter. + /// + /// Input to check + /// True if is Alpha character + public static bool IsUpperAlphabet(this ByteString byteString) + { + foreach (var value in byteString) + { + if (value is < 65 or > 90) + return false; + } + return true; + } + + /// + /// Denotes whether provided character is a lowercase letter. + /// + /// Input to check + /// True if is Alpha character + public static bool IsAlphabet(this ByteString byteString) + { + foreach (var value in byteString) + { + if (!((value >= 65 && value <= 90) || (value >= 97 && value <= 122))) + return false; + } + return true; + } + + /// + /// Returns the index of the first occurrence of a given value in an array. + /// + /// Array where to search. + /// Array to search. + /// Index where it is located or -1 + public static int IndexOf(this ByteString byteString, ByteString byteToFind) + { + return StdLib.MemorySearch(byteString, byteToFind); + } + + /// + /// Determines whether the beginning of this string instance matches the specified string when compared using the specified culture. + /// + /// Array where to search. + /// Array to search. + /// True if start with + public static bool StartWith(this ByteString byteString, ByteString byteToFind) + { + return StdLib.MemorySearch(byteString, byteToFind) == 0; + } + + /// + /// Determines whether the end of this string instance matches a specified string. + /// + /// Array where to search. + /// Array to search. + /// True if ends with + public static bool EndsWith(this ByteString byteString, ByteString byteToFind) + { + return StdLib.MemorySearch(byteString, byteToFind) + byteToFind.Length == byteString.Length; + } + + /// + /// Checks if the contains the given . + /// + /// to search. + /// to be searched. + /// + public static bool Contains(this ByteString byteString, ByteString byteToFind) + { + return StdLib.MemorySearch(byteString, byteToFind) != -1; + } +} diff --git a/tests/Neo.SmartContract.Framework.TestContracts/Contract_Regex.cs b/tests/Neo.SmartContract.Framework.TestContracts/Contract_Regex.cs new file mode 100644 index 000000000..718ef879b --- /dev/null +++ b/tests/Neo.SmartContract.Framework.TestContracts/Contract_Regex.cs @@ -0,0 +1,45 @@ +namespace Neo.SmartContract.Framework.UnitTests.TestClasses +{ + public class Contract_Regex : SmartContract + { + public static bool TestStartWith() + { + return ((ByteString)"Hello World").StartWith("Hello"); + } + + public static int TestIndexOf() + { + return ((ByteString)"Hello World").IndexOf("o"); + } + + public static bool TestEndWith() + { + return ((ByteString)"Hello World").EndsWith("World"); + } + + public static bool TestContains() + { + return ((ByteString)"Hello World").Contains("ll"); + } + + public static bool TestNumberOnly() + { + return ((ByteString)"0123456789").IsNumber(); + } + + public static bool TestAlphabetOnly() + { + return ((ByteString)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").IsAlphabet(); + } + + public static bool TestLowerAlphabetOnly() + { + return ((ByteString)"abcdefghijklmnopqrstuvwxyz").IsAlphabet(); + } + + public static bool TestUpperAlphabetOnly() + { + return ((ByteString)"ABCDEFGHIJKLMNOPQRSTUVWXYZ").IsAlphabet(); + } + } +} diff --git a/tests/Neo.SmartContract.Framework.UnitTests/RegexTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/RegexTest.cs new file mode 100644 index 000000000..4ff505b97 --- /dev/null +++ b/tests/Neo.SmartContract.Framework.UnitTests/RegexTest.cs @@ -0,0 +1,82 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.VM.Types; +using Neo.SmartContract.TestEngine; + +namespace Neo.SmartContract.Framework.UnitTests +{ + [TestClass] + public class RegexTest + { + private TestEngine.TestEngine _engine; + + [TestInitialize] + public void Init() + { + _engine = new TestEngine.TestEngine(snapshot: new TestDataCache()); + _engine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_Regex.cs"); + } + + [TestMethod] + public void TestStartWith() + { + var result = _engine.ExecuteTestCaseStandard("testStartWith"); + Assert.AreEqual(1, result.Count); + var item = result.Pop(); + Assert.IsTrue(item.GetBoolean()); + } + + [TestMethod] + public void TestIndexOf() + { + _engine.Reset(); + var result = _engine.ExecuteTestCaseStandard("testIndexOf"); + Assert.AreEqual(1, result.Count); + var item = result.Pop(); + Assert.AreEqual(4, item.GetInteger()); + } + + [TestMethod] + public void TestEndWith() + { + var result = _engine.ExecuteTestCaseStandard("testEndWith"); + Assert.AreEqual(1, result.Count); + var item = result.Pop(); + Assert.IsTrue(item.GetBoolean()); + } + [TestMethod] + public void TestContains() + { + var result = _engine.ExecuteTestCaseStandard("testContains"); + Assert.AreEqual(1, result.Count); + var item = result.Pop(); + Assert.IsTrue(item.GetBoolean()); + } + + [TestMethod] + public void TestNumberOnly() + { + var result = _engine.ExecuteTestCaseStandard("testNumberOnly"); + Assert.AreEqual(1, result.Count); + var item = result.Pop(); + Assert.IsTrue(item.GetBoolean()); + } + + [TestMethod] + public void TestAlphabetOnly() + { + var result = _engine.ExecuteTestCaseStandard("testAlphabetOnly"); + Assert.AreEqual(1, result.Count); + Assert.IsTrue(result.Pop().GetBoolean()); + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("testLowerAlphabetOnly"); + Assert.AreEqual(1, result.Count); + Assert.IsTrue(result.Pop().GetBoolean()); + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("testUpperAlphabetOnly"); + Assert.AreEqual(1, result.Count); + Assert.IsTrue(result.Pop().GetBoolean()); + } + } +} diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/ContractTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/ContractTest.cs index b05518240..aa364aec8 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/ContractTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/ContractTest.cs @@ -5,7 +5,6 @@ using Neo.VM; using Neo.VM.Types; using System; -using Neo.Persistence; using Neo.SmartContract.TestEngine; using Array = Neo.VM.Types.Array; diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs index 38b3fd537..c9a83b933 100644 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs +++ b/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs @@ -9,7 +9,7 @@ namespace Neo.SmartContract.Template.UnitTests.templates.neocontractowner { /// - /// You need to build the solution to resolve Ownable class. + /// You need to build the solution to resolve OracleRequest class. /// [TestClass] public class OracleRequestTests : TestBase