public void TestLexer() { const String csv = @",OtherText,""Quoted"" """"""QuotedWithQuote"""; var expected = new CsvLexeme[] { new CsvLexeme(CsvSyntaxItem.Delimiter, ","), new CsvLexeme(CsvSyntaxItem.Text, "OtherText"), new CsvLexeme(CsvSyntaxItem.Delimiter, ","), new CsvLexeme(CsvSyntaxItem.Quote, "\""), new CsvLexeme(CsvSyntaxItem.Text, "Quoted"), new CsvLexeme(CsvSyntaxItem.Quote, "\""), new CsvLexeme(CsvSyntaxItem.LineSeparator, "\r\n"), new CsvLexeme(CsvSyntaxItem.Quote, "\""), new CsvLexeme(CsvSyntaxItem.Quote, "\""), new CsvLexeme(CsvSyntaxItem.Quote, "\""), new CsvLexeme(CsvSyntaxItem.Text, "QuotedWithQuote"), new CsvLexeme(CsvSyntaxItem.Quote, "\""), new CsvLexeme(CsvSyntaxItem.EndOfFile, null) }; var lexer = new CsvLexer(new CsvSettings()); var actual = lexer.Scan(csv).ToArray(); Func<CsvLexeme, CsvSyntaxItem> typeSelector = (lexeme => lexeme.Type); Func<CsvLexeme, String> valueSelector = (lexeme => lexeme.Value); CollectionAssert.AreEqual(expected.Select(typeSelector).ToArray(), actual.Select(typeSelector).ToArray()); CollectionAssert.AreEqual(expected.Select(valueSelector).ToArray(), actual.Select(valueSelector).ToArray()); }
public void TestLexerCanReadFileStream() { var lexer = new CsvLexer(new CsvSettings()); var executionPath = new FileInfo(Assembly.GetAssembly(this.GetType()).Location); String sampleFilePath = Path.Combine(executionPath.Directory.FullName, "Sample.csv"); using (var fileStream = File.OpenRead(sampleFilePath)) using (var reader = new StreamReader(fileStream)) { var lexemes = lexer.Scan(reader).ToArray(); int actualTextLexemes = lexemes.Where(i => i.Type == CsvSyntaxItem.Text).Count(); Assert.AreEqual(4, actualTextLexemes, "Different number of values captured."); Assert.AreEqual(CsvSyntaxItem.EndOfFile, lexemes.Last().Type); } }