From b609d2149895db0adc5a3dbbf95b7ed687619c28 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 16:04:42 -0300 Subject: [PATCH 01/15] [green]: should know that the result for "TRUE" is true --- .../3_2_Boolean_Calculator/src/index.spec.ts | 8 +++++++- .../3_2_Boolean_Calculator/src/index.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 1cef211dc..7dd52b941 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -1,4 +1,10 @@ +import {BooleanCalculator} from "./index"; describe('boolean calculator', () => { - + describe('single values', () => { + it('should know that the result for "TRUE" is true', () => { + const expression = 'TRUE'; + expect(BooleanCalculator.run(expression)).toBeTruthy(); + }) + }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index e69de29bb..021f076ea 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -0,0 +1,8 @@ +export type Expression = string; +export type Result = boolean; + +export class BooleanCalculator { + static run(expression: Expression): Result { + return true + } +} \ No newline at end of file From 670912502adea6dec573ff5d23ac3052bafd0502 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 16:08:55 -0300 Subject: [PATCH 02/15] [green]: should know that the result for "FALSE" is false --- .../3_2_Boolean_Calculator/src/index.spec.ts | 7 ++++++- .../3_2_Boolean_Calculator/src/index.ts | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 7dd52b941..973b84d18 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -5,6 +5,11 @@ describe('boolean calculator', () => { it('should know that the result for "TRUE" is true', () => { const expression = 'TRUE'; expect(BooleanCalculator.run(expression)).toBeTruthy(); - }) + }); + + it('should know that the result for "FALSE" is false', () => { + const expression = 'FALSE'; + expect(BooleanCalculator.run(expression)).toBeFalsy(); + }); }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index 021f076ea..eb9366988 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -3,6 +3,9 @@ export type Result = boolean; export class BooleanCalculator { static run(expression: Expression): Result { - return true + if(expression === 'FALSE') { + return false; + } + return true; } } \ No newline at end of file From 1471e296bb8aafb08aaa8da320c55a1c502e8090 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 16:12:54 -0300 Subject: [PATCH 03/15] [green]: should know that the result for "NOT TRUE" is false --- .../3_2_Boolean_Calculator/src/index.spec.ts | 8 ++++++++ .../3_2_Boolean_Calculator/src/index.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 973b84d18..83722103a 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -1,6 +1,7 @@ import {BooleanCalculator} from "./index"; describe('boolean calculator', () => { + describe('single values', () => { it('should know that the result for "TRUE" is true', () => { const expression = 'TRUE'; @@ -12,4 +13,11 @@ describe('boolean calculator', () => { expect(BooleanCalculator.run(expression)).toBeFalsy(); }); }) + + describe('NOT operator', () => { + it('should know that the result for "NOT TRUE" is false', () => { + const expression = 'NOT TRUE'; + expect(BooleanCalculator.run(expression)).toBeFalsy(); + }) + }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index eb9366988..0ab74cf86 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -3,7 +3,7 @@ export type Result = boolean; export class BooleanCalculator { static run(expression: Expression): Result { - if(expression === 'FALSE') { + if(expression === 'FALSE' || expression === 'NOT TRUE') { return false; } return true; From e39348dac2655459fa59c4de97b0845c1d16dcf1 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 16:17:01 -0300 Subject: [PATCH 04/15] [green]: should know that the result for "NOT FALSE" is true --- .../3_2_Boolean_Calculator/src/index.spec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 83722103a..9d7a9e7e0 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -18,6 +18,11 @@ describe('boolean calculator', () => { it('should know that the result for "NOT TRUE" is false', () => { const expression = 'NOT TRUE'; expect(BooleanCalculator.run(expression)).toBeFalsy(); - }) + }); + + it('should know that the result for "NOT FALSE" is true', () => { + const expression = 'NOT FALSE'; + expect(BooleanCalculator.run(expression)).toBeTruthy(); + }); }) }) From b8950a2b9c8af084120cdcd266c08fa6a7f8764d Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 16:21:00 -0300 Subject: [PATCH 05/15] [green]: should know that the result for "TRUE AND FALSE" is false --- .../3_2_Boolean_Calculator/src/index.spec.ts | 7 +++++++ .../3_2_Boolean_Calculator/src/index.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 9d7a9e7e0..5e4186fed 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -25,4 +25,11 @@ describe('boolean calculator', () => { expect(BooleanCalculator.run(expression)).toBeTruthy(); }); }) + + describe('AND operator', () => { + it('should know that the result for "TRUE AND FALSE" is false', () => { + const expression = 'TRUE AND FALSE'; + expect(BooleanCalculator.run(expression)).toBeFalsy(); + }); + }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index 0ab74cf86..b0eb56544 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -3,7 +3,7 @@ export type Result = boolean; export class BooleanCalculator { static run(expression: Expression): Result { - if(expression === 'FALSE' || expression === 'NOT TRUE') { + if(expression === 'FALSE' || expression === 'NOT TRUE' || expression === 'TRUE AND FALSE') { return false; } return true; From 45d542d0bf2e3049b81abbcd1d2b99709c0cf06c Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 17:31:52 -0300 Subject: [PATCH 06/15] [green]: should know that the result for "TRUE AND TRUE" is true --- .../3_2_Boolean_Calculator/src/index.spec.ts | 5 +++++ .../3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts | 5 +---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 5e4186fed..4ffb99b5f 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -31,5 +31,10 @@ describe('boolean calculator', () => { const expression = 'TRUE AND FALSE'; expect(BooleanCalculator.run(expression)).toBeFalsy(); }); + + it('should know that the result for "TRUE AND TRUE" is true', () => { + const expression = 'TRUE AND TRUE'; + expect(BooleanCalculator.run(expression)).toBeTruthy(); + }); }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index b0eb56544..20d59d692 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -3,9 +3,6 @@ export type Result = boolean; export class BooleanCalculator { static run(expression: Expression): Result { - if(expression === 'FALSE' || expression === 'NOT TRUE' || expression === 'TRUE AND FALSE') { - return false; - } - return true; + return !(expression === 'FALSE' || expression === 'NOT TRUE' || expression === 'TRUE AND FALSE'); } } \ No newline at end of file From 0104b92a51fe2f6fce06178032b026db8bcca1b8 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 17:34:39 -0300 Subject: [PATCH 07/15] [green]: should know that the result for "TRUE OR FALSE" is true --- .../3_2_Boolean_Calculator/src/index.spec.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 4ffb99b5f..772278ffb 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -15,25 +15,32 @@ describe('boolean calculator', () => { }) describe('NOT operator', () => { + it('should know that the result for "NOT FALSE" is true', () => { + const expression = 'NOT FALSE'; + expect(BooleanCalculator.run(expression)).toBeTruthy(); + }); + it('should know that the result for "NOT TRUE" is false', () => { const expression = 'NOT TRUE'; expect(BooleanCalculator.run(expression)).toBeFalsy(); }); + }) - it('should know that the result for "NOT FALSE" is true', () => { - const expression = 'NOT FALSE'; + describe('AND operator', () => { + it('should know that the result for "TRUE AND TRUE" is true', () => { + const expression = 'TRUE AND TRUE'; expect(BooleanCalculator.run(expression)).toBeTruthy(); }); - }) - describe('AND operator', () => { it('should know that the result for "TRUE AND FALSE" is false', () => { const expression = 'TRUE AND FALSE'; expect(BooleanCalculator.run(expression)).toBeFalsy(); }); + }) - it('should know that the result for "TRUE AND TRUE" is true', () => { - const expression = 'TRUE AND TRUE'; + describe('OR operator', () => { + it('should know that the result for "TRUE OR FALSE" is true', () => { + const expression = 'TRUE OR FALSE'; expect(BooleanCalculator.run(expression)).toBeTruthy(); }); }) From 8d6aab9993b62739f54e6743570960a7371ff444 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 17:37:29 -0300 Subject: [PATCH 08/15] [green]: should know that the result for "FALSE OR FALSE" is false --- .../3_2_Boolean_Calculator/src/index.spec.ts | 5 +++++ .../3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 772278ffb..138ee8544 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -43,5 +43,10 @@ describe('boolean calculator', () => { const expression = 'TRUE OR FALSE'; expect(BooleanCalculator.run(expression)).toBeTruthy(); }); + + it('should know that the result for "FALSE OR FALSE" is false', () => { + const expression = 'FALSE OR FALSE'; + expect(BooleanCalculator.run(expression)).toBeFalsy(); + }); }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index 20d59d692..2a1a9682b 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -3,6 +3,6 @@ export type Result = boolean; export class BooleanCalculator { static run(expression: Expression): Result { - return !(expression === 'FALSE' || expression === 'NOT TRUE' || expression === 'TRUE AND FALSE'); + return !(expression === 'FALSE' || expression === 'NOT TRUE' || expression === 'TRUE AND FALSE' || expression === 'FALSE OR FALSE' ); } } \ No newline at end of file From 654c1d8216b8fb815be32839c97efed1441a4379 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Fri, 17 Jan 2025 17:39:51 -0300 Subject: [PATCH 09/15] [green]: should know that the result for "TRUE OR TRUE OR TRUE AND FALSE" is true --- .../3_2_Boolean_Calculator/src/index.spec.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 138ee8544..37d7e74d0 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -49,4 +49,11 @@ describe('boolean calculator', () => { expect(BooleanCalculator.run(expression)).toBeFalsy(); }); }) + + describe('combination of operations and precedence', () => { + it('should know that the result for "TRUE OR TRUE OR TRUE AND FALSE" is true', () => { + const expression = 'TRUE OR TRUE OR TRUE AND FALSE'; + expect(BooleanCalculator.run(expression)).toBeTruthy(); + }); + }) }) From 17e92b6813c40030f1213b02a3a4dcb9f8dc53fd Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Mon, 20 Jan 2025 11:28:37 -0300 Subject: [PATCH 10/15] [green]: should know that the result for "TRUE OR FALSE AND NOT FALSE" is true --- .../3_2_Boolean_Calculator/src/index.spec.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 37d7e74d0..0859f2a58 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -55,5 +55,10 @@ describe('boolean calculator', () => { const expression = 'TRUE OR TRUE OR TRUE AND FALSE'; expect(BooleanCalculator.run(expression)).toBeTruthy(); }); + + it('should know that the result for "TRUE OR FALSE AND NOT FALSE" is true', () => { + const expression = 'TRUE OR FALSE AND NOT FALSE'; + expect(BooleanCalculator.run(expression)).toBeTruthy() + }); }) }) From 7ed34ded7c52d7b34ad0b2f1e4e48f37197bd05d Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Mon, 20 Jan 2025 14:07:22 -0300 Subject: [PATCH 11/15] [green]: should know that the result for "(TRUE OR TRUE OR TRUE) AND FALSE" is false --- .../3_2_Boolean_Calculator/src/index.spec.ts | 5 ++ .../3_2_Boolean_Calculator/src/index.ts | 62 ++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 0859f2a58..390d67233 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -60,5 +60,10 @@ describe('boolean calculator', () => { const expression = 'TRUE OR FALSE AND NOT FALSE'; expect(BooleanCalculator.run(expression)).toBeTruthy() }); + + it('should know that the result for "(TRUE OR TRUE OR TRUE) AND FALSE" is false', () => { + const expression = '(TRUE OR TRUE OR TRUE) AND FALSE'; + expect(BooleanCalculator.run(expression)).toBeFalsy(); + }); }) }) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index 2a1a9682b..899ae44ad 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -3,6 +3,66 @@ export type Result = boolean; export class BooleanCalculator { static run(expression: Expression): Result { - return !(expression === 'FALSE' || expression === 'NOT TRUE' || expression === 'TRUE AND FALSE' || expression === 'FALSE OR FALSE' ); + const tokens: Expression[] = this.tokenize(expression); + const { value } = this.parseExpression(tokens); + return value; + } + + private static tokenize(expression: Expression): Expression[] { + return expression + .replace(/\(/g, ' ( ') + .replace(/\)/g, ' ) ') + .trim() + .split(/\s+/); + } + + private static parseExpression(tokens: string[]): { value: boolean; rest: string[] } { + return this.parseOr(tokens); + } + + private static parseOr(tokens: string[]): { value: boolean; rest: string[] } { + let { value: leftValue, rest: restTokens } = this.parseAnd(tokens); + while (restTokens[0] === 'OR') { + const nextRest = restTokens.slice(1); + const { value: rightValue, rest } = this.parseAnd(nextRest); + leftValue = leftValue || rightValue; + restTokens = rest; + } + return { value: leftValue, rest: restTokens }; + } + + private static parseAnd(tokens: string[]): { value: boolean; rest: string[] } { + let { value: leftValue, rest: restTokens } = this.parseNot(tokens); + while (restTokens[0] === 'AND') { + const nextRest = restTokens.slice(1); + const { value: rightValue, rest } = this.parseNot(nextRest); + leftValue = leftValue && rightValue; + restTokens = rest; + } + return { value: leftValue, rest: restTokens }; + } + + private static parseNot(tokens: string[]): { value: boolean; rest: string[] } { + if (tokens[0] === 'NOT') { + const nextRest = tokens.slice(1); + const { value, rest } = this.parseNot(nextRest); + return { value: !value, rest }; + } + return this.parsePrimary(tokens); + } + + private static parsePrimary(tokens: string[]): { value: boolean; rest: string[] } { + if (tokens[0] === '(') { + const nextRest = tokens.slice(1); + const { value, rest } = this.parseExpression(nextRest); + return { value, rest: rest.slice(1) }; + } + if (tokens[0] === 'TRUE') { + return { value: true, rest: tokens.slice(1) }; + } + if (tokens[0] === 'FALSE') { + return { value: false, rest: tokens.slice(1) }; + } + throw new Error(`Unexpected token: ${tokens[0]}`); } } \ No newline at end of file From e4471e86e4dcfd3fddae56f3c8da2a1096811e58 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Mon, 20 Jan 2025 14:08:26 -0300 Subject: [PATCH 12/15] [green]: should know that the result for "NOT (TRUE AND TRUE)" is false --- .../3_2_Boolean_Calculator/src/index.spec.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 390d67233..5d638351a 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -65,5 +65,10 @@ describe('boolean calculator', () => { const expression = '(TRUE OR TRUE OR TRUE) AND FALSE'; expect(BooleanCalculator.run(expression)).toBeFalsy(); }); + + it('should know that the result for "NOT (TRUE AND TRUE)" is false', () => { + const expression = 'NOT (TRUE AND TRUE)'; + expect(BooleanCalculator.run(expression)).toBeFalsy(); + }); }) }) From 90fd006eb0066162124f61e16b9c9250ec2c285c Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Mon, 20 Jan 2025 14:14:30 -0300 Subject: [PATCH 13/15] [refactor]: better readability --- .../3_2_Boolean_Calculator/src/index.ts | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts index 899ae44ad..c993a60f1 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.ts @@ -21,48 +21,44 @@ export class BooleanCalculator { } private static parseOr(tokens: string[]): { value: boolean; rest: string[] } { - let { value: leftValue, rest: restTokens } = this.parseAnd(tokens); - while (restTokens[0] === 'OR') { - const nextRest = restTokens.slice(1); - const { value: rightValue, rest } = this.parseAnd(nextRest); - leftValue = leftValue || rightValue; - restTokens = rest; + let { value: left, rest } = this.parseAnd(tokens) + while (rest[0] === 'OR') { + const partial = this.parseAnd(rest.slice(1)) + left = left || partial.value + rest = partial.rest } - return { value: leftValue, rest: restTokens }; + return { value: left, rest } } private static parseAnd(tokens: string[]): { value: boolean; rest: string[] } { - let { value: leftValue, rest: restTokens } = this.parseNot(tokens); - while (restTokens[0] === 'AND') { - const nextRest = restTokens.slice(1); - const { value: rightValue, rest } = this.parseNot(nextRest); - leftValue = leftValue && rightValue; - restTokens = rest; + let { value: left, rest } = this.parseNot(tokens) + while (rest[0] === 'AND') { + const partial = this.parseNot(rest.slice(1)) + left = left && partial.value + rest = partial.rest } - return { value: leftValue, rest: restTokens }; + return { value: left, rest } } private static parseNot(tokens: string[]): { value: boolean; rest: string[] } { if (tokens[0] === 'NOT') { - const nextRest = tokens.slice(1); - const { value, rest } = this.parseNot(nextRest); - return { value: !value, rest }; + const partial = this.parseNot(tokens.slice(1)) + return { value: !partial.value, rest: partial.rest } } - return this.parsePrimary(tokens); + return this.parsePrimary(tokens) } private static parsePrimary(tokens: string[]): { value: boolean; rest: string[] } { if (tokens[0] === '(') { - const nextRest = tokens.slice(1); - const { value, rest } = this.parseExpression(nextRest); - return { value, rest: rest.slice(1) }; + const partial = this.parseExpression(tokens.slice(1)) + return { value: partial.value, rest: partial.rest.slice(1) } } if (tokens[0] === 'TRUE') { - return { value: true, rest: tokens.slice(1) }; + return { value: true, rest: tokens.slice(1) } } if (tokens[0] === 'FALSE') { - return { value: false, rest: tokens.slice(1) }; + return { value: false, rest: tokens.slice(1) } } - throw new Error(`Unexpected token: ${tokens[0]}`); + throw new Error(`Unexpected token: ${tokens[0]}`) } -} \ No newline at end of file +} From 9c5249dea83781f2b33555fbfd7c152869e83e46 Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Mon, 20 Jan 2025 16:01:10 -0300 Subject: [PATCH 14/15] [refactor]: use of the .each --- .../3_2_Boolean_Calculator/src/index.spec.ts | 94 ++++++++----------- 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 5d638351a..80a3919c4 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -1,74 +1,56 @@ -import {BooleanCalculator} from "./index"; +import {BooleanCalculator, Expression, Result} from "./index"; describe('boolean calculator', () => { describe('single values', () => { - it('should know that the result for "TRUE" is true', () => { - const expression = 'TRUE'; - expect(BooleanCalculator.run(expression)).toBeTruthy(); - }); - - it('should know that the result for "FALSE" is false', () => { - const expression = 'FALSE'; - expect(BooleanCalculator.run(expression)).toBeFalsy(); - }); + const cases: [Expression, boolean][] = [ + ['TRUE', true], + ['FALSE', false] + ]; + it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + expect(BooleanCalculator.run(expression)).toBe(expected); + }) }) describe('NOT operator', () => { - it('should know that the result for "NOT FALSE" is true', () => { - const expression = 'NOT FALSE'; - expect(BooleanCalculator.run(expression)).toBeTruthy(); - }); - - it('should know that the result for "NOT TRUE" is false', () => { - const expression = 'NOT TRUE'; - expect(BooleanCalculator.run(expression)).toBeFalsy(); - }); + const cases: [Expression, boolean][] = [ + ['NOT FALSE', true], + ['NOT TRUE', false] + ]; + it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + expect(BooleanCalculator.run(expression)).toBe(expected); + }) }) describe('AND operator', () => { - it('should know that the result for "TRUE AND TRUE" is true', () => { - const expression = 'TRUE AND TRUE'; - expect(BooleanCalculator.run(expression)).toBeTruthy(); - }); - - it('should know that the result for "TRUE AND FALSE" is false', () => { - const expression = 'TRUE AND FALSE'; - expect(BooleanCalculator.run(expression)).toBeFalsy(); - }); + const cases: [Expression, boolean][] = [ + ['TRUE AND TRUE', true], + ['TRUE AND FALSE', false] + ]; + it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + expect(BooleanCalculator.run(expression)).toBe(expected); + }) }) describe('OR operator', () => { - it('should know that the result for "TRUE OR FALSE" is true', () => { - const expression = 'TRUE OR FALSE'; - expect(BooleanCalculator.run(expression)).toBeTruthy(); - }); - - it('should know that the result for "FALSE OR FALSE" is false', () => { - const expression = 'FALSE OR FALSE'; - expect(BooleanCalculator.run(expression)).toBeFalsy(); - }); + const cases: [Expression, boolean][] = [ + ['TRUE OR FALSE', true], + ['FALSE OR FALSE', false] + ]; + it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + expect(BooleanCalculator.run(expression)).toBe(expected); + }) }) describe('combination of operations and precedence', () => { - it('should know that the result for "TRUE OR TRUE OR TRUE AND FALSE" is true', () => { - const expression = 'TRUE OR TRUE OR TRUE AND FALSE'; - expect(BooleanCalculator.run(expression)).toBeTruthy(); - }); - - it('should know that the result for "TRUE OR FALSE AND NOT FALSE" is true', () => { - const expression = 'TRUE OR FALSE AND NOT FALSE'; - expect(BooleanCalculator.run(expression)).toBeTruthy() - }); - - it('should know that the result for "(TRUE OR TRUE OR TRUE) AND FALSE" is false', () => { - const expression = '(TRUE OR TRUE OR TRUE) AND FALSE'; - expect(BooleanCalculator.run(expression)).toBeFalsy(); - }); - - it('should know that the result for "NOT (TRUE AND TRUE)" is false', () => { - const expression = 'NOT (TRUE AND TRUE)'; - expect(BooleanCalculator.run(expression)).toBeFalsy(); - }); + const cases: [Expression, boolean][] = [ + ['TRUE OR TRUE OR TRUE AND FALSE', true], + ['TRUE OR FALSE AND NOT FALSE', true], + ['(TRUE OR TRUE OR TRUE) AND FALSE', false], + ['NOT (TRUE AND TRUE)', false] + ] + it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + expect(BooleanCalculator.run(expression)).toBe(expected); + }) }) }) From 686b614190283e65e59d6bfa379ee591443c89ef Mon Sep 17 00:00:00 2001 From: Jose Garrera Date: Mon, 20 Jan 2025 16:03:30 -0300 Subject: [PATCH 15/15] [refactor]: remove duplication --- .../3_2_Boolean_Calculator/src/index.spec.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts index 80a3919c4..cd5f02c0d 100644 --- a/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts +++ b/ThePhasesOfCraftship/2_best_practice_first/testingBasics/exercises/3_Manage_Complexity/3_2_Boolean_Calculator/src/index.spec.ts @@ -1,13 +1,14 @@ import {BooleanCalculator, Expression, Result} from "./index"; describe('boolean calculator', () => { + const testMessage = 'should know that the result for %s is %s'; describe('single values', () => { const cases: [Expression, boolean][] = [ ['TRUE', true], ['FALSE', false] ]; - it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + it.each(cases)(testMessage, (expression: Expression, expected: boolean) => { expect(BooleanCalculator.run(expression)).toBe(expected); }) }) @@ -17,7 +18,7 @@ describe('boolean calculator', () => { ['NOT FALSE', true], ['NOT TRUE', false] ]; - it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + it.each(cases)(testMessage, (expression: Expression, expected: boolean) => { expect(BooleanCalculator.run(expression)).toBe(expected); }) }) @@ -27,7 +28,7 @@ describe('boolean calculator', () => { ['TRUE AND TRUE', true], ['TRUE AND FALSE', false] ]; - it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + it.each(cases)(testMessage, (expression: Expression, expected: boolean) => { expect(BooleanCalculator.run(expression)).toBe(expected); }) }) @@ -37,7 +38,7 @@ describe('boolean calculator', () => { ['TRUE OR FALSE', true], ['FALSE OR FALSE', false] ]; - it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + it.each(cases)(testMessage, (expression: Expression, expected: boolean) => { expect(BooleanCalculator.run(expression)).toBe(expected); }) }) @@ -49,7 +50,7 @@ describe('boolean calculator', () => { ['(TRUE OR TRUE OR TRUE) AND FALSE', false], ['NOT (TRUE AND TRUE)', false] ] - it.each(cases)('should know that the result for %s is %s', (expression: Expression, expected: boolean) => { + it.each(cases)(testMessage, (expression: Expression, expected: boolean) => { expect(BooleanCalculator.run(expression)).toBe(expected); }) })