Created
August 8, 2020 16:23
Star
You must be signed in to star a gist
Test-Driven Development with Christmas Sundays
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
import findChristmasSundays from './findChristmasSundays' | |
describe('findChristmasSundays', () => { | |
it('throws an error, if the start year is not a positive integer', () => { | |
const expectedError = new Error('startYear must be a positive integer') | |
expect(() => findChristmasSundays(0.1, 1)) | |
.toThrowError(expectedError) | |
expect(() => findChristmasSundays(0, 1)) | |
.toThrowError(expectedError) | |
expect(() => findChristmasSundays(-1, 1)) | |
.toThrowError(expectedError) | |
}) | |
it('throws an error, if the end year is not a positive integer', () => { | |
const expectedError = new Error('endYear must be a positive integer') | |
expect(() => findChristmasSundays(1, 0.1)) | |
.toThrowError(expectedError) | |
expect(() => findChristmasSundays(1, 0)) | |
.toThrowError(expectedError) | |
expect(() => findChristmasSundays(1, -1)) | |
.toThrowError(expectedError) | |
}) | |
it('throws an error, if the end year is less than or equal to the start year', () => { | |
const earlierYear = 2000 | |
const laterYear = 2001 | |
const expectedError = new Error('endYear must be greater than startYear') | |
expect(() => findChristmasSundays(earlierYear, earlierYear)) | |
.toThrowError(expectedError) | |
expect(() => findChristmasSundays(laterYear, earlierYear)) | |
.toThrowError(expectedError) | |
}) | |
it('returns an array', () => { | |
expect(findChristmasSundays(2000, 2001)).toBeInstanceOf(Array) | |
}) | |
it('returns the start year, if the start year has Christmas on a Sunday', () => { | |
const christmasSundayYear = 2022 | |
const actual = findChristmasSundays(christmasSundayYear, christmasSundayYear + 1) | |
expect(actual).toContain(christmasSundayYear) | |
}) | |
it('does not return the start year, if the start year does not have Christmas on a Sunday', () => { | |
const nonChristmasSundayYear = 2020 | |
const actual = findChristmasSundays(nonChristmasSundayYear, nonChristmasSundayYear + 1) | |
expect(actual).not.toContain(nonChristmasSundayYear) | |
}) | |
it('returns all years where Christmas is on a Sunday within a range', () => { | |
const startYear = 2000 | |
const endYear = 2030 | |
const actual = findChristmasSundays(startYear, endYear) | |
const expected = [2005, 2011, 2016, 2022] | |
expect(actual).toStrictEqual(expected) | |
}) | |
it('does not include the end year, even if the end year has Christmas on a Sunday', () => { | |
const startYear = 2000 | |
const endYear = 2022 | |
const actual = findChristmasSundays(startYear, endYear) | |
const expected = [2005, 2011, 2016] | |
expect(actual).toStrictEqual(expected) | |
}) | |
}) |
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
function hasChristmasSunday (year: number): boolean { | |
const DAY_OF_WEEK_SUNDAY = 0 | |
// With Date, months are 0-indexed, so 11 means December | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | |
const christmasDate = new Date(year, 11, 25) | |
return christmasDate.getDay() === DAY_OF_WEEK_SUNDAY | |
} | |
function findChristmasSundays (startYear: number, endYear: number): number[] { | |
if (startYear <= 0 || !Number.isInteger(startYear)) { | |
throw new Error('startYear must be a positive integer') | |
} | |
if (endYear <= 0 || !Number.isInteger(endYear)) { | |
throw new Error('endYear must be a positive integer') | |
} | |
if (endYear <= startYear) { | |
throw new Error('endYear must be greater than startYear') | |
} | |
const christmasSundays = [] | |
for (let year = startYear; year < endYear; year++) { | |
if (hasChristmasSunday(year)) { | |
christmasSundays.push(year) | |
} | |
} | |
return christmasSundays | |
} | |
export default findChristmasSundays |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment