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
- NamePart.
  • Loading branch information
JoergHoffmannatGitHub committed Mar 4, 2023
1 parent 7cc0443 commit d708473
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 50 deletions.
69 changes: 69 additions & 0 deletions Gedcomx.Model.Test/NamePartTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Xml.Serialization;

using Gx.Common;
using Gx.Conclusion;
using Gx.Records;

using Gx.Types;

using Newtonsoft.Json;

using NUnit.Framework;

namespace Gedcomx.Model.Test
{
/// <summary>
/// Test calss for <see cref="NamePart"/>
/// </summary>
[TestFixture]
public class NamePartTest
{
[Test]
public void NamePartEmpty()
{
var sut = new NamePart();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void NamePartFilled()
{
var sut = new NamePart
{
// ExtensibleData
Id = "A-1",
// NamePart
Value = "John",
KnownType = NamePartType.Given,
Fields = { new Field() },
Qualifiers = { new Qualifier() }
};

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

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

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

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

Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<NamePart>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
}
16 changes: 14 additions & 2 deletions Gedcomx.Model.Test/XmlAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,23 @@ public static void ShouldContain(this string result, NameForm nameForm)
Assert.That(result, Does.Contain("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""));
Assert.That(result.Contains("lang"), Is.EqualTo(nameForm.Lang != null));
Assert.That(result.Contains("<fullText"), Is.EqualTo(nameForm.FullText != null));
Assert.That(result.Contains("part"), Is.EqualTo(nameForm.AnyParts()));
Assert.That(result.Contains("field"), Is.EqualTo(nameForm.AnyFields()));
Assert.That(result.Contains("<part"), Is.EqualTo(nameForm.AnyParts()));
Assert.That(result.Contains("<field"), Is.EqualTo(nameForm.AnyFields()));
result.ShouldContain(nameForm as ExtensibleData);
}

public static void ShouldContain(this string result, NamePart namePart)
{
Assert.That(result, Does.Contain("<NamePart "));
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("value"), Is.EqualTo(namePart.Value != null));
Assert.That(result.Contains("type="), Is.EqualTo(namePart.Type != null));
Assert.That(result.Contains("<field"), Is.EqualTo(namePart.AnyFields()));
Assert.That(result.Contains("<qualifier"), Is.EqualTo(namePart.AnyQualifiers()));
result.ShouldContain(namePart as ExtensibleData);
}

public static void ShouldContain(this string result, ExtensibleData extensibleData)
{
result.ShouldContainAttribute("id", extensibleData.Id);
Expand Down
73 changes: 28 additions & 45 deletions Gedcomx.Model/NamePart.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Gedcomx.Model.Rt;
using Gedcomx.Model.Util;
using Gx.Common;
using Gx.Records;
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.Common;
using Gx.Records;
using Gx.Types;

namespace Gx.Conclusion
{
Expand All @@ -31,7 +33,7 @@ public sealed partial class NamePart : Gx.Common.ExtensibleData
private System.Collections.Generic.List<Gx.Common.Qualifier> _qualifiers;

public NamePart()
: this(default(NamePartType), null)
: this(default, null)
{
}

Expand Down Expand Up @@ -102,13 +104,18 @@ public System.Collections.Generic.List<Gx.Records.Field> Fields
{
get
{
return this._fields;
return this._fields ?? (_fields = new System.Collections.Generic.List<Gx.Records.Field>());
}
set
{
this._fields = value;
}
}
public bool ShouldSerializeFields() => AnyFields();
public bool AnyFields()
{
return _fields?.Any() ?? false;
}
/// <summary>
/// The qualifiers associated with this name part.
/// </summary>
Expand All @@ -118,13 +125,18 @@ public System.Collections.Generic.List<Gx.Common.Qualifier> Qualifiers
{
get
{
return this._qualifiers;
return _qualifiers ?? (_qualifiers = new System.Collections.Generic.List<Gx.Common.Qualifier>());
}
set
{
this._qualifiers = value;
}
}
public bool ShouldSerializeQualifiers() => AnyQualifiers();
public bool AnyQualifiers()
{
return _qualifiers?.Any() ?? false;
}

/**
* Accept a visitor.
Expand Down Expand Up @@ -180,7 +192,10 @@ public NamePart SetValue(String value)
*/
public NamePart SetQualifier(Qualifier qualifier)
{
AddQualifier(qualifier);
if (qualifier != null)
{
Qualifiers.Add(qualifier);
}
return this;
}

Expand All @@ -190,44 +205,12 @@ public NamePart SetQualifier(Qualifier qualifier)
* @return this.
*/
public NamePart SetField(Field field)
{
AddField(field);
return this;
}

/**
* Add a qualifier associated with this name part.
*
* @param qualifier The qualifier to be added.
*/
public void AddQualifier(Qualifier qualifier)
{
if (qualifier != null)
{
if (_qualifiers == null)
{
_qualifiers = new List<Qualifier>();
}
_qualifiers.Add(qualifier);
}
}

/**
* Add a reference to the record field values being used as evidence.
*
* @param field The field to be added.
*/
public void AddField(Field field)
{
if (field != null)
{
if (_fields == null)
{
_fields = new List<Field>();
}

_fields.Add(field);
Fields.Add(field);
}
return this;
}
}
}
4 changes: 2 additions & 2 deletions Gedcomx.Model/Rt/GedcomxModelVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ public virtual void VisitNameForm(NameForm form)
public virtual void VisitNamePart(NamePart part)
{
this.contextStack.Push(part);
List<Field> fields = part.Fields;
if (fields != null)
if (part.AnyFields())
{
List<Field> fields = part.Fields;
foreach (Field field in fields)
{
field.Accept(this);
Expand Down
2 changes: 1 addition & 1 deletion Gedcomx.Util/GedcomxModelVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ public virtual void VisitNameForm(NameForm form)
public virtual void VisitNamePart(NamePart part)
{
this.contextStack.Push(part);
if (part.Fields != null)
if (part.AnyFields())
{
foreach (Field field in part.Fields)
{
Expand Down

0 comments on commit d708473

Please sign in to comment.