< Summary

Information
Class: CSVReaderTests.RowParserTests
Assembly: csvreadertests.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\Tests\CSVReaderTests\RowParserTests.cs
Line coverage
99%
Covered lines: 178
Uncovered lines: 1
Coverable lines: 179
Total lines: 383
Line coverage: 99.4%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\Tests\CSVReaderTests\RowParserTests.cs

#LineLine coverage
 1using ValidateLib.TabularData.Parsing;
 2using ValidateLib.Metadata.Descriptors;
 3
 4namespace CSVReaderTests
 5{
 6    public class RowParserTests
 7    {
 8        public readonly string testFilesDirectory;
 19        private DialectDescriptor defaultDialect = new DialectDescriptor();
 10        private Flags defaultFlags;
 111        public RowParserTests()
 112        {
 113            testFilesDirectory = Path.Combine(GetProjectDirectory(), "TestFiles", "RowParserTestFiles");
 114            defaultFlags = FlagsCreator.ExtractFlagsFromDialectDescriptor(defaultDialect);
 115        }
 16        static string GetProjectDirectory()
 117        {
 118            string? currentDirectory = Directory.GetCurrentDirectory();
 19
 120            while (!string.IsNullOrEmpty(currentDirectory))
 121            {
 122                string[] projectFiles = Directory.GetFiles(currentDirectory, "*.csproj");
 23
 124                if (projectFiles.Length > 0)
 125                    return currentDirectory;
 126                currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
 127            }
 28
 029            throw new Exception("Could not find project directory.");
 30
 31
 132        }
 33
 34        [Fact]
 35        public void SimpleTestWithDefaulDialect()
 136        {
 137            int inputCount = 3;
 138            var inputs = new string[]
 139            {
 140                "countryCode,latitude,longitude,name",
 141                "AD,42.546245,1.601554,Andorra",
 142                "AF,33.93911,67.709953,Afghanistan"
 143
 144            };
 45
 146            List<string[]> expectedOutputs = new List<string[]>()
 147            {
 148                new string[]
 149                {
 150                    "countryCode",
 151                    "latitude",
 152                    "longitude",
 153                    "name"
 154                },
 155                new string[]
 156                {
 157                    "AD",
 158                    "42.546245",
 159                    "1.601554",
 160                    "Andorra"
 161                },
 162                new string[]
 163                {
 164                    "AF",
 165                    "33.93911",
 166                    "67.709953",
 167                    "Afghanistan"
 168                }
 169            };
 70
 171            RowParser rowParser = new RowParser(defaultFlags);
 72
 173            for (int inputNumber = 0; inputNumber < inputCount; inputNumber++)
 174            {
 175                var cellValues = rowParser.ParseRow(inputs[inputNumber]);
 176                for (int i = 0 ; i < cellValues.Count; i++)
 177                {
 178                    Assert.Equal(expectedOutputs[inputNumber][i], cellValues[i]);
 179                }
 180            }
 181        }
 82
 83        [Fact]
 84        public void SimpleTestWithDefaulDialectWithQuotedValue()
 185        {
 186            var expectedOutput = new string[]
 187            {
 188
 189                "AE",
 190                "23.424076",
 191                "53.847818",
 192                "United Arab Emirates",
 193            };
 94
 195            string input = "AE,23.424076,53.847818,\"United Arab Emirates\"";
 196            RowParser rowParser = new RowParser(defaultFlags);
 97
 198            List<string> cellValues = rowParser.ParseRow(input);
 99
 1100            for(int i = 0 ; i < cellValues.Count; i++)
 1101            {
 1102                Assert.Equal(expectedOutput[i], cellValues[i]);
 1103            }
 104
 1105        }
 106
 107        [Fact]
 108        public void EscapedEscapedChar()
 1109        {
 1110            var expectedOutput = new string[]
 1111            {
 1112
 1113                "\"Good joke\"",
 1114                "really!"
 1115            };
 116
 1117            string input = "\"\"Good joke\"\",really!";
 1118            RowParser rowParser = new RowParser(defaultFlags);
 119
 1120            List<string> cellValues = rowParser.ParseRow(input);
 121
 1122            for (int i = 0; i < cellValues.Count; i++)
 1123            {
 1124                Assert.Equal(expectedOutput[i], cellValues[i]);
 1125            }
 126
 1127        }
 128
 129        [Fact]
 130        public void EscapedNewlineCharacter()
 1131        {
 1132            var expectedOutput = new string[]
 1133            {
 1134
 1135                "Good joke\nNEWLINE",
 1136                "Really!"
 1137            };
 138
 1139            string input = "Good joke\\\nNEWLINE,Really!";
 1140            var dialect = new DialectDescriptor();
 1141            dialect!.doubleQuote!._value = false;
 1142            var flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(dialect);
 1143            RowParser rowParser = new RowParser(flags);
 144
 1145            List<string> cellValues = rowParser.ParseRow(input);
 146
 1147            for (int i = 0; i < cellValues.Count; i++)
 1148            {
 1149                Assert.Equal(expectedOutput[i], cellValues[i]);
 1150            }
 151
 1152        }
 153
 154
 155        [Fact]
 156        public void TrimStart()
 1157        {
 1158            var expectedOutput = new string[]
 1159            {
 1160
 1161                "TrimStart ",
 1162                "TrimStart\t",
 1163                "TrimStart\n"
 1164            };
 165
 1166            string input = "   TrimStart ,  \t   TrimStart\t,   \n   \t TrimStart\n";
 1167            var dialect = new DialectDescriptor();
 1168            dialect.trim!._value = "start";
 1169            var flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(dialect);
 1170            RowParser rowParser = new RowParser(flags);
 171
 1172            List<string> cellValues = rowParser.ParseRow(input);
 173
 1174            for (int i = 0; i < cellValues.Count; i++)
 1175            {
 1176                Assert.Equal(expectedOutput[i], cellValues[i]);
 1177            }
 178
 1179        }
 180
 181        [Fact]
 182        public void TrimEnd()
 1183        {
 1184            var expectedOutput = new string[]
 1185            {
 1186
 1187                " TrimEnd",
 1188                "\tTrimEnd",
 1189                "\nTrimEnd"
 1190            };
 191
 1192            string input = " TrimEnd      ,\tTrimEnd  \t  ,\nTrimEnd \n  \t ";
 1193            var dialect = new DialectDescriptor();
 1194            dialect.trim!._value = "end";
 1195            var flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(dialect);
 1196            RowParser rowParser = new RowParser(flags);
 197
 1198            List<string> cellValues = rowParser.ParseRow(input);
 199
 1200            for (int i = 0; i < cellValues.Count; i++)
 1201            {
 1202                Assert.Equal(expectedOutput[i], cellValues[i]);
 1203            }
 204
 1205        }
 206
 207        [Fact]
 208        public void QuotedDelimiter()
 1209        {
 1210            var expectedOutput = new string[]
 1211            {
 1212
 1213                "Hey",
 1214                "Quoted,Delimiter",
 1215            };
 216
 1217            string input = "Hey,\"Quoted,Delimiter\"";
 1218            RowParser rowParser = new RowParser(defaultFlags);
 219
 1220            List<string> cellValues = rowParser.ParseRow(input);
 221
 1222            for (int i = 0; i < cellValues.Count; i++)
 1223            {
 1224                Assert.Equal(expectedOutput[i], cellValues[i]);
 1225            }
 226
 1227        }
 228
 229        [Fact]
 230        public void QuotedQuoteChar()
 1231        {
 1232            var expectedOutput = new string[]
 1233            {
 1234                "\"random sentence.\"",
 1235                "Hey",
 1236                "this is \"quoted quote char\" continue",
 1237
 1238            };
 239
 1240            string input = "\"\"\"random sentence.\"\"\",Hey,\"this is \"\"quoted quote char\"\" continue\"";
 1241            RowParser rowParser = new RowParser(defaultFlags);
 242
 1243            List<string> cellValues = rowParser.ParseRow(input);
 244
 1245            for (int i = 0; i < cellValues.Count; i++)
 1246            {
 1247                Assert.Equal(expectedOutput[i], cellValues[i]);
 1248            }
 249
 1250        }
 251
 252        /*
 253        [Fact]
 254        public void SimpleTestWithDefaulDialectWithEscapedChar()
 255        {
 256            var testFilePath = Path.Combine(testFilesDirectory, "escaped.csv");
 257            var expectedLines = new string[]
 258            {
 259                "countryCode,latitude,longitude,name",
 260                "AD,42.546245,1.601554,Andorra",
 261                "AE,23.424076,53.847818,\"United \"\"Arab\"\" Emirates\"",
 262                "AF,33.93911,67.709953,Afghanistan"
 263
 264            };
 265
 266            using (FileStream fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
 267            {
 268                CustomStreamReader csr = new CustomStreamReader(fs, 50, 6);
 269
 270                RowReader rowReader = new RowReader(defaultFlags);
 271                foreach (var line in expectedLines)
 272                {
 273                    Assert.Equal(line, rowReader.ReadRow(csr));
 274                }
 275            }
 276        }
 277
 278        [Fact]
 279        public void TestQuotedNewLineDefaulDialect()
 280        {
 281            var testFilePath = Path.Combine(testFilesDirectory, "newline.csv");
 282            var expectedLines = new string[]
 283            {
 284                "countryCode,latitude,longitude,name",
 285                "AD,42.546245,1.601554,Andorra",
 286                "AE,23.424076,53.847818,\"United\r\n Arab Emirates\"",
 287                "AF,33.93911,67.709953,Afghanistan"
 288
 289            };
 290
 291            using (FileStream fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
 292            {
 293                CustomStreamReader csr = new CustomStreamReader(fs, 50, 6);
 294
 295                RowReader rowReader = new RowReader(defaultFlags);
 296                foreach (var line in expectedLines)
 297                {
 298                    Assert.Equal(line, rowReader.ReadRow(csr));
 299                }
 300            }
 301        }
 302
 303        [Fact]
 304        public void SimpleTestWithDialectDifferentEndOfLine()
 305        {
 306            var testFilePathCsv = Path.Combine(testFilesDirectory, "Dialect", "test01", "countries.csv");
 307            var testFilePathMetadata = Path.Combine(testFilesDirectory, "Dialect", "test01", "countries-metadata.json");
 308
 309            var warnings = new List<Warning>();
 310
 311            var tableDescriptor = MetadataParserValidator.ProcessTable(
 312                warnings,
 313                testFilePathMetadata
 314            );
 315
 316            var expectedLines = new string[]
 317            {
 318                "countryCode,latitude,longitude,name",
 319                "AD,42.546245,1.601554,Andorra",
 320                "AE,23.424076,53.847818,\"United Arab Emirates\"",
 321                "AF,33.93911,67.709953,Afghanistan"
 322
 323            };
 324
 325            using (FileStream fs = new FileStream(testFilePathCsv, FileMode.Open, FileAccess.Read))
 326            {
 327
 328
 329                Flags flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(tableDescriptor.dialect!._value!);
 330                RowReader rowReader = new RowReader(flags);
 331
 332                CustomStreamReader csr = new CustomStreamReader(fs, 50, rowReader.MaximalLineTerminatorLength);
 333                foreach (var line in expectedLines)
 334                {
 335                    Assert.Equal(line, rowReader.ReadRow(csr));
 336                }
 337            }
 338
 339
 340        }
 341
 342
 343        [Fact]
 344        public void SimpleTestWithDialectDifferentQuoteChar()
 345        {
 346            var testFilePathCsv = Path.Combine(testFilesDirectory, "Dialect", "test02", "countries.csv");
 347            var testFilePathMetadata = Path.Combine(testFilesDirectory, "Dialect", "test02", "countries-metadata.json");
 348
 349            var warnings = new List<Warning>();
 350
 351            var tableDescriptor = MetadataParserValidator.ProcessTable(
 352                warnings,
 353                testFilePathMetadata
 354            );
 355
 356            var expectedLines = new string[]
 357            {
 358                "countryCode,latitude,longitude,name",
 359                "AD,42.546245,1.601554,Andorra",
 360                "AE,23.424076,53.847818,QCHUnited\r\n Arab EmiratesQCH",
 361                "AF,33.93911,67.709953,Afghanistan"
 362
 363            };
 364
 365            using (FileStream fs = new FileStream(testFilePathCsv, FileMode.Open, FileAccess.Read))
 366            {
 367
 368
 369                Flags flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(tableDescriptor.dialect!._value!);
 370                RowReader rowReader = new RowReader(flags);
 371
 372                CustomStreamReader csr = new CustomStreamReader(fs, 50, rowReader.MaximalLineTerminatorLength);
 373                foreach (var line in expectedLines)
 374                {
 375                    Assert.Equal(line, rowReader.ReadRow(csr));
 376                }
 377            }
 378
 379
 380        }
 381        */
 382    }
 383}