Skip to content

Commit e95db4b

Browse files
committed
.
1 parent c21eb74 commit e95db4b

File tree

4 files changed

+265
-7
lines changed

4 files changed

+265
-7
lines changed

docs/temp-file.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void StringConversion()
9191
Trace.WriteLine(content);
9292
}
9393
```
94-
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L333-L348' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileStringConversion' title='Start of snippet'>anchor</a></sup>
94+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L344-L359' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileStringConversion' title='Start of snippet'>anchor</a></sup>
9595
<!-- endSnippet -->
9696

9797

@@ -114,7 +114,7 @@ public void FileInfoConversion()
114114
Trace.WriteLine(directoryName);
115115
}
116116
```
117-
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L350-L364' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileFileInfoConversion' title='Start of snippet'>anchor</a></sup>
117+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L361-L375' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileFileInfoConversion' title='Start of snippet'>anchor</a></sup>
118118
<!-- endSnippet -->
119119

120120

@@ -135,7 +135,7 @@ public void InfoProperty()
135135
Trace.WriteLine(directoryName);
136136
}
137137
```
138-
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L366-L378' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileInfoProperty' title='Start of snippet'>anchor</a></sup>
138+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L377-L389' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileInfoProperty' title='Start of snippet'>anchor</a></sup>
139139
<!-- endSnippet -->
140140

141141

src/Benchmarks/OverwriteBenchmarks.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ public class OverwriteBenchmarks
1010
[GlobalSetup]
1111
public void Setup()
1212
{
13-
smallBuilder = new StringBuilder("Hello World! This is a small test string.");
13+
smallBuilder = new("Hello World! This is a small test string.");
1414

15-
mediumBuilder = new StringBuilder();
15+
mediumBuilder = new();
1616
for (var i = 0; i < 100; i++)
1717
{
1818
mediumBuilder.Append("This is sentence number ");
1919
mediumBuilder.Append(i);
2020
mediumBuilder.Append(". ");
2121
}
2222

23-
largeBuilder = new StringBuilder();
23+
largeBuilder = new();
2424
for (var i = 0; i < 10000; i++)
2525
{
2626
largeBuilder.Append("Content ");
2727
}
2828

2929
// Force multiple chunks
30-
multiChunkBuilder = new StringBuilder();
30+
multiChunkBuilder = new();
3131
for (var i = 0; i < 50; i++)
3232
{
3333
multiChunkBuilder.Append("Chunk ");

src/Verify.Tests/TempFileTests.cs

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ public void Create_WithEncoding_CreatesFileWithBom()
160160
Assert.True(bytes.Length >= 3);
161161
}
162162

163+
[Fact]
164+
public void CreateText_WithEncoding_CreatesFileWithBom()
165+
{
166+
using var temp = TempFile.Create(".txt", Encoding.UTF8);
167+
168+
Assert.True(File.Exists(temp.Path));
169+
var bytes = File.ReadAllBytes(temp.Path);
170+
// UTF8 BOM should be present
171+
Assert.True(bytes.Length >= 3);
172+
}
173+
163174
[Fact]
164175
public void Dispose_DeletesFile()
165176
{
@@ -380,4 +391,226 @@ public void InfoProperty()
380391
[Fact]
381392
public void Constructor_WithEmptyExtension_ThrowsArgumentException() =>
382393
Assert.Throws<ArgumentException>(() => new TempFile(string.Empty));
394+
395+
[Fact]
396+
public async Task CreateText_WithContent_CreatesFileWithContent()
397+
{
398+
var content = "Hello, World!";
399+
400+
using var temp = await TempFile.CreateText(content);
401+
402+
Assert.True(File.Exists(temp.Path));
403+
var readContent = await File.ReadAllTextAsync(temp.Path);
404+
Assert.Equal(content, readContent);
405+
}
406+
407+
[Fact]
408+
public async Task CreateText_WithExtension_CreatesFileWithExtensionAndContent()
409+
{
410+
var content = "Test content";
411+
412+
using var temp = await TempFile.CreateText(content, ".txt");
413+
414+
Assert.EndsWith(".txt", temp.Path);
415+
Assert.True(File.Exists(temp.Path));
416+
var readContent = await File.ReadAllTextAsync(temp.Path);
417+
Assert.Equal(content, readContent);
418+
}
419+
420+
[Fact]
421+
public async Task CreateText_WithEncoding_CreatesFileWithSpecifiedEncoding()
422+
{
423+
var content = "Test with special characters: äöü";
424+
425+
using var temp = await TempFile.CreateText(content, ".txt", Encoding.UTF8);
426+
427+
Assert.True(File.Exists(temp.Path));
428+
var readContent = await File.ReadAllTextAsync(temp.Path, Encoding.UTF8);
429+
Assert.Equal(content, readContent);
430+
}
431+
432+
[Fact]
433+
public async Task CreateText_WithAsciiEncoding_CreatesFileWithAsciiEncoding()
434+
{
435+
var content = "ASCII content only";
436+
437+
using var temp = await TempFile.CreateText(content, ".txt", Encoding.ASCII);
438+
439+
Assert.True(File.Exists(temp.Path));
440+
var readContent = await File.ReadAllTextAsync(temp.Path, Encoding.ASCII);
441+
Assert.Equal(content, readContent);
442+
}
443+
444+
[Fact]
445+
public async Task CreateText_WithEmptyContent_CreatesEmptyFile()
446+
{
447+
using var temp = await TempFile.CreateText(string.Empty, ".txt");
448+
449+
Assert.True(File.Exists(temp.Path));
450+
var readContent = await File.ReadAllTextAsync(temp.Path);
451+
Assert.Empty(readContent);
452+
}
453+
454+
[Fact]
455+
public async Task CreateText_WithMultilineContent_PreservesLineBreaks()
456+
{
457+
var content = """
458+
Line 1
459+
Line 2
460+
Line 3
461+
""";
462+
463+
using var temp = await TempFile.CreateText(content, ".txt");
464+
465+
var readContent = await File.ReadAllTextAsync(temp.Path);
466+
Assert.Equal(content, readContent);
467+
}
468+
469+
[Fact]
470+
public async Task CreateText_DisposesCorrectly()
471+
{
472+
string path;
473+
{
474+
using var temp = await TempFile.CreateText("content", ".txt");
475+
path = temp.Path;
476+
Assert.True(File.Exists(path));
477+
}
478+
479+
Assert.False(File.Exists(path));
480+
}
481+
482+
[Fact]
483+
public async Task CreateBinary_WithContent_CreatesFileWithBinaryContent()
484+
{
485+
var data = "Hello"u8.ToArray(); // "Hello" in ASCII
486+
487+
using var temp = await TempFile.CreateBinary(data);
488+
489+
Assert.True(File.Exists(temp.Path));
490+
var readData = await File.ReadAllBytesAsync(temp.Path);
491+
Assert.Equal(data, readData);
492+
}
493+
494+
[Fact]
495+
public async Task CreateBinary_WithExtension_CreatesFileWithExtensionAndContent()
496+
{
497+
byte[] data = [0x01, 0x02, 0x03, 0x04];
498+
499+
using var temp = await TempFile.CreateBinary(data, ".bin");
500+
501+
Assert.EndsWith(".bin", temp.Path);
502+
Assert.True(File.Exists(temp.Path));
503+
var readData = await File.ReadAllBytesAsync(temp.Path);
504+
Assert.Equal(data, readData);
505+
}
506+
507+
[Fact]
508+
public async Task CreateBinary_WithEmptyContent_CreatesEmptyFile()
509+
{
510+
using var temp = await TempFile.CreateBinary(ReadOnlyMemory<byte>.Empty, ".bin");
511+
512+
Assert.True(File.Exists(temp.Path));
513+
var readData = await File.ReadAllBytesAsync(temp.Path);
514+
Assert.Empty(readData);
515+
}
516+
517+
[Fact]
518+
public async Task CreateBinary_WithLargeBinaryData_WritesCorrectly()
519+
{
520+
var data = new byte[1024];
521+
Random.Shared.NextBytes(data);
522+
523+
using var temp = await TempFile.CreateBinary(data, ".dat");
524+
525+
Assert.True(File.Exists(temp.Path));
526+
var readData = await File.ReadAllBytesAsync(temp.Path);
527+
Assert.Equal(data, readData);
528+
Assert.Equal(1024, readData.Length);
529+
}
530+
531+
[Fact]
532+
public async Task CreateBinary_DisposesCorrectly()
533+
{
534+
string path;
535+
byte[] data = [0xFF, 0xFE, 0xFD];
536+
537+
{
538+
using var temp = await TempFile.CreateBinary(data, ".bin");
539+
path = temp.Path;
540+
Assert.True(File.Exists(path));
541+
}
542+
543+
Assert.False(File.Exists(path));
544+
}
545+
546+
[Fact]
547+
public async Task CreateBinary_WithImageData_CreatesValidFile()
548+
{
549+
// Simulate a simple 1x1 PNG (minimal valid PNG)
550+
byte[] pngData =
551+
[
552+
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature
553+
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52 // IHDR chunk start
554+
];
555+
556+
using var temp = await TempFile.CreateBinary(pngData, ".png");
557+
558+
Assert.EndsWith(".png", temp.Path);
559+
var readData = await File.ReadAllBytesAsync(temp.Path);
560+
Assert.Equal(pngData, readData);
561+
}
562+
563+
[Fact]
564+
public async Task CreateText_CanBeVerified()
565+
{
566+
using var temp = await TempFile.CreateText("test content", ".txt");
567+
568+
var content = await File.ReadAllTextAsync(temp.Path);
569+
await Verify(content);
570+
}
571+
572+
[Fact]
573+
public async Task CreateBinary_CanBeVerified()
574+
{
575+
byte[] data = [0x01, 0x02, 0x03, 0x04, 0x05];
576+
577+
using var temp = await TempFile.CreateBinary(data, ".bin");
578+
579+
var readData = await File.ReadAllBytesAsync(temp.Path);
580+
await Verify(readData);
581+
}
582+
583+
[Fact]
584+
public async Task CreateText_WithJsonContent_CreatesValidJsonFile()
585+
{
586+
var jsonContent = """
587+
{
588+
"name": "test",
589+
"value": 123
590+
}
591+
""";
592+
593+
using var temp = await TempFile.CreateText(jsonContent, ".json");
594+
595+
Assert.EndsWith(".json", temp.Path);
596+
var readContent = await File.ReadAllTextAsync(temp.Path);
597+
Assert.Equal(jsonContent, readContent);
598+
}
599+
600+
[Fact]
601+
public async Task CreateText_WithXmlContent_CreatesValidXmlFile()
602+
{
603+
var xmlContent = """
604+
<?xml version="1.0" encoding="utf-8"?>
605+
<root>
606+
<element>value</element>
607+
</root>
608+
""";
609+
610+
using var temp = await TempFile.CreateText(xmlContent, ".xml");
611+
612+
Assert.EndsWith(".xml", temp.Path);
613+
var readContent = await File.ReadAllTextAsync(temp.Path);
614+
Assert.Equal(xmlContent, readContent);
615+
}
383616
}

src/Verify/TempFile.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,31 @@ public static TempFile Create(string? extension = null, Encoding? encoding = nul
159159
return file;
160160
}
161161

162+
public static async Task<TempFile> CreateText(string content, string? extension = null, Encoding? encoding = null)
163+
{
164+
var file = new TempFile(extension);
165+
166+
if (encoding == null)
167+
{
168+
await File.WriteAllTextAsync(file, content);
169+
}
170+
else
171+
{
172+
await File.WriteAllTextAsync(file, content, encoding);
173+
}
174+
175+
return file;
176+
}
177+
178+
public static async Task<TempFile> CreateBinary(ReadOnlyMemory<byte> content, string? extension = null)
179+
{
180+
var file = new TempFile(extension);
181+
182+
await File.WriteAllBytesAsync(file, content);
183+
184+
return file;
185+
}
186+
162187
public void Dispose()
163188
{
164189
if (File.Exists(Path))

0 commit comments

Comments
 (0)