Skip to content

Commit

Permalink
Adding tests for refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoergHoffmannatGitHub committed Jan 3, 2025
1 parent 8309bef commit 71500a9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 21 deletions.
79 changes: 79 additions & 0 deletions FamilyShow.Tests/Controls/DateCalculatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Globalization;

namespace FamilyShow.Tests.Controls;

public class DateCalculatorTest
{
public static readonly TheoryData<string, string, string, string, string, string, string> CalculateButtonCases =
new()
{
{ "en-US", "22 JUL 1890", "22 JAN 1995", string.Empty, "7/22/1890", "1/22/1995", "105 years" },
{ "en-GB", "22 JUL 1890", string.Empty, "105", "22/07/1890", "22/07/1995", "105 years" },
{ "de-DE", string.Empty, "22 JAN 1995", "105", "22.01.1890", "22.01.1995", "105 years" },
{ "en-US", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty },
{ "en-US", string.Empty, string.Empty, "105", string.Empty, string.Empty, string.Empty },
{ "en-US", string.Empty, "22 JAN 1995", string.Empty, string.Empty, string.Empty, string.Empty },
{ "en-US", "22 JUL 1890", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty },
{ "en-US", "22 JAN 1995", "22 JUL 1890", string.Empty, string.Empty, string.Empty, string.Empty },
{ "en-US", "22 JUL 1890", "22 JAN 1995", "105", string.Empty, string.Empty, string.Empty },
{ "en-US", "invalid", "22 JAN 1995", string.Empty, string.Empty, string.Empty, string.Empty },
{ "en-US", "22 JUL 1890", "invalid", string.Empty, string.Empty, string.Empty, string.Empty },
{ "en-US", "invalid", string.Empty, "105", string.Empty, string.Empty, string.Empty },
{ "en-US", "22 JUL 1890", string.Empty, "invalid", string.Empty, string.Empty, string.Empty },
{ "en-US", string.Empty, "invalid", "105", string.Empty, string.Empty, string.Empty },
{ "en-US", string.Empty, "22 JAN 1995", "invalid", string.Empty, string.Empty, string.Empty },
};

[StaTheory, MemberData(nameof(CalculateButtonCases))]
public void CalculateButton_ClickTest(string culture, string date1, string date2, string age, string birthResult, string deathResult, string ageResult)
{
// Arrange
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(culture);
DateCalculator calculator = new();
calculator.Date1TextBox.Text = date1;
calculator.Date2TextBox.Text = date2;
calculator.AgeTextBox.Text = age;
// Act
calculator.CalculateButton_Click(null, null);
// Assert
Assert.Equal(birthResult, calculator.BirthResult.Content);
Assert.Equal(deathResult, calculator.DeathResult.Content);
Assert.Equal(ageResult, calculator.AgeResult.Content);
}

public static readonly TheoryData<string, string, bool, string, string, string, string> Calculate2ButtonCases =
new()
{
{ "en-US", "22 JUL 1890", true, "1", "1", "1", "8/23/1891" },
{ "en-US", "22 JUL 1890", false, "1", "1", "1", "6/21/1889" },
{ "en-US", "22 JUL 1890", true, "300", "45", "17", "2/18/1912" },
{ "en-US", "22 JUL 1890", false, "300", "45", "17", "12/25/1868" },
{ "en-US", "invalid", true, "1", "1", "1", string.Empty },
{ "en-US", "invalid", false, "1", "1", "1", string.Empty },
{ "en-US", "invalid", true, "300", "45", "17", string.Empty },
{ "en-US", "invalid", false, "300", "45", "17", string.Empty },
{ "en-US", "22 JUL 1890", true, "invalid", "1", "1", "8/22/1891" },
{ "en-US", "22 JUL 1890", false, "invalid", "1", "1", "6/22/1889" },
{ "en-US", "22 JUL 1890", true, "1", "invalid", "1", "7/23/1891" },
{ "en-US", "22 JUL 1890", false, "1", "invalid", "1", "7/21/1889" },
{ "en-US", "22 JUL 1890", true, "1", "1", "invalid", "8/23/1890" },
{ "en-US", "22 JUL 1890", false, "1", "1", "invalid", "6/21/1890" },
};

[StaTheory, MemberData(nameof(Calculate2ButtonCases))]
public void Calculate2Button_ClickTest(string culture, string date, bool add, string days, string months, string years, string result)
{
// Arrange
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(culture);
DateCalculator calculator = new();
calculator.ToBox.Text = date;
calculator.DayBox.Text = days;
calculator.MonthBox.Text = months;
calculator.YearBox.Text = years;
calculator.AddSubtractComboBox.SelectedIndex = add ? 0 : 1;
// Act
calculator.Calculate2Button_Click(null, null);
// Assert
Assert.Equal(result, calculator.Result2.Content);
}
}
1 change: 1 addition & 0 deletions FamilyShow.Tests/FamilyShow.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Xunit.StaFact" />
</ItemGroup>

<ItemGroup>
Expand Down
52 changes: 31 additions & 21 deletions FamilyShow/Controls/DateCalculator.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public event RoutedEventHandler CancelButtonClick
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CalculateButton_Click(object sender, RoutedEventArgs e)
internal void CalculateButton_Click(object sender, RoutedEventArgs e)
{
// Reset the results
DeathResult.Content = string.Empty;
Expand All @@ -48,22 +48,29 @@ private void CalculateButton_Click(object sender, RoutedEventArgs e)

try
{

// Get any input dates
DateTime s1 = App.StringToDate(Date1TextBox.Text);
DateTime s2 = App.StringToDate(Date2TextBox.Text);

if (!string.IsNullOrEmpty(Date1TextBox.Text))
if (!string.IsNullOrEmpty(Date1TextBox.Text) && s1 != DateTime.MinValue)
{
Date1TextBox.Text = s1.ToShortDateString();
}
else
{
Date1TextBox.Text = string.Empty;
}

if (!string.IsNullOrEmpty(Date2TextBox.Text))
if (!string.IsNullOrEmpty(Date2TextBox.Text) && s2 != DateTime.MinValue)
{
Date2TextBox.Text = s2.ToShortDateString();
}
else
{
Date2TextBox.Text = string.Empty;
}

int age = 0;
int age = -1;

// Get any input age
if (!string.IsNullOrEmpty(AgeTextBox.Text))
Expand All @@ -72,23 +79,23 @@ private void CalculateButton_Click(object sender, RoutedEventArgs e)
}

// If a birth date and death date are specified, calculate an age
if (!string.IsNullOrEmpty(Date1TextBox.Text) && !string.IsNullOrEmpty(Date2TextBox.Text))
if (!string.IsNullOrEmpty(Date1TextBox.Text) && !string.IsNullOrEmpty(Date2TextBox.Text) && age < 0)
{
AgeTextBox.Text = string.Empty;
age = -1;

TimeSpan span = s2.Subtract(s1);

DeathResult.Content = s2.ToShortDateString();
BirthResult.Content = s1.ToShortDateString();
AgeResult.Content = Math.Round(span.Days / 365.25, 0, MidpointRounding.AwayFromZero) + " " + Properties.Resources.years;

if (span >= TimeSpan.Zero)
{
DeathResult.Content = s2.ToShortDateString();
BirthResult.Content = s1.ToShortDateString();
AgeResult.Content = Math.Round(span.Days / 365.25, 0, MidpointRounding.AwayFromZero) + " " + Properties.Resources.years;
}
}

// If death data and age are specified, calculate a birth date
if (string.IsNullOrEmpty(Date1TextBox.Text) && !string.IsNullOrEmpty(Date2TextBox.Text) && age != -1)
if (string.IsNullOrEmpty(Date1TextBox.Text) && !string.IsNullOrEmpty(Date2TextBox.Text) && age >= 0)
{

int year = s2.Year;
int day = s2.Day;
int month = s2.Month;
Expand All @@ -102,9 +109,8 @@ private void CalculateButton_Click(object sender, RoutedEventArgs e)
}

// If birth data and age are specified, calculate a death date
if (!string.IsNullOrEmpty(Date1TextBox.Text) && string.IsNullOrEmpty(Date2TextBox.Text) && age != -1)
if (!string.IsNullOrEmpty(Date1TextBox.Text) && string.IsNullOrEmpty(Date2TextBox.Text) && age >= 0)
{

int year = s1.Year;
int month = s1.Month;
int day = s1.Day;
Expand All @@ -114,7 +120,6 @@ private void CalculateButton_Click(object sender, RoutedEventArgs e)
BirthResult.Content = s1.ToShortDateString();
DeathResult.Content = new DateTime(year, month, day).ToShortDateString();
AgeResult.Content = age + " " + Properties.Resources.years;

}
}
catch
Expand All @@ -126,30 +131,33 @@ private void CalculateButton_Click(object sender, RoutedEventArgs e)
AgeResult.Content = string.Empty;
ShowErrors123();
}

}

/// <summary>
/// Handler for the add/subtract operation.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Calculate2Button_Click(object sender, RoutedEventArgs e)
internal void Calculate2Button_Click(object sender, RoutedEventArgs e)
{
// Reset the results
Result2.Content = "";

// Hide errors if showing
HideErrors4();

if (!string.IsNullOrEmpty(ToBox.Text.ToString()))
if (!string.IsNullOrEmpty(ToBox.Text))
{
//Get the date input and try to add/subtract the specified number of days, months and years.
DateTime s = App.StringToDate(ToBox.Text);
if (!string.IsNullOrEmpty(ToBox.Text))
if (!string.IsNullOrEmpty(ToBox.Text) && s != DateTime.MinValue)
{
ToBox.Text = s.ToShortDateString();
}
else
{
return;
}

int i = 1;

Expand Down Expand Up @@ -183,7 +191,9 @@ private void Calculate2Button_Click(object sender, RoutedEventArgs e)
s = s.AddYears(years * i);
Result2.Content = s.ToShortDateString();
}
catch { ShowErrors4(); }
catch {
ShowErrors4();
}
}
}

Expand Down

0 comments on commit 71500a9

Please sign in to comment.