Skip to content

Commit

Permalink
test(roslyn): add unit tests for GetMemberType utility method (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo authored Nov 19, 2023
1 parent 84290c9 commit a387301
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class EmptyUxmlNameAnalyzer : DiagnosticAnalyzer
new LocalizableResourceString(nameof(Resources.UIC102_MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.UIC102_Description), Resources.ResourceManager, typeof(Resources));
private static readonly string Category = "Layout";
private const string Category = "Layout";

private static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class EmptyUxmlTraitNameAnalyzer : DiagnosticAnalyzer
new LocalizableResourceString(nameof(Resources.UIC103_MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.UIC103_Description), Resources.ResourceManager, typeof(Resources));
private static readonly string Category = "Layout";
private const string Category = "Layout";

private static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;

namespace UIComponents.Roslyn.Analyzers
{
Expand All @@ -18,7 +17,7 @@ public sealed class InvalidReadonlyMemberAnalyzer : DiagnosticAnalyzer
new LocalizableResourceString(nameof(Resources.UIC003_MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.UIC003_Description), Resources.ResourceManager, typeof(Resources));
private static readonly string Category = "Core";
private const string Category = "Core";

private static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class UIComponentIdenticalStylesheetAnalyzer : DiagnosticAnalyzer
new LocalizableResourceString(nameof(Resources.UIC101_MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.UIC101_Description), Resources.ResourceManager, typeof(Resources));
private static readonly string Category = "Layout";
private const string Category = "Layout";

private static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class UIComponentInternalMethodAnalyzer : DiagnosticAnalyzer
new LocalizableResourceString(nameof(Resources.UIC002_MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.UIC002_Description), Resources.ResourceManager, typeof(Resources));
private static readonly string Category = "Core";
private const string Category = "Core";

private static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;
using UIComponents.Roslyn.Common.Utilities;

namespace UIComponents.Roslyn.Analyzers
Expand All @@ -19,7 +18,7 @@ public sealed class UIComponentPartialAnalyzer : DiagnosticAnalyzer
new LocalizableResourceString(nameof(Resources.UIC001_MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.UIC001_Description), Resources.ResourceManager, typeof(Resources));
private static readonly string Category = "Core";
private const string Category = "Core";

private static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ public void Returns_Null_If_Member_Is_Not_Field_Or_Property()

Assert.Null(memberType);
}

[Fact]
public void Returns_FieldType_If_Member_Is_Field()
{
var fieldSymbol = Substitute.For<IFieldSymbol>();
var fieldType = Substitute.For<ITypeSymbol>();
fieldSymbol.Type.Returns(fieldType);

var memberType = RoslynUtilities.GetMemberType(fieldSymbol);

Assert.Equal(fieldType, memberType);
}

[Fact]
public void Returns_PropertyType_If_Member_Is_Property()
{
var propertySymbol = Substitute.For<IPropertySymbol>();
var propertyType = Substitute.For<ITypeSymbol>();
propertySymbol.Type.Returns(propertyType);

var memberType = RoslynUtilities.GetMemberType(propertySymbol);

Assert.Equal(propertyType, memberType);
}
}

public class GetTypeNameForNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public static string GetTypeNameForNamespace(ITypeSymbol type, string nameSpace)
var displayStringParts = displayString.Split('.');
var namespaceParts = nameSpace.Split('.');

// TODO: UGLY HACK. We don't want to shorten UIComponents types because doing so
// NOTE(joni): We don't want to shorten UIComponents types because doing so
// may lead to compile errors, since the shortened type names may no longer
// be valid. There should be some mechanism for generating using statements...
// be valid.
if (nameSpace.StartsWith("UIComponents."))
return displayString;

Expand Down Expand Up @@ -156,7 +156,7 @@ public static ITypeSymbol GetMemberType(ISymbol symbol)
{
if (symbol is IFieldSymbol fieldSymbol)
return fieldSymbol.Type;
else if (symbol is IPropertySymbol propertySymbol)
if (symbol is IPropertySymbol propertySymbol)
return propertySymbol.Type;

return null;
Expand Down

0 comments on commit a387301

Please sign in to comment.