-
Notifications
You must be signed in to change notification settings - Fork 46
/
MarkupExtensionTests.cs
160 lines (138 loc) · 7.21 KB
/
MarkupExtensionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// MIT License
//
// Copyright (c) 2016 Wojciech Nagórski
// Michael DeMond
//
// 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 ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ExtensionModel.Markup;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using ExtendedXmlSerializer.Tests.Support;
using FluentAssertions;
using JetBrains.Annotations;
using System;
using Xunit;
namespace ExtendedXmlSerializer.Tests.ExtensionModel.Markup
{
public class MarkupExtensionTests
{
[Fact]
public void Verify()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<Subject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-Subject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" PropertyName=""{MarkupExtensionTests-Extension}"" />");
subject.PropertyName.Should().Be(string.Concat(Extension.Message, Extension.None, " 6776"));
}
[Fact]
public void VerifyWithProperty()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<Subject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-Subject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" PropertyName=""{MarkupExtensionTests-Extension Number=3 * 3}"" />");
subject.PropertyName.Should().Be(string.Concat(Extension.Message, Extension.None, " 9"));
}
[Fact]
public void VerifyWithParameter()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<Subject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-Subject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" PropertyName=""{MarkupExtensionTests-Extension 'Provided Message!'}"" />");
subject.PropertyName.Should().Be(string.Concat(Extension.Message, "Provided Message!", " 6776"));
}
[Fact]
public void VerifyWithParameterAndProperty()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<Subject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-Subject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" PropertyName=""{MarkupExtensionTests-Extension 'Provided Message!', Number=3 * 4}"" />");
subject.PropertyName.Should().Be(string.Concat(Extension.Message, "Provided Message!", " 12"));
}
[Fact]
public void VerifyType()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<TypedSubject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-TypedSubject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" xmlns:sys=""https://extendedxmlserializer.github.io/system"" SuppliedType=""{exs:Type sys:int}"" AnotherProperty=""Hello World!"" />");
subject.SuppliedType.Should().Be(typeof(int));
subject.AnotherProperty.Should().Be("Hello World!");
}
[Fact]
public void VerifyMarkupExtension()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<TypedSubject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-TypedSubject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" xmlns:sys=""https://extendedxmlserializer.github.io/system"" AnotherProperty=""{MarkupExtensionTests-TypeName {exs:Type sys:dateTime}}"" />");
subject.AnotherProperty.Should().Be(typeof(DateTime).AssemblyQualifiedName);
}
[Fact]
public void VerifyStatic()
{
var container = new ConfigurationContainer();
container.EnableMarkupExtensions();
var serializer = new SerializationSupport(container);
var subject = serializer.Deserialize<DatedSubject>(@"<?xml version=""1.0"" encoding=""utf-8""?><MarkupExtensionTests-DatedSubject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.Markup;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" xmlns:sys=""https://extendedxmlserializer.github.io/system"" Date=""{exs:Static sys:dateTime.Now}"" DateNullable=""{exs:Static sys:dateTime.Now}"" />");
subject.Date.Should().NotBe(default(DateTime)).And.BeAfter(DateTime.Today);
subject.DateNullable.Should().NotBeNull().And.BeAfter(DateTime.Today);
}
sealed class Subject
{
public string PropertyName { get; [UsedImplicitly] set; }
}
sealed class DatedSubject
{
public DateTime Date { get; [UsedImplicitly] set; }
public DateTime? DateNullable { get; [UsedImplicitly] set; }
}
sealed class TypedSubject
{
public Type SuppliedType { get; [UsedImplicitly] set; }
public string AnotherProperty { get; [UsedImplicitly] set; }
}
sealed class TypeNameExtension : IMarkupExtension
{
readonly Type _type;
[UsedImplicitly]
public TypeNameExtension(Type type)
{
_type = type;
}
public object ProvideValue(IServiceProvider serviceProvider) => _type.AssemblyQualifiedName;
}
sealed class Extension : IMarkupExtension
{
public const string Message = "Hello World from Markup Extension! Your message is: ", None = "N/A";
readonly string _message;
[UsedImplicitly]
public Extension() : this(None) {}
public Extension(string message)
{
_message = message;
}
public object ProvideValue(IServiceProvider serviceProvider) => string.Concat(Message, _message, $" {Number.ToString()}");
public int Number { get; set; } = 6776;
}
}
}