forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringWriterExtensionsTest.cs
More file actions
113 lines (92 loc) · 3.67 KB
/
Copy pathStringWriterExtensionsTest.cs
File metadata and controls
113 lines (92 loc) · 3.67 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.WebPages.Test
{
public class StringWriterExtensionsTest
{
[Fact]
public void CopiesResult()
{
// Note that a preable is not expected on the generated stream.
string text = "Hello world";
Byte[] textInBytes = Encoding.UTF8.GetBytes(text);
string outputText;
Byte[] buffer = new Byte[1024];
using (MemoryStream stream = new MemoryStream(buffer))
using (StringWriter writer = new StringWriter())
using (StreamWriter outputWriter = new StreamWriter(stream))
{
writer.Write(text);
writer.CopyTo(outputWriter);
outputText = writer.ToString();
}
Assert.Equal(text, outputText, StringComparer.Ordinal);
for (int i = 0; i < textInBytes.Length; i++)
{
Assert.Equal(textInBytes[i], buffer[i]);
}
}
[Theory]
[InlineData(1)]
[InlineData(1023)]
[InlineData(1024)]
[InlineData(1025)]
[InlineData(20000)]
[InlineData(100000)]
public void OnlyUsesBufferUpToSize(int count)
{
string text = new string('a', count);
Byte[] textInBytes = Encoding.UTF8.GetBytes(text);
Mock<StreamWriter> mock;
Byte[] buffer = new Byte[textInBytes.Length + 100];
using (MemoryStream stream = new MemoryStream(buffer))
{
StringWriter writer = new StringWriter();
mock = new Mock<StreamWriter>(MockBehavior.Strict, stream) { CallBase = true };
mock.Setup(sw => sw.Write(It.IsAny<char[]>(),
It.IsAny<int>(),
It.Is<int>(c => c == StringWriterExtensions.BufferSize ||
c == textInBytes.Length % StringWriterExtensions.BufferSize)))
.Verifiable();
StreamWriter outputWriter = mock.Object;
writer.Write(text);
writer.CopyTo(outputWriter);
mock.Verify();
}
}
[Theory]
[InlineData(1)]
[InlineData(1023/7)]
[InlineData(20000/7)]
[InlineData(100000/7)]
public void ProperlyCopiesLargeSetsOfText(int count)
{
// The char א turns into a two byte sequence so we end up with a
// 7 byte sequence that is not a divider or 1024.
string text = string.Join(string.Empty, Enumerable.Repeat("abcdeא", count));
Byte[] textInBytes = Encoding.UTF8.GetBytes(text);
string outputText;
Byte[] buffer = new Byte[textInBytes.Length + 100];
using (MemoryStream stream = new MemoryStream(buffer))
using (StringWriter writer = new StringWriter())
{
using (StreamWriter outputWriter = new StreamWriter(stream))
{
writer.Write(text);
writer.CopyTo(outputWriter);
outputText = writer.ToString();
}
}
Assert.Equal(text, outputText, StringComparer.Ordinal);
for (int i = 0; i < textInBytes.Length; i++)
{
Assert.Equal(textInBytes[i], buffer[i]);
}
}
}
}