Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,21 @@ internal static bool IsTypeInNamespace(TagHelperDescriptor tagHelper, string @na

static bool IsTypeInNamespace(string typeNamespace, string @namespace)
{
// Either the typeName is not the full type name or this type is at the top level.
return string.IsNullOrEmpty(typeNamespace) || typeNamespace.Equals(@namespace, StringComparison.Ordinal);
if (string.IsNullOrEmpty(typeNamespace))
{
// Either the typeName is not the full type name or this type is at the top level.
return true;
}

// Remove global:: prefix from namespace.
const string globalPrefix = "global::";
var normalizedNamespace = @namespace.AsSpan();
if (@namespace.StartsWith(globalPrefix, StringComparison.Ordinal))
{
normalizedNamespace = normalizedNamespace[globalPrefix.Length..];
}

return normalizedNamespace.Equals(typeNamespace.AsSpan(), StringComparison.Ordinal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,10 @@ @using static SomeProject.SomeOtherFolder.Foo
[InlineData("", "", true)]
[InlineData("Foo", "Project", true)]
[InlineData("Project.Foo", "Project", true)]
[InlineData("Project.Foo", "global::Project", true)]
[InlineData("Project.Bar.Foo", "Project.Bar", true)]
[InlineData("Project.Foo", "Project.Bar", false)]
[InlineData("Project.Foo", "global::Project.Bar", false)]
[InlineData("Project.Bar.Foo", "Project", false)]
[InlineData("Bar.Foo", "Project", false)]
public void IsTypeInNamespace_WorksAsExpected(string typeName, string @namespace, bool expected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 expected shouldn't be a parameter here. It should be a variable inside the test which is initialized with a switch expression, e.g.:

var expected = (typeName, @namespace) switch
{
  ("", "") => true,
  ("Foo", "Project") => true,
  ("Project.Foo", "Project") => true,
  ("Project.Foo", "global::Project") => true,
  _ => false,
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would that be better? It would need duplicating the test data.

Copy link
Contributor

@sharwell sharwell Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would that be better?

The parameters to a test are part of the test identity. When expected outcomes are part of the parameter list, it is impossible to update code in a manner that changes the expected outcome of a test without deleting the entire test history. This improves both CI behavior and local iterations as the test is initially developed.

It would need duplicating the test data.

Yes, and for this type of situation it's completely acceptable. Duplication is not as much a maintenance problem when coupled (both copies are highly likely to appear on the same screen at the same time).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting. I wouldn't think expected outcomes of unit tests change, that's what snapshot tests are for. Although I guess the line between the two is not that clear.

Expand Down