Skip to content

Commit

Permalink
und - Added unit tests for MeCard
Browse files Browse the repository at this point in the history
We've added unit tests

---

We've added unit tests for the MeCard functionality.

---

Type: und
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 11, 2023
1 parent 56b0cb0 commit 9258ddd
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 1 deletion.
97 changes: 97 additions & 0 deletions VisualCard.Tests/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ namespace VisualCard.Tests
public static class ContactData
{
#region singleVcardTwoContactShort
private static readonly string singleMeCardContactShort =
"""
MECARD:N:Hood,Rick,,,;;
"""
;

private static readonly string singleVcardContactShortFromMeCard =
"""
BEGIN:VCARD
VERSION:3.0
FN:Rick Hood
N:Hood;Rick;;;;
END:VCARD

"""
;

private static readonly string singleVcardTwoContactShort =
"""
BEGIN:VCARD
Expand All @@ -41,6 +58,19 @@ public static class ContactData
"""
;

private static readonly Card singleVcardContactShortFromMeCardInstance = new
(
null,
"3.0"
)
{
ContactNames = new NameInfo[]
{
new NameInfo(0, Array.Empty<string>(), "Rick", "Hood", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>())
},
ContactFullName = "Rick Hood"
};

private static readonly Card singleVcardTwoContactShortInstance = new
(
null,
Expand Down Expand Up @@ -106,6 +136,53 @@ public static class ContactData
#endregion

#region singleVcardTwoContact
private static readonly string singleMeCardContact =
"""
MECARD:N:Sanders,John,,,;TEL:495-522-3560;EMAIL:john.s@acme.co;ADR:,,Los Angeles,,,,USA;NOTE:Note test for VisualCard;;
"""
;

private static readonly string singleVcardContactFromMeCard =
"""
BEGIN:VCARD
VERSION:3.0
FN:John Sanders
N:Sanders;John;;;;
TEL;TYPE=CELL:495-522-3560
ADR;TYPE=HOME:;;Los Angeles;;;;USA
EMAIL;TYPE=HOME:john.s@acme.co
NOTE:Note test for VisualCard
END:VCARD

"""
;

private static readonly Card singleVcardContactInstanceFromMeCard = new
(
null,
"3.0"
)
{
ContactNames = new NameInfo[]
{
new NameInfo(0, Array.Empty<string>(), "John", "Sanders", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>())
},
ContactFullName = "John Sanders",
ContactTelephones = new TelephoneInfo[]
{
new TelephoneInfo(0, Array.Empty<string>(), new string[] { "CELL" }, "495-522-3560")
},
ContactAddresses = new AddressInfo[]
{
new AddressInfo(0, Array.Empty<string>(), new string[] { "HOME" }, "", "", "Los Angeles", "", "", "", "USA")
},
ContactNotes = "Note test for VisualCard",
ContactMails = new EmailInfo[]
{
new EmailInfo(0, Array.Empty<string>(), new string[] { "HOME" }, "john.s@acme.co")
}
};

private static readonly string singleVcardTwoContact =
"""
BEGIN:VCARD
Expand Down Expand Up @@ -892,6 +969,24 @@ public static class ContactData
multipleVcardFourContacts,
};

/// <summary>
/// Test MeCard contacts
/// </summary>
public static readonly string[] meCardContacts =
{
singleMeCardContactShort,
singleMeCardContact,
};

/// <summary>
/// Test MeCard contacts
/// </summary>
public static readonly (string, string)[] vCardFromMeCardContacts =
{
(singleMeCardContactShort, singleVcardContactShortFromMeCard),
(singleMeCardContact, singleVcardContactFromMeCard),
};

/// <summary>
/// All the remaining valid contacts
/// </summary>
Expand Down Expand Up @@ -926,6 +1021,8 @@ public static class ContactData
vcardThreeOldSampleInstanceOne,
vcardThreeOldSampleInstanceTwo,
vcardThreeOldSampleInstanceThree,
singleVcardContactInstanceFromMeCard,
singleVcardContactShortFromMeCardInstance,
};
}
}
91 changes: 90 additions & 1 deletion VisualCard.Tests/ContactParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Shouldly;
using System.Collections.Generic;
using System.IO;
using VisualCard.Converters;
using VisualCard.Parsers;
using VisualCard.Parts;

Expand Down Expand Up @@ -127,6 +128,94 @@ public void ParseDifferentContactsSaveToStringAndTestEquality(string cardText)
foundCards.ShouldAllBe((b) => b);
}

[Test]
[TestCaseSource(typeof(ContactData), nameof(ContactData.vCardFromMeCardContacts))]
public void ParseAndCheckDifferentMeCardContacts((string, string) cardText)
{
List<BaseVcardParser> parsers = new();
Should.NotThrow(() => parsers = MeCard.GetContactsFromMeCardString(cardText.Item1));
Card card = default;
foreach (BaseVcardParser parser in parsers)
Should.NotThrow(() => card = parser.Parse());
card.SaveToString().ShouldBe(cardText.Item2);
}

[Test]
[TestCaseSource(typeof(ContactData), nameof(ContactData.meCardContacts))]
public void ParseDifferentMeCardContacts(string cardText)
{
List<BaseVcardParser> parsers = new();
Should.NotThrow(() => parsers = MeCard.GetContactsFromMeCardString(cardText));
foreach (BaseVcardParser parser in parsers)
Should.NotThrow(parser.Parse);
}

[Test]
[TestCaseSource(typeof(ContactData), nameof(ContactData.meCardContacts))]
public void ParseDifferentMeCardContactsAndTestEquality(string cardText)
{
List<BaseVcardParser> parsers = new();
List<Card> cards = new();

// Parse the cards
Should.NotThrow(() => parsers = MeCard.GetContactsFromMeCardString(cardText));
foreach (BaseVcardParser parser in parsers)
cards.Add(Should.NotThrow(parser.Parse));

// Test equality with available data
List<bool> foundCards = new();
foreach (Card card in cards)
{
bool found = false;
foreach (Card expectedCard in ContactData.vCardContactsInstances)
if (expectedCard == card)
{
found = true;
break;
}
foundCards.Add(found);
}
foundCards.ShouldAllBe((b) => b);
}

[Test]
[TestCaseSource(typeof(ContactData), nameof(ContactData.meCardContacts))]
public void ParseDifferentMeCardContactsSaveToStringAndTestEquality(string cardText)
{
List<BaseVcardParser> parsers = new();
List<Card> cards = new();
List<Card> savedCards = new();

// Parse the cards
Should.NotThrow(() => parsers = MeCard.GetContactsFromMeCardString(cardText));
foreach (BaseVcardParser parser in parsers)
cards.Add(Should.NotThrow(parser.Parse));

// Save all the cards to strings and re-parse
foreach (Card card in cards)
{
string saved = Should.NotThrow(card.SaveToString);
Should.NotThrow(() => parsers = CardTools.GetCardParsersFromString(saved));
foreach (BaseVcardParser parser in parsers)
savedCards.Add(Should.NotThrow(parser.Parse));
}

// Test equality with available data
List<bool> foundCards = new();
foreach (Card card in savedCards)
{
bool found = false;
foreach (Card expectedCard in ContactData.vCardContactsInstances)
if (expectedCard == card)
{
found = true;
break;
}
foundCards.Add(found);
}
foundCards.ShouldAllBe((b) => b);
}

[Test]
[TestCaseSource(typeof(ContactDataBogus), nameof(ContactDataBogus.invalidContacts))]
public void InvalidContactShouldThrowWhenGettingCardParsers(string cardText)
Expand Down Expand Up @@ -157,4 +246,4 @@ public void BogusButSeemsValidShouldNotThrowWhenParsing(string cardText)
Should.NotThrow(parser.Parse);
}
}
}
}
4 changes: 4 additions & 0 deletions VisualCard/Converters/MeCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static List<BaseVcardParser> GetContactsFromMeCardString(string meCardStr
if (value.StartsWith("ADR:"))
values[i] = $"ADR;TYPE=HOME{values[i].Substring(3)}";

// As VisualCard doesn't support EMAIL: yet, why don't we just make it EMAIL; until we improve type detecion?
if (value.StartsWith("EMAIL:"))
values[i] = $"EMAIL;TYPE=HOME{values[i].Substring(5)}";

// MeCard stores birthday date in this format:
// 19700310
// Digit 1-4: Year
Expand Down

0 comments on commit 9258ddd

Please sign in to comment.