Skip to content

Commit

Permalink
Add Test Case and fix bugs to make it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Mar 20, 2022
1 parent 95ce6f3 commit 5d3e8ce
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
2 changes: 1 addition & 1 deletion TestForFailure/AppState/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public partial class AppStateTest
[Description("Test if Tester is working")]
public void CheckUnitTester()
{
Assert.AreEqual(1 + 1, 2);
Assert.AreEqual(2, 1 + 1);
}
}
40 changes: 33 additions & 7 deletions TestForFailure/AppState/Database.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using YouAreAFailure.Classes;

using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Windows.Storage;

Expand Down Expand Up @@ -32,7 +33,7 @@ public async Task DatabaseLoading(string saveData, List<DateTime> expected)
var state = new AppState();
await state.DatabaseReady;

CollectionAssert.AreEquivalent(state.WatchedDate, expected);
CollectionAssert.AreEquivalent(expected, state.WatchedDate);
}

private static IEnumerable<object[]> DatabaseLoadingTestCase()
Expand Down Expand Up @@ -89,23 +90,27 @@ public async Task DatabaseSaverToday()
Assert.IsTrue(state.IsAllWatched);

CollectionAssert.AreEquivalent(
state.WatchedDate, new List<DateTime>() { today }
new List<DateTime>() { today }, state.WatchedDate
);

await state.DatabaseReady;

var wrote = await FileIO.ReadTextAsync(sf);

Assert.AreEqual(
wrote.Trim(),
$"{SaveHeader}\n{today.Year} {today.Month} {today.Day}"
$"{SaveHeader}\n{today.Year} {today.Month} {today.Day}",
wrote.Trim()
);
}

[TestMethod]
[Description("Test if database save as intended with given data")]
[DynamicData(nameof(DatabaseSaverTestCase), DynamicDataSourceType.Method)]
public async Task DatabaseSaver(List<DateTime> testData, string expected)
[DynamicData(nameof(DatabaseSaverTestCase), DynamicDataSourceType.Method,
DynamicDataDisplayName = nameof(DatabaseSaverCaseName))]
public async Task DatabaseSaver(
List<DateTime> testData,
string expected,
object? _ = null)
{
var sf = await ApplicationData.Current.RoamingFolder.CreateFileAsync(
AppState.FileName, CreationCollisionOption.OpenIfExists
Expand All @@ -119,7 +124,7 @@ public async Task DatabaseSaver(List<DateTime> testData, string expected)

var wrote = await FileIO.ReadTextAsync(sf);

Assert.AreEqual(wrote.Trim(), expected.Replace("\r\n", "\n").Trim());
Assert.AreEqual(expected.Replace("\r\n", "\n").Trim(), wrote.Trim());
}

private static IEnumerable<object[]> DatabaseSaverTestCase()
Expand All @@ -146,5 +151,26 @@ 2024 12 31
new List<DateTime>(),
$"{SaveHeader}\n",
};

var notToday = new DateTime(1980, 1, 1);
List<DateTime> dates = new();
string longBoi = $"{SaveHeader}\n";
var random = new Random();

for (int i = 0; i < 4000; i++)
{
notToday = notToday.AddDays(random.Next(3) + 1);
dates.Add(notToday);
longBoi += $"{notToday.Year} {notToday.Month} {notToday.Day}\n";
}

yield return new object[] { dates, longBoi, "Very Long Case (4000 Days)" };
}

public static string DatabaseSaverCaseName(MethodInfo _, object[] values)
{
string? overrideName = values.Length > 2 ? values[2] as string : null;

return overrideName ?? (values[1] as string)!;
}
}
64 changes: 58 additions & 6 deletions TestForFailure/AppState/StreakComputation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using YouAreAFailure.Classes;

using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

#nullable enable
Expand All @@ -11,31 +12,36 @@ public partial class AppStateTest
{
// Test related to Streak Computation

private readonly DateTime todayDefault = new(2022, 03, 30);

[TestMethod]
[Description("Test Streak Computation")]
[DynamicData(nameof(StreakComputationTestCase), DynamicDataSourceType.Method)]
[DynamicData(nameof(StreakComputationTestCase), DynamicDataSourceType.Method,
DynamicDataDisplayName = nameof(StreakComputationCaseName))]
public async Task StreakComputation(
List<DateTime> TestData,
int expectedCurrent,
int expectedLongest
)
int expectedLongest,
int _,
DateTime? today = null)
{
var state = new AppState();
await state.DatabaseReady;

var stateYeeter = new PrivateObject(state);

var today = new DateTime(2022, 03, 30);
today ??= todayDefault;
stateYeeter.SetField("Today", today);

state.WatchedDate = TestData;

Assert.AreEqual(state.ComputeCurrentStreak(), expectedCurrent);
Assert.AreEqual(state.ComputeLongestStreak(), expectedLongest);
Assert.AreEqual(expectedCurrent, state.ComputeCurrentStreak());
Assert.AreEqual(expectedLongest, state.ComputeLongestStreak());
}

private static IEnumerable<object[]> StreakComputationTestCase()
{
// Test 1
yield return new object[]
{
new List<DateTime>()
Expand All @@ -52,8 +58,10 @@ private static IEnumerable<object[]> StreakComputationTestCase()
},
2,
4,
1,
};

// Test 2
yield return new object[]
{
new List<DateTime>()
Expand All @@ -62,8 +70,10 @@ private static IEnumerable<object[]> StreakComputationTestCase()
},
0,
1,
2,
};

// Test 3
yield return new object[]
{
new List<DateTime>()
Expand All @@ -72,13 +82,55 @@ private static IEnumerable<object[]> StreakComputationTestCase()
},
1,
1,
3,
};

// Test 4
yield return new object[]
{
new List<DateTime>() {},
0,
0,
4,
};

// Test 5
yield return new object[]
{
new List<DateTime>()
{
new DateTime(2022, 03, 24),
new DateTime(2022, 03, 25),
new DateTime(2022, 03, 26),
new DateTime(2022, 03, 27),
new DateTime(2022, 03, 28),
new DateTime(2022, 03, 29),
new DateTime(2022, 03, 30),
},
7,
7,
5,
};

// Test 6

List<DateTime> dates = new();
var notToday = new DateTime(1980, 1, 3);

for (int i = 0; i < 10000; i++)
{
dates.Add(notToday.AddDays(2 * i));
}

yield return new object[] { dates, 0, 1, 6, new DateTime(9000, 1, 1) };
}

public static string StreakComputationCaseName(MethodInfo _, object[] values)
{
var expectedCurrent = values[1] as int?;
var expectedLongest = values[2] as int?;
var tcid = values[3] as int?;

return $"Test #{tcid}: Current = {expectedCurrent}, Longest = {expectedLongest}";
}
}
1 change: 1 addition & 0 deletions YouAreAFailure/Classes/AppState/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public int ComputeLongestStreak()
}
}

maxStreak = Math.Max(maxStreak, currStreak);
return maxStreak + 1;
}
}
2 changes: 1 addition & 1 deletion YouAreAFailure/Failure/Statistics.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
WatchStatus.Text = "Today Status: "
+ (App.Current.State.IsAllWatched
? "Treatment Completed!"
? "Treatment Completed!"
: "Go back and watch the video!");

FailureCalendar.FirstDayOfWeek = GlobalizationPreferences.WeekStartsOn;
Expand Down

0 comments on commit 5d3e8ce

Please sign in to comment.