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 4, 2025
1 parent 6951b01 commit 698e42d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 28 deletions.
88 changes: 88 additions & 0 deletions FamilyShow.Tests/AnotherCulture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Globalization;

/// <summary>
/// Makes it easy to execute test code under a specific culture.
/// </summary>
/// <example>
/// using (AnotherCulture.UnitedStates())
/// {
/// // Execute code in United States's culture.
/// }
/// // Back to the original culture.
/// </example>
/// <author>Joshua Poehls</author>
internal sealed class AnotherCulture : IDisposable
{
private bool _disposed;
private readonly CultureInfo _originalCurrentCulture;
private readonly CultureInfo _originalCurrentUICulture;
private readonly CultureInfo? _originalDefaultThreadCurrentCulture;
private readonly CultureInfo? _originalDefaultThreadCurrentUICulture;

public AnotherCulture(string cultureName)
{
_originalCurrentCulture = Thread.CurrentThread.CurrentCulture;
_originalCurrentUICulture = Thread.CurrentThread.CurrentUICulture;
_originalDefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentCulture;
_originalDefaultThreadCurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture;

var otherCulture = CultureInfo.CreateSpecificCulture(cultureName);

// Change the current thread's culture.
Thread.CurrentThread.CurrentCulture = otherCulture;
Thread.CurrentThread.CurrentUICulture = otherCulture;

// Change the default culture of any new threads created by the application domain.
// These properties are only available as of .NET 4.5.
CultureInfo.DefaultThreadCurrentCulture = otherCulture;
CultureInfo.DefaultThreadCurrentUICulture = otherCulture;
}

#region - IDisposable Implementation -

public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
Thread.CurrentThread.CurrentCulture = _originalCurrentCulture;
Thread.CurrentThread.CurrentUICulture = _originalCurrentUICulture;
CultureInfo.DefaultThreadCurrentCulture = _originalDefaultThreadCurrentCulture;
CultureInfo.DefaultThreadCurrentUICulture = _originalDefaultThreadCurrentUICulture;
}

// Clean up unmanaged resources here.

// Note disposing has been done.
_disposed = true;
}
}

~AnotherCulture()
{
Dispose(false);
}

#endregion

public static AnotherCulture UnitedStates()
{
return new AnotherCulture("en-US");
}
}
57 changes: 29 additions & 28 deletions FamilyShow.Tests/Controls/DateCalculatorTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Globalization;

namespace FamilyShow.Tests.Controls;
namespace FamilyShow.Tests.Controls;

public class DateCalculatorTest
{
Expand Down Expand Up @@ -32,19 +30,20 @@ public class DateCalculatorTest
[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);
CultureInfo.DefaultThreadCurrentUICulture = 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);
using (new AnotherCulture(culture))
{
// Arrange
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 =
Expand All @@ -69,17 +68,19 @@ public void CalculateButton_ClickTest(string culture, string date1, string date2
[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);
using (new AnotherCulture(culture))
{
// Arrange
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);
}
}
}

0 comments on commit 698e42d

Please sign in to comment.