diff --git a/NBi.Testing.GenbiL/Action/Case/LoadFileOptionalCaseActionTest.cs b/NBi.Testing.GenbiL/Action/Case/LoadFileOptionalCaseActionTest.cs new file mode 100644 index 000000000..432972379 --- /dev/null +++ b/NBi.Testing.GenbiL/Action/Case/LoadFileOptionalCaseActionTest.cs @@ -0,0 +1,38 @@ +using NBi.GenbiL; +using NBi.GenbiL.Action.Case; +using NBi.GenbiL.Stateful; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NBi.Testing.GenbiL.Action.Case +{ + public class LoadFileOptionalCaseActionTest + { + public class LoadOptionalCaseFromFileActionTestable : LoadOptionalCaseFromFileAction + { + public LoadOptionalCaseFromFileActionTestable(string filename, IEnumerable columnNames) + : base(filename, columnNames) { } + + protected override bool IsExistingFile() => false; + } + + [Test] + public void Execute_FileMissing_EmptyDataSetWithExpectedColumns() + { + var state = new GenerationState(); + var action = new LoadOptionalCaseFromFileActionTestable("file.csv", new[] { "foo", "bar" }); + action.Execute(state); + + var caseSet = state.CaseCollection.First().Value; + Assert.That(caseSet.Content.Rows, Has.Count.EqualTo(0)); + Assert.That(caseSet.Content.Columns, Has.Count.EqualTo(2)); + Assert.That(caseSet.Content.Columns.Cast().Select(x => x.ColumnName), Does.Contain("foo")); + Assert.That(caseSet.Content.Columns.Cast().Select(x => x.ColumnName), Does.Contain("bar")); + } + } +} diff --git a/NBi.Testing.GenbiL/NBi.Testing.GenbiL.csproj b/NBi.Testing.GenbiL/NBi.Testing.GenbiL.csproj index ea835a0d3..c23bfcf84 100644 --- a/NBi.Testing.GenbiL/NBi.Testing.GenbiL.csproj +++ b/NBi.Testing.GenbiL/NBi.Testing.GenbiL.csproj @@ -62,6 +62,7 @@ + diff --git a/NBi.Testing.GenbiL/Parser/CaseParserTest.cs b/NBi.Testing.GenbiL/Parser/CaseParserTest.cs index 517c09872..3b8f7bd99 100644 --- a/NBi.Testing.GenbiL/Parser/CaseParserTest.cs +++ b/NBi.Testing.GenbiL/Parser/CaseParserTest.cs @@ -23,6 +23,19 @@ public void SentenceParser_CaseLoadFileString_ValidCaseLoadSentence() Assert.That(((LoadCaseFromFileAction)result).Filename, Is.EqualTo("filename.csv")); } + [Test] + public void SentenceParser_CaseLoadOptionalFileString_ValidCaseLoadOptionalSentence() + { + var input = "case load optional file 'filename.csv' with columns 'foo', 'bar';"; + var result = Case.Parser.Parse(input); + + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.InstanceOf()); + Assert.That(((LoadOptionalCaseFromFileAction)result).Filename, Is.EqualTo("filename.csv")); + Assert.That(((LoadOptionalCaseFromFileAction)result).ColumnNames, Does.Contain("foo")); + Assert.That(((LoadOptionalCaseFromFileAction)result).ColumnNames, Does.Contain("bar")); + } + [Test] public void SentenceParser_CaseLoadQueryFileString_ValidCaseLoadSentence() { diff --git a/NBi.genbiL/Action/Case/LoadCaseFromFileAction.cs b/NBi.genbiL/Action/Case/LoadCaseFromFileAction.cs index 5c675f18d..d2156048f 100644 --- a/NBi.genbiL/Action/Case/LoadCaseFromFileAction.cs +++ b/NBi.genbiL/Action/Case/LoadCaseFromFileAction.cs @@ -15,13 +15,13 @@ public LoadCaseFromFileAction(string filename) public void Execute(GenerationState state) => Execute(state.CaseCollection.CurrentScope); - public void Execute(CaseSet testCases) + public virtual void Execute(CaseSet testCases) { var csvReader = new CsvReader(); testCases.Content = csvReader.ToDataTable(Filename, true); testCases.Content.AcceptChanges(); } - public string Display => $"Loading TestCases from CSV file '{Filename}'"; + public virtual string Display => $"Loading TestCases from CSV file '{Filename}'"; } } diff --git a/NBi.genbiL/Action/Case/LoadOptionalCaseFromFileAction.cs b/NBi.genbiL/Action/Case/LoadOptionalCaseFromFileAction.cs new file mode 100644 index 000000000..8a5690711 --- /dev/null +++ b/NBi.genbiL/Action/Case/LoadOptionalCaseFromFileAction.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using NBi.Core.FlatFile; +using NBi.GenbiL.Stateful; + +namespace NBi.GenbiL.Action.Case +{ + public class LoadOptionalCaseFromFileAction : LoadCaseFromFileAction + { + public IEnumerable ColumnNames { get; set; } + + public LoadOptionalCaseFromFileAction(string filename, IEnumerable columnNames) + : base(filename) => ColumnNames = columnNames; + + public override void Execute(CaseSet testCases) + { + if (IsExistingFile()) + base.Execute(testCases); + else + { + testCases.Content = new DataTable(); + foreach (var columnName in ColumnNames) + testCases.Content.Columns.Add(new DataColumn(columnName, typeof(object))); + testCases.Content.AcceptChanges(); + } + } + + protected virtual bool IsExistingFile() => File.Exists(Filename); + + public override string Display => $"Loading test-cases from CSV file '{Filename}', if missing create empty test-case with following columns {string.Join(", ", ColumnNames)}."; + } +} diff --git a/NBi.genbiL/NBi.genbiL.csproj b/NBi.genbiL/NBi.genbiL.csproj index 86e9aeb76..7ceccf817 100644 --- a/NBi.genbiL/NBi.genbiL.csproj +++ b/NBi.genbiL/NBi.genbiL.csproj @@ -68,6 +68,7 @@ + diff --git a/NBi.genbiL/Parser/Case.cs b/NBi.genbiL/Parser/Case.cs index a56123869..6fd3dfd4e 100644 --- a/NBi.genbiL/Parser/Case.cs +++ b/NBi.genbiL/Parser/Case.cs @@ -29,6 +29,18 @@ from filename in Grammar.QuotedTextual select new LoadCaseFromFileAction(filename) ); + readonly static Parser caseLoadOptionalFileParser = + ( + from load in Keyword.Load + from optional in Keyword.Optional + from loadType in loadTypeFileParser + from filename in Grammar.QuotedTextual + from with in Keyword.With + from columns in Keyword.Columns.Or(Keyword.Column) + from columnNames in Grammar.QuotedRecordSequence + select new LoadOptionalCaseFromFileAction(filename, columnNames) + ); + readonly static Parser caseLoadQueryFileParser = ( from load in Keyword.Load @@ -50,7 +62,7 @@ from connectionString in Grammar.QuotedTextual ); readonly static Parser caseLoadParser = - caseLoadFileParser.Or(caseLoadQueryFileParser).Or(caseLoadQueryParser); + caseLoadFileParser.Or(caseLoadOptionalFileParser).Or(caseLoadQueryFileParser).Or(caseLoadQueryParser); readonly static Parser caseRemoveParser = ( @@ -105,6 +117,7 @@ from text in Grammar.ExtendedQuotedRecordSequence select new FilterCaseAction(variableName, @operator, text, negation.IsDefined) ); + readonly static Parser caseScopeParser = ( from scope in Keyword.Scope diff --git a/NBi.genbiL/Parser/Keyword.cs b/NBi.genbiL/Parser/Keyword.cs index f5d634f67..564c126e6 100644 --- a/NBi.genbiL/Parser/Keyword.cs +++ b/NBi.genbiL/Parser/Keyword.cs @@ -62,5 +62,6 @@ class Keyword public static readonly Parser Left = Parse.IgnoreCase("Left").Text().Token(); public static readonly Parser Right = Parse.IgnoreCase("Right").Text().Token(); public static readonly Parser All = Parse.IgnoreCase("All").Text().Token(); + public static readonly Parser Optional = Parse.IgnoreCase("Optional").Text().Token(); } }