Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Pretzel.Logic/Extensions/YamlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using YamlDotNet.RepresentationModel;
Expand Down Expand Up @@ -73,16 +74,24 @@ private static object GetValue(YamlNode value)
var list = value as YamlSequenceNode;
if (list != null)
{
var listResults = new List<string>();
foreach (var entry in list.Children)
{
var node = entry as YamlScalarNode;
if (node != null)
if (list.Children.All(_ => _ is YamlScalarNode)) {
var listString = new List<string>();
foreach (var entry in list.Children)
{
var node = entry as YamlScalarNode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to avoid the double cast (is / as YamlScalarNode)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is: If I'd project the items with as and filter nulls, and compare the length of the lists. This is more readble in my opinion.

if (node != null) {
listString.Add(node.Value);
}
}
return listString;
} else {
var listResults = new List<object>();
foreach (var entry in list.Children)
{
listResults.Add(node.Value);
listResults.Add(GetValue(entry));
}
return listResults;
}
return listResults;
}

bool valueBool;
Expand Down
100 changes: 100 additions & 0 deletions src/Pretzel.Tests/YamlExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>)result["nestedlist"];
Assert.Equal(2, nestedlist.Count);
var nested0 = (List<string>)nestedlist[0];
var nested1 = (List<string>)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<string,object>)result["adictionary"];

Assert.Equal(true, (bool)adictionary["a_field"]);
Assert.Equal("3", adictionary["otherfield"]);
Assert.Equal(new []{"it","should", "work"}, (List<string>)adictionary["facts"]);

var inception = (Dictionary<string, object>)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<object>)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<string,object>)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"]);
}
}
}
}
}