forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensionsTests.cs
More file actions
112 lines (95 loc) · 3.86 KB
/
Copy pathStringExtensionsTests.cs
File metadata and controls
112 lines (95 loc) · 3.86 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
using System;
using System.IO;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class StringExtensionsTests
{
[Test]
public void Can_SplitOnFirst_char_needle()
{
var parts = "user:pass@w:rd".SplitOnFirst(':');
Assert.That(parts[0], Is.EqualTo("user"));
Assert.That(parts[1], Is.EqualTo("pass@w:rd"));
}
[Test]
public void Can_SplitOnFirst_string_needle()
{
var parts = "user:pass@w:rd".SplitOnFirst(":");
Assert.That(parts[0], Is.EqualTo("user"));
Assert.That(parts[1], Is.EqualTo("pass@w:rd"));
}
[Test]
public void Can_SplitOnLast_char_needle()
{
var parts = "user:name:pass@word".SplitOnLast(':');
Assert.That(parts[0], Is.EqualTo("user:name"));
Assert.That(parts[1], Is.EqualTo("pass@word"));
}
[Test]
public void Can_SplitOnLast_string_needle()
{
var parts = "user:name:pass@word".SplitOnLast(":");
Assert.That(parts[0], Is.EqualTo("user:name"));
Assert.That(parts[1], Is.EqualTo("pass@word"));
}
private static readonly char DirSep = Path.DirectorySeparatorChar;
private static readonly char AltDirSep = Path.DirectorySeparatorChar == '/' ? '\\' : '/';
[Test]
public void Does_get_ParentDirectory()
{
var dirSep = DirSep;
var filePath = "path{0}to{0}file".FormatWith(dirSep);
Assert.That(filePath.ParentDirectory(), Is.EqualTo("path{0}to".FormatWith(dirSep)));
Assert.That(filePath.ParentDirectory().ParentDirectory(), Is.EqualTo("path".FormatWith(dirSep)));
Assert.That(filePath.ParentDirectory().ParentDirectory().ParentDirectory(), Is.Null);
var filePathWithExt = "path{0}to{0}file/".FormatWith(dirSep);
Assert.That(filePathWithExt.ParentDirectory(), Is.EqualTo("path{0}to".FormatWith(dirSep)));
Assert.That(filePathWithExt.ParentDirectory().ParentDirectory(), Is.EqualTo("path".FormatWith(dirSep)));
Assert.That(filePathWithExt.ParentDirectory().ParentDirectory().ParentDirectory(), Is.Null);
}
[Test]
public void Does_get_ParentDirectory_of_AltDirectorySeperator()
{
var dirSep = AltDirSep;
var filePath = "path{0}to{0}file".FormatWith(dirSep);
Assert.That(filePath.ParentDirectory(), Is.EqualTo("path{0}to".FormatWith(dirSep)));
Assert.That(filePath.ParentDirectory().ParentDirectory(), Is.EqualTo("path".FormatWith(dirSep)));
Assert.That(filePath.ParentDirectory().ParentDirectory().ParentDirectory(), Is.Null);
var filePathWithExt = "path{0}to{0}file{0}".FormatWith(dirSep);
Assert.That(filePathWithExt.ParentDirectory(), Is.EqualTo("path{0}to".FormatWith(dirSep)));
Assert.That(filePathWithExt.ParentDirectory().ParentDirectory(), Is.EqualTo("path".FormatWith(dirSep)));
Assert.That(filePathWithExt.ParentDirectory().ParentDirectory().ParentDirectory(), Is.Null);
}
[Test]
public void Does_not_alter_filepath_without_extension()
{
var path = "path/dir.with.dot/to/file";
Assert.That(path.WithoutExtension(), Is.EqualTo(path));
Assert.That("path/to/file.ext".WithoutExtension(), Is.EqualTo("path/to/file"));
}
[Test]
public void Does_find_IndexOfAny_strings()
{
var text = "text with /* and <!--";
var pos = text.IndexOfAny("<!--", "/*");
//Console.WriteLine(text.Substring(pos));
Assert.That(pos, Is.EqualTo("text with ".Length));
}
[Test]
public void Does_ExtractContent_first_pattern_from_Document_without_marker()
{
var text = "text with random <!--comment--> and Contents: <!--Contents--> are here";
var extract = text.ExtractContents("<!--", "-->");
Assert.That(extract, Is.EqualTo("comment"));
}
[Test]
public void Does_ExtractContents_from_Document()
{
var text = "text with random <!--comment--> and Contents: <!--Contents--> are here";
var extract = text.ExtractContents("Contents:", "<!--", "-->");
Assert.That(extract, Is.EqualTo("Contents"));
}
}
}