public void YamlSpecsReadOutputFieldFormulaeWithFieldsExtractedFromLineClassesCaseInsensitively()
        {
            var reader = new YamlSpecReader();
            var parser = new FilterParser();

            parser.AddInitialValueFactory("Column", s => new ColumnInitialValue(s[0]));
            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new ExtractVisitor(),
                new ExtractableValuesVisitor(parser),
                new OutputSpecVisitor(null, null),
            };
            using (var sr = new StringReader("Import: \n  Line Classes:\n    - Foo:\n        Extract: { Fred: Column A, Bob: Column B }\nOutput: \n  - A: FreD + bob"))
            {
                var result     = reader.Parse(sr);
                var parameters = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase)
                {
                    ["Fred"] = "Foo",
                    ["Bob"]  = "Bar"
                };
                Assert.Collection(result.Output.Fields, x => Assert.Equal("FooBar", (string)x.OutputFormula(parameters)));
            }
        }
        public void YamlSpecsReadOutputFieldFormulae()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new OutputSpecVisitor(null, null),
            };
            using (var sr = new StringReader("Output: \n  - A: 1 + 1"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(result.Output.Fields, x => Assert.Equal(2m, (decimal)x.OutputFormula(null)));
            }
        }
        public void YamlSpecsReadOutputFieldsInOrder()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new OutputSpecVisitor(null, null),
            };
            using (var sr = new StringReader("Output: \n  - B: 1\n  - A: 3"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(result.Output.Fields, x => Assert.Equal("B", x.Name),
                                  x => Assert.Equal("A", x.Name));
            }
        }
        public void DoubleAsteriskDoesntMatchesThingsNotInTheSubpath()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new NullVisitor("/Import"),
                new NullVisitor("/Import/**")
            };
            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n      x : Bar\n    - Bar: Foo\nOutput: Boom!"))
            {
                var ex = Assert.Throws <UnknownNodeException>(() => reader.Parse(sr));
                Assert.Equal("/Output", ex.YamlPath);
            }
        }
        public void YamlSpecsReadImportObjectsAsMappings()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new NullVisitor("/Import/Foo")
            };
            using (var sr = new StringReader("Import: \n  Foo: Bar"))
            {
                var spec = reader.Parse(sr);
                Assert.NotNull(spec);
                Assert.NotNull(spec.Import);
            }
        }
        public void YamlSpecsReadOutputObjectsAsMappings()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new OutputSpecVisitor(null, null),
                new NullVisitor("/Output/Foo")
            };
            using (var sr = new StringReader("Output: \n  - Foo: 1"))
            {
                var spec = reader.Parse(sr);
                Assert.NotNull(spec);
                Assert.NotNull(spec.Output);
            }
        }
        public void YamlSpecsReadOutputRowAsBoolean()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new OutputRowVisitor(),
            };
            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n        Output Row: true"))
            {
                var result = reader.Parse(sr);
                Assert.True(result.Import.LineClasses[0].OutputLine);
            }
        }
        public void YamlSpecReadsMatchSheetsScalarAsRegularExpression()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new MatchSheetsVisitor(),
            };
            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n        Matching Sheets: .*"))
            {
                var result = reader.Parse(sr);
                Assert.Equal(".*", result.Import.LineClasses[0].MatchingSheets.ToString());
            }
        }
        public void YamlSpecsReadMatchingSection()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new MatchingSectionVisitor(),
            };
            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n        Matching Section: Monkey"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(result.Import.LineClasses, x => Assert.Equal("Monkey", x.MatchingSection));
            }
        }
        public void YamlSpecsReadMatchExpressionScalarAsSingleCellMatchWithColumnIndex0()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new MatchVisitor(),
            };
            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n          Match: .*"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(result.Import.LineClasses[0].MatchExpressions, x => Assert.Equal(0, x.ColumnIndex));
                Assert.Collection(result.Import.LineClasses[0].MatchExpressions, x => Assert.Equal(".*", x.Expression.ToString()));
            }
        }
        public void YamlSpecReadsLineClassesAsOrderedList()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new NullVisitor("**")
            };

            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n        x: Bar\n    - Bar:\n        x: Foo"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(result.Import.LineClasses, x => Assert.Equal("Foo", x.Name), x => Assert.Equal("Bar", x.Name));
            }
        }
        public void YamlSpecsReadQualifierAsChar()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new FileTypeVisitor(new Dictionary <string, IFileType> {
                    ["CSV"] = new CsvFileType()
                }),
                new QualifierVisitor()
            };
            using (var sr = new StringReader("Import: \n  File Type: CSV\n  Delimited Field Qualifier: \"\\\"\""))
            {
                var result = reader.Parse(sr);
                Assert.Equal('"', ((CsvFileType)result.Import.FileType).Qualifier);
            }
        }
        public void WhenOutputFieldNotPresentInLineClassesOrSpecialFields_Parse_Throws_Exception()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new ExtractVisitor(),
                new ExtractableValuesVisitor(new FilterParser()),
                new OutputSpecVisitor(null, null),
            };
            using (var sr = new StringReader("Import: \n  Line Classes:\n    - Foo:\n        Extract: { Fred: Column A, Bob: Column B }\nOutput: \n  - A: Steve"))
            {
                Assert.Throws <YamlProcessingException>(() => reader.Parse(sr));
            }
        }
        public void SheetMatchExpressionsAreCreatedCaseInsensitive()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new FileTypeVisitor(new Dictionary <string, IFileType> {
                    ["Excel"] = new ExcelFileType()
                }),
                new SheetsVisitor(),
                new SheetSequenceVisitor()
            };
            using (var sr = new StringReader("Import: \n  File Type: Excel\n  Sheets: [Sheet1]"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(((ExcelFileType)result.Import.FileType).SheetMatchExpressions,
                                  x => Assert.True(x.IsMatch("sheet1")));
            }
        }
Exemple #15
0
        public Spec Parse(TextReader yamlSpec)
        {
            var filterParser = new FilterParser();

            foreach (var initialValueKV in SupportedInitialValues)
            {
                filterParser.AddInitialValueFactory(initialValueKV.Key, initialValueKV.Value);
            }
            foreach (var filterKV in SupportedFilters)
            {
                filterParser.AddFilterFactory(filterKV.Key, filterKV.Value);
            }
            var reader = new YamlSpecReader()
            {
                Visitors = new List <IYamlNodeVisitor>
                {
                    new ImportSpecVisitor(),
                    new FileTypeVisitor(SupportedFileTypes),
                    new EncodingVisitor(),
                    new SeparatorVisitor(),
                    new QualifierVisitor(),
                    new SheetsVisitor(),
                    new SheetSequenceVisitor(),
                    new LookupVisitor(),
                    new LookupMapVisitor(),
                    new LineClassVisitor(),
                    new IndividualLineClassVisitor(),
                    new OutputRowVisitor(),
                    new MatchVisitor(),
                    new MatchCellsVisitor(),
                    new MatchSheetsVisitor(),
                    new SectionVisitor(),
                    new MatchingSectionVisitor(),
                    new ExtractVisitor(),
                    new ExtractableValuesVisitor(filterParser),
                    new OutputSpecVisitor(SupportedFormulaFunctions.ToArray(), SupportedVariables)
                }
            };

            return(reader.Parse(yamlSpec));
        }
        public void YamlSpecsReadSheetsAsArrayOfRegularExpressions()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new FileTypeVisitor(new Dictionary <string, IFileType> {
                    ["Excel"] = new ExcelFileType()
                }),
                new SheetsVisitor(),
                new SheetSequenceVisitor()
            };
            using (var sr = new StringReader("Import: \n  File Type: Excel\n  Sheets: [Sheet1, Sheet2, .*]"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(((ExcelFileType)result.Import.FileType).SheetMatchExpressions,
                                  x => Assert.Equal("Sheet1", x.ToString()),
                                  x => Assert.Equal("Sheet2", x.ToString()),
                                  x => Assert.Equal(".*", x.ToString()));
            }
        }
        public void YamlSpecsReadFieldExtractionsWithFilters()
        {
            var reader = new YamlSpecReader();
            var parser = new FilterParser();

            parser.AddFilterFactory("Trim", (s) => new TrimFilter());
            parser.AddInitialValueFactory("Column", s => new ColumnInitialValue(s[0]));
            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new LineClassVisitor(),
                new IndividualLineClassVisitor(),
                new ExtractVisitor(),
                new ExtractableValuesVisitor(parser)
            };
            using (var sr = new StringReader("Import: \n  Line Classes:\n    - Foo:\n        Extract: { Fred: Column B | Trim }"))
            {
                var result = reader.Parse(sr);
                var values = result.Import.LineClasses[0].ValuesToExtract;
                Assert.Collection(values, x => Assert.Equal("Fred", x.Name));
                Assert.Equal(1, ((ColumnInitialValue)values[0].InitialValue).Column);
                Assert.Collection(values[0].Filters, x => Assert.IsType <TrimFilter>(x));
            }
        }