Skip to content

Commit

Permalink
chore: Adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Apr 19, 2023
1 parent 62d9747 commit 284f2a4
Show file tree
Hide file tree
Showing 48 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using CommunityToolkit.Mvvm.SourceGenerators;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
Expand Down Expand Up @@ -117,7 +118,7 @@ protected override Project ApplyCompilationOptions(Project project)
foreach (var tree in compilation.SyntaxTrees.Skip(project.DocumentIds.Count))
{
WriteTreeToDiskIfNecessary(tree, resourceDirectory);
expectedNames.Add(Path.GetFileName(tree.FilePath));
expectedNames.Add(GetFileNameFromTree(tree));
}

var currentTestPrefix = $"Uno.UI.SourceGenerators.netcore.Tests.XamlCodeGeneratorTests.{TestOutputFolderName}.{_testMethodName}.";
Expand Down Expand Up @@ -155,12 +156,29 @@ public Test AddGeneratedSources()

using var reader = new StreamReader(resourceStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 4096, leaveOpen: true);
var name = resourceName.Substring(expectedPrefix.Length);
TestState.GeneratedSources.Add((typeof(XamlCodeGenerator), name, reader.ReadToEnd()));
var underscoreIndex = name.IndexOf('_');
var generatorName = name.Substring(0, underscoreIndex);
name = name.Substring(underscoreIndex + 1);

var type = generatorName switch
{
"XamlCodeGenerator" => typeof(XamlCodeGenerator),
"ObservablePropertyGenerator" => typeof(ObservablePropertyGenerator),
_ => throw new Exception("Unexpected generator name"),
};
TestState.GeneratedSources.Add((type, name, reader.ReadToEnd()));
}

return this;
}

private static string GetFileNameFromTree(SyntaxTree tree)
{
var generatorName = new DirectoryInfo(tree.FilePath).Parent!.Name;
generatorName = generatorName.Substring(generatorName.LastIndexOf('.') + 1);
return $"{generatorName}_{Path.GetFileName(tree.FilePath)}";
}

[Conditional("WRITE_EXPECTED")]
private static void WriteTreeToDiskIfNecessary(SyntaxTree tree, string resourceDirectory)
{
Expand All @@ -169,7 +187,8 @@ private static void WriteTreeToDiskIfNecessary(SyntaxTree tree, string resourceD
throw new ArgumentException("Syntax tree encoding was not specified");
}

var name = Path.GetFileName(tree.FilePath);
var name = GetFileNameFromTree(tree);

var filePath = Path.Combine(resourceDirectory, name);
Directory.CreateDirectory(resourceDirectory);
File.WriteAllText(filePath, tree.GetText().ToString(), tree.Encoding);
Expand Down

0 comments on commit 284f2a4

Please sign in to comment.