From 619ab136699c9dc7fab0d49c114cc100cd2c690d Mon Sep 17 00:00:00 2001 From: WinCPP Date: Fri, 8 Sep 2017 22:27:34 +0530 Subject: [PATCH] CoreFx #22406 - implement review comments --- .../tests/TextReader/TextReaderTests.cs | 199 +++++++++--------- .../TextReader/TextReaderTests.netcoreapp.cs | 38 ++-- .../tests/TextWriter/TextWriterTests.cs | 78 ++++--- .../TextWriter/TextWriterTests.netcoreapp.cs | 6 +- 4 files changed, 162 insertions(+), 159 deletions(-) diff --git a/src/System.IO/tests/TextReader/TextReaderTests.cs b/src/System.IO/tests/TextReader/TextReaderTests.cs index 768aa4f899d1..2830d1de84a7 100644 --- a/src/System.IO/tests/TextReader/TextReaderTests.cs +++ b/src/System.IO/tests/TextReader/TextReaderTests.cs @@ -10,27 +10,22 @@ namespace System.IO.Tests { public partial class TextReaderTests { - protected virtual char[] GetSmallData() - { - return "HELLO".ToCharArray(); - } + protected static readonly char[] SmallData; + protected static readonly char[] LargeData; + protected static readonly char[] ChArr; - protected virtual char[] GetLargeData() + static TextReaderTests() { - var testData = "HELLO".ToCharArray(); + SmallData = "HELLO".ToCharArray(); var data = new List(); - for (int i = 0; i < 1000; i++) + for (int count = 0; count < 1000; ++count) { - data.AddRange(testData); + data.AddRange(SmallData); } + LargeData = data.ToArray(); - return data.ToArray(); - } - - protected (char[] chArr, CharArrayTextReader textReader) GetCharArray() - { - var chArr = new char[] + ChArr = new char[] { char.MinValue, char.MaxValue, @@ -58,16 +53,18 @@ protected virtual char[] GetLargeData() 'K', '\u00E6' }; + } - var tr = new CharArrayTextReader(chArr); - - return (chArr, tr); + protected (char[] chArr, CharArrayTextReader textReader) GetCharArray() + { + var tr = new CharArrayTextReader(ChArr); + return (ChArr, tr); } [Fact] public void EndOfStream() { - using (var tr = new CharArrayTextReader(GetSmallData())) + using (var tr = new CharArrayTextReader(SmallData)) { var result = tr.ReadToEnd(); @@ -80,7 +77,7 @@ public void EndOfStream() [Fact] public void NotEndOfStream() { - using (var tr = new CharArrayTextReader(GetSmallData())) + using (var tr = new CharArrayTextReader(SmallData)) { var charBuff = new char[3]; var result = tr.Read(charBuff, 0, 3); @@ -96,7 +93,7 @@ public void NotEndOfStream() [Fact] public async Task ReadToEndAsync() { - using (var tr = new CharArrayTextReader(GetLargeData())) + using (var tr = new CharArrayTextReader(LargeData)) { var result = await tr.ReadToEndAsync(); @@ -108,45 +105,51 @@ public async Task ReadToEndAsync() public void TestRead() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - for (int i = 0; i < baseInfo.chArr.Length; i++) + using (var tr = baseInfo.textReader) { - int tmp = tr.Read(); - Assert.Equal((int)baseInfo.chArr[i], tmp); + for (int count = 0; count < baseInfo.chArr.Length; ++count) + { + int tmp = tr.Read(); + Assert.Equal((int)baseInfo.chArr[count], tmp); + } } - - tr.Dispose(); } [Fact] public void ArgumentNullOnNullArray() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - Assert.Throws(() => tr.Read(null, 0, 0)); + using (var tr = baseInfo.textReader) + { + Assert.Throws(() => tr.Read(null, 0, 0)); + } } [Fact] public void ArgumentOutOfRangeOnInvalidOffset() { - var tr = GetCharArray().textReader; - Assert.Throws(() => tr.Read(new char[0], -1, 0)); + using (var tr = GetCharArray().textReader) + { + Assert.Throws(() => tr.Read(new char[0], -1, 0)); + } } [Fact] public void ArgumentOutOfRangeOnNegativCount() { - var tr = GetCharArray().textReader; - AssertExtensions.Throws(null, () => tr.Read(new char[0], 0, 1)); + using (var tr = GetCharArray().textReader) + { + AssertExtensions.Throws(null, () => tr.Read(new char[0], 0, 1)); + } } [Fact] public void ArgumentExceptionOffsetAndCount() { - var tr = GetCharArray().textReader; - AssertExtensions.Throws(null, () => tr.Read(new char[0], 2, 0)); + using (var tr = GetCharArray().textReader) + { + AssertExtensions.Throws(null, () => tr.Read(new char[0], 2, 0)); + } } [Fact] @@ -164,16 +167,17 @@ public void EmptyInput() public void ReadCharArr() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - var chArr = new char[baseInfo.chArr.Length]; + using (var tr = baseInfo.textReader) + { + var chArr = new char[baseInfo.chArr.Length]; - var read = tr.Read(chArr, 0, chArr.Length); + var read = tr.Read(chArr, 0, chArr.Length); + Assert.Equal(chArr.Length, read); - Assert.Equal(chArr.Length, read); - for (int i = 0; i < baseInfo.chArr.Length; i++) - { - Assert.Equal(baseInfo.chArr[i], chArr[i]); + for (int count = 0; count < baseInfo.chArr.Length; ++count) + { + Assert.Equal(baseInfo.chArr[count], chArr[count]); + } } } @@ -181,16 +185,17 @@ public void ReadCharArr() public void ReadBlockCharArr() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - var chArr = new char[baseInfo.chArr.Length]; + using (var tr = baseInfo.textReader) + { + var chArr = new char[baseInfo.chArr.Length]; - var read = tr.ReadBlock(chArr, 0, chArr.Length); + var read = tr.ReadBlock(chArr, 0, chArr.Length); + Assert.Equal(chArr.Length, read); - Assert.Equal(chArr.Length, read); - for (int i = 0; i < baseInfo.chArr.Length; i++) - { - Assert.Equal(baseInfo.chArr[i], chArr[i]); + for (int count = 0; count < baseInfo.chArr.Length; ++count) + { + Assert.Equal(baseInfo.chArr[count], chArr[count]); + } } } @@ -198,16 +203,17 @@ public void ReadBlockCharArr() public async void ReadBlockAsyncCharArr() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - var chArr = new char[baseInfo.chArr.Length]; + using (var tr = baseInfo.textReader) + { + var chArr = new char[baseInfo.chArr.Length]; - var read = await tr.ReadBlockAsync(chArr, 0, chArr.Length); + var read = await tr.ReadBlockAsync(chArr, 0, chArr.Length); + Assert.Equal(chArr.Length, read); - Assert.Equal(chArr.Length, read); - for (int i = 0; i < baseInfo.chArr.Length; i++) - { - Assert.Equal(baseInfo.chArr[i], chArr[i]); + for (int count = 0; count < baseInfo.chArr.Length; ++count) + { + Assert.Equal(baseInfo.chArr[count], chArr[count]); + } } } @@ -215,17 +221,17 @@ public async void ReadBlockAsyncCharArr() public async Task ReadAsync() { var baseInfo = GetCharArray(); + using (var tr = baseInfo.textReader) + { + var chArr = new char[baseInfo.chArr.Length]; - var tr = baseInfo.textReader; - - var chArr = new char[baseInfo.chArr.Length]; - - var read = await tr.ReadAsync(chArr, 4, 3); + var read = await tr.ReadAsync(chArr, 4, 3); + Assert.Equal(read, 3); - Assert.Equal(read, 3); - for (int i = 0; i < 3; i++) - { - Assert.Equal(baseInfo.chArr[i], chArr[i + 4]); + for (int count = 0; count < 3; ++count) + { + Assert.Equal(baseInfo.chArr[count], chArr[count + 4]); + } } } @@ -233,52 +239,55 @@ public async Task ReadAsync() public void ReadLines() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - string valueString = new string(baseInfo.chArr); - + using (var tr = baseInfo.textReader) + { + string valueString = new string(baseInfo.chArr); - var data = tr.ReadLine(); - Assert.Equal(valueString.Substring(0, valueString.IndexOf('\r')), data); + var data = tr.ReadLine(); + Assert.Equal(valueString.Substring(0, valueString.IndexOf('\r')), data); - data = tr.ReadLine(); - Assert.Equal(valueString.Substring(valueString.IndexOf('\r') + 1, 3), data); + data = tr.ReadLine(); + Assert.Equal(valueString.Substring(valueString.IndexOf('\r') + 1, 3), data); - data = tr.ReadLine(); - Assert.Equal(valueString.Substring(valueString.IndexOf('\n') + 1, 2), data); + data = tr.ReadLine(); + Assert.Equal(valueString.Substring(valueString.IndexOf('\n') + 1, 2), data); - data = tr.ReadLine(); - Assert.Equal((valueString.Substring(valueString.LastIndexOf('\n') + 1)), data); + data = tr.ReadLine(); + Assert.Equal((valueString.Substring(valueString.LastIndexOf('\n') + 1)), data); + } } [Fact] public void ReadLines2() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; + using (var tr = baseInfo.textReader) + { + string valueString = new string(baseInfo.chArr); - string valueString = new string(baseInfo.chArr); + var temp = new char[10]; + tr.Read(temp, 0, 1); + var data = tr.ReadLine(); - var temp = new char[10]; - tr.Read(temp, 0, 1); - var data = tr.ReadLine(); - Assert.Equal(valueString.Substring(1, valueString.IndexOf('\r') - 1), data); + Assert.Equal(valueString.Substring(1, valueString.IndexOf('\r') - 1), data); + } } [Fact] public async Task ReadLineAsyncContinuousNewLinesAndTabs() { var newLineTabData = new char[] { '\n', '\n', '\r', '\r', '\n' }; - var tr = new CharArrayTextReader(newLineTabData); - - for (int i = 0; i < 4; i++) + using (var tr = new CharArrayTextReader(newLineTabData)) { - var data = await tr.ReadLineAsync(); - Assert.Equal(string.Empty, data); + for (int count = 0; count < 4; ++count) + { + var data = await tr.ReadLineAsync(); + Assert.Equal(string.Empty, data); + } + + var eol = await tr.ReadLineAsync(); + Assert.Null(eol); } - - var eol = await tr.ReadLineAsync(); - Assert.Null(eol); } } } diff --git a/src/System.IO/tests/TextReader/TextReaderTests.netcoreapp.cs b/src/System.IO/tests/TextReader/TextReaderTests.netcoreapp.cs index e58f2ac7f5eb..67659de406f7 100644 --- a/src/System.IO/tests/TextReader/TextReaderTests.netcoreapp.cs +++ b/src/System.IO/tests/TextReader/TextReaderTests.netcoreapp.cs @@ -14,17 +14,18 @@ public partial class TextReaderTests public void ReadSpan() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - var chArr = new char[baseInfo.chArr.Length]; - var chSpan = new Span(chArr, 0, baseInfo.chArr.Length); + using (var tr = baseInfo.textReader) + { + var chArr = new char[baseInfo.chArr.Length]; + var chSpan = new Span(chArr, 0, baseInfo.chArr.Length); - var read = tr.Read(chSpan); + var read = tr.Read(chSpan); + Assert.Equal(chArr.Length, read); - Assert.Equal(chArr.Length, read); - for (int i = 0; i < baseInfo.chArr.Length; i++) - { - Assert.Equal(baseInfo.chArr[i], chArr[i]); + for (int i = 0; i < baseInfo.chArr.Length; i++) + { + Assert.Equal(baseInfo.chArr[i], chArr[i]); + } } } @@ -32,17 +33,18 @@ public void ReadSpan() public void ReadBlockSpan() { var baseInfo = GetCharArray(); - var tr = baseInfo.textReader; - - var chArr = new char[baseInfo.chArr.Length]; - var chSpan = new Span(chArr, 0, baseInfo.chArr.Length); + using (var tr = baseInfo.textReader) + { + var chArr = new char[baseInfo.chArr.Length]; + var chSpan = new Span(chArr, 0, baseInfo.chArr.Length); - var read = tr.Read(chSpan); + var read = tr.Read(chSpan); + Assert.Equal(chArr.Length, read); - Assert.Equal(chArr.Length, read); - for (int i = 0; i < baseInfo.chArr.Length; i++) - { - Assert.Equal(baseInfo.chArr[i], chArr[i]); + for (int i = 0; i < baseInfo.chArr.Length; i++) + { + Assert.Equal(baseInfo.chArr[i], chArr[i]); + } } } } diff --git a/src/System.IO/tests/TextWriter/TextWriterTests.cs b/src/System.IO/tests/TextWriter/TextWriterTests.cs index 764910f47bfa..7b52581ade26 100644 --- a/src/System.IO/tests/TextWriter/TextWriterTests.cs +++ b/src/System.IO/tests/TextWriter/TextWriterTests.cs @@ -11,19 +11,21 @@ namespace System.IO.Tests { public partial class TextWriterTests { - protected static string FormatStringOneObject = "Object is {0}"; - protected static string FormatStringTwoObjects = $"Object are '{0}', {SecondObject}"; - protected static string FormatStringThreeObjects = $"Objects are {0}, {SecondObject}, {ThirdObject}"; - protected static string FormatStringMultipleObjects = "Multiple Objects are: {0}, {1}, {2}"; + protected static readonly string FormatStringOneObject = "Object is {0}"; + protected static readonly string FormatStringTwoObjects = $"Object are '{0}', {SecondObject}"; + protected static readonly string FormatStringThreeObjects = $"Objects are {0}, {SecondObject}, {ThirdObject}"; + protected static readonly string FormatStringMultipleObjects = "Multiple Objects are: {0}, {1}, {2}"; - protected static object FirstObject = (object)1; - protected static object SecondObject = (object)"[second object]"; - protected static object ThirdObject = (object)""; - protected static object[] MultipleObjects = new object[] { FirstObject, SecondObject, ThirdObject }; + protected static readonly object FirstObject = (object)1; + protected static readonly object SecondObject = (object)"[second object]"; + protected static readonly object ThirdObject = (object)""; + protected static readonly object[] MultipleObjects = new object[] { FirstObject, SecondObject, ThirdObject }; - protected char[] GetCharArray() + protected static readonly char[] ChArr; + + static TextWriterTests() { - return new char[] + ChArr = new char[] { char.MinValue, char.MaxValue, @@ -65,36 +67,33 @@ protected static CharArrayTextWriter GetTextWriter() [Fact] public void WriteCharTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - for (int count = 0; count < chArr.Length; ++count) + for (int count = 0; count < ChArr.Length; ++count) { - tw.Write(chArr[count]); + tw.Write(ChArr[count]); } - Assert.Equal(new string(chArr), tw.Text); + Assert.Equal(new string(ChArr), tw.Text); } } [Fact] public void WriteCharArrayTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - tw.Write(chArr); - Assert.Equal(new string(chArr), tw.Text); + tw.Write(ChArr); + Assert.Equal(new string(ChArr), tw.Text); } } [Fact] public void WriteCharArrayIndexCountTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - tw.Write(chArr, 3, 5); - Assert.Equal(new string(chArr, 3, 5), tw.Text); + tw.Write(ChArr, 3, 5); + Assert.Equal(new string(ChArr, 3, 5), tw.Text); } } @@ -225,8 +224,8 @@ public void WriteStringTest() { using (CharArrayTextWriter tw = GetTextWriter()) { - tw.Write(new string(GetCharArray())); - Assert.Equal(new string(GetCharArray()), tw.Text); + tw.Write(new string(ChArr)); + Assert.Equal(new string(ChArr), tw.Text); } } @@ -297,36 +296,33 @@ public void WriteLineTest() [Fact] public void WriteLineCharTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - for (int count = 0; count < chArr.Length; ++count) + for (int count = 0; count < ChArr.Length; ++count) { - tw.WriteLine(chArr[count]); + tw.WriteLine(ChArr[count]); } - Assert.Equal(string.Join(tw.NewLine, chArr.Select(ch => ch.ToString()).ToArray()) + tw.NewLine, tw.Text); + Assert.Equal(string.Join(tw.NewLine, ChArr.Select(ch => ch.ToString()).ToArray()) + tw.NewLine, tw.Text); } } [Fact] public void WriteLineCharArrayTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - tw.WriteLine(chArr); - Assert.Equal(new string(chArr) + tw.NewLine, tw.Text); + tw.WriteLine(ChArr); + Assert.Equal(new string(ChArr) + tw.NewLine, tw.Text); } } [Fact] public void WriteLineCharArrayIndexCountTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - tw.WriteLine(chArr, 3, 5); - Assert.Equal(new string(chArr, 3, 5) + tw.NewLine, tw.Text); + tw.WriteLine(ChArr, 3, 5); + Assert.Equal(new string(ChArr, 3, 5) + tw.NewLine, tw.Text); } } @@ -456,8 +452,8 @@ public void WriteLineStringTest() { using (CharArrayTextWriter tw = GetTextWriter()) { - tw.WriteLine(new string(GetCharArray())); - Assert.Equal(new string(GetCharArray()) + tw.NewLine, tw.Text); + tw.WriteLine(new string(ChArr)); + Assert.Equal(new string(ChArr) + tw.NewLine, tw.Text); } } @@ -530,7 +526,7 @@ public async void WriteAsyncStringTest() { using (CharArrayTextWriter tw = GetTextWriter()) { - var toWrite = new string(GetCharArray()); + var toWrite = new string(ChArr); await tw.WriteAsync(toWrite); Assert.Equal(toWrite, tw.Text); } @@ -539,11 +535,10 @@ public async void WriteAsyncStringTest() [Fact] public async void WriteAsyncCharArrayIndexCountTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - await tw.WriteAsync(chArr, 3, 5); - Assert.Equal(new string(chArr, 3, 5), tw.Text); + await tw.WriteAsync(ChArr, 3, 5); + Assert.Equal(new string(ChArr, 3, 5), tw.Text); } } @@ -576,7 +571,7 @@ public async void WriteLineAsyncStringTest() { using (CharArrayTextWriter tw = GetTextWriter()) { - var toWrite = new string(GetCharArray()); + var toWrite = new string(ChArr); await tw.WriteLineAsync(toWrite); Assert.Equal(toWrite + tw.NewLine, tw.Text); } @@ -587,9 +582,8 @@ public async void WriteLineAsyncCharArrayIndexCount() { using (CharArrayTextWriter tw = GetTextWriter()) { - char[] chArr = GetCharArray(); - await tw.WriteLineAsync(chArr, 3, 5); - Assert.Equal(new string(chArr, 3, 5) + tw.NewLine, tw.Text); + await tw.WriteLineAsync(ChArr, 3, 5); + Assert.Equal(new string(ChArr, 3, 5) + tw.NewLine, tw.Text); } } diff --git a/src/System.IO/tests/TextWriter/TextWriterTests.netcoreapp.cs b/src/System.IO/tests/TextWriter/TextWriterTests.netcoreapp.cs index a44fb5f67f69..80abfbd84c19 100644 --- a/src/System.IO/tests/TextWriter/TextWriterTests.netcoreapp.cs +++ b/src/System.IO/tests/TextWriter/TextWriterTests.netcoreapp.cs @@ -14,10 +14,9 @@ public partial class TextWriterTests [Fact] public void WriteCharSpanTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - var rs = new ReadOnlySpan(chArr, 4, 6); + var rs = new ReadOnlySpan(ChArr, 4, 6); tw.Write(rs); Assert.Equal(new string(rs), tw.Text); } @@ -26,10 +25,9 @@ public void WriteCharSpanTest() [Fact] public void WriteLineCharSpanTest() { - char[] chArr = GetCharArray(); using (CharArrayTextWriter tw = GetTextWriter()) { - var rs = new ReadOnlySpan(chArr, 4, 6); + var rs = new ReadOnlySpan(ChArr, 4, 6); tw.WriteLine(rs); Assert.Equal(new string(rs) + tw.NewLine, tw.Text); }