Skip to content

Commit

Permalink
Finished the date literal tests and fixed a boundary bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Feb 24, 2022
1 parent 8ae7cb9 commit 49d47c9
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,13 @@ public inherited sharing class ortoo_DateLiterals
public Integer compare( DateTime value, Date startDate, Date endDate )
{
Contract.requires( startDate <= endDate, 'compare called with a start date that is higher than the end date ('+ startDate + ', ' + endDate + ')' );

if ( value < DateTime.newInstance( startDate.year(), startDate.month(), startDate.day(), 0, 0, 0 ) )
{
return -1;
}
if ( value > DateTime.newInstance( endDate.year(), endDate.month(), endDate.day(), 23, 59, 29 ) )
Date dayAfterTheEndDate = endDate.addDays( 1 );
if ( value >= DateTime.newInstance( dayAfterTheEndDate.year(), dayAfterTheEndDate.month(), dayAfterTheEndDate.day(), 0, 0, 0 ) )
{
return 1;
}
Expand All @@ -614,8 +616,10 @@ public inherited sharing class ortoo_DateLiterals

private class DateToDateRangeComparer
{
public Integer compare( Date value, Date startDate, Date endDate ) {
public Integer compare( Date value, Date startDate, Date endDate )
{
Contract.requires( startDate <= endDate, 'compare called with a start date that is higher than the end date ('+ startDate + ', ' + endDate + ')' );

if ( value < startDate )
{
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,4 +602,108 @@ private without sharing class ortoo_DateLiteralsTest
System.assertEquals( Date.newInstance( 2024, 03, 01 ), literal.getStartDate(), 'NextNMonths_getStartDate, will return the start of next month' );
System.assertEquals( Date.newInstance( 2024, 07, 31 ), literal.getEndDate(), 'NextNMonths_getEndDate, will return the end of the month, n months in the future' );
}

@isTest
private static void compare_whenGivenADateBeforeTheRange_returnsMinus1() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( Date.newInstance( 2024, 02, 21 ) );
Test.stopTest();

System.assertEquals( -1, got, 'compare, when given a date before the range, will return minus 1' );
}

@isTest
private static void compare_whenGivenADateInsideTheRange_returnsZero() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( Date.newInstance( 2024, 02, 23 ) );
Test.stopTest();

System.assertEquals( 0, got, 'compare, when given a date inside the range, will return zero' );
}

@isTest
private static void compare_whenGivenADateAfterTheRange_returns1() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( Date.newInstance( 2024, 02, 24 ) );
Test.stopTest();

System.assertEquals( 1, got, 'compare, when given a date after the range, will return one' );
}

@isTest
private static void compare_whenGivenADateTimeJustBeforeTheRange_returnsMinus1() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( DateTime.newInstance( 2024, 02, 21, 23, 59, 59 ) );
Test.stopTest();

System.assertEquals( -1, got, 'compare, when given a datetime just before the range, will return minus 1' );
}

@isTest
private static void compare_whenGivenADateTimeJustAtStartOfTheRange_returnsZero() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( DateTime.newInstance( 2024, 02, 22, 0, 0, 1 ) );
Test.stopTest();

System.assertEquals( 0, got, 'compare, when given a datetime just at the start of the range, will return 0' );
}

@isTest
private static void compare_whenGivenADateTimeInTheMiddleOfTheRange_returnsZero() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( DateTime.newInstance( 2024, 02, 22, 12, 0, 0 ) );
Test.stopTest();

System.assertEquals( 0, got, 'compare, when given a datetime in the middle of the range, will return 0' );
}

@isTest
private static void compare_whenGivenADateTimeJustAtEndOfTheRange_returnsZero() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( DateTime.newInstance( 2024, 02, 23, 23, 59, 59 ) );
Test.stopTest();

System.assertEquals( 0, got, 'compare, when given a datetime just at the end of the range, will return 0' );
}

@isTest
private static void compare_whenGivenADateTimeJustAfterTheRange_returns1() // NOPMD: Test method name format
{
ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 );
ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 );

Test.startTest();
Integer got = literal.compare( DateTime.newInstance( 2024, 02, 24, 0, 0, 0 ) );
Test.stopTest();

System.assertEquals( 1, got, 'compare, when given a datetime just after the range, will return 1' );
}
}

0 comments on commit 49d47c9

Please sign in to comment.