diff --git a/src/Pretzel.Logic/Extensions/YamlExtensions.cs b/src/Pretzel.Logic/Extensions/YamlExtensions.cs index 761338271..1301ea5f9 100644 --- a/src/Pretzel.Logic/Extensions/YamlExtensions.cs +++ b/src/Pretzel.Logic/Extensions/YamlExtensions.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.IO; using System.Text.RegularExpressions; using YamlDotNet.RepresentationModel; @@ -73,16 +74,24 @@ private static object GetValue(YamlNode value) var list = value as YamlSequenceNode; if (list != null) { - var listResults = new List(); - foreach (var entry in list.Children) - { - var node = entry as YamlScalarNode; - if (node != null) + if (list.Children.All(_ => _ is YamlScalarNode)) { + var listString = new List(); + foreach (var entry in list.Children) + { + var node = entry as YamlScalarNode; + if (node != null) { + listString.Add(node.Value); + } + } + return listString; + } else { + var listResults = new List(); + foreach (var entry in list.Children) { - listResults.Add(node.Value); + listResults.Add(GetValue(entry)); } + return listResults; } - return listResults; } bool valueBool; diff --git a/src/Pretzel.Tests/YamlExtensionsTests.cs b/src/Pretzel.Tests/YamlExtensionsTests.cs index f11c69001..9a9fefdf2 100644 --- a/src/Pretzel.Tests/YamlExtensionsTests.cs +++ b/src/Pretzel.Tests/YamlExtensionsTests.cs @@ -121,6 +121,106 @@ public void YamlHeader_WithSampleData_HandleBoolean() Assert.Equal("True", result["other"].ToString()); } + [Fact] + public void YamlHeader_WithListOfList_CanBeAccessed() { + const string header = @"--- +text: is a simple string +nestedlist: + - [this, is, a] + - [nested, list] +--- + +# Test + +This is a test of YAML parsing"; + var result = header.YamlHeader(); + + Assert.Equal("is a simple string", (string)result["text"]); + + var nestedlist = (List)result["nestedlist"]; + Assert.Equal(2, nestedlist.Count); + var nested0 = (List)nestedlist[0]; + var nested1 = (List)nestedlist[1]; + Assert.Equal(3, nested0.Count); + Assert.Equal(2, nested1.Count); + } + + + [Fact] + public void YamlHeader_WithDictionary_CanBeAccessed() { + const string header = @"--- +text: is a simple string +adictionary: + a_field: true + otherfield: 3 + facts: + - it + - should + - work + inception: + furternested: it is + but_it_works: false + coeff: 1.0 +--- + +# Test + +This is a test of YAML parsing"; + var result = header.YamlHeader(); + + Assert.Equal("is a simple string", (string)result["text"]); + + var adictionary = (Dictionary)result["adictionary"]; + + Assert.Equal(true, (bool)adictionary["a_field"]); + Assert.Equal("3", adictionary["otherfield"]); + Assert.Equal(new []{"it","should", "work"}, (List)adictionary["facts"]); + + var inception = (Dictionary)adictionary["inception"]; + Assert.Equal("it is", (string)inception["furternested"]); + Assert.Equal(false, (bool)inception["but_it_works"]); + Assert.Equal("1.0", inception["coeff"]); + } + + [Fact] + public void YamlHeader_WithListOfDictionary_CanBeAccessed() { + const string header = @"--- +text: is a simple string +adictlist: + - nr: 1 + name: John Doe + skip: false + - nr: 2 + name: John Smith + skip: true + - nr: 3 + name: Lady Lorem + skip: false +--- + +# Test + +This is a test of YAML parsing"; + var result = header.YamlHeader(); + + Assert.Equal("is a simple string", (string)result["text"]); + + var adictlist = (List)result["adictlist"]; + + var items = new [] { + new { nr = "1", name = "John Doe", skip = false }, + new { nr = "2", name = "John Smith", skip = true }, + new { nr = "3", name = "Lady Lorem", skip = false } + }; + + Assert.Equal(items.Length, adictlist.Count); + for(int i=0; i< items.Length; i++){ + var item = (Dictionary)adictlist[i]; + Assert.Equal(items[i].nr, (string)item["nr"]); + Assert.Equal(items[i].name, (string)item["name"]); + Assert.Equal(items[i].skip, (bool)item["skip"]); + } + } } } } \ No newline at end of file