-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Tests.cs
238 lines (193 loc) · 5.9 KB
/
Tests.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
public class Tests
{
[Test]
public void CreateFile_overwrite_binary()
{
AllFiles.CreateFile("foo.bmp");
AllFiles.CreateFile("foo.bmp");
True(File.Exists("foo.bmp"));
}
[Test]
public void CreateFile_NoDir_binary()
{
if (Directory.Exists("myTempDir"))
{
Directory.Delete("myTempDir", true);
}
AllFiles.CreateFile("myTempDir/foo.bmp");
True(File.Exists("myTempDir/foo.bmp"));
}
[Test]
public void CreateFile_preamble()
{
AllFiles.CreateFile("foo.txt", true);
var preamble = Encoding.UTF8.GetPreamble();
var bytes = File.ReadAllBytes("foo.txt");
if (bytes.Length < preamble.Length ||
preamble
.Where((p, i) => p != bytes[i])
.Any())
{
throw new ArgumentException("Not utf8-BOM");
}
}
[Test]
public void CreateFile_overwrite_txt()
{
AllFiles.CreateFile("foo.txt", true);
AllFiles.CreateFile("foo.txt", true);
True(File.Exists("foo.txt"));
}
[Test]
public void CreateFile_NoDir_txt()
{
if (Directory.Exists("myTempDir"))
{
Directory.Delete("myTempDir", true);
}
AllFiles.CreateFile("myTempDir/foo.txt", true);
True(File.Exists("myTempDir/foo.txt"));
}
[Test]
public void TryCreateFile_overwrite_txt()
{
True(AllFiles.TryCreateFile("foo.txt", true));
True(AllFiles.TryCreateFile("foo.txt", true));
True(File.Exists("foo.txt"));
}
[Test]
public void TryCreateFile_NoDir_txt()
{
if (Directory.Exists("myTempDir"))
{
Directory.Delete("myTempDir", true);
}
True(AllFiles.TryCreateFile("myTempDir/foo.txt", true));
True(File.Exists("myTempDir/foo.txt"));
}
[Test]
public void TryCreateFile_overwrite_binary()
{
True(AllFiles.TryCreateFile("foo.bmp"));
True(AllFiles.TryCreateFile("foo.bmp"));
True(File.Exists("foo.bmp"));
}
[Test]
public void TryCreateFile_NoDir_binary()
{
if (Directory.Exists("myTempDir"))
{
Directory.Delete("myTempDir", true);
}
True(AllFiles.TryCreateFile("myTempDir/foo.bmp"));
True(File.Exists("myTempDir/foo.bmp"));
}
[Test]
public void Unknown_extension()
{
Throws<Exception>(() => AllFiles.GetPathFor("txt"));
False(AllFiles.TryGetPathFor("txt", out var result));
Null(result);
False(AllFiles.TryGetPathFor(".txt", out result));
Null(result);
False(AllFiles.TryCreateFile("foo.txt"));
Null(result);
Throws<Exception>(() => AllFiles.GetPathFor(".txt"));
Throws<Exception>(() => AllFiles.CreateFile("foo.txt"));
}
[Test]
public void GetPathFor()
{
#region GetPathFor
var path = AllFiles.GetPathFor(".jpg");
#endregion
NotNull(path);
True(File.Exists(path));
path = AllFiles.GetPathFor("jpg");
NotNull(path);
True(File.Exists(path));
}
[Test]
public void CreateFile()
{
var pathOfFileToCreate = "file.jpg";
File.Delete(pathOfFileToCreate);
#region CreateFile
AllFiles.CreateFile(pathOfFileToCreate);
#endregion
True(File.Exists(pathOfFileToCreate));
File.Delete(pathOfFileToCreate);
AllFiles.CreateFile("foo.txt", true);
True(File.Exists("foo.txt"));
File.Delete("foo.txt");
True(AllFiles.TryCreateFile(pathOfFileToCreate));
True(File.Exists(pathOfFileToCreate));
File.Delete(pathOfFileToCreate);
False(AllFiles.TryCreateFile("foo.txt"));
False(File.Exists("foo.txt"));
File.Delete("foo.txt");
True(AllFiles.TryCreateFile("foo.txt", true));
True(File.Exists("foo.txt"));
File.Delete("foo.txt");
}
[Test]
public void IsEmptyFile()
{
#region IsEmptyFile
var path = AllFiles.GetPathFor(".jpg");
True(AllFiles.IsEmptyFile(path));
var temp = Path.GetTempFileName();
False(AllFiles.IsEmptyFile(temp));
#endregion
File.Delete(temp);
}
[Test]
public void AllPaths()
{
IsNotEmpty(AllFiles.AllPaths);
#region AllPaths
foreach (var path in AllFiles.AllPaths)
{
Trace.WriteLine(path);
}
#endregion
}
static string ThisFile([CallerFilePath] string testFile = "") =>
testFile;
//[Test]
#pragma warning disable CA1822
public void UseFile()
#pragma warning restore CA1822
{
var pathToFile = ThisFile();
#region UseFile
AllFiles.UseFile(Category.Document, pathToFile);
IsTrue(AllFiles.DocumentPaths.Contains(pathToFile));
#endregion
}
#if NET9_0
[Test]
public async Task WriteExtensions()
{
var md = Path.Combine(SolutionDirectoryFinder.Find(), "extensions.include.md");
File.Delete(md);
await using var writer = File.CreateText(md);
await WriteCategory(writer, "Archive", AllFiles.Archives);
await WriteCategory(writer, "Document", AllFiles.Documents);
await WriteCategory(writer, "Image", AllFiles.Images);
await WriteCategory(writer, "Sheet", AllFiles.Sheets);
await WriteCategory(writer, "Slide", AllFiles.Slides);
}
#endif
static async Task WriteCategory(StreamWriter writer, string category, IReadOnlyDictionary<string, EmptyFile> files)
{
await writer.WriteLineAsync("");
await writer.WriteLineAsync($"### {category}");
await writer.WriteLineAsync("");
foreach (var file in files.OrderBy(_ => _.Key))
{
var size = Size.Suffix(new FileInfo(file.Value.Path).Length);
await writer.WriteLineAsync($" * {file.Key} ({size})");
}
}
}