Skip to content

Commit 4e4cdf7

Browse files
committed
Set up a new constructor for creating a SchemaTextReader directly from a string.
1 parent 3d9ed51 commit 4e4cdf7

11 files changed

Lines changed: 57 additions & 56 deletions

Schema Tests/TextSchemaTestUtil.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

Schema Tests/text/reader/SchemaTextReaderMatchingTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace schema.text.reader;
66
internal class SchemaTextReaderMatchingTests {
77
[Test]
88
public void TestReadUpToStartOfTerminator_Char() {
9-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
9+
using var tr = new SchemaTextReader("abc,,xyz, 123");
1010

1111
Assert.AreEqual("abc", tr.ReadUpToStartOfTerminator(','));
1212
tr.AssertString(",");
@@ -20,7 +20,7 @@ public void TestReadUpToStartOfTerminator_Char() {
2020

2121
[Test]
2222
public void TestReadUpToStartOfTerminator_ReadOnlySpanChar() {
23-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
23+
using var tr = new SchemaTextReader("abc,,xyz, 123");
2424

2525
Assert.AreEqual("abc", tr.ReadUpToStartOfTerminator([',']));
2626
tr.AssertString(",");
@@ -34,7 +34,7 @@ public void TestReadUpToStartOfTerminator_ReadOnlySpanChar() {
3434

3535
[Test]
3636
public void TestReadUpToStartOfTerminator_ReadOnlySpanString() {
37-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
37+
using var tr = new SchemaTextReader("abc,,xyz, 123");
3838

3939
Assert.AreEqual("abc", tr.ReadUpToStartOfTerminator([","]));
4040
tr.AssertString(",");
@@ -48,7 +48,7 @@ public void TestReadUpToStartOfTerminator_ReadOnlySpanString() {
4848

4949
[Test]
5050
public void TestReadUpToAndPastTerminator_Char() {
51-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
51+
using var tr = new SchemaTextReader("abc,,xyz, 123");
5252

5353
Assert.AreEqual("abc", tr.ReadUpToAndPastTerminator(','));
5454
Assert.AreEqual(string.Empty,
@@ -59,7 +59,7 @@ public void TestReadUpToAndPastTerminator_Char() {
5959

6060
[Test]
6161
public void TestReadUpToAndPastTerminator_ReadOnlySpanChar() {
62-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
62+
using var tr = new SchemaTextReader("abc,,xyz, 123");
6363

6464
Assert.AreEqual("abc", tr.ReadUpToAndPastTerminator([',']));
6565
Assert.AreEqual(string.Empty,
@@ -70,7 +70,7 @@ public void TestReadUpToAndPastTerminator_ReadOnlySpanChar() {
7070

7171
[Test]
7272
public void TestReadUpToAndPastTerminator_ReadOnlySpanString() {
73-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
73+
using var tr = new SchemaTextReader("abc,,xyz, 123");
7474

7575
Assert.AreEqual("abc", tr.ReadUpToAndPastTerminator([","]));
7676
Assert.AreEqual(string.Empty,
@@ -81,7 +81,7 @@ public void TestReadUpToAndPastTerminator_ReadOnlySpanString() {
8181

8282
[Test]
8383
public void TestReadWhile() {
84-
using var tr = TextSchemaTestUtil.CreateTextReader("0001111");
84+
using var tr = new SchemaTextReader("0001111");
8585

8686
Assert.AreEqual(string.Empty, tr.ReadWhile("a"));
8787
Assert.AreEqual("000", tr.ReadWhile("0"));

Schema Tests/text/reader/SchemaTextReaderMultilineTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal class SchemaTextReaderMultilineTests {
88
public void TestReadAcrossMultipleLinesSeparately() {
99
var inputText = "1 2 3\n4, 5, 6\n7\n8\n9\nfoobar";
1010

11-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
11+
using var tr = new SchemaTextReader(inputText);
1212
Assert.AreEqual(1, tr.ReadInt32());
1313
Assert.AreEqual(2, tr.ReadInt32());
1414
Assert.AreEqual(3, tr.ReadInt32());
@@ -28,7 +28,7 @@ public void TestReadAcrossMultipleLinesSeparately() {
2828
public void TestReadAcrossMultipleLinesCombined() {
2929
var inputText = "1 2 3\nfoobar";
3030

31-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
31+
using var tr = new SchemaTextReader(inputText);
3232
Assert.AreEqual(new[] { 1, 2, 3 },
3333
tr.ReadInt32s(
3434
TextReaderConstants.WHITESPACE_CHARS,

Schema Tests/text/reader/SchemaTextReaderNumberTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private void ReadAndAssert_<T>(
2424
[TestCase("0", 0)]
2525
[TestCase("255", 255)]
2626
public void TestReadAndAssertByte(string inputText, byte expectedValue) {
27-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
27+
using var tr = new SchemaTextReader(inputText);
2828
this.ReadAndAssert_(tr, expectedValue, tr.ReadByte, tr.AssertByte);
2929
}
3030

@@ -36,7 +36,7 @@ public void TestReadAndAssertByte(string inputText, byte expectedValue) {
3636
[TestCase("0Xff", 255)]
3737
[TestCase("ff", 255)]
3838
public void TestReadAndAssertHexByte(string inputText, byte expectedValue) {
39-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
39+
using var tr = new SchemaTextReader(inputText);
4040
this.ReadAndAssert_(tr, expectedValue, tr.ReadHexByte, tr.AssertHexByte);
4141
}
4242

@@ -45,7 +45,7 @@ public void TestReadAndAssertHexByte(string inputText, byte expectedValue) {
4545
[TestCase("127", 127)]
4646
[TestCase("-128", -128)]
4747
public void TestReadAndAssertSByte(string inputText, sbyte expectedValue) {
48-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
48+
using var tr = new SchemaTextReader(inputText);
4949
this.ReadAndAssert_(tr, expectedValue, tr.ReadSByte, tr.AssertSByte);
5050
}
5151

@@ -54,7 +54,7 @@ public void TestReadAndAssertSByte(string inputText, sbyte expectedValue) {
5454
[TestCase("0xFF", -1)]
5555
public void
5656
TestReadAndAssertHexSByte(string inputText, sbyte expectedValue) {
57-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
57+
using var tr = new SchemaTextReader(inputText);
5858
this.ReadAndAssert_(tr,
5959
expectedValue,
6060
tr.ReadHexSByte,
@@ -65,7 +65,7 @@ public void
6565
[TestCase("0", 0)]
6666
[TestCase("12345", 12345)]
6767
public void TestReadAndAssertInt16(string inputText, short expectedValue) {
68-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
68+
using var tr = new SchemaTextReader(inputText);
6969
this.ReadAndAssert_(tr, expectedValue, tr.ReadInt16, tr.AssertInt16);
7070
}
7171

@@ -74,39 +74,39 @@ public void TestReadAndAssertInt16(string inputText, short expectedValue) {
7474
[TestCase("12345", (ushort) 12345)]
7575
public void
7676
TestReadAndAssertUInt16(string inputText, ushort expectedValue) {
77-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
77+
using var tr = new SchemaTextReader(inputText);
7878
this.ReadAndAssert_(tr, expectedValue, tr.ReadUInt16, tr.AssertUInt16);
7979
}
8080

8181
[Test]
8282
[TestCase("0", 0)]
8383
[TestCase("1234567", 1234567)]
8484
public void TestReadAndAssertInt32(string inputText, int expectedValue) {
85-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
85+
using var tr = new SchemaTextReader(inputText);
8686
this.ReadAndAssert_(tr, expectedValue, tr.ReadInt32, tr.AssertInt32);
8787
}
8888

8989
[Test]
9090
[TestCase("0", (uint) 0)]
9191
[TestCase("1234567", (uint) 1234567)]
9292
public void TestReadAndAssertUInt32(string inputText, uint expectedValue) {
93-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
93+
using var tr = new SchemaTextReader(inputText);
9494
this.ReadAndAssert_(tr, expectedValue, tr.ReadUInt32, tr.AssertUInt32);
9595
}
9696

9797
[Test]
9898
[TestCase("0", 0)]
9999
[TestCase("123456789", 123456789)]
100100
public void TestReadAndAssertInt64(string inputText, long expectedValue) {
101-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
101+
using var tr = new SchemaTextReader(inputText);
102102
this.ReadAndAssert_(tr, expectedValue, tr.ReadInt64, tr.AssertInt64);
103103
}
104104

105105
[Test]
106106
[TestCase("0", (ulong) 0)]
107107
[TestCase("123456789", (ulong) 123456789)]
108108
public void TestReadAndAssertUInt64(string inputText, ulong expectedValue) {
109-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
109+
using var tr = new SchemaTextReader(inputText);
110110
this.ReadAndAssert_(tr, expectedValue, tr.ReadUInt64, tr.AssertUInt64);
111111
}
112112

@@ -116,7 +116,7 @@ public void TestReadAndAssertUInt64(string inputText, ulong expectedValue) {
116116
[TestCase("0.01", 0.01f)]
117117
[TestCase("-0.01", -0.01f)]
118118
public void TestReadAndAssertSingle(string inputText, float expectedValue) {
119-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
119+
using var tr = new SchemaTextReader(inputText);
120120
this.ReadAndAssert_(tr, expectedValue, tr.ReadSingle, tr.AssertSingle);
121121
}
122122

@@ -127,7 +127,7 @@ public void TestReadAndAssertSingle(string inputText, float expectedValue) {
127127
[TestCase("-0.01", -0.01)]
128128
public void
129129
TestReadAndAssertDouble(string inputText, double expectedValue) {
130-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
130+
using var tr = new SchemaTextReader(inputText);
131131
this.ReadAndAssert_(tr, expectedValue, tr.ReadDouble, tr.AssertDouble);
132132
}
133133
}

Schema Tests/text/reader/SchemaTextReaderNumbersTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace schema.text.reader;
88
internal class SchemaTextReaderNumbersTests {
99
[Test]
1010
public void TestReadBytesNull() {
11-
using var tr = TextSchemaTestUtil.CreateTextReader("0,, ,255");
11+
using var tr = new SchemaTextReader("0,, ,255");
1212
Asserts.Equal([0, null, null, 255],
1313
tr.ReadBytesIncludingEmpty(
1414
TextReaderConstants.COMMA_CHARS,
@@ -17,7 +17,7 @@ public void TestReadBytesNull() {
1717

1818
[Test]
1919
public void TestReadHexBytesNull() {
20-
using var tr = TextSchemaTestUtil.CreateTextReader("0x00,, ,0xff");
20+
using var tr = new SchemaTextReader("0x00,, ,0xff");
2121
Asserts.Equal([0, null, null, 255],
2222
tr.ReadHexBytesIncludingEmpty(
2323
TextReaderConstants.COMMA_CHARS,
@@ -32,7 +32,7 @@ public void TestReadHexBytesNull() {
3232
[TestCase("0, , 255", new byte[] { 0, 255 })]
3333
[TestCase("0, 255\n1", new byte[] { 0, 255 })]
3434
public void TestReadBytes(string inputText, byte[] expectedValues) {
35-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
35+
using var tr = new SchemaTextReader(inputText);
3636
Asserts.Equal(expectedValues,
3737
tr.ReadBytes(TextReaderConstants.COMMA_CHARS,
3838
TextReaderConstants.NEWLINE_CHARS));
@@ -44,7 +44,7 @@ public void TestReadBytes(string inputText, byte[] expectedValues) {
4444
[TestCase("0x00, 0xff", new byte[] { 0, 255 })]
4545
[TestCase("0x00, 0xff\n0x01", new byte[] { 0, 255 })]
4646
public void TestReadHexBytes(string inputText, byte[] expectedValues) {
47-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
47+
using var tr = new SchemaTextReader(inputText);
4848
Asserts.Equal(expectedValues,
4949
tr.ReadHexBytes(TextReaderConstants.COMMA_CHARS,
5050
TextReaderConstants.NEWLINE_CHARS));
@@ -55,7 +55,7 @@ public void TestReadHexBytes(string inputText, byte[] expectedValues) {
5555
[TestCase("\n0", new float[0])]
5656
[TestCase("-.01, 0.01", new[] { -.01f, 0.01f })]
5757
public void TestReadSingles(string inputText, float[] expectedValues) {
58-
using var tr = TextSchemaTestUtil.CreateTextReader(inputText);
58+
using var tr = new SchemaTextReader(inputText);
5959
Asserts.Equal(expectedValues,
6060
tr.ReadSingles(TextReaderConstants.COMMA_CHARS,
6161
TextReaderConstants.NEWLINE_CHARS));

Schema Tests/text/reader/SchemaTextReaderPositionTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace schema.text.reader;
66
internal class SchemaTextReaderPositionTests {
77
[Test]
88
public void TestGetPositions() {
9-
using var tr = TextSchemaTestUtil.CreateTextReader("abc");
9+
using var tr = new SchemaTextReader("abc");
1010

1111
Assert.AreEqual(0, tr.Position);
1212
Assert.AreEqual(0, tr.LineNumber);
@@ -20,7 +20,7 @@ public void TestGetPositions() {
2020

2121
[Test]
2222
public void TestGetPositionsWithTabs() {
23-
using var tr = TextSchemaTestUtil.CreateTextReader("\t1\t");
23+
using var tr = new SchemaTextReader("\t1\t");
2424

2525
Assert.AreEqual(0, tr.Position);
2626
Assert.AreEqual(0, tr.LineNumber);
@@ -44,7 +44,7 @@ public void TestGetPositionsWithTabs() {
4444

4545
[Test]
4646
public void TestGetPositionsAcrossLines() {
47-
using var tr = TextSchemaTestUtil.CreateTextReader("ab\n12\nfoo");
47+
using var tr = new SchemaTextReader("ab\n12\nfoo");
4848

4949
Assert.AreEqual(0, tr.Position);
5050
Assert.AreEqual(0, tr.LineNumber);
@@ -83,7 +83,7 @@ public void TestGetPositionsAcrossLines() {
8383

8484
[Test]
8585
public void TestGetPositionsAcrossLinesWhenReadingMultiple() {
86-
using var tr = TextSchemaTestUtil.CreateTextReader("abc\n\t1\t23\nfoo");
86+
using var tr = new SchemaTextReader("abc\n\t1\t23\nfoo");
8787

8888
Assert.AreEqual(0, tr.Position);
8989
Assert.AreEqual(0, tr.LineNumber);
@@ -117,7 +117,7 @@ public void TestGetPositionsAcrossLinesWhenReadingMultiple() {
117117

118118
[Test]
119119
public void TestLength() {
120-
using var tr = TextSchemaTestUtil.CreateTextReader("abc");
120+
using var tr = new SchemaTextReader("abc");
121121
Assert.AreEqual(3, tr.Length);
122122
}
123123
}

Schema Tests/text/reader/SchemaTextReaderStringTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace schema.text.reader;
88
internal class SchemaTextReaderStringTests {
99
[Test]
1010
public void TestReadChar() {
11-
using var tr = TextSchemaTestUtil.CreateTextReader("abc");
11+
using var tr = new SchemaTextReader("abc");
1212

1313
Assert.AreEqual('a', tr.ReadChar());
1414
Assert.AreEqual('b', tr.ReadChar());
@@ -17,7 +17,7 @@ public void TestReadChar() {
1717

1818
[Test]
1919
public void TestAssertChar() {
20-
using var tr = TextSchemaTestUtil.CreateTextReader("abc");
20+
using var tr = new SchemaTextReader("abc");
2121

2222
tr.AssertChar('a');
2323
tr.AssertChar('b');
@@ -26,7 +26,7 @@ public void TestAssertChar() {
2626

2727
[Test]
2828
public void TestReadStrings() {
29-
using var tr = TextSchemaTestUtil.CreateTextReader("abc,,xyz, 123");
29+
using var tr = new SchemaTextReader("abc,,xyz, 123");
3030
Assert.AreEqual(new[] { "abc", String.Empty, "xyz", " 123" },
3131
tr.ReadStrings(new[] { "," }, new[] { "\n" }));
3232
}

Schema Tests/text/reader/TextReaderExtensionTests_Read.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal partial class TextReaderExtensionTests {
1414
[TestCase("'foo bar'", ExpectedResult = "'foo bar'")]
1515
[TestCase("\"foo bar\"", ExpectedResult = "\"foo bar\"")]*/
1616
public string TestReadWord(string text) {
17-
using var tr = TextSchemaTestUtil.CreateTextReader(text);
17+
using var tr = new SchemaTextReader(text);
1818
return tr.ReadWord();
1919
}
2020

@@ -42,7 +42,7 @@ public string TestReadWord(string text) {
4242
[TestCase("\"foo bar\"", ExpectedResult = new[] { "\"foo bar\"" })]
4343
[TestCase("`foo bar`", ExpectedResult = new[] { "`foo bar`" })]
4444
public string[] TestReadArguments(string text) {
45-
using var tr = TextSchemaTestUtil.CreateTextReader(text);
45+
using var tr = new SchemaTextReader(text);
4646
return tr.ReadArguments([','], [')']);
4747
}
4848
}

Schema Tests/text/reader/TextReaderExtensionTests_Skip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal partial class TextReaderExtensionTests {
1313
[TestCase(" // 123\n// abc\n foo", ExpectedResult = "foo")]
1414
[TestCase("/* 123 \n \n * */foo", ExpectedResult = "foo")]
1515
public string TestSkipCommentsAndWhitespace(string text) {
16-
using var tr = TextSchemaTestUtil.CreateTextReader(text);
16+
using var tr = new SchemaTextReader(text);
1717
tr.SkipCommentsAndWhitespace();
1818
return tr.ReadRemainder();
1919
}

Schema/src/text/reader/SchemaTextReader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ namespace schema.text.reader;
99
public sealed partial class SchemaTextReader : ITextReader {
1010
private readonly ISeekableReadableStream baseStream_;
1111

12+
public SchemaTextReader(string text, int tabWidth = 4) : this(
13+
StreamUtil.FromString(text),
14+
tabWidth) { }
15+
1216
public SchemaTextReader(Stream baseStream, int tabWidth = 4)
1317
: this(new ReadableStream(baseStream), tabWidth) { }
1418

0 commit comments

Comments
 (0)