@@ -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}
0 commit comments