Repository navigation
Expand file tree
/
Copy pathEncodingDetectorTests.cs
More file actions
161 lines (134 loc) · 6.44 KB
/
Copy pathEncodingDetectorTests.cs
File metadata and controls
161 lines (134 loc) · 6.44 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
using System;
using System.IO;
using System.Text;
using FolderDiffIL4DotNet.Core.Text;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Core.Text
{
[Trait("Category", "Unit")]
public class EncodingDetectorTests : IDisposable
{
private readonly string _tempDir;
public EncodingDetectorTests()
{
EncodingDetector.RegisterCodePages();
_tempDir = Path.Combine(Path.GetTempPath(), "EncodingDetectorTests_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
}
public void Dispose()
{
if (Directory.Exists(_tempDir))
{
Directory.Delete(_tempDir, recursive: true);
}
}
private string CreateTempFile(string name, byte[] content)
{
var path = Path.Combine(_tempDir, name);
File.WriteAllBytes(path, content);
return path;
}
[Fact]
public void DetectFileEncoding_Utf8WithBom_ReturnsUtf8()
{
var bom = new byte[] { 0xEF, 0xBB, 0xBF };
var content = Encoding.UTF8.GetBytes("テスト日本語");
var bytes = new byte[bom.Length + content.Length];
Buffer.BlockCopy(bom, 0, bytes, 0, bom.Length);
Buffer.BlockCopy(content, 0, bytes, bom.Length, content.Length);
var path = CreateTempFile("utf8_bom.txt", bytes);
var encoding = EncodingDetector.DetectFileEncoding(path);
// Should detect UTF-8 from BOM / BOM から UTF-8 を検出すべき
Assert.Equal(Encoding.UTF8.CodePage, encoding.CodePage);
}
[Fact]
public void DetectFileEncoding_Utf8WithoutBom_ReturnsUtf8()
{
var content = Encoding.UTF8.GetBytes("テスト日本語 test");
var path = CreateTempFile("utf8_no_bom.txt", content);
var encoding = EncodingDetector.DetectFileEncoding(path);
Assert.Equal(Encoding.UTF8.CodePage, encoding.CodePage);
}
[Fact]
public void DetectFileEncoding_AsciiOnly_ReturnsUtf8()
{
var content = Encoding.ASCII.GetBytes("Hello World 123");
var path = CreateTempFile("ascii.txt", content);
var encoding = EncodingDetector.DetectFileEncoding(path);
// Pure ASCII is valid UTF-8 / 純粋な ASCII は有効な UTF-8
Assert.Equal(Encoding.UTF8.CodePage, encoding.CodePage);
}
[Fact]
public void DetectFileEncoding_Utf16LeBom_ReturnsUtf16Le()
{
var bom = new byte[] { 0xFF, 0xFE };
var content = Encoding.Unicode.GetBytes("テスト");
var bytes = new byte[bom.Length + content.Length];
Buffer.BlockCopy(bom, 0, bytes, 0, bom.Length);
Buffer.BlockCopy(content, 0, bytes, bom.Length, content.Length);
var path = CreateTempFile("utf16le.txt", bytes);
var encoding = EncodingDetector.DetectFileEncoding(path);
Assert.Equal(Encoding.Unicode.CodePage, encoding.CodePage);
}
[Fact]
public void DetectFileEncoding_Utf16BeBom_ReturnsUtf16Be()
{
var bom = new byte[] { 0xFE, 0xFF };
var content = Encoding.BigEndianUnicode.GetBytes("テスト");
var bytes = new byte[bom.Length + content.Length];
Buffer.BlockCopy(bom, 0, bytes, 0, bom.Length);
Buffer.BlockCopy(content, 0, bytes, bom.Length, content.Length);
var path = CreateTempFile("utf16be.txt", bytes);
var encoding = EncodingDetector.DetectFileEncoding(path);
Assert.Equal(Encoding.BigEndianUnicode.CodePage, encoding.CodePage);
}
[Fact]
public void DetectFileEncoding_ShiftJis_ReturnsNonUtf8()
{
// Shift_JIS encoded Japanese text / Shift_JIS エンコードの日本語テキスト
var shiftJis = Encoding.GetEncoding(932);
var content = shiftJis.GetBytes("日本語のテキストファイル");
var path = CreateTempFile("shiftjis.txt", content);
var encoding = EncodingDetector.DetectFileEncoding(path);
// Should NOT return UTF-8 since Shift_JIS bytes are invalid UTF-8
// Shift_JIS バイト列は無効な UTF-8 なので UTF-8 を返すべきではない
Assert.NotEqual(Encoding.UTF8.CodePage, encoding.CodePage);
}
[SkippableFact]
public void DetectFileEncoding_ShiftJis_CanDecodeJapaneseCorrectly()
{
// This test requires a Japanese locale (ANSI code page 932) for the
// auto-detection fallback to select Shift_JIS. Skip on other locales.
// このテストは自動検出フォールバックが Shift_JIS を選択するために
// 日本語ロケール(ANSI コードページ 932)を必要とします。
Skip.If(
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage != 932,
"Requires Japanese locale (ANSI code page 932)");
var shiftJis = Encoding.GetEncoding(932);
const string originalText = "日本語のテキストファイル\r\nテスト行";
var content = shiftJis.GetBytes(originalText);
var path = CreateTempFile("shiftjis_roundtrip.txt", content);
var encoding = EncodingDetector.DetectFileEncoding(path);
var readBack = File.ReadAllText(path, encoding);
Assert.Equal(originalText, readBack);
}
[Fact]
public void DetectFileEncoding_EmptyFile_ReturnsUtf8()
{
var path = CreateTempFile("empty.txt", Array.Empty<byte>());
var encoding = EncodingDetector.DetectFileEncoding(path);
// Empty file should be treated as UTF-8 / 空ファイルは UTF-8 として扱うべき
Assert.Equal(Encoding.UTF8.CodePage, encoding.CodePage);
}
[Fact]
public void DetectFileEncoding_SmallFile_StillDetects()
{
// Verify detection works with very small files / 非常に小さいファイルでも検出できることを検証
var shiftJis = Encoding.GetEncoding(932);
var content = shiftJis.GetBytes("テスト");
var path = CreateTempFile("small_file.txt", content);
var encoding = EncodingDetector.DetectFileEncoding(path);
Assert.NotEqual(Encoding.UTF8.CodePage, encoding.CodePage);
}
}
}