Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions test-pnpm/morning_routine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
As a forgetful person, I need to remember my morning routine. Because of this, I have created a program that, depending on the time of day, will tell me what I should be doing:

```
From 06:00 to 06:59 - Do exercise
From 07:00 to 07:59 - Read and study
From 08:00 to 08:59 - Have breakfast
```

## Introduction

This kata could be done with two approaches:

- From scratch
- Refactoring the provided implementation

## Approach from scratch

### Story: Morning Routine Reminder

#### User Story:

`As a forgetful person,` `I want a program that reminds me of my morning routine,` `So that I can stay on track with my activities.`

**Scenario 1**: Display "Do exercise" between 06:00 and 06:59 **(Done)**

`Given the current time is between 06:00 and 06:59` `When I request the routine activity` `Then the system should display "Do exercise"`

**Scenario 2**: Display "Read and study" between 07:00 and 07:59 **(Done)**

`Given the current time is between 07:00 and 07:59` `When I request the routine activity` `Then the system should display "Read and study"`

**Scenario 3**: Display "Have breakfast" between 08:00 and 08:59 **(Done)**

`Given the current time is between 08:00 and 08:59` `When I request the routine activity` `Then the system should display "Have breakfast"`

**Scenario 4**: Display "No activity" outside the defined time range **(Done)**

```
Given the current time is before 05:59 or after 09:00
When I request the routine activity
Then the system should display "No activity"
```

## Iteration 2 - More precise time

Now I'd like to be able to add things to do, that take less than an Hour as well as update the list of things to do for example:

```
From 06:00 to 06:59 - Do exercise
From 07:00 to 07:29 - Read
From 07:30 to 07:59 - Study
From 08:00 to 08:59 - Have breakfast
From 06:00 to 06:44 - Do exercise
From 06:45 to 06:59 - Take a shower
From 07:00 to 07:29 - Read
From 07:30 to 07:59 - Study
From 08:00 to 09:00 - Have breakfast
```

## Conclusion

Thank you to ***Emmanuel Valverde*** from *Codurance*. This is really nice kata, not hard but made me think about dates and timezones, something all developers will face at some time of their careers. It is very simple to get started, will include a way of reducing the if or switch patterns for more interesting tuples, not tricky and easy to show very simple TDD paterns. Easy to do in any language and easy to simplify. So for **iteration 2**, I would probably use a functional style, rather than class and get more specific on the minutes with the tuple so will talk it through in theory but not do. Thanks Codurance and Emmanuel for a gem.

```javascript
export function remindMeAt(dateTime: Date): string {
const activities: [string, (dateTime: Date) => boolean][] = [
['Read', isReadTime],
// ... others here
];

for (const [activity, condition] of activities) {
if (condition(dateTime)) {
return activity;
}
}

return 'No activity';
}

function isReadTime(dateTime: Date): boolean {
return dateTime.getHours() === 6 && dateTime.getMinutes() >= 0 && dateTime.getMinutes() <= 29;
}
// ... others here
```



58 changes: 58 additions & 0 deletions test-pnpm/morning_routine/morning.routine.resolver.should.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { MorningRoutineResolver } from './morning.routine.resolver';

describe('MorningRoutineResolver should', () => {
const sut = new MorningRoutineResolver();

test('remind me to do exercise between 06:00 and 6:59', () => {
const ukTimeBST = '2023-06-01T06:30:00+01:00';
const expected = 'Do exercise';

const result = sut.remindMeAt(new Date(ukTimeBST));

expect(result).toBe(expected);
});

test('remind me not to do exercise between not 06:00 and 6:59', () => {
const ukTimeBST = '2023-06-01T05:30:00+01:00';
const notExpected = 'Do exercise';

const result = sut.remindMeAt(new Date(ukTimeBST));

expect(result).not.toBe(notExpected);
});

test('remind me to read and study between 7.00 and 7:59', () => {
const ukTimeBST = '2023-06-01T07:30:00+01:00';
const expected = 'Read and study';

const result = sut.remindMeAt(new Date(ukTimeBST));

expect(result).toBe(expected);
});

test('remind me not to read and study not between 7.00 and 7:59', () => {
const ukTimeBST = '2023-06-01T05:30:00+01:00';
const expected = 'Read and study';

const result = sut.remindMeAt(new Date(ukTimeBST));

expect(result).not.toBe(expected);
});

test('remind me to have breakfast between 8.00 and 8:59', () => {
const ukTimeBST = '2023-06-01T08:30:00+01:00';
const expected = 'Have breakfast';

const result = sut.remindMeAt(new Date(ukTimeBST));

expect(result).toBe(expected);
});

test.each([
{ value: '2023-06-01T05:59:00+01:00', expected: 'No activity' },
{ value: '2023-06-01T09:00:00+01:00', expected: 'No activity' },
])('remind $value to equal $expected', ({ value, expected }) => {
const actual = sut.remindMeAt(new Date(value));
expect(actual).toBe(expected);
});
});
27 changes: 27 additions & 0 deletions test-pnpm/morning_routine/morning.routine.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class MorningRoutineResolver {
public remindMeAt(dateTime: Date): string {
const activities: [string, (dateTime: Date) => boolean][] = [
['Do exercise', this.isExerciseHour],
['Read and study', this.isReadAndStudyHour],
['Have breakfast', this.isBreakfastHour],
];
for (const [activity, condition] of activities) {
if (condition(dateTime)) {
return activity;
}
}
return 'No activity';
}

private isBreakfastHour(dateTime: Date) {
return dateTime.getHours() === 8;
}

private isReadAndStudyHour(dateTime: Date) {
return dateTime.getHours() === 7;
}

private isExerciseHour(dateTime: Date) {
return dateTime.getHours() === 6;
}
}
Loading