public void ElementAt() { IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt"); IParsedLine firstLine = file.NextLine(); string lastElement = firstLine.ElementAt <string>(firstLine.Count - 1); Assert.Equal("food", lastElement); IParsedLine peekedSecondLine = file.PeekNextLine(); int number = peekedSecondLine.ElementAt <int>(1); Assert.Equal(100, number); const double modifiedNumber = 100 + Math.PI; peekedSecondLine.Append($" {modifiedNumber}"); IParsedLine secondLine = file.NextLine(); int numberAgain = secondLine.ElementAt <int>(1); Assert.Equal(number, numberAgain); double addedNumber = secondLine.ElementAt <double>(secondLine.Count - 1); Assert.Equal(modifiedNumber, addedNumber, 10); }
public void LineAt() { IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt"); IParsedLine lineAt0 = file.LineAt(0); int n = lineAt0.PeekNextElement <int>(); Assert.Equal(23, n); int nPlusOne = n + 1; lineAt0.Append($" {nPlusOne}"); file.Append(new ParsedLine(new[] { nPlusOne.ToString() })); int totalNumberOfLines = file.Count; IParsedLine firstLine = file.NextLine(); int nAgain = firstLine.NextElement <int>(); string str = firstLine.NextElement <string>(); int incrementedN = firstLine.NextElement <int>(); Assert.Equal(n, nAgain); Assert.Equal(nPlusOne, incrementedN); Assert.Equal("food", str); IParsedLine lastLine = file.LastLine(); Assert.Equal(incrementedN, lastLine.PeekNextElement <int>()); for (int lineIndex = 1; lineIndex < totalNumberOfLines - 1; ++lineIndex) { IParsedLine line = file.NextLine(); int counter = line.NextElement <int>(); for (int j = 0; j < counter; ++j) { line.NextElement <int>(); } while (!line.Empty) { line.NextElement <string>(); } } IParsedLine extraLine = file.NextLine(); Assert.Equal(incrementedN, extraLine.NextElement <int>()); Assert.True(extraLine.Empty); Assert.Throws <ArgumentOutOfRangeException>(() => extraLine.ElementAt <string>(1)); Assert.Throws <InvalidOperationException>(() => extraLine.LastElement <string>()); Assert.True(file.Empty); Assert.Throws <ArgumentOutOfRangeException>(() => file.LineAt(1)); Assert.Throws <InvalidOperationException>(() => file.LastLine()); }