示例#1
0
        public void CsvFileReaderWriter_ReadTest()
        {
            var csvText = "1;2;3\n\"1;2\";\"1;2\n";
            var reader  = new StringReader(csvText);
            var target  = new CsvFileReaderWriter(
                '\n',
                ';',
                '"',
                (i, j, txt) => txt.Trim(new[] { '"' }));
            var tabularItem = new TabularListsItem();

            target.Read(tabularItem, reader);

            // Número de linhas
            Assert.AreEqual(2, tabularItem.Count);

            // Primeira linha
            var row = tabularItem[0];

            Assert.AreEqual(3, row.Count);
            Assert.AreEqual("1", row[0].GetCellValue <string>());
            Assert.AreEqual("2", row[1].GetCellValue <string>());
            Assert.AreEqual("3", row[2].GetCellValue <string>());

            // Segunda linha
            row = tabularItem[1];
            Assert.AreEqual(2, row.Count);
            Assert.AreEqual("1;2", row[0].GetCellValue <string>());
            Assert.AreEqual("1;2\n", row[1].GetCellValue <string>());

            // Aplicação de validações sem validar dados existentes.
            var validation = new FuncDrivenColumnValudation <object>();

            validation.RegisterValidator(
                0,
                v => { if (v is int)
                       {
                           return(true);
                       }
                       else
                       {
                           return(false);
                       } });
            tabularItem.AddValidation(validation, false);

            // Aplicação de validações com dados existentes
            validation = new FuncDrivenColumnValudation <object>();
            validation.RegisterValidator(
                0,
                v => { return(v is string); });
            tabularItem.AddValidation(validation, true);
        }
示例#2
0
        public void TabularItem_ValidationPassTest()
        {
            var csvText       = "1;2\n3;4\n5;6";
            var reader        = new StringReader(csvText);
            var csvFileReader = new CsvFileReaderWriter(
                '\n',
                ';',
                '"',
                (i, j, txt) => int.Parse(txt.Trim(new[] { '"' })));
            var tabularItem               = new TabularListsItem();
            var dataValidation            = new FuncDrivenColumnValudation <object>();
            Func <object, bool> validator = o => o is int;

            dataValidation.RegisterValidator(0, validator);
            dataValidation.RegisterValidator(1, validator);
            csvFileReader.Read(tabularItem, reader);
        }
        public void RunTest_TabularItem()
        {
            var csvStringBuilder = new StringBuilder();

            csvStringBuilder.AppendLine("12, 9,7,6,4");
            csvStringBuilder.AppendLine("12,11,8,7,5");
            csvStringBuilder.AppendLine("13,10,9,6,3");
            csvStringBuilder.Append("12,8,6,4,2");
            var csvString       = csvStringBuilder.ToString();
            var csvStringReader = new StringReader(csvString);

            var parser    = new IntegerParser <string>();
            var csvParser = new CsvFileParser <ITabularItem, int, string, string>(
                "new_line",
                "comma",
                (i, j) => parser);

            csvParser.AddIgnoreType("space");
            csvParser.AddIgnoreType("carriage_return");

            var csvSymbolReader = new StringSymbolReader(csvStringReader, true, false);
            var adder           = new TabularItemAdder <int>();
            var tabularItem     = new TabularListsItem();

            csvParser.Parse(csvSymbolReader, tabularItem, adder);

            var integerDomain        = new IntegerDomain();
            var numberTdecomposition = new IntegerMinWeightTdecomposition <int>(
                Comparer <int> .Default,
                integerDomain);

            var result          = numberTdecomposition.Run(18, tabularItem);
            var expectedMedians = new List <int>()
            {
                3, 5, 5, 5
            };

            Assert.AreEqual(17, result.Cost);
            CollectionAssert.AreEquivalent(expectedMedians, result.Medians.ToList());
        }
示例#4
0
        public void TabularItem_ValidationChangeCellValueFailTest()
        {
            var csvText       = "1;2\n3;4\n5;6";
            var reader        = new StringReader(csvText);
            var csvFileReader = new CsvFileReaderWriter(
                '\n',
                ';',
                '"',
                (i, j, txt) => int.Parse(txt.Trim(new[] { '"' })));
            var tabularItem = new TabularListsItem();

            csvFileReader.Read(tabularItem, reader);
            Func <object, bool> validator = o => o is int;
            var dataValidation            = new FuncDrivenColumnValudation <object>();

            dataValidation.RegisterValidator(0, validator);
            dataValidation.RegisterValidator(1, validator);
            tabularItem.AddValidation(dataValidation, false);

            // Tenta alterar a célula para um valor não inteiro
            tabularItem[0][0].SetCellValue <string>("1");
        }