diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml
index 2a3a7f8..d60dad7 100644
--- a/.github/workflows/ci-build.yml
+++ b/.github/workflows/ci-build.yml
@@ -26,7 +26,7 @@ jobs:
- name: Install .NET Core
uses: actions/setup-dotnet@v1
with:
- dotnet-version: 5.0.301
+ dotnet-version: 6.0.101
- name: NBGV
id: nbgv
diff --git a/src/Avalonia.NameGenerator.Sandbox/App.xaml.cs b/src/Avalonia.NameGenerator.Sandbox/App.xaml.cs
index 12210d4..896de18 100644
--- a/src/Avalonia.NameGenerator.Sandbox/App.xaml.cs
+++ b/src/Avalonia.NameGenerator.Sandbox/App.xaml.cs
@@ -1,17 +1,20 @@
using Avalonia.Markup.Xaml;
+using Avalonia.NameGenerator.Sandbox.ViewModels;
using Avalonia.NameGenerator.Sandbox.Views;
-namespace Avalonia.NameGenerator.Sandbox
+namespace Avalonia.NameGenerator.Sandbox;
+
+public class App : Application
{
- public class App : Application
- {
- public override void Initialize() => AvaloniaXamlLoader.Load(this);
+ public override void Initialize() => AvaloniaXamlLoader.Load(this);
- public override void OnFrameworkInitializationCompleted()
+ public override void OnFrameworkInitializationCompleted()
+ {
+ var view = new SignUpView
{
- var view = new SignUpView();
- view.Show();
- base.OnFrameworkInitializationCompleted();
- }
+ ViewModel = new SignUpViewModel()
+ };
+ view.Show();
+ base.OnFrameworkInitializationCompleted();
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Sandbox/Avalonia.NameGenerator.Sandbox.csproj b/src/Avalonia.NameGenerator.Sandbox/Avalonia.NameGenerator.Sandbox.csproj
index a0218f4..1e66c7e 100644
--- a/src/Avalonia.NameGenerator.Sandbox/Avalonia.NameGenerator.Sandbox.csproj
+++ b/src/Avalonia.NameGenerator.Sandbox/Avalonia.NameGenerator.Sandbox.csproj
@@ -1,7 +1,7 @@
Exe
- net5
+ net6
preview
false
true
@@ -19,4 +19,7 @@
+
+
+
diff --git a/src/Avalonia.NameGenerator.Sandbox/Controls/CustomTextBox.cs b/src/Avalonia.NameGenerator.Sandbox/Controls/CustomTextBox.cs
index 264baf5..543d73c 100644
--- a/src/Avalonia.NameGenerator.Sandbox/Controls/CustomTextBox.cs
+++ b/src/Avalonia.NameGenerator.Sandbox/Controls/CustomTextBox.cs
@@ -1,6 +1,10 @@
+using System;
using Avalonia.Controls;
+using Avalonia.Styling;
-namespace Avalonia.NameGenerator.Sandbox.Controls
+namespace Avalonia.NameGenerator.Sandbox.Controls;
+
+public class CustomTextBox : TextBox, IStyleable
{
- public class CustomTextBox : TextBox { }
+ Type IStyleable.StyleKey => typeof(TextBox);
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Sandbox/Program.cs b/src/Avalonia.NameGenerator.Sandbox/Program.cs
index 39d3d0f..6133430 100644
--- a/src/Avalonia.NameGenerator.Sandbox/Program.cs
+++ b/src/Avalonia.NameGenerator.Sandbox/Program.cs
@@ -1,15 +1,14 @@
using Avalonia.ReactiveUI;
-namespace Avalonia.NameGenerator.Sandbox
+namespace Avalonia.NameGenerator.Sandbox;
+
+internal static class Program
{
- internal static class Program
- {
- public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
+ public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
- private static AppBuilder BuildAvaloniaApp()
- => AppBuilder.Configure()
- .UseReactiveUI()
- .UsePlatformDetect()
- .LogToTrace();
- }
+ private static AppBuilder BuildAvaloniaApp()
+ => AppBuilder.Configure()
+ .UseReactiveUI()
+ .UsePlatformDetect()
+ .LogToTrace();
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Sandbox/ViewModels/SignUpViewModel.cs b/src/Avalonia.NameGenerator.Sandbox/ViewModels/SignUpViewModel.cs
new file mode 100644
index 0000000..fc54364
--- /dev/null
+++ b/src/Avalonia.NameGenerator.Sandbox/ViewModels/SignUpViewModel.cs
@@ -0,0 +1,70 @@
+using System.Reactive;
+using ReactiveUI;
+using ReactiveUI.Validation.Extensions;
+using ReactiveUI.Validation.Helpers;
+
+namespace Avalonia.NameGenerator.Sandbox.ViewModels;
+
+public class SignUpViewModel : ReactiveValidationObject
+{
+ private string _userName = string.Empty;
+ private string _password = string.Empty;
+ private string _confirmPassword = string.Empty;
+
+ public SignUpViewModel()
+ {
+ this.ValidationRule(
+ vm => vm.UserName,
+ name => !string.IsNullOrWhiteSpace(name),
+ "UserName is required.");
+
+ this.ValidationRule(
+ vm => vm.Password,
+ password => !string.IsNullOrWhiteSpace(password),
+ "Password is required.");
+
+ this.ValidationRule(
+ vm => vm.Password,
+ password => password?.Length > 2,
+ password => $"Password should be longer, current length: {password.Length}");
+
+ this.ValidationRule(
+ vm => vm.ConfirmPassword,
+ confirmation => !string.IsNullOrWhiteSpace(confirmation),
+ "Confirm password field is required.");
+
+ var passwordsObservable =
+ this.WhenAnyValue(
+ x => x.Password,
+ x => x.ConfirmPassword,
+ (password, confirmation) =>
+ password == confirmation);
+
+ this.ValidationRule(
+ vm => vm.ConfirmPassword,
+ passwordsObservable,
+ "Passwords must match.");
+
+ SignUp = ReactiveCommand.Create(() => {}, this.IsValid());
+ }
+
+ public ReactiveCommand SignUp { get; }
+
+ public string UserName
+ {
+ get => _userName;
+ set => this.RaiseAndSetIfChanged(ref _userName, value);
+ }
+
+ public string Password
+ {
+ get => _password;
+ set => this.RaiseAndSetIfChanged(ref _password, value);
+ }
+
+ public string ConfirmPassword
+ {
+ get => _confirmPassword;
+ set => this.RaiseAndSetIfChanged(ref _confirmPassword, value);
+ }
+}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml b/src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml
index b300765..a187d05 100644
--- a/src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml
+++ b/src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml
@@ -2,36 +2,23 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Avalonia.NameGenerator.Sandbox.Controls"
x:Class="Avalonia.NameGenerator.Sandbox.Views.SignUpView">
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+/// This is a sample view class with typed x:Name references generated using
+/// .NET 5 source generators. The class has to be partial because x:Name
+/// references are living in a separate partial class file. See also:
+/// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
+///
+public partial class SignUpView : ReactiveWindow
{
- ///
- /// This is a sample view class with typed x:Name references generated at compile-time using
- /// .NET 5 source generators. The class should be marked with [GenerateTypedNameReferences],
- /// this attribute is also compile-time generated. The class has to be partial because x:Name
- /// references are living in a separate partial class file. See also:
- /// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
- ///
- public partial class SignUpView : Window
+ public SignUpView()
{
- public SignUpView()
+ // The InitializeComponent method is also generated automatically
+ // and lives in the autogenerated part of the partial class.
+ InitializeComponent();
+ this.WhenActivated(disposables =>
{
- InitializeComponent();
+ this.Bind(ViewModel, x => x.UserName, x => x.UserNameTextBox.Text)
+ .DisposeWith(disposables);
+ this.Bind(ViewModel, x => x.Password, x => x.PasswordTextBox.Text)
+ .DisposeWith(disposables);
+ this.Bind(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordTextBox.Text)
+ .DisposeWith(disposables);
+ this.BindCommand(ViewModel, x => x.SignUp, x => x.SignUpButton)
+ .DisposeWith(disposables);
+
+ this.BindValidation(ViewModel, x => x.UserName, x => x.UserNameValidation.Text)
+ .DisposeWith(disposables);
+ this.BindValidation(ViewModel, x => x.Password, x => x.PasswordValidation.Text)
+ .DisposeWith(disposables);
+ this.BindValidation(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordValidation.Text)
+ .DisposeWith(disposables);
+
+ var newLineFormatter = new SingleLineFormatter(Environment.NewLine);
+ this.BindValidation(ViewModel, x => x.CompoundValidation.Text, newLineFormatter)
+ .DisposeWith(disposables);
+
+ // The references to text boxes below are also auto generated.
+ // Use Ctrl+Click in order to view the generated sources.
UserNameTextBox.Text = "Joseph!";
- UserNameValidation.Text = "User name is valid.";
- PasswordTextBox.Text = "qwerty";
- PasswordValidation.Text = "Password is valid.";
- ConfirmPasswordTextBox.Text = "qwerty";
- ConfirmPasswordValidation.Text = "Password confirmation is valid.";
- SignUpButton.Content = "Sign up please!";
- CompoundValidation.Text = "Everything is okay.";
- AwesomeListView.VirtualizationMode = ItemVirtualizationMode.None;
- }
+ PasswordTextBox.Text = "1234";
+ ConfirmPasswordTextBox.Text = "1234";
+ });
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/Avalonia.NameGenerator.Tests.csproj b/src/Avalonia.NameGenerator.Tests/Avalonia.NameGenerator.Tests.csproj
index c2d3d58..8f450e1 100644
--- a/src/Avalonia.NameGenerator.Tests/Avalonia.NameGenerator.Tests.csproj
+++ b/src/Avalonia.NameGenerator.Tests/Avalonia.NameGenerator.Tests.csproj
@@ -1,7 +1,7 @@
Exe
- net5
+ net6
preview
false
true
diff --git a/src/Avalonia.NameGenerator.Tests/GlobPatternTests.cs b/src/Avalonia.NameGenerator.Tests/GlobPatternTests.cs
index 68ee426..c255beb 100644
--- a/src/Avalonia.NameGenerator.Tests/GlobPatternTests.cs
+++ b/src/Avalonia.NameGenerator.Tests/GlobPatternTests.cs
@@ -1,32 +1,31 @@
using Avalonia.NameGenerator.Generator;
using Xunit;
-namespace Avalonia.NameGenerator.Tests
+namespace Avalonia.NameGenerator.Tests;
+
+public class GlobPatternTests
{
- public class GlobPatternTests
+ [Theory]
+ [InlineData("*", "anything", true)]
+ [InlineData("", "anything", false)]
+ [InlineData("Views/*", "Views/SignUpView.xaml", true)]
+ [InlineData("Views/*", "Extensions/SignUpView.xaml", false)]
+ [InlineData("*SignUpView*", "Extensions/SignUpView.xaml", true)]
+ [InlineData("*SignUpView.paml", "Extensions/SignUpView.xaml", false)]
+ [InlineData("*.xaml", "Extensions/SignUpView.xaml", true)]
+ public void Should_Match_Glob_Expressions(string pattern, string value, bool matches)
{
- [Theory]
- [InlineData("*", "anything", true)]
- [InlineData("", "anything", false)]
- [InlineData("Views/*", "Views/SignUpView.xaml", true)]
- [InlineData("Views/*", "Extensions/SignUpView.xaml", false)]
- [InlineData("*SignUpView*", "Extensions/SignUpView.xaml", true)]
- [InlineData("*SignUpView.paml", "Extensions/SignUpView.xaml", false)]
- [InlineData("*.xaml", "Extensions/SignUpView.xaml", true)]
- public void Should_Match_Glob_Expressions(string pattern, string value, bool matches)
- {
- Assert.Equal(matches, new GlobPattern(pattern).Matches(value));
- }
+ Assert.Equal(matches, new GlobPattern(pattern).Matches(value));
+ }
- [Theory]
- [InlineData("Views/SignUpView.xaml", true, new[] { "*.xaml", "Extensions/*" })]
- [InlineData("Extensions/SignUpView.paml", true, new[] { "*.xaml", "Extensions/*" })]
- [InlineData("Extensions/SignUpView.paml", false, new[] { "*.xaml", "Views/*" })]
- [InlineData("anything", true, new[] { "*", "*" })]
- [InlineData("anything", false, new[] { "", "" })]
- public void Should_Match_Glob_Pattern_Groups(string value, bool matches, string[] patterns)
- {
- Assert.Equal(matches, new GlobPatternGroup(patterns).Matches(value));
- }
+ [Theory]
+ [InlineData("Views/SignUpView.xaml", true, new[] { "*.xaml", "Extensions/*" })]
+ [InlineData("Extensions/SignUpView.paml", true, new[] { "*.xaml", "Extensions/*" })]
+ [InlineData("Extensions/SignUpView.paml", false, new[] { "*.xaml", "Views/*" })]
+ [InlineData("anything", true, new[] { "*", "*" })]
+ [InlineData("anything", false, new[] { "", "" })]
+ public void Should_Match_Glob_Pattern_Groups(string value, bool matches, string[] patterns)
+ {
+ Assert.Equal(matches, new GlobPatternGroup(patterns).Matches(value));
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/InitializeComponent/GeneratedInitializeComponent/InitializeComponentCode.cs b/src/Avalonia.NameGenerator.Tests/InitializeComponent/GeneratedInitializeComponent/InitializeComponentCode.cs
index 59ee051..309e866 100644
--- a/src/Avalonia.NameGenerator.Tests/InitializeComponent/GeneratedInitializeComponent/InitializeComponentCode.cs
+++ b/src/Avalonia.NameGenerator.Tests/InitializeComponent/GeneratedInitializeComponent/InitializeComponentCode.cs
@@ -2,35 +2,34 @@
using System.Linq;
using System.Threading.Tasks;
-namespace Avalonia.NameGenerator.Tests.InitializeComponent.GeneratedInitializeComponent
+namespace Avalonia.NameGenerator.Tests.InitializeComponent.GeneratedInitializeComponent;
+
+public static class InitializeComponentCode
{
- public static class InitializeComponentCode
- {
- public const string NamedControl = "NamedControl.txt";
- public const string NamedControls = "NamedControls.txt";
- public const string XNamedControl = "xNamedControl.txt";
- public const string XNamedControls = "xNamedControls.txt";
- public const string NoNamedControls = "NoNamedControls.txt";
- public const string CustomControls = "CustomControls.txt";
- public const string DataTemplates = "DataTemplates.txt";
- public const string SignUpView = "SignUpView.txt";
- public const string FieldModifier = "FieldModifier.txt";
- public const string AttachedProps = "AttachedProps.txt";
- public const string AttachedPropsWithDevTools = "AttachedPropsWithDevTools.txt";
- public const string ControlWithoutWindow = "ControlWithoutWindow.txt";
+ public const string NamedControl = "NamedControl.txt";
+ public const string NamedControls = "NamedControls.txt";
+ public const string XNamedControl = "xNamedControl.txt";
+ public const string XNamedControls = "xNamedControls.txt";
+ public const string NoNamedControls = "NoNamedControls.txt";
+ public const string CustomControls = "CustomControls.txt";
+ public const string DataTemplates = "DataTemplates.txt";
+ public const string SignUpView = "SignUpView.txt";
+ public const string FieldModifier = "FieldModifier.txt";
+ public const string AttachedProps = "AttachedProps.txt";
+ public const string AttachedPropsWithDevTools = "AttachedPropsWithDevTools.txt";
+ public const string ControlWithoutWindow = "ControlWithoutWindow.txt";
- public static async Task Load(string generatedCodeResourceName)
- {
- var assembly = typeof(XamlXNameResolverTests).Assembly;
- var fullResourceName = assembly
- .GetManifestResourceNames()
- .First(name => name.Contains("InitializeComponent") &&
- name.Contains("GeneratedInitializeComponent") &&
- name.EndsWith(generatedCodeResourceName));
+ public static async Task Load(string generatedCodeResourceName)
+ {
+ var assembly = typeof(XamlXNameResolverTests).Assembly;
+ var fullResourceName = assembly
+ .GetManifestResourceNames()
+ .First(name => name.Contains("InitializeComponent") &&
+ name.Contains("GeneratedInitializeComponent") &&
+ name.EndsWith(generatedCodeResourceName));
- await using var stream = assembly.GetManifestResourceStream(fullResourceName);
- using var reader = new StreamReader(stream!);
- return await reader.ReadToEndAsync();
- }
+ await using var stream = assembly.GetManifestResourceStream(fullResourceName);
+ using var reader = new StreamReader(stream!);
+ return await reader.ReadToEndAsync();
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/InitializeComponent/InitializeComponentTests.cs b/src/Avalonia.NameGenerator.Tests/InitializeComponent/InitializeComponentTests.cs
index d5e3343..d3339a2 100644
--- a/src/Avalonia.NameGenerator.Tests/InitializeComponent/InitializeComponentTests.cs
+++ b/src/Avalonia.NameGenerator.Tests/InitializeComponent/InitializeComponentTests.cs
@@ -7,57 +7,56 @@
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
-namespace Avalonia.NameGenerator.Tests.InitializeComponent
+namespace Avalonia.NameGenerator.Tests.InitializeComponent;
+
+public class InitializeComponentTests
{
- public class InitializeComponentTests
+ [Theory]
+ [InlineData(InitializeComponentCode.NamedControl, View.NamedControl, false)]
+ [InlineData(InitializeComponentCode.NamedControls, View.NamedControls, false)]
+ [InlineData(InitializeComponentCode.XNamedControl, View.XNamedControl, false)]
+ [InlineData(InitializeComponentCode.XNamedControls, View.XNamedControls, false)]
+ [InlineData(InitializeComponentCode.NoNamedControls, View.NoNamedControls, false)]
+ [InlineData(InitializeComponentCode.CustomControls, View.CustomControls, false)]
+ [InlineData(InitializeComponentCode.DataTemplates, View.DataTemplates, false)]
+ [InlineData(InitializeComponentCode.SignUpView, View.SignUpView, false)]
+ [InlineData(InitializeComponentCode.FieldModifier, View.FieldModifier, false)]
+ [InlineData(InitializeComponentCode.AttachedPropsWithDevTools, View.AttachedProps, true)]
+ [InlineData(InitializeComponentCode.AttachedProps, View.AttachedProps, false)]
+ [InlineData(InitializeComponentCode.ControlWithoutWindow, View.ControlWithoutWindow, true)]
+ [InlineData(InitializeComponentCode.ControlWithoutWindow, View.ControlWithoutWindow, false)]
+ public async Task Should_Generate_FindControl_Refs_From_Avalonia_Markup_File(
+ string expectation,
+ string markup,
+ bool devToolsMode)
{
- [Theory]
- [InlineData(InitializeComponentCode.NamedControl, View.NamedControl, false)]
- [InlineData(InitializeComponentCode.NamedControls, View.NamedControls, false)]
- [InlineData(InitializeComponentCode.XNamedControl, View.XNamedControl, false)]
- [InlineData(InitializeComponentCode.XNamedControls, View.XNamedControls, false)]
- [InlineData(InitializeComponentCode.NoNamedControls, View.NoNamedControls, false)]
- [InlineData(InitializeComponentCode.CustomControls, View.CustomControls, false)]
- [InlineData(InitializeComponentCode.DataTemplates, View.DataTemplates, false)]
- [InlineData(InitializeComponentCode.SignUpView, View.SignUpView, false)]
- [InlineData(InitializeComponentCode.FieldModifier, View.FieldModifier, false)]
- [InlineData(InitializeComponentCode.AttachedPropsWithDevTools, View.AttachedProps, true)]
- [InlineData(InitializeComponentCode.AttachedProps, View.AttachedProps, false)]
- [InlineData(InitializeComponentCode.ControlWithoutWindow, View.ControlWithoutWindow, true)]
- [InlineData(InitializeComponentCode.ControlWithoutWindow, View.ControlWithoutWindow, false)]
- public async Task Should_Generate_FindControl_Refs_From_Avalonia_Markup_File(
- string expectation,
- string markup,
- bool devToolsMode)
- {
- var excluded = devToolsMode ? null : "Avalonia.Diagnostics";
- var compilation =
- View.CreateAvaloniaCompilation(excluded)
- .WithCustomTextBox();
+ var excluded = devToolsMode ? null : "Avalonia.Diagnostics";
+ var compilation =
+ View.CreateAvaloniaCompilation(excluded)
+ .WithCustomTextBox();
- var types = new RoslynTypeSystem(compilation);
- var classResolver = new XamlXViewResolver(
- types,
- MiniCompiler.CreateDefault(
- new RoslynTypeSystem(compilation),
- MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
+ var types = new RoslynTypeSystem(compilation);
+ var classResolver = new XamlXViewResolver(
+ types,
+ MiniCompiler.CreateDefault(
+ new RoslynTypeSystem(compilation),
+ MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
- var xaml = await View.Load(markup);
- var classInfo = classResolver.ResolveView(xaml);
- var nameResolver = new XamlXNameResolver();
- var names = nameResolver.ResolveNames(classInfo.Xaml);
+ var xaml = await View.Load(markup);
+ var classInfo = classResolver.ResolveView(xaml);
+ var nameResolver = new XamlXNameResolver();
+ var names = nameResolver.ResolveNames(classInfo.Xaml);
- var generator = new InitializeComponentCodeGenerator(types);
+ var generator = new InitializeComponentCodeGenerator(types);
- var code = generator
- .GenerateCode("SampleView", "Sample.App", classInfo.XamlType, names)
- .Replace("\r", string.Empty);
+ var code = generator
+ .GenerateCode("SampleView", "Sample.App", classInfo.XamlType, names)
+ .Replace("\r", string.Empty);
- var expected = await InitializeComponentCode.Load(expectation);
+ var expected = await InitializeComponentCode.Load(expectation);
- CSharpSyntaxTree.ParseText(code);
- Assert.Equal(expected.Replace("\r", string.Empty), code);
- }
+ CSharpSyntaxTree.ParseText(code);
+ Assert.Equal(expected.Replace("\r", string.Empty), code);
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/MiniCompilerTests.cs b/src/Avalonia.NameGenerator.Tests/MiniCompilerTests.cs
index 2b59e82..51e8149 100644
--- a/src/Avalonia.NameGenerator.Tests/MiniCompilerTests.cs
+++ b/src/Avalonia.NameGenerator.Tests/MiniCompilerTests.cs
@@ -8,53 +8,52 @@
using XamlX.Parsers;
using Xunit;
-namespace Avalonia.NameGenerator.Tests
+namespace Avalonia.NameGenerator.Tests;
+
+public class MiniCompilerTests
{
- public class MiniCompilerTests
+ private const string AvaloniaXaml = "";
+ private const string MiniClass = "namespace Example { public class Valid { public int Foo() => 21; } }";
+ private const string MiniInvalidXaml = "";
+ private const string MiniValidXaml = "";
+
+ [Fact]
+ public void Should_Resolve_Types_From_Simple_Valid_Xaml_Markup()
+ {
+ var xaml = XDocumentXamlParser.Parse(MiniValidXaml);
+ var compilation = CreateBasicCompilation(MiniClass);
+ MiniCompiler.CreateDefault(new RoslynTypeSystem(compilation)).Transform(xaml);
+
+ Assert.NotNull(xaml.Root);
+ }
+
+ [Fact]
+ public void Should_Throw_When_Unable_To_Resolve_Types_From_Simple_Invalid_Markup()
{
- private const string AvaloniaXaml = "";
- private const string MiniClass = "namespace Example { public class Valid { public int Foo() => 21; } }";
- private const string MiniInvalidXaml = "";
- private const string MiniValidXaml = "";
-
- [Fact]
- public void Should_Resolve_Types_From_Simple_Valid_Xaml_Markup()
- {
- var xaml = XDocumentXamlParser.Parse(MiniValidXaml);
- var compilation = CreateBasicCompilation(MiniClass);
- MiniCompiler.CreateDefault(new RoslynTypeSystem(compilation)).Transform(xaml);
-
- Assert.NotNull(xaml.Root);
- }
-
- [Fact]
- public void Should_Throw_When_Unable_To_Resolve_Types_From_Simple_Invalid_Markup()
- {
- var xaml = XDocumentXamlParser.Parse(MiniInvalidXaml);
- var compilation = CreateBasicCompilation(MiniClass);
- var compiler = MiniCompiler.CreateDefault(new RoslynTypeSystem(compilation));
-
- Assert.Throws(() => compiler.Transform(xaml));
- }
-
- [Fact]
- public void Should_Resolve_Types_From_Simple_Avalonia_Markup()
- {
- var xaml = XDocumentXamlParser.Parse(AvaloniaXaml);
- var compilation = View.CreateAvaloniaCompilation();
- MiniCompiler.CreateDefault(new RoslynTypeSystem(compilation)).Transform(xaml);
-
- Assert.NotNull(xaml.Root);
- }
-
- private static CSharpCompilation CreateBasicCompilation(string source) =>
- CSharpCompilation
- .Create("BasicLib", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
- .AddReferences(MetadataReference.CreateFromFile(typeof(string).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(ITypeDescriptorContext).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(ISupportInitialize).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(TypeConverterAttribute).Assembly.Location))
- .AddSyntaxTrees(CSharpSyntaxTree.ParseText(source));
+ var xaml = XDocumentXamlParser.Parse(MiniInvalidXaml);
+ var compilation = CreateBasicCompilation(MiniClass);
+ var compiler = MiniCompiler.CreateDefault(new RoslynTypeSystem(compilation));
+
+ Assert.Throws(() => compiler.Transform(xaml));
+ }
+
+ [Fact]
+ public void Should_Resolve_Types_From_Simple_Avalonia_Markup()
+ {
+ var xaml = XDocumentXamlParser.Parse(AvaloniaXaml);
+ var compilation = View.CreateAvaloniaCompilation();
+ MiniCompiler.CreateDefault(new RoslynTypeSystem(compilation)).Transform(xaml);
+
+ Assert.NotNull(xaml.Root);
}
+
+ private static CSharpCompilation CreateBasicCompilation(string source) =>
+ CSharpCompilation
+ .Create("BasicLib", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(string).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(ITypeDescriptorContext).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(ISupportInitialize).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(TypeConverterAttribute).Assembly.Location))
+ .AddSyntaxTrees(CSharpSyntaxTree.ParseText(source));
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/OnlyProperties/GeneratedCode/OnlyPropertiesCode.cs b/src/Avalonia.NameGenerator.Tests/OnlyProperties/GeneratedCode/OnlyPropertiesCode.cs
index 0651c53..b2c253b 100644
--- a/src/Avalonia.NameGenerator.Tests/OnlyProperties/GeneratedCode/OnlyPropertiesCode.cs
+++ b/src/Avalonia.NameGenerator.Tests/OnlyProperties/GeneratedCode/OnlyPropertiesCode.cs
@@ -3,32 +3,31 @@
using System.Reflection;
using System.Threading.Tasks;
-namespace Avalonia.NameGenerator.Tests.OnlyProperties.GeneratedCode
+namespace Avalonia.NameGenerator.Tests.OnlyProperties.GeneratedCode;
+
+public static class OnlyPropertiesCode
{
- public static class OnlyPropertiesCode
- {
- public const string NamedControl = "NamedControl.txt";
- public const string NamedControls = "NamedControls.txt";
- public const string XNamedControl = "xNamedControl.txt";
- public const string XNamedControls = "xNamedControls.txt";
- public const string NoNamedControls = "NoNamedControls.txt";
- public const string CustomControls = "CustomControls.txt";
- public const string DataTemplates = "DataTemplates.txt";
- public const string SignUpView = "SignUpView.txt";
- public const string AttachedProps = "AttachedProps.txt";
- public const string FieldModifier = "FieldModifier.txt";
- public const string ControlWithoutWindow = "ControlWithoutWindow.txt";
+ public const string NamedControl = "NamedControl.txt";
+ public const string NamedControls = "NamedControls.txt";
+ public const string XNamedControl = "xNamedControl.txt";
+ public const string XNamedControls = "xNamedControls.txt";
+ public const string NoNamedControls = "NoNamedControls.txt";
+ public const string CustomControls = "CustomControls.txt";
+ public const string DataTemplates = "DataTemplates.txt";
+ public const string SignUpView = "SignUpView.txt";
+ public const string AttachedProps = "AttachedProps.txt";
+ public const string FieldModifier = "FieldModifier.txt";
+ public const string ControlWithoutWindow = "ControlWithoutWindow.txt";
- public static async Task Load(string generatedCodeResourceName)
- {
- var assembly = typeof(XamlXNameResolverTests).Assembly;
- var fullResourceName = assembly
- .GetManifestResourceNames()
- .First(name => name.Contains("OnlyProperties") && name.EndsWith(generatedCodeResourceName));
+ public static async Task Load(string generatedCodeResourceName)
+ {
+ var assembly = typeof(XamlXNameResolverTests).Assembly;
+ var fullResourceName = assembly
+ .GetManifestResourceNames()
+ .First(name => name.Contains("OnlyProperties") && name.EndsWith(generatedCodeResourceName));
- await using var stream = assembly.GetManifestResourceStream(fullResourceName);
- using var reader = new StreamReader(stream!);
- return await reader.ReadToEndAsync();
- }
+ await using var stream = assembly.GetManifestResourceStream(fullResourceName);
+ using var reader = new StreamReader(stream!);
+ return await reader.ReadToEndAsync();
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/OnlyProperties/OnlyPropertiesTests.cs b/src/Avalonia.NameGenerator.Tests/OnlyProperties/OnlyPropertiesTests.cs
index 4ecacd2..41471c8 100644
--- a/src/Avalonia.NameGenerator.Tests/OnlyProperties/OnlyPropertiesTests.cs
+++ b/src/Avalonia.NameGenerator.Tests/OnlyProperties/OnlyPropertiesTests.cs
@@ -6,47 +6,46 @@
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
-namespace Avalonia.NameGenerator.Tests.OnlyProperties
+namespace Avalonia.NameGenerator.Tests.OnlyProperties;
+
+public class OnlyPropertiesTests
{
- public class OnlyPropertiesTests
+ [Theory]
+ [InlineData(OnlyPropertiesCode.NamedControl, View.NamedControl)]
+ [InlineData(OnlyPropertiesCode.NamedControls, View.NamedControls)]
+ [InlineData(OnlyPropertiesCode.XNamedControl, View.XNamedControl)]
+ [InlineData(OnlyPropertiesCode.XNamedControls, View.XNamedControls)]
+ [InlineData(OnlyPropertiesCode.NoNamedControls, View.NoNamedControls)]
+ [InlineData(OnlyPropertiesCode.CustomControls, View.CustomControls)]
+ [InlineData(OnlyPropertiesCode.DataTemplates, View.DataTemplates)]
+ [InlineData(OnlyPropertiesCode.SignUpView, View.SignUpView)]
+ [InlineData(OnlyPropertiesCode.AttachedProps, View.AttachedProps)]
+ [InlineData(OnlyPropertiesCode.FieldModifier, View.FieldModifier)]
+ [InlineData(OnlyPropertiesCode.ControlWithoutWindow, View.ControlWithoutWindow)]
+ public async Task Should_Generate_FindControl_Refs_From_Avalonia_Markup_File(string expectation, string markup)
{
- [Theory]
- [InlineData(OnlyPropertiesCode.NamedControl, View.NamedControl)]
- [InlineData(OnlyPropertiesCode.NamedControls, View.NamedControls)]
- [InlineData(OnlyPropertiesCode.XNamedControl, View.XNamedControl)]
- [InlineData(OnlyPropertiesCode.XNamedControls, View.XNamedControls)]
- [InlineData(OnlyPropertiesCode.NoNamedControls, View.NoNamedControls)]
- [InlineData(OnlyPropertiesCode.CustomControls, View.CustomControls)]
- [InlineData(OnlyPropertiesCode.DataTemplates, View.DataTemplates)]
- [InlineData(OnlyPropertiesCode.SignUpView, View.SignUpView)]
- [InlineData(OnlyPropertiesCode.AttachedProps, View.AttachedProps)]
- [InlineData(OnlyPropertiesCode.FieldModifier, View.FieldModifier)]
- [InlineData(OnlyPropertiesCode.ControlWithoutWindow, View.ControlWithoutWindow)]
- public async Task Should_Generate_FindControl_Refs_From_Avalonia_Markup_File(string expectation, string markup)
- {
- var compilation =
- View.CreateAvaloniaCompilation()
- .WithCustomTextBox();
+ var compilation =
+ View.CreateAvaloniaCompilation()
+ .WithCustomTextBox();
- var classResolver = new XamlXViewResolver(
+ var classResolver = new XamlXViewResolver(
+ new RoslynTypeSystem(compilation),
+ MiniCompiler.CreateDefault(
new RoslynTypeSystem(compilation),
- MiniCompiler.CreateDefault(
- new RoslynTypeSystem(compilation),
- MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
+ MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
- var xaml = await View.Load(markup);
- var classInfo = classResolver.ResolveView(xaml);
- var nameResolver = new XamlXNameResolver();
- var names = nameResolver.ResolveNames(classInfo.Xaml);
+ var xaml = await View.Load(markup);
+ var classInfo = classResolver.ResolveView(xaml);
+ var nameResolver = new XamlXNameResolver();
+ var names = nameResolver.ResolveNames(classInfo.Xaml);
- var generator = new OnlyPropertiesCodeGenerator();
- var code = generator
- .GenerateCode("SampleView", "Sample.App", classInfo.XamlType, names)
- .Replace("\r", string.Empty);
+ var generator = new OnlyPropertiesCodeGenerator();
+ var code = generator
+ .GenerateCode("SampleView", "Sample.App", classInfo.XamlType, names)
+ .Replace("\r", string.Empty);
- var expected = await OnlyPropertiesCode.Load(expectation);
- CSharpSyntaxTree.ParseText(code);
- Assert.Equal(expected.Replace("\r", string.Empty), code);
- }
+ var expected = await OnlyPropertiesCode.Load(expectation);
+ CSharpSyntaxTree.ParseText(code);
+ Assert.Equal(expected.Replace("\r", string.Empty), code);
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/Views/View.cs b/src/Avalonia.NameGenerator.Tests/Views/View.cs
index 0fca5c7..f028019 100644
--- a/src/Avalonia.NameGenerator.Tests/Views/View.cs
+++ b/src/Avalonia.NameGenerator.Tests/Views/View.cs
@@ -7,64 +7,63 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
-namespace Avalonia.NameGenerator.Tests.Views
+namespace Avalonia.NameGenerator.Tests.Views;
+
+public static class View
{
- public static class View
- {
- public const string NamedControl = "NamedControl.xml";
- public const string NamedControls = "NamedControls.xml";
- public const string XNamedControl = "xNamedControl.xml";
- public const string XNamedControls = "xNamedControls.xml";
- public const string NoNamedControls = "NoNamedControls.xml";
- public const string CustomControls = "CustomControls.xml";
- public const string DataTemplates = "DataTemplates.xml";
- public const string SignUpView = "SignUpView.xml";
- public const string AttachedProps = "AttachedProps.xml";
- public const string FieldModifier = "FieldModifier.xml";
- public const string ControlWithoutWindow = "ControlWithoutWindow.xml";
+ public const string NamedControl = "NamedControl.xml";
+ public const string NamedControls = "NamedControls.xml";
+ public const string XNamedControl = "xNamedControl.xml";
+ public const string XNamedControls = "xNamedControls.xml";
+ public const string NoNamedControls = "NoNamedControls.xml";
+ public const string CustomControls = "CustomControls.xml";
+ public const string DataTemplates = "DataTemplates.xml";
+ public const string SignUpView = "SignUpView.xml";
+ public const string AttachedProps = "AttachedProps.xml";
+ public const string FieldModifier = "FieldModifier.xml";
+ public const string ControlWithoutWindow = "ControlWithoutWindow.xml";
- public static async Task Load(string viewName)
- {
- var assembly = typeof(XamlXNameResolverTests).Assembly;
- var fullResourceName = assembly
- .GetManifestResourceNames()
- .First(name => name.EndsWith(viewName));
-
- await using var stream = assembly.GetManifestResourceStream(fullResourceName);
- using var reader = new StreamReader(stream!);
- return await reader.ReadToEndAsync();
- }
+ public static async Task Load(string viewName)
+ {
+ var assembly = typeof(XamlXNameResolverTests).Assembly;
+ var fullResourceName = assembly
+ .GetManifestResourceNames()
+ .First(name => name.EndsWith(viewName));
- public static CSharpCompilation CreateAvaloniaCompilation(string excludedPattern = null)
- {
- var compilation = CSharpCompilation
- .Create("AvaloniaLib", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
- .AddReferences(MetadataReference.CreateFromFile(typeof(string).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(ITypeDescriptorContext).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(ISupportInitialize).Assembly.Location))
- .AddReferences(MetadataReference.CreateFromFile(typeof(TypeConverterAttribute).Assembly.Location));
+ await using var stream = assembly.GetManifestResourceStream(fullResourceName);
+ using var reader = new StreamReader(stream!);
+ return await reader.ReadToEndAsync();
+ }
- var avaloniaAssemblyLocation = typeof(TextBlock).Assembly.Location;
- var avaloniaAssemblyDirectory = Path.GetDirectoryName(avaloniaAssemblyLocation);
- var avaloniaAssemblyReferences = Directory
- .EnumerateFiles(avaloniaAssemblyDirectory!)
- .Where(file => file.EndsWith(".dll") &&
- file.Contains("Avalonia") &&
- (string.IsNullOrWhiteSpace(excludedPattern) || !file.Contains(excludedPattern)))
- .Select(file => MetadataReference.CreateFromFile(file))
- .ToList();
+ public static CSharpCompilation CreateAvaloniaCompilation(string excludedPattern = null)
+ {
+ var compilation = CSharpCompilation
+ .Create("AvaloniaLib", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(string).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(ITypeDescriptorContext).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(ISupportInitialize).Assembly.Location))
+ .AddReferences(MetadataReference.CreateFromFile(typeof(TypeConverterAttribute).Assembly.Location));
- return compilation.AddReferences(avaloniaAssemblyReferences);
- }
+ var avaloniaAssemblyLocation = typeof(TextBlock).Assembly.Location;
+ var avaloniaAssemblyDirectory = Path.GetDirectoryName(avaloniaAssemblyLocation);
+ var avaloniaAssemblyReferences = Directory
+ .EnumerateFiles(avaloniaAssemblyDirectory!)
+ .Where(file => file.EndsWith(".dll") &&
+ file.Contains("Avalonia") &&
+ (string.IsNullOrWhiteSpace(excludedPattern) || !file.Contains(excludedPattern)))
+ .Select(file => MetadataReference.CreateFromFile(file))
+ .ToList();
- public static CSharpCompilation WithCustomTextBox(this CSharpCompilation compilation) =>
- compilation.AddSyntaxTrees(
- CSharpSyntaxTree.ParseText(
- "using Avalonia.Controls;" +
- "namespace Controls {" +
- " public class CustomTextBox : TextBox { }" +
- " public class EvilControl { }" +
- "}"));
+ return compilation.AddReferences(avaloniaAssemblyReferences);
}
+
+ public static CSharpCompilation WithCustomTextBox(this CSharpCompilation compilation) =>
+ compilation.AddSyntaxTrees(
+ CSharpSyntaxTree.ParseText(
+ "using Avalonia.Controls;" +
+ "namespace Controls {" +
+ " public class CustomTextBox : TextBox { }" +
+ " public class EvilControl { }" +
+ "}"));
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/XamlXClassResolverTests.cs b/src/Avalonia.NameGenerator.Tests/XamlXClassResolverTests.cs
index a0d9ec4..f0a1114 100644
--- a/src/Avalonia.NameGenerator.Tests/XamlXClassResolverTests.cs
+++ b/src/Avalonia.NameGenerator.Tests/XamlXClassResolverTests.cs
@@ -4,36 +4,35 @@
using Avalonia.NameGenerator.Tests.Views;
using Xunit;
-namespace Avalonia.NameGenerator.Tests
+namespace Avalonia.NameGenerator.Tests;
+
+public class XamlXClassResolverTests
{
- public class XamlXClassResolverTests
+ [Theory]
+ [InlineData("Sample.App", "NamedControl", View.NamedControl)]
+ [InlineData("Sample.App", "AttachedProps", View.AttachedProps)]
+ [InlineData("Sample.App", "CustomControls", View.CustomControls)]
+ [InlineData("Sample.App", "DataTemplates", View.DataTemplates)]
+ [InlineData("Sample.App", "FieldModifier", View.FieldModifier)]
+ [InlineData("Sample.App", "NamedControls", View.NamedControls)]
+ [InlineData("Sample.App", "NoNamedControls", View.NoNamedControls)]
+ [InlineData("Sample.App", "SignUpView", View.SignUpView)]
+ [InlineData("Sample.App", "xNamedControl", View.XNamedControl)]
+ [InlineData("Sample.App", "xNamedControls", View.XNamedControls)]
+ public async Task Should_Resolve_Base_Class_From_Xaml_File(string nameSpace, string className, string markup)
{
- [Theory]
- [InlineData("Sample.App", "NamedControl", View.NamedControl)]
- [InlineData("Sample.App", "AttachedProps", View.AttachedProps)]
- [InlineData("Sample.App", "CustomControls", View.CustomControls)]
- [InlineData("Sample.App", "DataTemplates", View.DataTemplates)]
- [InlineData("Sample.App", "FieldModifier", View.FieldModifier)]
- [InlineData("Sample.App", "NamedControls", View.NamedControls)]
- [InlineData("Sample.App", "NoNamedControls", View.NoNamedControls)]
- [InlineData("Sample.App", "SignUpView", View.SignUpView)]
- [InlineData("Sample.App", "xNamedControl", View.XNamedControl)]
- [InlineData("Sample.App", "xNamedControls", View.XNamedControls)]
- public async Task Should_Resolve_Base_Class_From_Xaml_File(string nameSpace, string className, string markup)
- {
- var xaml = await View.Load(markup);
- var compilation = View
- .CreateAvaloniaCompilation()
- .WithCustomTextBox();
+ var xaml = await View.Load(markup);
+ var compilation = View
+ .CreateAvaloniaCompilation()
+ .WithCustomTextBox();
- var types = new RoslynTypeSystem(compilation);
- var resolver = new XamlXViewResolver(
- types,
- MiniCompiler.CreateDefault(types, MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
+ var types = new RoslynTypeSystem(compilation);
+ var resolver = new XamlXViewResolver(
+ types,
+ MiniCompiler.CreateDefault(types, MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
- var resolvedClass = resolver.ResolveView(xaml);
- Assert.Equal(className, resolvedClass.ClassName);
- Assert.Equal(nameSpace, resolvedClass.Namespace);
- }
+ var resolvedClass = resolver.ResolveView(xaml);
+ Assert.Equal(className, resolvedClass.ClassName);
+ Assert.Equal(nameSpace, resolvedClass.Namespace);
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator.Tests/XamlXNameResolverTests.cs b/src/Avalonia.NameGenerator.Tests/XamlXNameResolverTests.cs
index 8447af4..b6c4b13 100644
--- a/src/Avalonia.NameGenerator.Tests/XamlXNameResolverTests.cs
+++ b/src/Avalonia.NameGenerator.Tests/XamlXNameResolverTests.cs
@@ -8,116 +8,115 @@
using Avalonia.NameGenerator.Tests.Views;
using Xunit;
-namespace Avalonia.NameGenerator.Tests
+namespace Avalonia.NameGenerator.Tests;
+
+public class XamlXNameResolverTests
{
- public class XamlXNameResolverTests
+ [Theory]
+ [InlineData(View.NamedControl)]
+ [InlineData(View.XNamedControl)]
+ [InlineData(View.AttachedProps)]
+ public async Task Should_Resolve_Types_From_Avalonia_Markup_File_With_Named_Control(string resource)
+ {
+ var xaml = await View.Load(resource);
+ var controls = ResolveNames(xaml);
+
+ Assert.NotEmpty(controls);
+ Assert.Equal(1, controls.Count);
+ Assert.Equal("UserNameTextBox", controls[0].Name);
+ Assert.Equal(typeof(TextBox).FullName, controls[0].TypeName);
+ }
+
+ [Theory]
+ [InlineData(View.NamedControls)]
+ [InlineData(View.XNamedControls)]
+ public async Task Should_Resolve_Types_From_Avalonia_Markup_File_With_Named_Controls(string resource)
+ {
+ var xaml = await View.Load(resource);
+ var controls = ResolveNames(xaml);
+
+ Assert.NotEmpty(controls);
+ Assert.Equal(3, controls.Count);
+ Assert.Equal("UserNameTextBox", controls[0].Name);
+ Assert.Equal("PasswordTextBox", controls[1].Name);
+ Assert.Equal("SignUpButton", controls[2].Name);
+ Assert.Equal(typeof(TextBox).FullName, controls[0].TypeName);
+ Assert.Equal(typeof(TextBox).FullName, controls[1].TypeName);
+ Assert.Equal(typeof(Button).FullName, controls[2].TypeName);
+ }
+
+ [Fact]
+ public async Task Should_Resolve_Types_From_Avalonia_Markup_File_With_Custom_Controls()
{
- [Theory]
- [InlineData(View.NamedControl)]
- [InlineData(View.XNamedControl)]
- [InlineData(View.AttachedProps)]
- public async Task Should_Resolve_Types_From_Avalonia_Markup_File_With_Named_Control(string resource)
- {
- var xaml = await View.Load(resource);
- var controls = ResolveNames(xaml);
-
- Assert.NotEmpty(controls);
- Assert.Equal(1, controls.Count);
- Assert.Equal("UserNameTextBox", controls[0].Name);
- Assert.Equal(typeof(TextBox).FullName, controls[0].TypeName);
- }
-
- [Theory]
- [InlineData(View.NamedControls)]
- [InlineData(View.XNamedControls)]
- public async Task Should_Resolve_Types_From_Avalonia_Markup_File_With_Named_Controls(string resource)
- {
- var xaml = await View.Load(resource);
- var controls = ResolveNames(xaml);
-
- Assert.NotEmpty(controls);
- Assert.Equal(3, controls.Count);
- Assert.Equal("UserNameTextBox", controls[0].Name);
- Assert.Equal("PasswordTextBox", controls[1].Name);
- Assert.Equal("SignUpButton", controls[2].Name);
- Assert.Equal(typeof(TextBox).FullName, controls[0].TypeName);
- Assert.Equal(typeof(TextBox).FullName, controls[1].TypeName);
- Assert.Equal(typeof(Button).FullName, controls[2].TypeName);
- }
-
- [Fact]
- public async Task Should_Resolve_Types_From_Avalonia_Markup_File_With_Custom_Controls()
- {
- var xaml = await View.Load(View.CustomControls);
- var controls = ResolveNames(xaml);
-
- Assert.NotEmpty(controls);
- Assert.Equal(3, controls.Count);
- Assert.Equal("ClrNamespaceRoutedViewHost", controls[0].Name);
- Assert.Equal("UriRoutedViewHost", controls[1].Name);
- Assert.Equal("UserNameTextBox", controls[2].Name);
- Assert.Equal(typeof(RoutedViewHost).FullName, controls[0].TypeName);
- Assert.Equal(typeof(RoutedViewHost).FullName, controls[1].TypeName);
- Assert.Equal("Controls.CustomTextBox", controls[2].TypeName);
- }
-
- [Fact]
- public async Task Should_Not_Resolve_Named_Controls_From_Avalonia_Markup_File_Without_Named_Controls()
- {
- var xaml = await View.Load(View.NoNamedControls);
- var controls = ResolveNames(xaml);
-
- Assert.Empty(controls);
- }
-
- [Fact]
- public async Task Should_Not_Resolve_Elements_From_DataTemplates()
- {
- var xaml = await View.Load(View.DataTemplates);
- var controls = ResolveNames(xaml);
-
- Assert.NotEmpty(controls);
- Assert.Equal(2, controls.Count);
- Assert.Equal("UserNameTextBox", controls[0].Name);
- Assert.Equal("NamedListBox", controls[1].Name);
- Assert.Equal(typeof(TextBox).FullName, controls[0].TypeName);
- Assert.Equal(typeof(ListBox).FullName, controls[1].TypeName);
- }
-
- [Fact]
- public async Task Should_Resolve_Names_From_Complex_Views()
- {
- var xaml = await View.Load(View.SignUpView);
- var controls = ResolveNames(xaml);
-
- Assert.NotEmpty(controls);
- Assert.Equal(9, controls.Count);
- Assert.Equal("UserNameTextBox", controls[0].Name);
- Assert.Equal("UserNameValidation", controls[1].Name);
- Assert.Equal("PasswordTextBox", controls[2].Name);
- Assert.Equal("PasswordValidation", controls[3].Name);
- Assert.Equal("AwesomeListView", controls[4].Name);
- Assert.Equal("ConfirmPasswordTextBox", controls[5].Name);
- Assert.Equal("ConfirmPasswordValidation", controls[6].Name);
- Assert.Equal("SignUpButton", controls[7].Name);
- Assert.Equal("CompoundValidation", controls[8].Name);
- }
-
- private static IReadOnlyList ResolveNames(string xaml)
- {
- var compilation =
- View.CreateAvaloniaCompilation()
- .WithCustomTextBox();
-
- var classResolver = new XamlXViewResolver(
+ var xaml = await View.Load(View.CustomControls);
+ var controls = ResolveNames(xaml);
+
+ Assert.NotEmpty(controls);
+ Assert.Equal(3, controls.Count);
+ Assert.Equal("ClrNamespaceRoutedViewHost", controls[0].Name);
+ Assert.Equal("UriRoutedViewHost", controls[1].Name);
+ Assert.Equal("UserNameTextBox", controls[2].Name);
+ Assert.Equal(typeof(RoutedViewHost).FullName, controls[0].TypeName);
+ Assert.Equal(typeof(RoutedViewHost).FullName, controls[1].TypeName);
+ Assert.Equal("Controls.CustomTextBox", controls[2].TypeName);
+ }
+
+ [Fact]
+ public async Task Should_Not_Resolve_Named_Controls_From_Avalonia_Markup_File_Without_Named_Controls()
+ {
+ var xaml = await View.Load(View.NoNamedControls);
+ var controls = ResolveNames(xaml);
+
+ Assert.Empty(controls);
+ }
+
+ [Fact]
+ public async Task Should_Not_Resolve_Elements_From_DataTemplates()
+ {
+ var xaml = await View.Load(View.DataTemplates);
+ var controls = ResolveNames(xaml);
+
+ Assert.NotEmpty(controls);
+ Assert.Equal(2, controls.Count);
+ Assert.Equal("UserNameTextBox", controls[0].Name);
+ Assert.Equal("NamedListBox", controls[1].Name);
+ Assert.Equal(typeof(TextBox).FullName, controls[0].TypeName);
+ Assert.Equal(typeof(ListBox).FullName, controls[1].TypeName);
+ }
+
+ [Fact]
+ public async Task Should_Resolve_Names_From_Complex_Views()
+ {
+ var xaml = await View.Load(View.SignUpView);
+ var controls = ResolveNames(xaml);
+
+ Assert.NotEmpty(controls);
+ Assert.Equal(9, controls.Count);
+ Assert.Equal("UserNameTextBox", controls[0].Name);
+ Assert.Equal("UserNameValidation", controls[1].Name);
+ Assert.Equal("PasswordTextBox", controls[2].Name);
+ Assert.Equal("PasswordValidation", controls[3].Name);
+ Assert.Equal("AwesomeListView", controls[4].Name);
+ Assert.Equal("ConfirmPasswordTextBox", controls[5].Name);
+ Assert.Equal("ConfirmPasswordValidation", controls[6].Name);
+ Assert.Equal("SignUpButton", controls[7].Name);
+ Assert.Equal("CompoundValidation", controls[8].Name);
+ }
+
+ private static IReadOnlyList ResolveNames(string xaml)
+ {
+ var compilation =
+ View.CreateAvaloniaCompilation()
+ .WithCustomTextBox();
+
+ var classResolver = new XamlXViewResolver(
+ new RoslynTypeSystem(compilation),
+ MiniCompiler.CreateDefault(
new RoslynTypeSystem(compilation),
- MiniCompiler.CreateDefault(
- new RoslynTypeSystem(compilation),
- MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
-
- var classInfo = classResolver.ResolveView(xaml);
- var nameResolver = new XamlXNameResolver();
- return nameResolver.ResolveNames(classInfo.Xaml);
- }
+ MiniCompiler.AvaloniaXmlnsDefinitionAttribute));
+
+ var classInfo = classResolver.ResolveView(xaml);
+ var nameResolver = new XamlXNameResolver();
+ return nameResolver.ResolveNames(classInfo.Xaml);
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator/Compiler/DataTemplateTransformer.cs b/src/Avalonia.NameGenerator/Compiler/DataTemplateTransformer.cs
index 8296353..abfd48b 100644
--- a/src/Avalonia.NameGenerator/Compiler/DataTemplateTransformer.cs
+++ b/src/Avalonia.NameGenerator/Compiler/DataTemplateTransformer.cs
@@ -1,18 +1,17 @@
using XamlX.Ast;
using XamlX.Transform;
-namespace Avalonia.NameGenerator.Compiler
+namespace Avalonia.NameGenerator.Compiler;
+
+internal class DataTemplateTransformer : IXamlAstTransformer
{
- internal class DataTemplateTransformer : IXamlAstTransformer
+ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
{
- public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
- {
- if (node is XamlAstObjectNode objectNode &&
- objectNode.Type is XamlAstXmlTypeReference typeReference &&
- (typeReference.Name == "DataTemplate" ||
- typeReference.Name == "ControlTemplate"))
- objectNode.Children.Clear();
- return node;
- }
+ if (node is XamlAstObjectNode objectNode &&
+ objectNode.Type is XamlAstXmlTypeReference typeReference &&
+ (typeReference.Name == "DataTemplate" ||
+ typeReference.Name == "ControlTemplate"))
+ objectNode.Children.Clear();
+ return node;
}
}
\ No newline at end of file
diff --git a/src/Avalonia.NameGenerator/Compiler/MiniCompiler.cs b/src/Avalonia.NameGenerator/Compiler/MiniCompiler.cs
index 5dd5f36..237dcdd 100644
--- a/src/Avalonia.NameGenerator/Compiler/MiniCompiler.cs
+++ b/src/Avalonia.NameGenerator/Compiler/MiniCompiler.cs
@@ -5,41 +5,40 @@
using XamlX.Transform.Transformers;
using XamlX.TypeSystem;
-namespace Avalonia.NameGenerator.Compiler
+namespace Avalonia.NameGenerator.Compiler;
+
+internal sealed class MiniCompiler : XamlCompiler