This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathRazorSyntaxFactsServiceExtensionsTest.cs
107 lines (87 loc) · 3.3 KB
/
RazorSyntaxFactsServiceExtensionsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 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 Microsoft.AspNetCore.Razor.Language;
using Xunit;
namespace Microsoft.VisualStudio.Editor.Razor
{
public class RazorSyntaxFactsServiceExtensionsTest
{
[Fact]
public void IsTagHelperSpan_ReturnsTrue()
{
// Arrange
var syntaxTree = GetSyntaxTree(
@"<div>
<taghelper />
</div>");
var location = new SourceSpan(9 + Environment.NewLine.Length, 13);
var service = new DefaultRazorSyntaxFactsService();
// Act
var result = service.IsTagHelperSpan(syntaxTree, location);
// Assert
Assert.True(result);
}
[Fact]
public void IsTagHelperSpan_ReturnsFalse()
{
// Arrange
var syntaxTree = GetSyntaxTree(
@"<div>
<taghelper></taghelper>
</div>");
var location = new SourceSpan(0, 4);
var service = new DefaultRazorSyntaxFactsService();
// Act
var result = service.IsTagHelperSpan(syntaxTree, location);
// Assert
Assert.False(result);
}
[Fact]
public void IsTagHelperSpan_NullSyntaxTree_ReturnsFalse()
{
// Arrange
var location = new SourceSpan(0, 4);
var service = new DefaultRazorSyntaxFactsService();
// Act
var result = service.IsTagHelperSpan(null, location);
// Assert
Assert.False(result);
}
private static RazorSyntaxTree GetSyntaxTree(string source)
{
var taghelper = TagHelperDescriptorBuilder.Create("TestTagHelper", "TestAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("taghelper"))
.TypeName("TestTagHelper")
.Build();
var projectEngine = RazorProjectEngine.Create(builder =>
{
builder.AddTagHelpers(taghelper);
builder.Features.Add(new DesignTimeOptionsFeature(designTime: true));
});
var sourceDocument = RazorSourceDocument.Create(source, "test.cshtml");
var addTagHelperImport = RazorSourceDocument.Create("@addTagHelper *, TestAssembly", "import.cshtml");
var codeDocument = RazorCodeDocument.Create(sourceDocument, new[] { addTagHelperImport });
projectEngine.Engine.Process(codeDocument);
return codeDocument.GetSyntaxTree();
}
private class DesignTimeOptionsFeature : IConfigureRazorParserOptionsFeature, IConfigureRazorCodeGenerationOptionsFeature
{
private bool _designTime;
public DesignTimeOptionsFeature(bool designTime)
{
_designTime = designTime;
}
public int Order { get; }
public RazorEngine Engine { get; set; }
public void Configure(RazorParserOptionsBuilder options)
{
options.SetDesignTime(_designTime);
}
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
options.SetDesignTime(_designTime);
}
}
}
}