Skip to content

Commit

Permalink
Added tests and fixed root list
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCooke committed Jan 29, 2023
1 parent 72641d1 commit 6a98f68
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public override void Write(ClassSyntaxReceiver classSyntaxReceiver)
{
var classObject = o.Value;
Write($"if (type == typeof({classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)})) return new {classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}();");
Write($"if (type == typeof(System.Collections.Generic.List<{classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}>)) return new System.Collections.Generic.List<{classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}>();");
}
Write($"throw new ArgumentOutOfRangeException(\"Unknown type: \" + type.ToString());");
UnIndent(); Write("}");
Expand Down
1 change: 1 addition & 0 deletions YamlDotNet.Samples/YamlDotNet.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\YamlDotNet.Test\YamlDotNet.Test.csproj" />
<ProjectReference Include="..\YamlDotNet\YamlDotNet.csproj">
<Project>{BF32DE1B-6276-4341-B212-F8862ADBBA7A}</Project>
<Name>YamlDotNet</Name>
Expand Down
105 changes: 105 additions & 0 deletions YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using Xunit;
using YamlDotNet.Serialization;

namespace YamlDotNet.Test.Analyzers.StaticGenerator
{
public class ObjectTests
{
[Fact]
public void InheritedMembersWorks()
{
var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build();
var yaml = @"NotInherited: hello
Inherited: world
";
var actual = deserializer.Deserialize<SerializedInheritedClass>(yaml);
Assert.Equal("hello", actual.NotInherited);
Assert.Equal("world", actual.Inherited);
var serializer = new StaticSerializerBuilder(new StaticContext()).Build();
var actualYaml = serializer.Serialize(actual);
Assert.Equal(yaml, actualYaml);
}

[Fact]
public void RegularObjectWorks()
{
var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build();
var yaml = @"Prop1: hello
Prop2: 1
Hello: world
Inner:
Prop1: a
Prop2: 2
";
var actual = deserializer.Deserialize<RegularObjectOuter>(yaml);
Assert.Equal("hello", actual.Prop1);
Assert.Equal(1, actual.Prop2);
Assert.Equal("world", actual.Member);
Assert.Equal("I am ignored", actual.Ignored);
Assert.NotNull(actual.Inner);
Assert.Equal("a", actual.Inner.Prop1);
Assert.Equal(2, actual.Inner.Prop2);

var serializer = new StaticSerializerBuilder(new StaticContext()).Build();
var actualYaml = serializer.Serialize(actual);
yaml = @"Prop1: hello
Prop2: 1
# A Description
Hello: ""world""
Inner:
Prop1: a
Prop2: 2
";
Assert.Equal(yaml, actualYaml);
}
}
public class InheritedClass
{
public string Inherited { get; set; }
}

[YamlSerializable]
public class SerializedInheritedClass : InheritedClass
{
public string NotInherited { get; set; }
}

[YamlSerializable]
public class RegularObjectOuter
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
[YamlMember(Alias = "Hello", Description = "A Description", ScalarStyle = YamlDotNet.Core.ScalarStyle.DoubleQuoted)]
public string Member { get; set; }
[YamlIgnore]
public string Ignored { get; set; } = "I am ignored";
public RegularObjectInner Inner { get; set; }
}

[YamlSerializable]
public class RegularObjectInner
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
}
}
63 changes: 63 additions & 0 deletions YamlDotNet.Test/Analyzers/StaticGenerator/RootCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Collections.Generic;
using Xunit;
using YamlDotNet.Serialization;

namespace YamlDotNet.Test.Analyzers.StaticGenerator
{
public class RootCollectionTests
{
[Fact]
public void RootArrayWorks()
{
var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build();
var yaml = @"
- Test: hello
- Test: world
";

var actual = deserializer.Deserialize<RootObject[]>(yaml);
Assert.Equal("hello", actual[0].Test);
Assert.Equal("world", actual[1].Test);
}

[Fact]
public void RootListWorks()
{
var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build();
var yaml = @"
- Test: hello
- Test: world
";

var actual = deserializer.Deserialize<List<RootObject>>(yaml);
Assert.Equal("hello", actual[0].Test);
Assert.Equal("world", actual[1].Test);
}
}
[YamlSerializable]
public class RootObject
{
public string Test { get; set; }
}
}
30 changes: 30 additions & 0 deletions YamlDotNet.Test/Analyzers/StaticGenerator/StaticAoTContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using YamlDotNet.Serialization;

namespace YamlDotNet.Test.Analyzers.StaticGenerator
{
[YamlStaticContext]
public partial class StaticContext : YamlDotNet.Serialization.StaticContext
{
}
}
4 changes: 4 additions & 0 deletions YamlDotNet.Test/YamlDotNet.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AssemblyOriginatorKeyFile>..\YamlDotNet.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<LangVersion>8.0</LangVersion>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<Import Project="../build/common.props" />
Expand Down Expand Up @@ -32,6 +33,9 @@
<Project>{BF32DE1B-6276-4341-B212-F8862ADBBA7A}</Project>
<Name>YamlDotNet</Name>
</ProjectReference>
<ProjectReference Include="..\YamlDotNet.Analyzers.StaticGenerator\YamlDotNet.Analyzers.StaticGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 6a98f68

Please sign in to comment.