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

[Compiler Fix] https://github.com/neo-project/neo-devpack-dotnet/issues/992 #994

Merged
merged 6 commits into from
Mar 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ private void ConvertIdentifierNameExpression(SemanticModel model, IdentifierName
Call(model, property.GetMethod!);
}
break;
case ITypeSymbol type:
IsType(type.GetPatternType());
Push(true);
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
throw new CompilationException(expression, DiagnosticId.SyntaxNotSupported, $"Unsupported symbol: {symbol}");
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Neo.Compiler.CSharp.TestContracts/Contract_Pattern.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Numerics;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services;

Expand Down Expand Up @@ -74,5 +75,16 @@ public void TestTypePattern(object o1)
case bool: break;
}
}

public int TestTypePattern2(object t)
{
return t switch
{
byte[] or string or ByteString => 0,
bool => 1,
BigInteger => 2,
_ => 5
};
}
}
}
45 changes: 45 additions & 0 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Pattern.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.TestEngine;
using Neo.VM.Types;

namespace Neo.Compiler.CSharp.UnitTests
{
Expand Down Expand Up @@ -86,5 +87,49 @@ public void Between3_Test()
value = result.Pop().GetBoolean();
Assert.AreEqual(false, value);
}

[TestMethod]
public void TestTypePattern_Test()
{
using var testengine = new TestEngine();
testengine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_Pattern.cs");

var result = testengine.ExecuteTestCaseStandard("testTypePattern2", 1);
var value = result.Pop().GetInteger();
Assert.AreEqual(2, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("testTypePattern2", "1");

value = result.Pop().GetInteger();
Assert.AreEqual(0, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("testTypePattern2", new ByteString(new byte[] { 1 }));
value = result.Pop().GetInteger();
Assert.AreEqual(0, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("testTypePattern2", new byte[] { 1 });
value = result.Pop().GetInteger();
Assert.AreEqual(0, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("testTypePattern2", true);
value = result.Pop().GetInteger();
Assert.AreEqual(1, value);

testengine.Reset();
testengine.ExecuteTestCaseStandard("testTypePattern", "1");
Assert.AreEqual(VM.VMState.HALT, testengine.State);

testengine.Reset();
testengine.ExecuteTestCaseStandard("testTypePattern", 1);
Assert.AreEqual(VM.VMState.HALT, testengine.State);

testengine.Reset();
testengine.ExecuteTestCaseStandard("testTypePattern", true);
Assert.AreEqual(VM.VMState.HALT, testengine.State);
}
}
}
Loading