Skip to content

Commit

Permalink
Merge pull request #17946 from Youssef1313/adjust-do-gen-tests
Browse files Browse the repository at this point in the history
test: Adjust Given_DependencyObjectGenerator
  • Loading branch information
Youssef1313 authored Aug 16, 2024
2 parents 52d65c8 + 5db8676 commit abd7376
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
7 changes: 7 additions & 0 deletions build/ci/.azure-devops-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ jobs:
artifactName: 'NugetPackages-Artifacts-reference-$(XAML_FLAVOR_BUILD)'
downloadPath: '$(Agent.WorkFolder)'

# Unfortunate, but this is needed specifically for a single test: TestAndroidViewImplementingDependencyObject
- task: DownloadBuildArtifacts@0
displayName: Download build artifact
inputs:
artifactName: 'NugetPackages-Artifacts-netcoremobile-$(XAML_FLAVOR_BUILD)'
downloadPath: '$(Agent.WorkFolder)'

- task: ExtractFiles@1
displayName: Restore binaries structure
inputs:
Expand Down
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,51 @@ 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.AddPackages([new PackageIdentity("Uno.Diagnostics.Eventing", "2.1.0")]);
private static readonly ReferenceAssemblies _net80 = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Uno.Diagnostics.Eventing", "2.1.0")]);

private const string Configuration =
#if DEBUG
"Debug";
#else
"Release";
#endif

private const string TFM = "net8.0";

private static MetadataReference[] BuildUnoReferences(bool isAndroid)
{
string[] availableTargets = isAndroid
? [Path.Combine("Uno.UI.netcoremobile", Configuration, $"{TFM}-android")]
: [
Path.Combine("Uno.UI.Skia", Configuration, TFM),
Path.Combine("Uno.UI.Reference", Configuration, TFM),
Path.Combine("Uno.UI.Tests", Configuration, TFM),
];

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 +70,10 @@ private async Task TestAndroid(string testCode, params DiagnosticResult[] expect
{
Sources = { testCode },
},
ReferenceAssemblies = _Net70AndroidWithUno,
ReferenceAssemblies = _net80Android,
};

test.TestState.AdditionalReferences.AddRange(BuildUnoReferences(isAndroid: true));
test.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync();
}
Expand Down Expand Up @@ -65,7 +111,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 +123,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 +338,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 abd7376

Please sign in to comment.