forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameExtensionsTest.cs
More file actions
176 lines (144 loc) · 7.01 KB
/
Copy pathNameExtensionsTest.cs
File metadata and controls
176 lines (144 loc) · 7.01 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 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.Collections.Generic;
using Microsoft.TestCommon;
using Microsoft.Web.UnitTestUtil;
namespace System.Web.Mvc.Html.Test
{
public class NameExtensionsTest
{
[Fact]
public void NonStronglyTypedWithNoPrefix()
{
// Arrange
HtmlHelper html = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
// Act & Assert
Assert.Equal("", html.IdForModel().ToHtmlString());
Assert.Equal("foo", html.Id("foo").ToHtmlString());
Assert.Equal("foo_bar", html.Id("foo.bar").ToHtmlString());
Assert.Equal(String.Empty, html.Id("<script>alert(\"XSS!\")</script>").ToHtmlString());
Assert.Equal("", html.NameForModel().ToHtmlString());
Assert.Equal("foo", html.Name("foo").ToHtmlString());
Assert.Equal("foo.bar", html.Name("foo.bar").ToHtmlString());
Assert.Equal("<script>alert("XSS!")</script>", html.Name("<script>alert(\"XSS!\")</script>").ToHtmlString());
}
[Fact]
public void NonStronglyTypedWithPrefix()
{
// Arrange
HtmlHelper html = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
html.ViewData.TemplateInfo.HtmlFieldPrefix = "prefix";
// Act & Assert
Assert.Equal("prefix", html.IdForModel().ToHtmlString());
Assert.Equal("prefix_foo", html.Id("foo").ToHtmlString());
Assert.Equal("prefix_foo_bar", html.Id("foo.bar").ToHtmlString());
Assert.Equal("prefix", html.NameForModel().ToHtmlString());
Assert.Equal("prefix.foo", html.Name("foo").ToHtmlString());
Assert.Equal("prefix.foo.bar", html.Name("foo.bar").ToHtmlString());
}
[Fact]
public void StronglyTypedWithNoPrefix()
{
// Arrange
HtmlHelper<OuterClass> html = MvcHelper.GetHtmlHelper(new ViewDataDictionary<OuterClass>());
// Act & Assert
Assert.Equal("IntValue", html.IdFor(m => m.IntValue).ToHtmlString());
Assert.Equal("Inner_StringValue", html.IdFor(m => m.Inner.StringValue).ToHtmlString());
Assert.Equal("IntValue", html.NameFor(m => m.IntValue).ToHtmlString());
Assert.Equal("Inner.StringValue", html.NameFor(m => m.Inner.StringValue).ToHtmlString());
}
[Fact]
public void StronglyTypedWithPrefix()
{
// Arrange
HtmlHelper<OuterClass> html = MvcHelper.GetHtmlHelper(new ViewDataDictionary<OuterClass>());
html.ViewData.TemplateInfo.HtmlFieldPrefix = "prefix";
// Act & Assert
Assert.Equal("prefix_IntValue", html.IdFor(m => m.IntValue).ToHtmlString());
Assert.Equal("prefix_Inner_StringValue", html.IdFor(m => m.Inner.StringValue).ToHtmlString());
Assert.Equal("prefix.IntValue", html.NameFor(m => m.IntValue).ToHtmlString());
Assert.Equal("prefix.Inner.StringValue", html.NameFor(m => m.Inner.StringValue).ToHtmlString());
}
// Regression test for codeplex #554 - editor templates for collections would add an extra dot to the
// generated name.
[Fact]
public void StronglyTypedCollectionWithPrefix()
{
// Arrange
HtmlHelper<List<OuterClass>> html = MvcHelper.GetHtmlHelper(new ViewDataDictionary<List<OuterClass>>());
html.ViewData.TemplateInfo.HtmlFieldPrefix = "prefix";
// Act & Assert
Assert.Equal("prefix_0__IntValue", html.IdFor(m => m[0].IntValue).ToHtmlString());
Assert.Equal("prefix_0__Inner_StringValue", html.IdFor(m => m[0].Inner.StringValue).ToHtmlString());
Assert.Equal("prefix[0].IntValue", html.NameFor(m => m[0].IntValue).ToHtmlString());
Assert.Equal("prefix[0].Inner.StringValue", html.NameFor(m => m[0].Inner.StringValue).ToHtmlString());
}
[Theory]
[PropertyData("IdEncodedData_NoHtmlEncode", PropertyType = typeof(EncodedDataSets))]
public void Id_IdEncodes_PropertyName(string text, string expectedText)
{
// Arrange
var viewData = new ViewDataDictionary<string>(model: null);
var helper = MvcHelper.GetHtmlHelper<string>(viewData);
// Act
var result = helper.Id(text);
// Assert
Assert.Equal(expectedText, result.ToHtmlString());
}
[Theory]
[PropertyData("IdEncodedData_NoHtmlEncode", PropertyType = typeof(EncodedDataSets))]
public void IdHelpers_IdEncode_Prefix(string text, string expectedText)
{
// Arrange
var viewData = new ViewDataDictionary<string>(model: null);
viewData.TemplateInfo.HtmlFieldPrefix = text;
var helper = MvcHelper.GetHtmlHelper<string>(viewData);
// Act
var idResult = helper.Id("");
var idForResult = helper.IdFor(m => m);
var idForModelResult = helper.IdForModel();
// Assert
Assert.Equal(expectedText, idResult.ToHtmlString());
Assert.Equal(expectedText, idForResult.ToHtmlString());
Assert.Equal(expectedText, idForModelResult.ToHtmlString());
}
[Theory]
[PropertyData("AttributeEncodedData_NoHtmlEncode", PropertyType = typeof(EncodedDataSets))]
public void Name_AttributeEncodes_PropertyName(string text, string expectedText)
{
// Arrange
var viewData = new ViewDataDictionary<string>(model: null);
var helper = MvcHelper.GetHtmlHelper<string>(viewData);
// Act
var result = helper.Name(text);
// Assert
Assert.Equal(expectedText, result.ToHtmlString());
}
[Theory]
[PropertyData("AttributeEncodedData_NoHtmlEncode", PropertyType = typeof(EncodedDataSets))]
public void NameHelpers_AttributeEncode_Prefix(string text, string expectedText)
{
// Arrange
var viewData = new ViewDataDictionary<string>(model: null);
viewData.TemplateInfo.HtmlFieldPrefix = text;
var helper = MvcHelper.GetHtmlHelper<string>(viewData);
// Act
var nameResult = helper.Name("");
var nameForResult = helper.NameFor(m => m);
var nameForModelResult = helper.NameForModel();
// Assert
Assert.Equal(expectedText, nameResult.ToHtmlString());
Assert.Equal(expectedText, nameForResult.ToHtmlString());
Assert.Equal(expectedText, nameForModelResult.ToHtmlString());
}
private sealed class OuterClass
{
public InnerClass Inner { get; set; }
public int IntValue { get; set; }
}
private sealed class InnerClass
{
public string StringValue { get; set; }
}
}
}