forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringHelperFixture.cs
169 lines (143 loc) · 5.11 KB
/
StringHelperFixture.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
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
using System;
using System.Collections.Generic;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.UtilityTest
{
/// <summary>
/// Summary description for StringHelperFixture.
/// </summary>
[TestFixture]
public class StringHelperFixture
{
[Test]
public void GetClassnameFromFQType()
{
const string typeName = "ns1.ns2.classname, as1.as2., pk, lang";
const string expected = "classname";
Assert.AreEqual(expected, StringHelper.GetClassname(typeName));
}
[Test]
public void GetClassnameFromFQClass()
{
const string typeName = "ns1.ns2.classname";
const string expected = "classname";
Assert.AreEqual(expected, StringHelper.GetClassname(typeName));
}
[Test]
public void GetFullClassNameForGenericType()
{
string typeName = typeof (IDictionary<int, string>).AssemblyQualifiedName;
string expected = typeof (IDictionary<int, string>).FullName;
Assert.AreEqual(expected, StringHelper.GetFullClassname(typeName));
typeName = "some.namespace.SomeType`1[[System.Int32, mscorlib], System.Int32], some.assembly";
expected = "some.namespace.SomeType`1[[System.Int32, mscorlib],[System.Int32]]";
Assert.AreEqual(expected, StringHelper.GetFullClassname(typeName));
}
[Test]
public void CountUnquotedParams()
{
// Base case, no values
Assert.AreEqual(0, StringHelper.CountUnquoted("abcd eftf", '?'));
// Simple case
Assert.AreEqual(1, StringHelper.CountUnquoted("abcd ? eftf", '?'));
// Multiple values
Assert.AreEqual(2, StringHelper.CountUnquoted("abcd ? ef ? tf", '?'));
// Quoted values
Assert.AreEqual(1, StringHelper.CountUnquoted("abcd ? ef '?' tf", '?'));
}
/// <summary>
/// Try to locate single quotes which isn't allowed
/// </summary>
[Test]
public void CantCountQuotes()
{
Assert.Throws<ArgumentOutOfRangeException>(() => StringHelper.CountUnquoted("abcd eftf", StringHelper.SingleQuote));
}
/// <summary>
/// Qualify a name with a prefix
/// </summary>
[Test]
public void Qualify()
{
Assert.AreEqual("a.b", StringHelper.Qualify("a", "b"), "Qualified names differ");
}
/// <summary>
/// Qualify an array of names with a prefix
/// </summary>
[Test]
public void QualifyArray()
{
string[] simple = { "b", "c" };
string[] qual = { "a.b", "a.c" };
string[] result = StringHelper.Qualify("a", simple);
for (int i = 0; i < result.Length; i++)
{
Assert.AreEqual(qual[i], result[i], "Qualified names differ");
}
}
[Test]
public void GenerateAliasForGenericTypeName()
{
const string typeName = "A`1[B]";
string alias = StringHelper.GenerateAlias(typeName, 10);
Assert.IsFalse(alias.Contains("`"), "alias '{0}' should not contain backticks", alias);
Assert.IsFalse(alias.Contains("["), "alias '{0}' should not contain left bracket", alias);
Assert.IsFalse(alias.Contains("]"), "alias '{0}' should not contain right bracket", alias);
}
[Test]
public void GetClassnameFromGenericType()
{
const string typeName = "classname`1[innerns1.innerClass]";
const string expected = "classname`1[[innerns1.innerClass]]";
Assert.AreEqual(expected, StringHelper.GetClassname(typeName));
}
[Test]
public void GetClassnameFromGenericFQClass()
{
const string typeName = "ns1.ns2.classname`1[innerns1.innerClass]";
const string expected = "classname`1[[innerns1.innerClass]]";
Assert.AreEqual(expected, StringHelper.GetClassname(typeName));
}
[Test]
public void IsBackticksEnclosed()
{
Assert.That(!StringHelper.IsBackticksEnclosed(null));
Assert.That(!StringHelper.IsBackticksEnclosed("`something"));
Assert.That(!StringHelper.IsBackticksEnclosed("something`"));
Assert.That(StringHelper.IsBackticksEnclosed("`something`"));
}
[Test]
public void PurgeBackticksEnclosing()
{
Assert.That(StringHelper.PurgeBackticksEnclosing(null), Is.Null);
Assert.That(StringHelper.PurgeBackticksEnclosing("`something"), Is.EqualTo("`something"));
Assert.That(StringHelper.PurgeBackticksEnclosing("something`"), Is.EqualTo("something`"));
Assert.That(StringHelper.PurgeBackticksEnclosing("`something`"), Is.EqualTo("something"));
}
[TestCase("ab", 0, -1, 0)]
[TestCase("a\r\nb", 0, 1, 2)]
[TestCase("a\nb", 0, 1, 1)]
[TestCase("ab\r\nfoo\r\n", 4, 7, 2)]
public void IndexOfAnyNewLineReturnsIndexAndLength(string str, int startIndex, int expectedIndex,
int expectedMatchLength)
{
int matchLength;
var matchIndex = str.IndexOfAnyNewLine(startIndex, out matchLength);
Assert.That(matchIndex, Is.EqualTo(expectedIndex));
Assert.That(matchLength, Is.EqualTo(expectedMatchLength));
}
[TestCase("ab", 0, false, 0)]
[TestCase("a\r\nb", 0, false, 0)]
[TestCase("a\nb", 1, true, 1)]
[TestCase("a\r\nb", 1, true, 2)]
public void IsAnyNewLineMatchAndLength(string str, int startIndex, bool expectNewLine,
int expectedMatchLength)
{
int matchLength;
var isNewLine = str.IsAnyNewLine(startIndex, out matchLength);
Assert.That(isNewLine, Is.EqualTo(expectNewLine));
Assert.That(matchLength, Is.EqualTo(expectedMatchLength));
}
}
}