Skip to content

Commit

Permalink
Merge branch 'feature/list_of_values_when_settling_local-variable_val…
Browse files Browse the repository at this point in the history
…ues_(#434)' into develop
  • Loading branch information
Cédric L. Charlier committed Jan 6, 2019
2 parents 177d6ff + 8e1a5f6 commit abcfedf
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 26 deletions.
10 changes: 7 additions & 3 deletions NBi.Core/Scalar/Resolver/CSharpScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public static object Function()
string finalCode = string.Format(codeTemplate, code);

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
CompilerParameters parameters = new CompilerParameters()
{
GenerateInMemory = true,
GenerateExecutable = false,
};

CompilerResults results = provider.CompileAssemblyFromSource(parameters, finalCode);

Expand All @@ -75,5 +77,7 @@ public static object Function()
Type function = results.CompiledAssembly.GetType("NBi.Core.Variable.Dynamic.VariableClass");
return function.GetMethod("Function");
}

object IScalarResolver.Execute() => Execute();
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Scalar/Resolver/EnvironmentScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public T Execute()
var value = Environment.GetEnvironmentVariable(args.Name, EnvironmentVariableTarget.User);
return (T)Convert.ChangeType(value, typeof(T));
}

object IScalarResolver.Execute() => Execute();
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Scalar/Resolver/FormatScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ public string Execute()
var value = formatter.Execute(args.Text);
return value;
}

object IScalarResolver.Execute() => Execute();
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Scalar/Resolver/GlobalVariableScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ public T Execute()

return (T)output;
}

object IScalarResolver.Execute() => Execute();
}
}
9 changes: 7 additions & 2 deletions NBi.Core/Scalar/Resolver/IScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

namespace NBi.Core.Scalar.Resolver
{
public interface IScalarResolver<T>
public interface IScalarResolver
{
T Execute();
object Execute();
}

public interface IScalarResolver<T> : IScalarResolver
{
new T Execute();
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Scalar/Resolver/LiteralScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public T Execute()
Trace.WriteLineIf(Extensibility.NBiTraceSwitch.TraceVerbose, $"Literal evaluated to: {output}");
return (T)output;
}

object IScalarResolver.Execute() => Execute();
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Scalar/Resolver/ProjectionResultSetScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public T Execute()

return (T)Convert.ChangeType(projectionResult, typeof(T));
}

object IScalarResolver.Execute() => Execute();
}
}
2 changes: 2 additions & 0 deletions NBi.Core/Scalar/Resolver/QueryScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ public T Execute()
var value = ExecuteQuery(cmd);
return (T)Convert.ChangeType(value, typeof(T));
}

object IScalarResolver.Execute() => Execute();
}
}
13 changes: 10 additions & 3 deletions NBi.Core/Sequence/Resolver/ListSequenceResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NBi.Core.Scalar.Resolver;
using NBi.Core.Scalar.Caster;
using NBi.Core.Scalar.Resolver;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -24,8 +25,14 @@ public ListSequenceResolver(ListSequenceResolverArgs args)
public List<T> Execute()
{
var list = new List<T>();
foreach (var arg in args.Objects)
list.Add(new LiteralScalarResolver<T>(arg).Execute());
foreach (var resolver in args.Resolvers)
{
var obj = resolver.Execute();
var caster = new CasterFactory<T>().Instantiate();
var value = caster.Execute(obj);
list.Add(value);
}

return list;
}
}
Expand Down
9 changes: 5 additions & 4 deletions NBi.Core/Sequence/Resolver/ListSequenceResolverArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NBi.Core.Variable;
using NBi.Core.Scalar.Resolver;
using NBi.Core.Variable;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,11 +10,11 @@ namespace NBi.Core.Sequence.Resolver
{
public class ListSequenceResolverArgs : ISequenceResolverArgs
{
public IEnumerable<object> Objects { get; }
public IEnumerable<IScalarResolver> Resolvers { get; }

public ListSequenceResolverArgs(IEnumerable<object> objects)
public ListSequenceResolverArgs(IEnumerable<IScalarResolver> resolvers)
{
this.Objects = objects;
Resolvers = resolvers;
}
}
}
20 changes: 20 additions & 0 deletions NBi.Testing/Unit/Xml/Resources/LocalVariableXmlTestSuite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@
</no-rows>
</assert>
</test>
<test name="Instances are defined through a single variable" uid="0001">
<instance-settling>
<local-variable name="season" type="text">
<item>Spring</item>
<item>Summer</item>
<item>Fall</item>
<item>Winter</item>
</local-variable>
</instance-settling>
<system-under-test>
<resultSet file="~MyData_{@firstDayOfMonth:yyyy}_{@firstDayOfMonth:MM}.csv"/>
</system-under-test>
<assert>
<no-rows>
<predicate name="#1">
<null/>
</predicate>
</no-rows>
</assert>
</test>
</testSuite>
78 changes: 64 additions & 14 deletions NBi.Testing/Unit/Xml/Variables/LocalVariableXmlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ protected TestSuiteXml DeserializeSample()

using (Stream stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("NBi.Testing.Unit.Xml.Resources.LocalVariableXmlTestSuite.xml"))
using (StreamReader reader = new StreamReader(stream))
manager.Read(reader);
using (StreamReader reader = new StreamReader(stream))
manager.Read(reader);

return manager.TestSuite;
}
Expand Down Expand Up @@ -63,18 +63,68 @@ public void Serialize_InstanceSetting_LocalVariableCorrectlySerialized()
};

var serializer = new XmlSerializer(typeof(InstanceSettlingXml));
var stream = new MemoryStream();
var writer = new StreamWriter(stream, Encoding.UTF8);
serializer.Serialize(writer, instanceSetting);
var content = Encoding.UTF8.GetString(stream.ToArray());
writer.Close();
stream.Close();

Debug.WriteLine(content);

Assert.That(content, Is.StringContaining("<local-variable"));
Assert.That(content, Is.StringContaining("name=\"firstOfMonth\""));
Assert.That(content, Is.StringContaining("type=\"dateTime\""));
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
serializer.Serialize(writer, instanceSetting);
var content = Encoding.UTF8.GetString(stream.ToArray());

Debug.WriteLine(content);

Assert.That(content, Is.StringContaining("<local-variable"));
Assert.That(content, Is.StringContaining("name=\"firstOfMonth\""));
Assert.That(content, Is.StringContaining("type=\"dateTime\""));
Assert.That(content, Is.Not.StringContaining("<item"));
}
}

[Test]
public void Deserialize_Items_ListOfItems()
{
TestSuiteXml ts = DeserializeSample();
var variable = ts.Tests[1].InstanceSettling.Variable as InstanceVariableXml;

// Check the properties of the object.
Assert.That(variable.Items, Is.Not.Null);
Assert.That(variable.Items, Has.Count.EqualTo(4));
Assert.That(variable.Items, Has.Member("Spring"));
Assert.That(variable.Items, Has.Member("Summer"));
Assert.That(variable.Items, Has.Member("Fall"));
Assert.That(variable.Items, Has.Member("Winter"));
}

[Test]
public void Serialize_Items_ItemCorrectlySerialized()
{
var root = new InstanceSettlingXml()
{
Variable = new InstanceVariableXml()
{
Name = "season",
Type = ColumnType.Text,
Items = new List<string>() { "Spring", "Summer", "Fall", "Winter" }
}
};

var serializer = new XmlSerializer(root.GetType());
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
serializer.Serialize(writer, root);
var content = Encoding.UTF8.GetString(stream.ToArray());

Debug.WriteLine(content);

Assert.That(content, Is.StringContaining("<local-variable"));
Assert.That(content, Is.StringContaining("name=\"season\""));
Assert.That(content, Is.StringContaining("type=\"text\""));
Assert.That(content, Is.Not.StringContaining("loop"));
Assert.That(content, Is.StringContaining("<item"));
Assert.That(content, Is.StringContaining(">Spring<"));
Assert.That(content, Is.StringContaining(">Summer<"));
Assert.That(content, Is.StringContaining(">Fall<"));
Assert.That(content, Is.StringContaining(">Winter<"));
}
}
}
}
1 change: 1 addition & 0 deletions NBi.Xml/Schema/BaseType.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@
<xs:complexType name="local-variable-type">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element name="loop-sentinel" type="loop-sentinel-type"/>
<xs:element name="item" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="type" type="column-type-enum"/>
Expand Down
6 changes: 6 additions & 0 deletions NBi.Xml/Variables/InstanceVariableXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ public class InstanceVariableXml

[XmlElement("loop-sentinel")]
public SentinelLoopXml SentinelLoop { get; set; }

[XmlElement("item")]
public List<string> Items { get; set; } = new List<string>();

[XmlIgnore]
public bool ItemsSpecified { get => Items.Count > 0; set { } }
}
}

0 comments on commit abcfedf

Please sign in to comment.