-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
FlaggedUser.AgeRanges
to flag users based on birthday
- Loading branch information
1 parent
5f72f2d
commit cb4fcbe
Showing
8 changed files
with
514 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using FluentAssertions; | ||
using ModShark.Utils; | ||
|
||
namespace ModShark.Tests.Utils; | ||
|
||
public class AgeRangeTests | ||
{ | ||
[Test] | ||
public void GetStartDate_ShouldReturnStartDate_WhenSet() | ||
{ | ||
var birthday = new DateTime(2024, 1, 1); | ||
var start = new Age(1, 2, 3); | ||
var expected = start.ToDate(birthday); | ||
|
||
var range = new AgeRange(start, null); | ||
var actual = range.GetStartDate(birthday); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void GetStartDate_ShouldReturnMinDate_WhenNotSet() | ||
{ | ||
var birthday = new DateTime(2024, 1, 1); | ||
|
||
var range = new AgeRange(null, null); | ||
var actual = range.GetStartDate(birthday); | ||
|
||
actual.Should().Be(DateTime.MinValue); | ||
} | ||
|
||
[Test] | ||
public void GetEndDate_ShouldReturnEndDate_WhenSet() | ||
{ | ||
var birthday = new DateTime(2024, 1, 1); | ||
var end = new Age(1, 2, 3); | ||
var expected = end.ToDate(birthday); | ||
|
||
var range = new AgeRange(null, end); | ||
var actual = range.GetEndDate(birthday); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void GetEndDate_ShouldReturnMaxDate_WhenNotSet() | ||
{ | ||
var birthday = new DateTime(2024, 1, 1); | ||
|
||
var range = new AgeRange(null, null); | ||
var actual = range.GetEndDate(birthday); | ||
|
||
actual.Should().Be(DateTime.MaxValue); | ||
} | ||
|
||
[TestCase("1y2m3d - 4y5m6d", 1, 2, 3, 4, 5, 6)] | ||
[TestCase("1y - 2y", 1, 0, 0, 2, 0, 0)] | ||
[TestCase("1m - 1y", 0, 1, 0, 1, 0, 0)] | ||
[TestCase("123y - 456y", 123, 0, 0, 456, 0, 0)] | ||
[TestCase(" 1y - 3y ", 1, 0, 0, 3, 0, 0)] | ||
public void Parse_ShouldParseStandardInputs(string input, int y1, int m1, int d1, int y2, int m2, int d2) | ||
{ | ||
var start = new Age(y1, m1, d1); | ||
var end = new Age(y2, m2, d2); | ||
var expected = new AgeRange(start, end); | ||
|
||
var actual = AgeRange.Parse(input); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void Parse_ShouldAllowEndToBeExcluded() | ||
{ | ||
var start = new Age(1, 0, 0); | ||
|
||
var actual = AgeRange.Parse("1y"); | ||
|
||
actual.Start.Should().Be(start); | ||
actual.End.Should().BeNull(); | ||
} | ||
|
||
[TestCase("")] | ||
[TestCase(" ")] | ||
[TestCase("1z")] | ||
[TestCase("Xy")] | ||
[TestCase("1y2m3d4h")] | ||
[TestCase("-1y")] | ||
[TestCase("1 z")] | ||
[TestCase("1y 2m 3d")] | ||
public void Parse_ShouldThrow_WhenInputIsInvalid(string input) | ||
{ | ||
Assert.Throws<ArgumentException>(() => | ||
{ | ||
Age.Parse(input); | ||
}); | ||
} | ||
|
||
[TestCase(true, 18, 0, 0)] | ||
[TestCase(true, 19, 0, 0)] | ||
[TestCase(true, 18, 1, 0)] | ||
[TestCase(true, 18, 0, 1)] | ||
[TestCase(false, 25, 0, 0)] | ||
[TestCase(false, 17, 11, 30)] | ||
[TestCase(false, 17, 0, 30)] | ||
[TestCase(false, 17, 11, 0)] | ||
[TestCase(false, 17, 1, 1)] | ||
[TestCase(false, 26, 0, 0)] | ||
[TestCase(false, 25, 1, 0)] | ||
[TestCase(false, 25, 0, 1)] | ||
public void IsInRange_Age_ShouldReturnTrueWhenAgeIsInRange(bool expected, int y, int m, int d) | ||
{ | ||
var range = new AgeRange(new Age(18, 0, 0), new Age(25, 0, 0)); | ||
var age = new Age(y, m, d); | ||
|
||
var actual = range.IsInRange(age); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[TestCase(true, 18, 0, 0)] | ||
[TestCase(true, 19, 0, 0)] | ||
[TestCase(true, 18, 1, 0)] | ||
[TestCase(true, 18, 0, 1)] | ||
[TestCase(false, 25, 0, 0)] | ||
[TestCase(false, 17, 11, 30)] | ||
[TestCase(false, 17, 0, 30)] | ||
[TestCase(false, 17, 11, 0)] | ||
[TestCase(false, 17, 1, 1)] | ||
[TestCase(false, 26, 0, 0)] | ||
[TestCase(false, 25, 1, 0)] | ||
[TestCase(false, 25, 0, 1)] | ||
public void IsInRange_Date_ShouldReturnTrueWhenInRange(bool expected, int y, int m, int d) | ||
{ | ||
var range = new AgeRange(new Age(18, 0, 0), new Age(25, 0, 0)); | ||
var birthday = new DateTime(2000, 1, 1); | ||
var today = birthday.AddYears(y).AddMonths(m).AddDays(d); | ||
|
||
var actual = range.IsInRange(birthday, today); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void IsInRange_Date_ShouldThrow_WhenBirthdayIsFuture() | ||
{ | ||
var range = new AgeRange(null, null); | ||
|
||
Assert.Throws<ArgumentOutOfRangeException>(() => | ||
{ | ||
range.IsInRange(new DateTime(2024, 12, 31), new DateTime(2024, 1, 1)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using FluentAssertions; | ||
using ModShark.Utils; | ||
|
||
namespace ModShark.Tests.Utils; | ||
|
||
public class AgeTests | ||
{ | ||
[Test] | ||
public void Comparisons_ShouldRespectMemberPriority() | ||
{ | ||
var first = new Age(0, 4, 5); | ||
var second = new Age(1, 2, 3); | ||
var third = new Age(1, 3, 3); | ||
var fourth = new Age(1, 3, 4); | ||
var fourth2 = new Age(1, 3, 4); | ||
|
||
first.Should().BeLessThan(second); | ||
second.Should().BeLessThan(third); | ||
third.Should().BeLessThan(fourth); | ||
|
||
fourth.Should().BeGreaterThan(third); | ||
third.Should().BeGreaterThan(second); | ||
second.Should().BeGreaterThan(first); | ||
|
||
fourth2.Should().Be(fourth); | ||
} | ||
|
||
[Test] | ||
public void ToDate_ShouldAddYears() | ||
{ | ||
var birthday = new DateTime(2024, 8, 1); | ||
var age = new Age(1); | ||
|
||
var result = age.ToDate(birthday); | ||
|
||
var expected = new DateTime(2025, 8, 1); | ||
result.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void ToDate_ShouldAddMonths() | ||
{ | ||
var birthday = new DateTime(2024, 8, 1); | ||
var age = new Age(0, 1); | ||
|
||
var result = age.ToDate(birthday); | ||
|
||
var expected = new DateTime(2024, 9, 1); | ||
result.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void ToDate_ShouldAddDays() | ||
{ | ||
var birthday = new DateTime(2024, 8, 1); | ||
var age = new Age(0, 0, 1); | ||
|
||
var result = age.ToDate(birthday); | ||
|
||
var expected = new DateTime(2024, 8, 2); | ||
result.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void ToDate_ShouldAddAllValues() | ||
{ | ||
var birthday = new DateTime(2024, 8, 1); | ||
var age = new Age(1, 2, 3); | ||
|
||
var result = age.ToDate(birthday); | ||
|
||
var expected = new DateTime(2025, 10, 4); | ||
result.Should().Be(expected); | ||
} | ||
|
||
[TestCase("1y2m3d", 1, 2, 3)] | ||
[TestCase("1y", 1, 0, 0)] | ||
[TestCase("1m", 0, 1, 0)] | ||
[TestCase("1d", 0, 0, 1)] | ||
[TestCase("1y1m", 1, 1, 0)] | ||
[TestCase("1m1d", 0, 1, 1)] | ||
[TestCase("1y1d", 1, 0, 1)] | ||
[TestCase("123y", 123, 0, 0)] | ||
[TestCase("12y34m56d", 12, 34, 56)] | ||
[TestCase(" 1y ", 1, 0, 0)] | ||
public void Parse_ShouldParseValidInputs(string input, int years, int months, int days) | ||
{ | ||
var expected = new Age(years, months, days); | ||
|
||
var actual = Age.Parse(input); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[TestCase("")] | ||
[TestCase(" ")] | ||
[TestCase("1z")] | ||
[TestCase("Xy")] | ||
[TestCase("1y2m3d4h")] | ||
[TestCase("-1y")] | ||
[TestCase("1 z")] | ||
[TestCase("1y 2m 3d")] | ||
public void Parse_ShouldThrow_WhenInputIsInvalid(string input) | ||
{ | ||
Assert.Throws<ArgumentException>(() => | ||
{ | ||
Age.Parse(input); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.