This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for extensible directives.
- Also involved adding comparers for directive descriptors and their pieces. #853
- Loading branch information
1 parent
b513709
commit 749866d
Showing
9 changed files
with
658 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Microsoft.AspNetCore.Razor.Evolution/DirectiveDescriptorComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
internal class DirectiveDescriptorComparer : IEqualityComparer<DirectiveDescriptor> | ||
{ | ||
public static readonly DirectiveDescriptorComparer Default = new DirectiveDescriptorComparer(); | ||
|
||
protected DirectiveDescriptorComparer() | ||
{ | ||
} | ||
|
||
public bool Equals(DirectiveDescriptor descriptorX, DirectiveDescriptor descriptorY) | ||
{ | ||
if (descriptorX == descriptorY) | ||
{ | ||
return true; | ||
} | ||
|
||
return descriptorX != null && | ||
string.Equals(descriptorX.Name, descriptorY.Name, StringComparison.Ordinal) && | ||
descriptorX.Kind == descriptorY.Kind && | ||
Enumerable.SequenceEqual( | ||
descriptorX.Tokens, | ||
descriptorY.Tokens, | ||
DirectiveTokenDescriptorComparer.Default); | ||
} | ||
|
||
public int GetHashCode(DirectiveDescriptor descriptor) | ||
{ | ||
if (descriptor == null) | ||
{ | ||
throw new ArgumentNullException(nameof(descriptor)); | ||
} | ||
|
||
var hashCodeCombiner = HashCodeCombiner.Start(); | ||
hashCodeCombiner.Add(descriptor.Name, StringComparer.Ordinal); | ||
hashCodeCombiner.Add(descriptor.Kind); | ||
|
||
return hashCodeCombiner.CombinedHash; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Microsoft.AspNetCore.Razor.Evolution/DirectiveTokenDescriptorComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
internal class DirectiveTokenDescriptorComparer : IEqualityComparer<DirectiveTokenDescriptor> | ||
{ | ||
public static readonly DirectiveTokenDescriptorComparer Default = new DirectiveTokenDescriptorComparer(); | ||
|
||
protected DirectiveTokenDescriptorComparer() | ||
{ | ||
} | ||
|
||
public bool Equals(DirectiveTokenDescriptor descriptorX, DirectiveTokenDescriptor descriptorY) | ||
{ | ||
if (descriptorX == descriptorY) | ||
{ | ||
return true; | ||
} | ||
|
||
return descriptorX != null && | ||
string.Equals(descriptorX.Value, descriptorY.Value, StringComparison.Ordinal) && | ||
descriptorX.Kind == descriptorY.Kind; | ||
} | ||
|
||
public int GetHashCode(DirectiveTokenDescriptor descriptor) | ||
{ | ||
if (descriptor == null) | ||
{ | ||
throw new ArgumentNullException(nameof(descriptor)); | ||
} | ||
|
||
var hashCodeCombiner = HashCodeCombiner.Start(); | ||
hashCodeCombiner.Add(descriptor.Value, StringComparer.Ordinal); | ||
hashCodeCombiner.Add(descriptor.Kind); | ||
|
||
return hashCodeCombiner.CombinedHash; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
test/Microsoft.AspNetCore.Razor.Evolution.Test/DirectiveDescriptorBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
public class DirectiveDescriptorBuilderTest | ||
{ | ||
[Fact] | ||
public void Create_BuildsSingleLineDirectiveDescriptor() | ||
{ | ||
// Act | ||
var descriptor = DirectiveDescriptorBuilder.Create("custom").Build(); | ||
|
||
// Assert | ||
Assert.Equal(DirectiveDescriptorKind.SingleLine, descriptor.Kind); | ||
} | ||
|
||
[Fact] | ||
public void CreateRazorBlock_BuildsRazorBlockDirectiveDescriptor() | ||
{ | ||
// Act | ||
var descriptor = DirectiveDescriptorBuilder.CreateRazorBlock("custom").Build(); | ||
|
||
// Assert | ||
Assert.Equal(DirectiveDescriptorKind.RazorBlock, descriptor.Kind); | ||
} | ||
|
||
[Fact] | ||
public void CreateCodeBlock_BuildsCodeBlockDirectiveDescriptor() | ||
{ | ||
// Act | ||
var descriptor = DirectiveDescriptorBuilder.CreateCodeBlock("custom").Build(); | ||
|
||
// Assert | ||
Assert.Equal(DirectiveDescriptorKind.CodeBlock, descriptor.Kind); | ||
} | ||
|
||
[Fact] | ||
public void AddType_AddsToken() | ||
{ | ||
// Arrange | ||
var builder = DirectiveDescriptorBuilder.Create("custom"); | ||
|
||
// Act | ||
var descriptor = builder.AddType().Build(); | ||
|
||
// Assert | ||
var token = Assert.Single(descriptor.Tokens); | ||
Assert.Equal(DirectiveTokenKind.Type, token.Kind); | ||
} | ||
|
||
[Fact] | ||
public void AddMember_AddsToken() | ||
{ | ||
// Arrange | ||
var builder = DirectiveDescriptorBuilder.Create("custom"); | ||
|
||
// Act | ||
var descriptor = builder.AddMember().Build(); | ||
|
||
// Assert | ||
var token = Assert.Single(descriptor.Tokens); | ||
Assert.Equal(DirectiveTokenKind.Member, token.Kind); | ||
} | ||
|
||
[Fact] | ||
public void AddString_AddsToken() | ||
{ | ||
// Arrange | ||
var builder = DirectiveDescriptorBuilder.Create("custom"); | ||
|
||
// Act | ||
var descriptor = builder.AddString().Build(); | ||
|
||
// Assert | ||
var token = Assert.Single(descriptor.Tokens); | ||
Assert.Equal(DirectiveTokenKind.String, token.Kind); | ||
} | ||
|
||
[Fact] | ||
public void AddLiteral_AddsToken() | ||
{ | ||
// Arrange | ||
var builder = DirectiveDescriptorBuilder.Create("custom"); | ||
|
||
// Act | ||
var descriptor = builder.AddLiteral(",").Build(); | ||
|
||
// Assert | ||
var token = Assert.Single(descriptor.Tokens); | ||
Assert.Equal(DirectiveTokenKind.Literal, token.Kind); | ||
Assert.Equal(",", token.Value); | ||
} | ||
|
||
[Fact] | ||
public void AddX_MaintainsMultipleTokens() | ||
{ | ||
// Arrange | ||
var builder = DirectiveDescriptorBuilder.Create("custom"); | ||
|
||
// Act | ||
var descriptor = builder | ||
.AddType() | ||
.AddMember() | ||
.AddString() | ||
.AddLiteral(",") | ||
.Build(); | ||
|
||
// Assert | ||
Assert.Collection(descriptor.Tokens, | ||
token => Assert.Equal(DirectiveTokenKind.Type, token.Kind), | ||
token => Assert.Equal(DirectiveTokenKind.Member, token.Kind), | ||
token => Assert.Equal(DirectiveTokenKind.String, token.Kind), | ||
token => | ||
{ | ||
Assert.Equal(DirectiveTokenKind.Literal, token.Kind); | ||
Assert.Equal(",", token.Value); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.