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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Web.Razor.Parser;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Razor.Test.Text
{
public class TextReaderExtensionsTest
{
[Fact]
public void ReadUntilWithCharThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(null, '@'), "reader");
}
[Fact]
public void ReadUntilInclusiveWithCharThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(null, '@', inclusive: true), "reader");
}
[Fact]
public void ReadUntilWithMultipleTerminatorsThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(null, '/', '>'), "reader");
}
[Fact]
public void ReadUntilInclusiveWithMultipleTerminatorsThrowsArgNullIfReaderNull()
{
// NOTE: Using named parameters would be difficult here, hence the inline comment
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(null, /* inclusive */ true, '/', '>'), "reader");
}
[Fact]
public void ReadUntilWithPredicateThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(null, c => true), "reader");
}
[Fact]
public void ReadUntilInclusiveWithPredicateThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(null, c => true, inclusive: true), "reader");
}
[Fact]
public void ReadUntilWithPredicateThrowsArgExceptionIfPredicateNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(new StringReader("Foo"), (Predicate<char>)null), "condition");
}
[Fact]
public void ReadUntilInclusiveWithPredicateThrowsArgExceptionIfPredicateNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadUntil(new StringReader("Foo"), (Predicate<char>)null, inclusive: true), "condition");
}
[Fact]
public void ReadWhileWithPredicateThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadWhile(null, c => true), "reader");
}
[Fact]
public void ReadWhileInclusiveWithPredicateThrowsArgNullIfReaderNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadWhile(null, c => true, inclusive: true), "reader");
}
[Fact]
public void ReadWhileWithPredicateThrowsArgNullIfPredicateNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadWhile(new StringReader("Foo"), (Predicate<char>)null), "condition");
}
[Fact]
public void ReadWhileInclusiveWithPredicateThrowsArgNullIfPredicateNull()
{
Assert.ThrowsArgumentNull(() => TextReaderExtensions.ReadWhile(new StringReader("Foo"), (Predicate<char>)null, inclusive: true), "condition");
}
[Fact]
public void ReadUntilWithCharReadsAllTextUpToSpecifiedCharacterButNotPast()
{
RunReaderTest("foo bar baz @biz", "foo bar baz ", '@', r => r.ReadUntil('@'));
}
[Fact]
public void ReadUntilWithCharWithInclusiveFlagReadsAllTextUpToSpecifiedCharacterButNotPastIfInclusiveFalse()
{
RunReaderTest("foo bar baz @biz", "foo bar baz ", '@', r => r.ReadUntil('@', inclusive: false));
}
[Fact]
public void ReadUntilWithCharWithInclusiveFlagReadsAllTextUpToAndIncludingSpecifiedCharacterIfInclusiveTrue()
{
RunReaderTest("foo bar baz @biz", "foo bar baz @", 'b', r => r.ReadUntil('@', inclusive: true));
}
[Fact]
public void ReadUntilWithCharReadsToEndIfSpecifiedCharacterNotFound()
{
RunReaderTest("foo bar baz", "foo bar baz", -1, r => r.ReadUntil('@'));
}
[Fact]
public void ReadUntilWithMultipleTerminatorsReadsUntilAnyTerminatorIsFound()
{
RunReaderTest("<bar/>", "<bar", '/', r => r.ReadUntil('/', '>'));
}
[Fact]
public void ReadUntilWithMultipleTerminatorsHonorsInclusiveFlagWhenFalse()
{
// NOTE: Using named parameters would be difficult here, hence the inline comment
RunReaderTest("<bar/>", "<bar", '/', r => r.ReadUntil( /* inclusive */ false, '/', '>'));
}
[Fact]
public void ReadUntilWithMultipleTerminatorsHonorsInclusiveFlagWhenTrue()
{
// NOTE: Using named parameters would be difficult here, hence the inline comment
RunReaderTest("<bar/>", "<bar/", '>', r => r.ReadUntil( /* inclusive */ true, '/', '>'));
}
[Fact]
public void ReadUntilWithPredicateStopsWhenPredicateIsTrue()
{
RunReaderTest("foo bar baz 0 zoop zork zoink", "foo bar baz ", '0', r => r.ReadUntil(c => Char.IsDigit(c)));
}
[Fact]
public void ReadUntilWithPredicateHonorsInclusiveFlagWhenFalse()
{
RunReaderTest("foo bar baz 0 zoop zork zoink", "foo bar baz ", '0', r => r.ReadUntil(c => Char.IsDigit(c), inclusive: false));
}
[Fact]
public void ReadUntilWithPredicateHonorsInclusiveFlagWhenTrue()
{
RunReaderTest("foo bar baz 0 zoop zork zoink", "foo bar baz 0", ' ', r => r.ReadUntil(c => Char.IsDigit(c), inclusive: true));
}
[Fact]
public void ReadWhileWithPredicateStopsWhenPredicateIsFalse()
{
RunReaderTest("012345a67890", "012345", 'a', r => r.ReadWhile(c => Char.IsDigit(c)));
}
[Fact]
public void ReadWhileWithPredicateHonorsInclusiveFlagWhenFalse()
{
RunReaderTest("012345a67890", "012345", 'a', r => r.ReadWhile(c => Char.IsDigit(c), inclusive: false));
}
[Fact]
public void ReadWhileWithPredicateHonorsInclusiveFlagWhenTrue()
{
RunReaderTest("012345a67890", "012345a", '6', r => r.ReadWhile(c => Char.IsDigit(c), inclusive: true));
}
private static void RunReaderTest(string testString, string expectedOutput, int expectedPeek, Func<TextReader, string> action)
{
// Arrange
StringReader reader = new StringReader(testString);
// Act
string read = action(reader);
// Assert
Assert.Equal(expectedOutput, read);
if (expectedPeek == -1)
{
Assert.True(reader.Peek() == -1, "Expected that the reader would be positioned at the end of the input stream");
}
else
{
Assert.Equal((char)expectedPeek, (char)reader.Peek());
}
}
}
}
|