Skip to content

Commit

Permalink
add - doc - Added card generator
Browse files Browse the repository at this point in the history
---

Using the random names list, we've added card generator. Please note that it's currently limited in terms of features, because we currently need none other than the names, the telephones, and the emails.

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 4, 2024
1 parent 8af4669 commit 6bb3ddc
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
22 changes: 22 additions & 0 deletions VisualCard.Tests/ContactParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.IO;
using VisualCard.Converters;
using VisualCard.Exceptions;
using VisualCard.Extras;
using VisualCard.Parts;

namespace VisualCard.Tests
Expand Down Expand Up @@ -90,6 +91,27 @@ internal void ReparseDifferentContactsInternal(string cardText)
}
}

[TestMethod]
public void ParseGeneratedContacts()
{
Card[] cards;
Should.NotThrow(() => cards = CardGenerator.GenerateCards());
}

[TestMethod]
public void ReparseGeneratedContacts()
{
Card[] cards = [];
Should.NotThrow(() => cards = CardGenerator.GenerateCards());

// Save all the cards to strings and re-parse
foreach (Card card in cards)
{
string saved = Should.NotThrow(card.SaveToString);
Should.NotThrow(() => CardTools.GetCardsFromString(saved));
}
}

[TestMethod]
[DynamicData(nameof(ContactData.singleVcardContactShorts), typeof(ContactData))]
public void ParseDifferentContactsAndTestEqualityShorts(string cardText) =>
Expand Down
118 changes: 118 additions & 0 deletions VisualCard/Extras/CardGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VisualCard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using Textify.NameGen;
using VisualCard.Parts;
using VisualCard.Parts.Enums;
using VisualCard.Parts.Implementations;

namespace VisualCard.Extras
{
/// <summary>
/// A card generator
/// </summary>
public static class CardGenerator
{
private static readonly Random rng = new();

/// <summary>
/// Generates cards
/// </summary>
/// <returns>A list of generated cards (by default, it generates up to 100 cards.)</returns>
public static Card[] GenerateCards()
{
int cardNumbers = rng.Next(1, 101);
return GenerateCards(cardNumbers);
}

/// <summary>
/// Generates cards
/// </summary>
/// <param name="cards">Number of cards to generate</param>
/// <returns>A list of generated cards or an empty array if <paramref name="cards"/> is less than or equal to zero.</returns>
public static Card[] GenerateCards(int cards)
{
if (cards <= 0)
return [];

// Get first and last names
string[] firstNames = NameGenerator.GenerateFirstNames(cards);
string[] lastNames = NameGenerator.GenerateLastNames(cards);
string[] mailHosts = ["gmail.com", "mail.com", "outlook.com", "hotmail.com", "yahoo.com"];
List<Card> cardList = [];

for (int i = 0; i < cards; i++)
{
// Get first and last names from the card index
string firstName = firstNames[i];
string lastName = lastNames[i];
if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName))
continue;

// Now, generate random telephone numbers and e-mail addresses
bool generateTelephone = rng.NextDouble() < 0.3;
bool generateEmail = rng.NextDouble() < 0.3;
Dictionary<string, string> telephones = [];
Dictionary<string, string> emails = [];
if (generateTelephone)
{
bool generateWorkTelephone = rng.NextDouble() < 0.25;
int firstPart = rng.Next(1000);
int secondPart = rng.Next(1000);
int thirdPart = rng.Next(10000);
telephones.Add("HOME", $"{firstPart:D3}-{secondPart:D3}-{thirdPart:D4}");
if (generateWorkTelephone)
{
firstPart = rng.Next(1000);
secondPart = rng.Next(1000);
thirdPart = rng.Next(10000);
telephones.Add("WORK", $"{firstPart:D3}-{secondPart:D3}-{thirdPart:D4}");
}
}
if (generateEmail)
{
bool generateWorkEmail = rng.NextDouble() < 0.25;
string lastNameNormalized = lastName.ToLower().Replace(" ", "-");
string emailName = char.ToLower(firstName[0]) + "." + lastNameNormalized;
string mailHost = mailHosts[rng.Next(mailHosts.Length)];
emails.Add("HOME", $"{emailName}@{mailHost}");
if (generateWorkEmail)
emails.Add("WORK", $"{emailName}@{lastNameNormalized}.com");
}

// Now, convert generated parts to actual VisualCard parts
var card = new Card(new(2, 1));
card.AddPartToArray(PartsArrayEnum.Names, new NameInfo(0, [], [], "", firstName, lastName, [], [], []));
card.AddPartToArray(PartsArrayEnum.FullName, new FullNameInfo(0, [], [], "", $"{firstName} {lastName}"));
foreach (var telephone in telephones)
card.AddPartToArray(PartsArrayEnum.Telephones, new TelephoneInfo(0, [], [telephone.Key], "", telephone.Value));
foreach (var email in emails)
card.AddPartToArray(PartsArrayEnum.Mails, new EmailInfo(0, [], [email.Key], "", email.Value));
string cardString = card.ToString();

// Verify the generated card and add it to the list of cards
var finalCard = CardTools.GetCardsFromString(cardString)[0];
cardList.Add(finalCard);
}
return [.. cardList];
}
}
}

0 comments on commit 6bb3ddc

Please sign in to comment.