コード例 #1
0
        public void WriteCsv()
        {
            // Arrange
            var helper = new Helper();

            string delimiter = ",";

            var data = new CsvData();

            data.Add("col1", new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H"
            });
            data.Add("col2", new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H"
            });
            data.Add("col3", new List <string>()
            {
                "A", "B", "C", "D", "E", "F", "G", "H"
            });

            // Act
            var result = helper.WriteCsv(
                data,
                delimiter);

            // Assert
            Assert.Contains(",", result[0]);
            Assert.True(result[0].Split(",").Count() == 3);
            Assert.True(result.Count() == 9);
        }
コード例 #2
0
        public void GetLines()
        {
            // Arrange
            var csvData = new CsvData();
            var aListA  = new List <string>()
            {
                "A1", "A2", "A3", "A4"
            };
            var aListB = new List <string>()
            {
                "B1", "B2", "B3", "B4"
            };

            csvData.Add("A", aListA);
            csvData.Add("B", aListB);

            // Act
            var result = csvData.GetLines();


            // Assert
            for (int i = 0; i < aListA.Count; i++)
            {
                Assert.Equal(aListA[i], result.ToList()[i]["A"]);
                Assert.Equal(aListB[i], result.ToList()[i]["B"]);
            }
        }
コード例 #3
0
        public async Task CountTest()
        {
            // Arrange
            var fieldsCountRule = this.CreateFieldsCountRule();

            var csvDataOk = new CsvData();
            var aListA    = new List <string>()
            {
                "A1", "A2", "A3", "A4"
            };
            var aListB = new List <string>()
            {
                "B1", "B2", "B3", "B4"
            };

            csvDataOk.Add("A", aListA);
            csvDataOk.Add("B", aListB);

            var csvDataWrong = new CsvData();
            var aListC       = new List <string>()
            {
                "C1"
            };

            csvDataWrong.Add("A", aListA);
            csvDataWrong.Add("B", aListB);
            csvDataWrong.Add("C", aListC);



            // Assert
            Assert.IsTrue(await fieldsCountRule.Check(csvDataOk));
            await Assert.ThrowsExceptionAsync <FileFormatException>(() => fieldsCountRule.Check(csvDataWrong));
        }
コード例 #4
0
        public void Replace()
        {
            // Arrange
            IEnumerable <ITupleReplace> replaces = new List <ITupleReplace>()
            {
                CreateReplace("A", "Z"),
                CreateReplace("B", "V", "B"),
            };

            var csvDataOk = new CsvData();
            var aListA    = new List <string>()
            {
                "A1", "A2", "A3", "A4"
            };
            var aListB = new List <string>()
            {
                "AB1", "AB2", "AB3", "AB4"
            };

            csvDataOk.Add("A", aListA);
            csvDataOk.Add("B", aListB);
            var lines = csvDataOk.GetLines();

            // Act
            var result = CsvActionCreator.Create(
                replaces);



            // Assert
            foreach (var item in result)
            {
                lines.ToList().ForEach(item);
            }

            lines.ToList().ForEach(
                x =>
            {
                Assert.IsTrue(x["A"].Contains("Z"));
                Assert.IsTrue(!x["A"].Contains("A"));
                Assert.IsTrue(x["B"].Contains("Z"));
                Assert.IsTrue(!x["B"].Contains("A"));

                Assert.IsTrue(x["B"].Contains("V"));
                Assert.IsTrue(!x["B"].Contains("B"));
            }
                );
        }
コード例 #5
0
        public void Types()
        {
            // Arrange
            IEnumerable <ITypeColumn> types = new List <ITypeColumn>()
            {
                CreateType(ETypeColumn.text, "Z"),
                CreateType(ETypeColumn.number, "B"),
                CreateType(ETypeColumn.number, "C")
            };

            var csvDataOk = new CsvData();
            var aListA    = new List <string>()
            {
                "A1", "A2", "A3", "A4"
            };
            var aListB = new List <string>()
            {
                "1", "2", "3", "4"
            };
            var aListC = new List <string>()
            {
                "c1", "c2", "c3", "c4"
            };

            csvDataOk.Add("A", aListA);
            csvDataOk.Add("B", aListB);
            csvDataOk.Add("C", aListC);

            var lines = csvDataOk.GetLines();

            // Act
            var result = CsvActionCreator.Create(types).ToList();


            foreach (var item in lines)
            {
                result[0](item);
                result[1](item);
                Assert.ThrowsException <FormatException>(() => result[2](item));
            }

            // Assert
        }
コード例 #6
0
        public async Task NameTest()
        {
            // Arrange
            var fieldsNameRule = new FieldsNameRule();

            // Act
            var csvDataOk = new CsvData();
            var aListA    = new List <string>()
            {
                "A1", "A2", "A3", "A4"
            };
            var aListB = new List <string>()
            {
                "B1", "B2", "B3", "B4"
            };

            csvDataOk.Add("A", aListA);
            csvDataOk.Add("B", aListB);


            // Assert
            Assert.IsTrue(await fieldsNameRule.Check(csvDataOk));
        }
コード例 #7
0
        public static IEnumerable <ICsvLine> AddIntLine(this IEnumerable <ICsvLine> lines, int?repeatValue = null)
        {
            var csvData = new CsvData();
            var aListA  = repeatValue == null ? new List <string>()
            {
                "1", "2", "3", "4"
            } : new List <string>()
            {
                repeatValue.ToString(), repeatValue.ToString(), repeatValue.ToString(), repeatValue.ToString()
            };

            csvData.Add("int", aListA);


            lines = lines.Append(csvData.GetLines().First());
            return(lines);
        }
コード例 #8
0
        public static IEnumerable <ICsvLine> AddStringLine(this IEnumerable <ICsvLine> lines, string repeatValue = null)
        {
            var csvData = new CsvData();
            var aListA  = repeatValue == null ? new List <string>()
            {
                "S1", "S2", "S3", "S4"
            } : new List <string>()
            {
                repeatValue, repeatValue, repeatValue, repeatValue
            };

            csvData.Add("string", aListA);


            lines = lines.Append(csvData.GetLines().First());
            return(lines);
        }