Skip to content

Commit

Permalink
test: Adjust Given_DependencyObjectGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 15, 2024
1 parent 831afec commit 21a9b83
Showing 1 changed file with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
Expand All @@ -14,9 +16,54 @@ namespace Uno.UI.SourceGenerators.Tests.DependencyObjectGeneratorTests;
[TestClass]
public class Given_DependencyObjectGenerator
{
private static readonly ImmutableArray<PackageIdentity> _unoPackage = ImmutableArray.Create(new PackageIdentity("Uno.WinUI", "5.0.118"));
private static readonly ReferenceAssemblies _Net70AndroidWithUno = ReferenceAssemblies.Net.Net80Android.AddPackages(_unoPackage);
private static readonly ReferenceAssemblies _Net70WithUno = ReferenceAssemblies.Net.Net80.AddPackages(_unoPackage);
private static readonly ReferenceAssemblies _net80Android = ReferenceAssemblies.Net.Net80Android;
private static readonly ReferenceAssemblies _net80 = ReferenceAssemblies.Net.Net80;

private static MetadataReference[] BuildUnoReferences(bool isAndroid)
{
const string configuration =
#if DEBUG
"Debug";
#else
"Release";
#endif
string[] availableTargets;
if (isAndroid)
{
availableTargets = [Path.Combine("Uno.UI.netcoremobile", configuration, "net8.0")];
}
else
{
availableTargets = [
Path.Combine("Uno.UI.Skia", configuration, "net8.0"),
Path.Combine("Uno.UI.Reference", configuration, "net8.0"),
Path.Combine("Uno.UI.Tests", configuration, "net8.0"),
];
}

var unoUIBase = Path.Combine(
Path.GetDirectoryName(typeof(Given_DependencyObjectGenerator).Assembly.Location)!,
"..",
"..",
"..",
"..",
"..",
"Uno.UI",
"bin"
);
var unoTarget = availableTargets
.Select(t => Path.Combine(unoUIBase, t, "Uno.UI.dll"))
.FirstOrDefault(File.Exists);
if (unoTarget is null)
{
throw new InvalidOperationException($"Unable to find Uno.UI.dll in {string.Join(",", availableTargets)}");
}

return Directory.GetFiles(Path.GetDirectoryName(unoTarget)!, "*.dll")
.Select(f => MetadataReference.CreateFromFile(Path.GetFullPath(f)))
.ToArray();
}


private async Task TestAndroid(string testCode, params DiagnosticResult[] expectedDiagnostics)
{
Expand All @@ -26,8 +73,10 @@ private async Task TestAndroid(string testCode, params DiagnosticResult[] expect
{
Sources = { testCode },
},
ReferenceAssemblies = _Net70AndroidWithUno,
ReferenceAssemblies = _net80,
};

test.TestState.AdditionalReferences.AddRange(BuildUnoReferences(isAndroid: true));
test.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync();
}
Expand Down Expand Up @@ -65,7 +114,7 @@ public void UnregisterPropertyChangedCallback(DependencyProperty dp, long token)
[TestMethod]
public async Task TestNested()
{
var test = """
var testCode = """
using Windows.UI.Core;
using Microsoft.UI.Xaml;
Expand All @@ -77,11 +126,11 @@ internal partial class Inner : DependencyObject
}
""";

await new Verify.Test
var test = new Verify.Test
{
TestState =
{
Sources = { test },
Sources = { testCode },
GeneratedSources =
{
{ (@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.DependencyObject.DependencyObjectGenerator\OuterClass.Inner.cs", SourceText.From("""
Expand Down Expand Up @@ -292,7 +341,10 @@ public void SuspendBindings() =>
""", Encoding.UTF8)) }
}
},
ReferenceAssemblies = _Net70WithUno,
}.RunAsync();
ReferenceAssemblies = _net80,
};

test.TestState.AdditionalReferences.AddRange(BuildUnoReferences(isAndroid: false));
await test.RunAsync();
}
}

0 comments on commit 21a9b83

Please sign in to comment.