diff --git a/src/DelegateDecompiler.Tests/ThrowIntegrationTests.cs b/src/DelegateDecompiler.Tests/ThrowIntegrationTests.cs new file mode 100644 index 00000000..7de52aea --- /dev/null +++ b/src/DelegateDecompiler.Tests/ThrowIntegrationTests.cs @@ -0,0 +1,44 @@ +using System; +using System.Linq.Expressions; +using NUnit.Framework; + +namespace DelegateDecompiler.Tests +{ + [TestFixture] + public class ThrowTests : DecompilerTestsBase + { + [Test] + public void SimpleThrow() + { + Action compiled = () => throw new ArgumentException("test"); + + // Create expected expression programmatically to match what decompiler produces + var constructor = typeof(ArgumentException).GetConstructor(new[] { typeof(string) }); + var messageConstant = Expression.Constant("test"); + var newExpression = Expression.New(constructor, messageConstant); + var throwExpression = Expression.Throw(newExpression); + var expectedExpression = Expression.Lambda(throwExpression); + + Test(compiled, expectedExpression); + } + + [Test] + public void ConditionalThrowInTernaryOperator() + { + Func compiled = x => x > 0 ? "positive" : throw new ArgumentException("negative"); + + // Create expected expression programmatically for conditional with throw + var parameter = Expression.Parameter(typeof(int), "x"); + var positiveConstant = Expression.Constant("positive"); + var constructor = typeof(ArgumentException).GetConstructor(new[] { typeof(string) }); + var messageConstant = Expression.Constant("negative"); + var newExpression = Expression.New(constructor, messageConstant); + var throwExpression = Expression.Throw(newExpression, typeof(string)); + var condition = Expression.GreaterThan(parameter, Expression.Constant(0)); + var conditionalExpression = Expression.Condition(condition, positiveConstant, throwExpression); + var expectedExpression = Expression.Lambda>(conditionalExpression, parameter); + + Test(compiled, expectedExpression); + } + } +} \ No newline at end of file diff --git a/src/DelegateDecompiler/Processor.cs b/src/DelegateDecompiler/Processor.cs index 64de954d..e73c87f2 100644 --- a/src/DelegateDecompiler/Processor.cs +++ b/src/DelegateDecompiler/Processor.cs @@ -59,6 +59,7 @@ public static Expression Process(bool isStatic, VariableInfo[] locals, IList