|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright(c) 2022 ICARUS Consulting GmbH |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +using System; |
| 24 | +using System.IO; |
| 25 | +using System.Text; |
| 26 | +using Xunit; |
| 27 | +using Yaapii.Atoms.Bytes; |
| 28 | +using Yaapii.Atoms.Enumerable; |
| 29 | +using Yaapii.Atoms.Text; |
| 30 | + |
| 31 | +namespace Yaapii.Atoms.IO.Tests |
| 32 | +{ |
| 33 | + public sealed class WriterAsOutputStreamTest |
| 34 | + { |
| 35 | + [Fact] |
| 36 | + public void WritesContentToFile() |
| 37 | + { |
| 38 | + var inputFile = new TempFile("large-text.txt"); |
| 39 | + var outputFile = new TempFile("copy-text.txt"); |
| 40 | + using (inputFile) |
| 41 | + { |
| 42 | + using (outputFile) |
| 43 | + { |
| 44 | + var inputPath = Path.GetFullPath(inputFile.Value()); |
| 45 | + var outputPath = Path.GetFullPath(outputFile.Value()); |
| 46 | + |
| 47 | + //Create large file |
| 48 | + new LengthOf( |
| 49 | + new InputOf( |
| 50 | + new TeeInputStream( |
| 51 | + new MemoryStream( |
| 52 | + new BytesOf( |
| 53 | + new Text.Joined(",", |
| 54 | + new HeadOf<string>( |
| 55 | + new Endless<string>("Hello World"), |
| 56 | + 1000 |
| 57 | + ) |
| 58 | + ) |
| 59 | + ).AsBytes() |
| 60 | + ), |
| 61 | + new OutputTo( |
| 62 | + new Uri(inputPath) |
| 63 | + ).Stream() |
| 64 | + ) |
| 65 | + ) |
| 66 | + ).Value(); |
| 67 | + |
| 68 | + Assert.True( |
| 69 | + new LengthOf( |
| 70 | + new InputOf( |
| 71 | + new TeeInputStream( |
| 72 | + new InputOf( |
| 73 | + inputPath |
| 74 | + ).Stream(), |
| 75 | + new WriterAsOutputStream( |
| 76 | + new StreamWriter(outputPath) |
| 77 | + ) |
| 78 | + ) |
| 79 | + ) |
| 80 | + ).Value() == |
| 81 | + new LengthOf( |
| 82 | + new InputOf( |
| 83 | + new Uri(Path.GetFullPath(outputPath)) |
| 84 | + ) |
| 85 | + ).Value(), |
| 86 | + "input and output are not the same size" |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void RejectsReading() |
| 94 | + { |
| 95 | + Assert.ThrowsAny<NotImplementedException>( |
| 96 | + () => new WriterAsOutputStream( |
| 97 | + new StreamWriter( |
| 98 | + new MemoryStream() |
| 99 | + ) |
| 100 | + ).Read(new byte[] {}, 0, 1) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void RejectsSeeking() |
| 106 | + { |
| 107 | + Assert.ThrowsAny<NotImplementedException>( |
| 108 | + () => new WriterAsOutputStream( |
| 109 | + new StreamWriter( |
| 110 | + new MemoryStream() |
| 111 | + ) |
| 112 | + ).Seek(3L, SeekOrigin.Begin) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public void RejectsSettingLength() |
| 118 | + { |
| 119 | + Assert.ThrowsAny<NotImplementedException>( |
| 120 | + () => new WriterAsOutputStream( |
| 121 | + new StreamWriter( |
| 122 | + new MemoryStream() |
| 123 | + ) |
| 124 | + ).SetLength(5L) |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
0 commit comments