forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensionTests.cs
More file actions
118 lines (100 loc) · 3.33 KB
/
StringExtensionTests.cs
File metadata and controls
118 lines (100 loc) · 3.33 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;
using ServiceStack.Common.Extensions;
using ServiceStack.Text;
namespace ServiceStack.Common.Tests
{
[TestFixture]
public class StringExtensionTests
{
[Test]
public void To_works_with_ValueTypes()
{
Assert.That(1.ToString().To<int>(), Is.EqualTo(1));
}
[Test]
public void To_on_null_or_empty_string_returns_default_value_supplied()
{
const string nullString = null;
Assert.That("".To(1), Is.EqualTo(1));
Assert.That("".To(default(int)), Is.EqualTo(default(int)));
Assert.That(nullString.To(1), Is.EqualTo(1));
}
[Test]
public void To_ValueType_on_null_or_empty_string_returns_default_value()
{
Assert.That("".To<int>(), Is.EqualTo(default(int)));
}
[Test]
public void To_UrlEncode()
{
const string url = "http://www.servicestack.net/a?b=c&d=f";
var urlEncoded = url.UrlEncode();
Assert.That(urlEncoded, Is.EqualTo(HttpUtility.UrlEncode(url)));
}
[Test]
public void To_UrlDecode()
{
const string url = "http://www.servicestack.net/a?b=c&d=f";
var urlEncoded = url.UrlEncode();
var decodedUrl = urlEncoded.UrlDecode();
Assert.That(decodedUrl, Is.EqualTo(url));
}
[Test]
public void UrlFormat_encodes_components()
{
const string url = "http://www.servicestack.net/a?b={0}&d={1}";
const string arg1 = "as@if.com";
const string arg2 = "&=";
var urlFormat = url.UrlFormat(arg1, arg2);
var expectedUrlFormat = string.Format(url, arg1.UrlEncode(), arg2.UrlEncode());
Assert.That(urlFormat, Is.EqualTo(expectedUrlFormat));
}
[Test]
public void ErrorCode_to_English_format()
{
const string code = "EmailAddressIsInvalid";
Assert.That(code.ToEnglish(), Is.EqualTo("Email address is invalid"));
}
[Test]
public void Print_special_chars()
{
var specialChars = new List<char> { '"', ':', ',', '%' };
specialChars.ForEach(x => Console.WriteLine(x + " = " + ((int)x).ToString("x")));
}
[Test]
public void HexEscape_escapes_special_chars()
{
var specialChars = new List<char> { '"', ':', ',', '%' };
const string unescapedString = "\"1st 2:nd 3r,d 4th%";
const string expectedString = "%221st 2%3and 3r%2cd 4th%25";
Assert.That(unescapedString.HexEscape(specialChars.ToArray()), Is.EqualTo(expectedString));
}
[Test]
public void HexUnescape_unescapes_special_chars()
{
var specialChars = new List<char> { '"', ':', ',', '%' };
const string escapedString = "%221st 2%3and 3r%2cd 4th%25";
const string expectedString = "\"1st 2:nd 3r,d 4th%";
Assert.That(escapedString.HexUnescape(specialChars.ToArray()), Is.EqualTo(expectedString));
}
[Test]
public void SafeVarName_strips_illegal_chars()
{
Assert.That("with space".SafeVarName(), Is.EqualTo("with_space"));
Assert.That("with @+:\\illegals".SafeVarName(), Is.EqualTo("with_____illegals"));
Assert.That("UPPER_lower_0123456789".SafeVarName(), Is.EqualTo("UPPER_lower_0123456789"));
}
[Test]
public void Glob_finds_right_strings()
{
var input = new[] { "Foo", "Boo", "Hoo", "Baz" }.ToList();
var expected = input.Where(s => s.EndsWith("oo")).ToList();
var actual = input.Where(s => s.Glob("*oo")).ToList();
Assert.That(actual, Is.EquivalentTo(expected));
}
}
}