Skip to content

GuOrg/Gu.SerializationAsserts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gu.SerializationAsserts

License NuGet NuGet Build status

Contents

1. Xml

1.1. XmlAssert

Class exposing method for comparing xml strings.

1.1.1. Equal

XmlAssert.Equal(expectedXml, actualXml);

Compares two xml strings. If they are not equal an AssertException is thrown. The exception has a message that can give hints about what is wrong, sample:

  Xml differ at line 4 index 21.
  Expected: 4| <Value Attribute="1">2</Value>
  But was:  4| <Value Attribute="1">Wrong</Value>
  ----------------------------------^

1.1.2 With custom comparer

[Test]
public void EqualWithCustomElementComparer()
{
    var expected = "<Foo>" +
                   "  <Bar>1</Bar>" +
                   "</Foo>";

    var actual = "<Foo>" +
                 "  <Bar>  1.0  </Bar>" +
                 "</Foo>";
    XmlAssert.Equal(expected, actual, new ElementDoubleValueComparer(), XmlAssertOptions.IgnoreDeclaration | XmlAssertOptions.IgnoreNamespaces);
}

private class ElementDoubleValueComparer : IEqualityComparer<XElement>
{
    public bool Equals(XElement x, XElement y)
    {
        return double.Parse(x.Value, CultureInfo.InvariantCulture) == double.Parse(y.Value, CultureInfo.InvariantCulture);
    }

    int IEqualityComparer<XElement>.GetHashCode(XElement obj)
    {
        throw new System.NotImplementedException();
    }
}

1.2. XmlSerializerAssert

A collection of helpers for tests using XmlSerializer.

To test that an instance can be roundtripped use:

1.2.1. RoundTrip

  1. Serializes actual to xml
  2. Deserializes xml
  3. Compares the instances using FieldAsser.Equal
  4. Creates a ContainerClass<T> and roundtrips it. The reason we do this is to catch errors with ReadEndElement or reading outside the end of the element.
  5. Returns the roundtripped instance in case you want to do additional asserts.
var actual = new Dummy { Value = 2 };
XmlSerializerAssert.RoundTrip(actual);

1.2.2. Equal

To assert that serialization produces the expected xml and that the instance can be roundtripped use:

var actual = new Dummy { Value = 2 };
var expectedXml = "<Dummy>\r\n" +
                  "  <Value>2</Value>\r\n" +
                  "</Dummy>";
var roundtrip = XmlSerializerAssert.Equal(expectedXml, actual, XmlAssertOptions.IgnoreNamespaces | XmlAssertOptions.IgnoreDeclaration);
  1. Serializes actual to xml
  2. Calls XmlAssert.Equal(expectedXml, actual, options)
  3. Calls XmlSerializerAssert.Roundtrip(actual)
  4. Returns the roundtripped instance in case you want to do additional asserts.

To compare equality of the xml produced when serializing two instances use:

var expected = new Dummy { Value = 2 };
var actual = new Dummy { Value = 2 };
XmlSerializerAssert.Equal(expected, actual);

Checks if two instances produces the same xml. Useful for deep equals.

  1. Serializes expected and actual
  2. Calls XmlAssert.Equal(expectedXml, actualXml)

1.1.3. ToXml

Serialize an object:

var dummy = new Dummy { Value = 2 };
string xml = XmlSerializerAssert.ToXml(dummy);

1.1.4. FromXml

Deserialize xml to an object:

var foo = XmlSerializerAssert.FromXml(xml);

1.2. DataContractSerializerAssert

Same as XmlSerializerAssert but uses DataContractSerializer

1.2.1 HasDataContractAttribute<T>()

Checks that T has [DataContract]

1.2.1 AllPropertiesHasDataMemberAttributes<T>()

Checks that all properties for T has the [DataMember] attribute.

1.3. XmlAssertOptions

Use XmlAssertOptions to specify how the the xml is compared. Sample:

XmlAssert.Equal(expected, actual, XmlAssertOptions.IgnoreDeclaration | XmlAssertOptions.IgnoreNamespaces);

1.3.1 The options are:

  • Verbatim, the default and strictest mode.
  • IgnoreDeclaration, ignore Xml declaration when comparing.
  • IgnoreNamespaces, perhaps useful for less verbose tests.
  • IgnoreElementOrder
  • IgnoreAttributeOrder
  • IgnoreOrder = IgnoreElementOrder | IgnoreAttributeOrder

2. BinaryFormatterAssert

Same as XmlSerializerAssert but uses BinaryFormatter

3. JSON

3.1 JsonAssert

Same as XmlAssert but for JSON.

3.2 JsonSerializerAssert

Same as XmlSerializerAssert but uses JsonSerializer

4. FieldAssert

Checks that all fields are structurally equal recursively. Compares IEquatable<T> types using object.Equals(). Compares collections by element.

var d1 = new Dummy { Value = 1 };
var d2 = new Dummy { Value = 1 };
FieldAssert.Equal(d1, d1);

If assert fails the output looks like this:

Found this difference between expected and actual:
expected[1].foo[2].bar.baz: 5
  actual[1].foo[2].bar.baz: 2

5. FieldComparer<T>

NUnit's CollectionAssert takes an IComparer

CollectionAssert.AreEqual(actual, expected, FieldComparer.For<Foo>());

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages