Exemplo n.º 1
0
        public async Task Generate()
        {
            // Arrange
            var mockConfiguration = this.mockRepository.Create <IConfiguration>();
            var mockInputConfig   = this.mockRepository.Create <IInputConfig>();
            var mockOutputConfig  = this.mockRepository.Create <IOutputConfig>();



            //config
            mockInputConfig.Setup(x => x.delimiter).Returns(';');
            mockOutputConfig.Setup(x => x.delimiter).Returns('#');

            //configuration
            mockConfiguration
            .Setup(x => x.input)
            .Returns(() => mockInputConfig.Object);

            mockConfiguration
            .Setup(x => x.output)
            .Returns(() => mockOutputConfig.Object);



            var      helper = new Helper(null, null, null, mockConfiguration.Object);
            ICsvData data   = helper.ConvertData(System.IO.File.ReadAllLines($"{Directory.GetCurrentDirectory()}/files/myCSV.csv"));

            // Act
            var result = await helper.Generate(data);


            // Assert

            //Campos

            var i = 0;

            foreach (var item in data.GetLines().First().Keys)
            {
                var val = result.Data[0].Split(mockOutputConfig.Object.delimiter)[i++];
                Assert.IsTrue(item == val);
            }

            i = 0;

            foreach (var line in data.GetLines())
            {
                i++;
                var j = 0;
                foreach (var item in line)
                {
                    var val = result.Data[i].Split(mockOutputConfig.Object.delimiter)[j++];
                    Assert.IsTrue(item.Value == val);
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IResult> Generate(ICsvData data)
        {
            string[]         lines  = null;
            List <ErrorLine> errors = null;

            if (this.Config == null)
            {
                throw new ArgumentException("Config can not be null");
            }

            foreach (var item in DataRules)
            {
                try
                {
                    await item.Check(data);
                }
                catch (Exception oEx)
                {
                    errors = errors ?? new List <ErrorLine>();
                    errors.Add(new ErrorLine(oEx.Message));
                }
            }

            if (errors == null)
            {
                long i           = 0;
                var  resultLines = new List <ICsvLine>();

                foreach (var line in data.GetLines())
                {
                    foreach (var item in LineRules)
                    {
                        try
                        {
                            await item.Check(line);
                        }
                        catch (Exception oEx)
                        {
                            errors = errors ?? new List <ErrorLine>();
                            errors.Add(new ErrorLine(oEx.Message, i));
                        }
                    }

                    foreach (var item in Actions)
                    {
                        await Task.Run(() =>
                        {
                            try
                            {
                                item(line);
                            }
                            catch (Exception oEx)
                            {
                                errors = errors ?? new List <ErrorLine>();
                                errors.Add(new ErrorLine(oEx.Message, i));
                            }
                        });
                    }

                    i++;
                    resultLines.Add(line);
                }

                lines = DataToString(data.Keys, resultLines, errors, Config.output);
            }


            return(new Result
            {
                Data = lines,
                Errors = errors?.OrderBy(x => x.Index).Select(x => x.ToString()).ToArray()
            });
        }