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

Fix concat #356

Merged
merged 9 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mono.Cecil;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -645,7 +646,18 @@ private int ConvertCall(OpCode src, NeoMethod to)
else if (src.tokenMethod.Contains("::Concat("))
{
//"System.String System.String::Concat(System.String,System.String)"
Convert1by1(VM.OpCode.CAT, src, to);
if (src.tokenUnknown is MethodReference mref)
{
//Concat can have more than two arguments
for (int x = 0; x < mref.Parameters.Count - 1; x++)
{
Convert1by1(VM.OpCode.CAT, src, to);
}
}
else
{
Convert1by1(VM.OpCode.CAT, src, to);
}
Insert1(VM.OpCode.CONVERT, "", to, new byte[] { (byte)VM.Types.StackItemType.ByteString });
return 0;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Neo.SmartContract.Framework;

namespace Neo.Compiler.MSIL.UnitTests.TestClasses
{
public class Contract_Concat : SmartContract.Framework.SmartContract
{
public static string TestStringAdd1(string a)
{
return a + "hello";
}

public static string TestStringAdd2(string a, string b)
{
return a + b + "hello";
}

public static string TestStringAdd3(string a, string b, string c)
{
return a + b + c + "hello";
}
}
}
47 changes: 47 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.VM;
using Neo.VM.Types;

namespace Neo.Compiler.MSIL.UnitTests
{
[TestClass]
public class UnitTest_Concat
{
private TestEngine _engine;

[TestInitialize]
public void Init()
{
_engine = new TestEngine();
_engine.AddEntryScript("./TestClasses/Contract_Concat.cs");
}

[TestMethod]
public void TestStringAdd1()
{
_engine.Reset();
var result = _engine.GetMethod("testStringAdd1").Run("a").ConvertTo(StackItemType.ByteString);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual("ahello", result.GetString());
}

[TestMethod]
public void TestStringAdd2()
{
_engine.Reset();
var result = _engine.GetMethod("testStringAdd2").Run("a", "b").ConvertTo(StackItemType.ByteString);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual("abhello", result.GetString());
}

[TestMethod]
public void TestStringAdd3()
{
_engine.Reset();
var result = _engine.GetMethod("testStringAdd3").Run("a", "b", "c").ConvertTo(StackItemType.ByteString);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual("abchello", result.GetString());
}
}
}