Repository navigation
Expand file tree
/
Copy pathTextSanitizerTests.cs
More file actions
103 lines (90 loc) · 3.11 KB
/
Copy pathTextSanitizerTests.cs
File metadata and controls
103 lines (90 loc) · 3.11 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
using FolderDiffIL4DotNet.Core.Text;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Core.Text
{
[Trait("Category", "Unit")]
public class TextSanitizerTests
{
[Fact]
public void ToSafeFileName_NormalInput_ReturnsUnchanged()
{
Assert.Equal("hello_world", TextSanitizer.ToSafeFileName("hello_world"));
}
[Fact]
public void ToSafeFileName_Null_ReturnsEmpty()
{
Assert.Equal(string.Empty, TextSanitizer.ToSafeFileName(null));
}
[Fact]
public void ToSafeFileName_Empty_ReturnsEmpty()
{
Assert.Equal(string.Empty, TextSanitizer.ToSafeFileName(string.Empty));
}
[Fact]
public void ToSafeFileName_ColonReplaced_ReturnsUnderscore()
{
var result = TextSanitizer.ToSafeFileName("file:name");
Assert.DoesNotContain(":", result);
Assert.Contains("_", result);
}
[Fact]
public void ToSafeFileName_LongInput_IsTruncatedWithHash()
{
var longName = new string('a', 200);
var result = TextSanitizer.ToSafeFileName(longName);
Assert.True(result.Length < longName.Length);
Assert.Contains("_.._", result);
}
[Fact]
public void ToSafeFileName_ShortInput_NotTruncated()
{
var shortName = new string('a', 50);
var result = TextSanitizer.ToSafeFileName(shortName);
Assert.Equal(shortName, result);
}
[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("hello", "hello")]
public void Sanitize_BasicInputs(string input, string expected)
{
Assert.Equal(expected, TextSanitizer.Sanitize(input));
}
[Fact]
public void Sanitize_BackslashAndSlashAndColon_ReplacedWithDot()
{
var result = TextSanitizer.Sanitize("a\\b/c:d");
Assert.Equal("a.b.c.d", result);
}
[Fact]
public void Sanitize_ConsecutiveDots_Collapsed()
{
var result = TextSanitizer.Sanitize("a\\\\b");
Assert.DoesNotContain("..", result);
}
[Fact]
public void Sanitize_LeadingTrailingDots_Trimmed()
{
var result = TextSanitizer.Sanitize("\\value\\");
Assert.False(result.StartsWith("."));
Assert.False(result.EndsWith("."));
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData("hello world", false)]
[InlineData("ASCII-only_123!@#", false)]
public void ContainsNonAscii_AsciiOnly_ReturnsFalse(string input, bool expected)
{
Assert.Equal(expected, TextSanitizer.ContainsNonAscii(input));
}
[Theory]
[InlineData("日本語", true)]
[InlineData("hello世界", true)]
[InlineData("caf\u00e9", true)]
public void ContainsNonAscii_NonAscii_ReturnsTrue(string input, bool expected)
{
Assert.Equal(expected, TextSanitizer.ContainsNonAscii(input));
}
}
}