Skip to content

Commit

Permalink
fixed the test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Aug 28, 2024
1 parent 42dbd78 commit 269df57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
19 changes: 7 additions & 12 deletions Src/Basic.Reference.Assemblies.UnitTests/CompilationUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Operations;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -11,14 +12,8 @@
namespace Basic.Reference.Assemblies.UnitTests;
internal static class CompilationUtil
{
public static MemoryStream CompileToLibrary(string code, string assemblyName)
public static MemoryStream CompileToLibrary(string code, string assemblyName, IEnumerable<MetadataReference> references)
{
var references =
#if NET
Net80.References.All;
#else
Net461.References.All;
#endif
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create(
assemblyName,
Expand Down Expand Up @@ -48,23 +43,23 @@ static string GetMessage(IEnumerable<Diagnostic> diagnostics)
}
}

public static Assembly CompileToLibraryAndLoad(string code, string assemblyName)
public static Assembly CompileToLibraryAndLoad(string code, string assemblyName, IEnumerable<MetadataReference> references)
{
var stream = CompileToLibrary(code, assemblyName);
var stream = CompileToLibrary(code, assemblyName, references);
return Load(stream, assemblyName);
}

/// <summary>
/// Compile and run the code expecting to find a static Lib.Go method
/// </summary>
public static string? CompileAndRun(string code, string assemblyName)
public static string? CompileAndRun(string code, string assemblyName, IEnumerable<MetadataReference> references)
{
var assembly = CompileToLibraryAndLoad(code, assemblyName);
var assembly = CompileToLibraryAndLoad(code, assemblyName, references);
var libType = assembly
.GetTypes()
.Where(x => x.Name == "Lib")
.Single();
var method = libType.GetMethod("Go", BindingFlags.Static | BindingFlags.NonPublic);
var method = libType.GetMethod("Go", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var obj = method!.Invoke(null, null);
return (string?)obj;
}
Expand Down
12 changes: 11 additions & 1 deletion Src/Basic.Reference.Assemblies.UnitTests/SanityUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace Basic.Reference.Assemblies.UnitTests;
public class SanityUnitTests(ITestOutputHelper outputHelper)
{
public ITestOutputHelper TestOutputHelper { get; } = outputHelper;
public bool IsCoreClr =>
#if NET
true;
#else
false;
#endif

[Theory]
[MemberData(nameof(TestData.ApplicationReferences), MemberType = typeof(TestData))]
Expand Down Expand Up @@ -190,7 +196,11 @@ public static string Go()
}
}
""";
var actual = CompilationUtil.CompileAndRun(source, nameof(RunTuple));

var references = IsCoreClr
? Net80.References.All
: [.. Net461.References.All, .. Net461.ExtraReferences.All];
var actual = CompilationUtil.CompileAndRun(source, nameof(RunTuple), references);
Assert.Equal("(1, 2)", actual);
}
}

0 comments on commit 269df57

Please sign in to comment.