コード例 #1
0
        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)));
            }
        }
コード例 #2
0
        public void WhenNoVisitorChecksTheNodeThrowsUnknownNodeException()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>();
            reader.Visitors = new List <IYamlNodeVisitor>();
            using (var sr = new StringReader("Output: Foo"))
            {
                var exception = Assert.Throws <UnknownNodeException>(() => reader.Parse(sr));
                Assert.Equal("/Output", exception.YamlPath);
            }
        }
コード例 #3
0
        public void YamlReaderWrapsVisitorExcetpionsInYamlProcessingExceptions()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new OutputSpecVisitor(null, null),
            };
            using (var sr = new StringReader("Output: \n  - A: '\"Hello\" + 1'"))
            {
                var exception = Assert.Throws <YamlProcessingException>(() => reader.Parse(sr));
            }
        }
コード例 #4
0
        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)));
            }
        }
コード例 #5
0
        public void DoubleAsteriskMatchesAllSubpathsRecursively()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new NullVisitor("**")
            };
            using (var sr = new StringReader("Import: \n  Line Classes: \n    - Foo: \n        x : Bar\n    - Bar: Foo"))
            {
                var result = reader.Parse(sr);
                //Success if it doesn't throw!
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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));
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
        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));
            }
        }
コード例 #11
0
        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());
            }
        }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
        public void WhenSpecContainsLookups_ThenTheyAreAddedToLookupDictionary()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new LookupVisitor(),
                new LookupMapVisitor()
            };
            using (var sr = new StringReader("Lookups:\n  Foo: \n    Key1: Value1\n    Key2: Value2"))
            {
                var result = reader.Parse(sr);
                Assert.Collection(result.Lookups,
                                  map => Assert.Collection(new SortedDictionary <string, string>(map.Value),
                                                           kv => Assert.Equal(new KeyValuePair <string, string>("Key1", "Value1"), kv),
                                                           kv => Assert.Equal(new KeyValuePair <string, string>("Key2", "Value2"), kv)));
            }
        }
コード例 #14
0
        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));
            }
        }
コード例 #15
0
        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);
            }
        }
コード例 #16
0
        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));
            }
        }
コード例 #17
0
        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()));
            }
        }
コード例 #18
0
        public void YamlSpecsReadFileTypeEnum()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new FileTypeVisitor(new Dictionary <string, IFileType>
                {
                    ["Fixed Width"] = new FixedWidthFileType()
                }),
            };

            using (var sr = new StringReader("Import: \n  File Type: Fixed Width"))
            {
                var result = reader.Parse(sr);
                Assert.Equal("Fixed Width", result.Import.FileType.Format);
            }
        }
コード例 #19
0
        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")));
            }
        }
コード例 #20
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));
        }
コード例 #21
0
        public void GivenFileTypeIsCsv_WhenEncodingIsUTF7_SetEncodingToUTF7()
        {
            var reader = new YamlSpecReader();

            reader.Visitors = new List <IYamlNodeVisitor>
            {
                new ImportSpecVisitor(),
                new FileTypeVisitor(new Dictionary <string, IFileType>
                {
                    ["CSV"] = new CsvFileType()
                }),
                new EncodingVisitor()
            };

            using (var sr = new StringReader("Import:\n  File Type: CSV\n  Encoding: UTF-7"))
            {
                var result   = reader.Parse(sr);
                var fileType = (CsvFileType)result.Import.FileType;
                Assert.Equal("Unicode (UTF-7)", fileType.Encoding.EncodingName);
            }
        }
コード例 #22
0
        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()));
            }
        }
コード例 #23
0
        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));
            }
        }