Skip to content

Commit 6144565

Browse files
author
Meerow
authored
Adds Tests for WriterAsOutputStream
Adds Tests for WriterAsOutputStream
2 parents 2060d95 + 7bc0868 commit 6144565

File tree

3 files changed

+140
-20
lines changed

3 files changed

+140
-20
lines changed

src/Yaapii.Atoms/IO/WriterAsOutputStream.cs

+11-19
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Yaapii.Atoms.IO
3535
/// <summary>
3636
/// <see cref="StreamWriter"/> as a writable <see cref="Stream"/>.
3737
/// </summary>
38-
internal class WriterAsOutputStream : Stream, IDisposable
38+
public sealed class WriterAsOutputStream : Stream, IDisposable
3939
{
4040
/// <summary>
4141
/// the writer
@@ -50,49 +50,41 @@ internal class WriterAsOutputStream : Stream, IDisposable
5050
/// <summary>
5151
/// <see cref="StreamWriter"/> as a writable <see cref="Stream"/>.
5252
/// </summary>
53-
/// <param name="wtr">a writer</param>
54-
internal WriterAsOutputStream(StreamWriter wtr) : this(wtr, Encoding.UTF8)
53+
public WriterAsOutputStream(StreamWriter writer) : this(writer, Encoding.UTF8)
5554
{ }
5655

5756
/// <summary>
5857
/// <see cref="StreamWriter"/> as a writable <see cref="Stream"/>.
5958
/// </summary>
60-
/// <param name="wtr">a writer</param>
61-
/// <param name="encoding">encoding of the writer</param>
62-
internal WriterAsOutputStream(StreamWriter wtr, string encoding) : this(wtr, Encoding.GetEncoding(encoding))
59+
public WriterAsOutputStream(StreamWriter writer, string encoding) : this(writer, Encoding.GetEncoding(encoding))
6360
{ }
6461

6562
/// <summary>
6663
/// <see cref="StreamWriter"/> as a writable <see cref="Stream"/>.
6764
/// </summary>
68-
/// <param name="wtr">a writer</param>
69-
/// <param name="enc">encoding of the writer</param>
70-
internal WriterAsOutputStream(StreamWriter wtr, Encoding enc) : this(
71-
wtr,
65+
public WriterAsOutputStream(StreamWriter writer, Encoding encoding) : this(
66+
writer,
7267
new Live<Decoder>(() =>
7368
{
74-
var ddr = enc.GetDecoder();
75-
ddr.Fallback = DecoderFallback.ExceptionFallback;
76-
return ddr;
69+
var decoder = encoding.GetDecoder();
70+
decoder.Fallback = DecoderFallback.ExceptionFallback;
71+
return decoder;
7772
})
7873
)
7974
{ }
8075

8176
/// <summary>
8277
/// <see cref="StreamWriter"/> as a writable <see cref="Stream"/>.
8378
/// </summary>
84-
/// <param name="wtr">a writer</param>
85-
/// <param name="ddr">charset decoder for the writer</param>
86-
internal WriterAsOutputStream(StreamWriter wtr, IScalar<Decoder> ddr) : base()
79+
public WriterAsOutputStream(StreamWriter writer, IScalar<Decoder> decoder) : base()
8780
{
88-
this._writer = wtr;
89-
this._decoder = ddr;
81+
this._writer = writer;
82+
this._decoder = decoder;
9083
}
9184

9285
/// <summary>
9386
/// <see cref="StreamWriter"/> as a writable <see cref="Stream"/>.
9487
/// </summary>
95-
/// <param name="data">some data</param>
9688
public void Write(int data)
9789
{
9890
this.Write(new byte[] { (byte)data }, 0, 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+

tests/Yaapii.Atoms.Tests/IO/WriterAsOutputTest.cs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
using Xunit;
2626
using Yaapii.Atoms.Bytes;
2727
using Yaapii.Atoms.Enumerable;
28-
using Yaapii.Atoms.Text;
2928

3029
namespace Yaapii.Atoms.IO.Tests
3130
{

0 commit comments

Comments
 (0)