|
| 1 | +using EasySourceGenerators.Abstractions; |
| 2 | + |
| 3 | +namespace EasySourceGenerators.Tests; |
| 4 | + |
| 5 | +[TestFixture] |
| 6 | +public class FluentBodyReturningConstantTests |
| 7 | +{ |
| 8 | + [Test] |
| 9 | + public void FluentBodyReturningConstant_ProducesExpectedRuntimeOutput() |
| 10 | + { |
| 11 | + TestFluentConstantClass instance = new TestFluentConstantClass(); |
| 12 | + |
| 13 | + string result = instance.GetGreeting(); |
| 14 | + |
| 15 | + Assert.That(result, Is.EqualTo("Hello from fluent API")); |
| 16 | + } |
| 17 | + |
| 18 | + [Test] |
| 19 | + public void FluentBodyReturningConstant_ProducesExpectedGeneratedCode() |
| 20 | + { |
| 21 | + string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestFluentConstantClass_GetGreeting.g.cs"); |
| 22 | + string expectedCode = """ |
| 23 | + namespace EasySourceGenerators.Tests; |
| 24 | +
|
| 25 | + partial class TestFluentConstantClass |
| 26 | + { |
| 27 | + public partial string GetGreeting() |
| 28 | + { |
| 29 | + return "Hello from fluent API"; |
| 30 | + } |
| 31 | + } |
| 32 | + """.ReplaceLineEndings("\n").TrimEnd(); |
| 33 | + |
| 34 | + Assert.That(generatedCode, Is.EqualTo(expectedCode)); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void FluentBodyReturningConstant_Int_ProducesExpectedRuntimeOutput() |
| 39 | + { |
| 40 | + int result = TestFluentConstantIntClass.GetMagicNumber(); |
| 41 | + |
| 42 | + Assert.That(result, Is.EqualTo(42)); |
| 43 | + } |
| 44 | + |
| 45 | + [Test] |
| 46 | + public void FluentBodyReturningConstant_Int_ProducesExpectedGeneratedCode() |
| 47 | + { |
| 48 | + string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestFluentConstantIntClass_GetMagicNumber.g.cs"); |
| 49 | + string expectedCode = """ |
| 50 | + namespace EasySourceGenerators.Tests; |
| 51 | +
|
| 52 | + static partial class TestFluentConstantIntClass |
| 53 | + { |
| 54 | + public static partial int GetMagicNumber() |
| 55 | + { |
| 56 | + return 42; |
| 57 | + } |
| 58 | + } |
| 59 | + """.ReplaceLineEndings("\n").TrimEnd(); |
| 60 | + |
| 61 | + Assert.That(generatedCode, Is.EqualTo(expectedCode)); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +public partial class TestFluentConstantClass |
| 66 | +{ |
| 67 | + public partial string GetGreeting(); |
| 68 | + |
| 69 | + [MethodBodyGenerator(nameof(GetGreeting))] |
| 70 | + static IMethodBodyGenerator GetGreeting_Generator() => |
| 71 | + Generate |
| 72 | + .MethodBody().ForMethod().WithReturnType<string>().WithNoParameters() |
| 73 | + .BodyReturningConstant(() => "Hello from fluent API"); |
| 74 | +} |
| 75 | + |
| 76 | +public static partial class TestFluentConstantIntClass |
| 77 | +{ |
| 78 | + public static partial int GetMagicNumber(); |
| 79 | + |
| 80 | + [MethodBodyGenerator(nameof(GetMagicNumber))] |
| 81 | + static IMethodBodyGenerator GetMagicNumber_Generator() => |
| 82 | + Generate |
| 83 | + .MethodBody().ForMethod().WithReturnType<int>().WithNoParameters() |
| 84 | + .BodyReturningConstant(() => 42); |
| 85 | +} |
0 commit comments