Skip to content

Commit

Permalink
Merge branch 'release/v1.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalpen committed Aug 29, 2022
2 parents 7219866 + 8c05a2e commit f5ed997
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 6 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# http://www.appveyor.com/docs/appveyor-yml

environment:
base_version: 1.2.2
base_version: 1.2.3

version: $(base_version).{build}

Expand Down
2 changes: 1 addition & 1 deletion build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Build : NukeBuild
AbsolutePath TestsDirectory => RootDirectory / "Tests";

[Parameter("Version to be injected in the Build")]
public string Version { get; set; } = $"1.2.2";
public string Version { get; set; } = $"1.2.3";

[Parameter("The Buildnumber provided by the CI")]
public string BuildNo = "2";
Expand Down
137 changes: 137 additions & 0 deletions src/Tests/YamlMap.Tests/Reader/ParseQuotatedTypeStringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace YamlMap.Tests.Reader
{
[TestFixture]
public class ParseQuotatedTypeStringTests
{
[Test]
public void ParseQuotatedTypeString_DoubleQuotations()
{
var lines = new[]
{
"Value: \"[value] test\""
};

var reader = new YamlReader();
var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues;

Assert.AreEqual("[value] test", parsed.Value);
}

[Test]
public void ParseQuotatedTypeString_SingleQuotations()
{
var lines = new[]
{
"Value: '[value] test'"
};

var reader = new YamlReader();
var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues;

Assert.AreEqual("[value] test", parsed.Value);
}

[Test]
public void ParseQuotatedTypeString_DoubleQuotations_InnerSingle()
{
var lines = new[]
{
"Value: \"[value] 'test\""
};

var reader = new YamlReader();
var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues;

Assert.AreEqual("[value] 'test", parsed.Value);
}

[Test]
public void ParseQuotatedTypeString_SingleQuotations_InnerDouble()
{
var lines = new[]
{
"Value: '[value] \"test'"
};

var reader = new YamlReader();
var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues;

Assert.AreEqual("[value] \"test", parsed.Value);
}

[Test]
public void ParseQuotatedTypeString_DoubleQuotations_NoEnd()
{
var lines = new[]
{
"Value: \"[value] test"
};

var reader = new YamlReader();
Assert.Throws<InvalidConfigurationException>(() => reader.Read(typeof(PrimitiveValues), lines));
}

[Test]
public void ParseQuotatedTypeString_SingleQuotations_NoEnd()
{
var lines = new[]
{
"Value: '[value] test"
};

var reader = new YamlReader();
Assert.Throws<InvalidConfigurationException>(() => reader.Read(typeof(PrimitiveValues), lines));
}

[Test]
public void ParseQuotatedTypeString_DoubleQuotations_EndInString()
{
var lines = new[]
{
"Value: \"[value] t\"est"
};

var reader = new YamlReader();
Assert.Throws<InvalidConfigurationException>(() => reader.Read(typeof(PrimitiveValues), lines));
}

[Test]
public void ParseQuotatedTypeString_SingleQuotations_EndInString()
{
var lines = new[]
{
"Value: '[value] t'est"
};

var reader = new YamlReader();
Assert.Throws<InvalidConfigurationException>(() => reader.Read(typeof(PrimitiveValues), lines));
}


[Test]
public void ParseQuotatedTypeString_DoubleQuotations_Special()
{
var lines = new[]
{
"Value: \"c: is a drive\""
};

var reader = new YamlReader();
var parsed = reader.Read(typeof(PrimitiveValues), lines) as PrimitiveValues;

Assert.AreEqual("c: is a drive", parsed.Value);
}

public class PrimitiveValues
{
public string Value { get; set; }

public List<string> ValueList { get; set; }
}
}
}
104 changes: 104 additions & 0 deletions src/Tests/YamlMap.Tests/Reader/YamlReaderDictionaryTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace YamlMap.Tests.Reader
{
[TestFixture]
public class YamlReaderDictionaryTypeTests
{
[Test]
public void YamlReaderDictionaryType_Dictionary()
{
var lines = new[]
{
"Dictionary:",
" Name: one",
" Id: 1"
};

var reader = new YamlReader();
var data = reader.Read(typeof(StringNode), lines) as StringNode;

Assert.That(data.Dictionary.Count() == 2);
Assert.That(data.Dictionary["Name"] == "one");
Assert.That(data.Dictionary["Id"] == "1");
}

[Test]
public void YamlReaderDictionaryType_Dictionary_Interface()
{
var lines = new[]
{
"IDictionary:",
" Name: one",
" Id: 1"
};

var reader = new YamlReader();
var data = reader.Read(typeof(StringNode), lines) as StringNode;

Assert.That(data.IDictionary.Count() == 2);
Assert.That(data.IDictionary["Name"] == "one");
Assert.That(data.IDictionary["Id"] == "1");
}

[Test]
public void YamlReaderDictionaryType_Dictionary_List()
{
// this is a list of object instead of a dictionary
// to use dictionary remove the -
var lines = new[]
{
"Dictionary:",
" - Name: one",
" - Id: 1"
};

var reader = new YamlReader();
Assert.Throws<InvalidConfigurationException>(() => reader.Read(typeof(StringNode), lines));
}

[Test]
public void YamlReaderDictionaryType_Dictionary_Objects()
{
var lines = new[]
{
"ObjectDictionary:",
" one:",
" Id: 1",
" Name: test one",
" two:",
" Id: 2",
" Name: test two"
};

var reader = new YamlReader();
var data = reader.Read(typeof(StringNode), lines) as StringNode;

Assert.That(data.ObjectDictionary.Count() == 2);
Assert.That(data.ObjectDictionary["one"].Id == 1);
Assert.That(data.ObjectDictionary["one"].Name == "test one");
Assert.That(data.ObjectDictionary["two"].Id == 2);
Assert.That(data.ObjectDictionary["two"].Name == "test two");
}

public class StringNode
{
public Dictionary<string, string> Dictionary { get; set; }

public IDictionary<string, string> IDictionary { get; set; }

public Dictionary<string, DictObject> ObjectDictionary { get; set; }
}

public class DictObject
{
public int Id { get; set; }

public string Name { get; set; }
}
}
}
17 changes: 17 additions & 0 deletions src/Tests/YamlMap.Tests/Serialization/DeserializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ public void Serializer_Deserialize()
item.MatchSnapshot();
}

[Test]
public void Serializer_Deserialize_ByType()
{

var value = new StringBuilder().AppendLine("Simple: root")
.AppendLine("StringList:")
.AppendLine(" - one")
.AppendLine(" - 2")
.AppendLine("ObjList:")
.AppendLine(" - Simple: simple")
.AppendLine(" - Child:")
.AppendLine(" Simple: child").ToString();

var item = Serializer.Deserialize(typeof(TestlItem), value);
item.MatchSnapshot();
}

[Test]
public void Deserialize_Type()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---data
Child:
ObjList:
Child:
ObjList:
Simple: simple
StringList:
Child:
Child:
ObjList:
Simple: child
StringList:
ObjList:
Simple: null
StringList:
Simple: root
StringList:
one
2
13 changes: 12 additions & 1 deletion src/YamlMap/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,16 @@ public static string Serialize<T>(T item)
return reader.Read<T>(yaml);
}

}
/// <summary>
/// Deserialize a string to a object
/// </summary>
/// <param name="type"></param>
/// <param name="yaml"></param>
/// <returns></returns>
public static object Deserialize(Type type, string yaml)
{
var reader = new YamlReader();
return reader.Read(type, yaml);
}
}
}
30 changes: 27 additions & 3 deletions src/YamlMap/YamlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public class YamlReader
return Read<T>(lines);
}

/// <summary>
/// Read a yaml string amd map to a object of the given type
/// </summary>
/// <param name="type"></param>
/// <param name="yaml"></param>
/// <returns></returns>
public object Read(Type type, string yaml)
{
var lines = yaml.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

return Read(type, lines);
}

/// <summary>
/// Read a array of yaml string and map it to a object
/// </summary>
Expand All @@ -32,18 +45,29 @@ public class YamlReader
/// <returns></returns>
public T Read<T>(string[] lines) where T : class, new()
{
var deserializer = new TokenDeserializer(typeof(T), null);
return (T)Read(typeof(T), lines);
}

/// <summary>
/// Read a array of yaml string amd map to a object of the given type
/// </summary>
/// <param name="type"></param>
/// <param name="lines"></param>
/// <returns></returns>
public object Read(Type type, string[] lines)
{
var deserializer = new TokenDeserializer(type, null);

var scanner = new Scanner(lines);
var parser = new Parser(scanner);
var tokens = parser.Parse();

for (var i = 0; i < tokens.Count; i++)
{
deserializer.Deserialize(tokens[i]);
deserializer.Deserialize(tokens[i]);
}

return (T)deserializer.Node;
return deserializer.Node;
}

private string FindFile(string file)
Expand Down

0 comments on commit f5ed997

Please sign in to comment.