Skip to content

Commit

Permalink
Add Tests to fix FamilySearch#43 and FamilySearch#48.
Browse files Browse the repository at this point in the history
- Field.
  • Loading branch information
JoergHoffmannatGitHub committed Mar 12, 2023
1 parent c0921fd commit f1475d3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Gedcomx.Model.Test/FacetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Gedcomx.Model.Test;
/// Test calss for <see cref="Facet"/>
/// </summary>
[TestFixture]
public class FacetTEst
public class FacetTest
{
[Test]
public void FacetEmpty()
Expand Down
66 changes: 66 additions & 0 deletions Gedcomx.Model.Test/FieldTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Xml.Serialization;

using Gx.Records;

using Gx.Types;

using Newtonsoft.Json;

using NUnit.Framework;

namespace Gedcomx.Model.Test;

/// <summary>
/// Test calss for <see cref="Field"/>
/// </summary>
[TestFixture]
public class FieldTest
{
[Test]
public void FieldEmpty()
{
Field sut = new();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void FieldObjectInitialization()
{
Field sut = new()
{
// ExtensibleData
Id = "F-1",
// HypermediaEnabledData
Links = { new(), { "rel", new Uri("https://www.familysearch.org/platform/collections/tree") }, { "rel", "template" } },
// Field
KnownType = FieldType.Abusua,
Values = { new() }
};

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

private static void VerifyXmlSerialization(Field sut)
{
XmlSerializer serializer = new(typeof(Field));
using MemoryStream stream = new();
serializer.Serialize(stream, sut);

stream.Seek(0, SeekOrigin.Begin);
var result = new StreamReader(stream).ReadToEnd();
result.ShouldContain(sut);
}

private static void VerifyJsonSerialization(Field sut)
{
JsonSerializerSettings jsonSettings = new()
{
NullValueHandling = NullValueHandling.Ignore
};

Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Field>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
10 changes: 10 additions & 0 deletions Gedcomx.Model.Test/XmlAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ public static void ShouldContain(this string result, Facet facet)
result.ShouldContain(facet as HypermediaEnabledData);
}

public static void ShouldContain(this string result, Field field)
{
Assert.That(result, Does.Contain("<Field "));
Assert.That(result, Does.Contain("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));
Assert.That(result, Does.Contain("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""));
Assert.That(result.Contains("type="), Is.EqualTo(field.Type != null));
Assert.That(result.Contains("<value "), Is.EqualTo(field.AnyValues()));
result.ShouldContain(field as HypermediaEnabledData);
}

public static void ShouldContain(this string result, Document document)
{
Assert.That(result, Does.Contain("<document "));
Expand Down
36 changes: 15 additions & 21 deletions Gedcomx.Model/Field.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using Gedcomx.Model.Rt;
using Gedcomx.Model.Util;
using Gx.Types;
// <auto-generated>
// <auto-generated>
//
//
// Generated by <a href="http://enunciate.codehaus.org">Enunciate</a>.
// </auto-generated>
using System;
using System.Collections.Generic;
using System.Linq;

using Gedcomx.Model.Rt;
using Gedcomx.Model.Util;

using Gx.Types;

namespace Gx.Records
{
Expand Down Expand Up @@ -67,13 +70,18 @@ public System.Collections.Generic.List<Gx.Records.FieldValue> Values
{
get
{
return this._values;
return this._values ?? (_values = new System.Collections.Generic.List<Gx.Records.FieldValue>());
}
set
{
this._values = value;
}
}
public bool ShouldSerializeValues() => AnyValues();
public bool AnyValues()
{
return _values?.Any() ?? false;
}

public void Accept(IGedcomxModelVisitor visitor)
{
Expand Down Expand Up @@ -110,26 +118,12 @@ public Field SetType(FieldType type)
* @return this.
*/
public Field SetValue(FieldValue value)
{
AddValue(value);
return this;
}

/**
* Add a reference to the record value values being used as evidence.
*
* @param value The value to be added.
*/
public void AddValue(FieldValue value)
{
if (value != null)
{
if (_values == null)
{
_values = new List<FieldValue>();
}
_values.Add(value);
Values.Add(value);
}
return this;
}
}
}
6 changes: 3 additions & 3 deletions Gedcomx.Model/Rt/GedcomxModelVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ public virtual void VisitField(Field field)
{
this.contextStack.Push(field);

List<FieldValue> values = field.Values;
if (values != null)
if (field.AnyValues())
{
foreach (FieldValue value in values)
var values = field.Values;
foreach (var value in values)
{
value.Accept(this);
}
Expand Down
4 changes: 2 additions & 2 deletions Gedcomx.Util/GedcomxModelVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ public virtual void VisitField(Field field)
{
this.contextStack.Push(field);

if (field.Values != null)
if (field.AnyValues())
{
foreach (FieldValue v in field.Values)
foreach (var v in field.Values)
{
VisitFieldValue(v);
}
Expand Down

0 comments on commit f1475d3

Please sign in to comment.