|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.Loader; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Shouldly; |
| 9 | + |
| 10 | +namespace ManagedCode.Communication.Tests.Results; |
| 11 | + |
| 12 | +public sealed class ResultThrowIfProblemFlowTests |
| 13 | +{ |
| 14 | + private const string AssemblyName = "CommunicationThrowIfProblemRegression"; |
| 15 | + private const string TrustedAssembliesKey = "TRUSTED_PLATFORM_ASSEMBLIES"; |
| 16 | + private const string EntryType = "Scenario"; |
| 17 | + private const string EntryMethod = "Execute"; |
| 18 | + private const string Consumer = """ |
| 19 | + #nullable enable |
| 20 | + using ManagedCode.Communication; |
| 21 | + using ManagedCode.Communication.CollectionResultT; |
| 22 | +
|
| 23 | + public static class Scenario |
| 24 | + { |
| 25 | + private const string Payload = "payload"; |
| 26 | + private const string FailureTitle = "The entry could not be read."; |
| 27 | + private const string FailureDetail = "The requested entry does not exist."; |
| 28 | +
|
| 29 | + private static int Read(Result<string> result) |
| 30 | + { |
| 31 | + result.ThrowIfProblem(); |
| 32 | + return result.Value.Length; |
| 33 | + } |
| 34 | +
|
| 35 | + private static int ReadNullable(Result<string?> result) |
| 36 | + { |
| 37 | + result.ThrowIfProblem(); |
| 38 | + return result.Value.Length; |
| 39 | + } |
| 40 | +
|
| 41 | + private static int ReadCollection(CollectionResult<string> result) |
| 42 | + { |
| 43 | + result.ThrowIfProblem(); |
| 44 | + return result.Value.Length + result.Collection.Length; |
| 45 | + } |
| 46 | +
|
| 47 | + public static bool Execute() |
| 48 | + { |
| 49 | + if (Read(Result<string>.Succeed(Payload)) != Payload.Length || |
| 50 | + ReadNullable(Result<string?>.Succeed(Payload)) != Payload.Length || |
| 51 | + ReadCollection(CollectionResult<string>.Succeed([Payload])) != 2 || |
| 52 | + ReadCollection(CollectionResult<string>.Succeed([])) != 0) |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | +
|
| 57 | + var problem = Problem.Create(FailureTitle, FailureDetail); |
| 58 | + var failed = Result<string>.Fail(problem); |
| 59 | + try { _ = Read(failed); return false; } |
| 60 | + catch (ProblemException exception) |
| 61 | + { |
| 62 | + if (!ReferenceEquals(exception.Problem, problem)) { return false; } |
| 63 | + } |
| 64 | +
|
| 65 | + var nullable = Result<string?>.Fail(problem); |
| 66 | + try { _ = ReadNullable(nullable); return false; } |
| 67 | + catch (ProblemException exception) |
| 68 | + { |
| 69 | + if (!ReferenceEquals(exception.Problem, problem)) { return false; } |
| 70 | + } |
| 71 | +
|
| 72 | + var page = CollectionResult<string>.Fail(problem); |
| 73 | + try { _ = ReadCollection(page); return false; } |
| 74 | + catch (ProblemException exception) |
| 75 | + { |
| 76 | + if (!ReferenceEquals(exception.Problem, problem)) { return false; } |
| 77 | + } |
| 78 | +
|
| 79 | + var emptyPage = default(CollectionResult<string>); |
| 80 | + if (emptyPage.Value.Length != 0 || emptyPage.Collection.Length != 0) { return false; } |
| 81 | + try { _ = Read(default); return false; } |
| 82 | + catch (ProblemException exception) |
| 83 | + { |
| 84 | + if (exception.Problem is null) { return false; } |
| 85 | + } |
| 86 | + try { _ = ReadNullable(default); return false; } |
| 87 | + catch (ProblemException exception) |
| 88 | + { |
| 89 | + if (exception.Problem is null) { return false; } |
| 90 | + } |
| 91 | + try { _ = ReadCollection(emptyPage); return false; } |
| 92 | + catch (ProblemException exception) |
| 93 | + { |
| 94 | + return exception.Problem is not null; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + """; |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void ThrowIfProblem_ConsumerCompilesWithoutNullableWarnings_ExecutesSuccessFailureAndDefault() |
| 102 | + { |
| 103 | + var references = ((string)AppContext.GetData(TrustedAssembliesKey)!).Split(Path.PathSeparator) |
| 104 | + .Append(typeof(Result).Assembly.Location).Distinct().Select(path => MetadataReference.CreateFromFile(path)); |
| 105 | + var compilation = CSharpCompilation.Create(AssemblyName, |
| 106 | + [CSharpSyntaxTree.ParseText(Consumer)], references, |
| 107 | + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, |
| 108 | + nullableContextOptions: NullableContextOptions.Enable, generalDiagnosticOption: ReportDiagnostic.Error)); |
| 109 | + using var image = new MemoryStream(); |
| 110 | + var emitted = compilation.Emit(image); |
| 111 | + emitted.Success.ShouldBeTrue(string.Join(Environment.NewLine, emitted.Diagnostics)); |
| 112 | + emitted.Diagnostics.ShouldBeEmpty(); |
| 113 | + image.Position = 0; |
| 114 | + var context = new AssemblyLoadContext(AssemblyName, isCollectible: true); |
| 115 | + try |
| 116 | + { |
| 117 | + var assembly = context.LoadFromStream(image); |
| 118 | + assembly.GetType(EntryType)!.GetMethod(EntryMethod, BindingFlags.Public | BindingFlags.Static)! |
| 119 | + .Invoke(null, null).ShouldBe(true); |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + context.Unload(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments