Skip to content

Commit c780bb1

Browse files
committed
Adds example for demonstrating Replace Conditional with Polymorphism refactoring
1 parent 7b4ba69 commit c780bb1

File tree

10 files changed

+4052
-0
lines changed

10 files changed

+4052
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Sample for Replace Conditional with Polymorphism Refactoring
2+
3+
The `calculator/index.js` contains the original implementation that uses a conditional to determine the operation to perform. The file `calculator/index.refactored.js` contains that uses polymorphism to separate the different operations.
4+
5+
The same set of tests are used to evaluate the correctness of each file. These are defined in `calculator/calculatorTests.js`. The `index.test.js` and `index.refactored.test.js` files import the appropriate `Calculator` instance to test.
6+
7+
## Running the Tests
8+
9+
Assuming that you have `node` version 20.5.1 or higher, and at least version 9.8.0 of `npm`, then you should be able to run the tests with `npm run test` after installing the dependencies with `npm install`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
export default function calculatorTests(Calculator, name) {
2+
describe(`${name} Calculator`, () => {
3+
describe('add', () => {
4+
test('with two integers', () => {
5+
const calculator = new Calculator();
6+
calculator.execute('add 5 3');
7+
expect(calculator.result).toBe(8);
8+
});
9+
10+
test('with two floats', () => {
11+
const calculator = new Calculator();
12+
calculator.execute('add 5.5 3.3');
13+
expect(calculator.result).toBe(8.8);
14+
});
15+
16+
test('with two negative numbers', () => {
17+
const calculator = new Calculator();
18+
calculator.execute('add -5 -3');
19+
expect(calculator.result).toBe(-8);
20+
});
21+
22+
test('with first number negative', () => {
23+
const calculator = new Calculator();
24+
calculator.execute('add -5 3');
25+
expect(calculator.result).toBe(-2);
26+
});
27+
28+
test('with second number negative', () => {
29+
const calculator = new Calculator();
30+
calculator.execute('add 5 -3');
31+
expect(calculator.result).toBe(2);
32+
})
33+
34+
test('with two negative floats', () => {
35+
const calculator = new Calculator();
36+
calculator.execute('add -5.5 -3.3');
37+
expect(calculator.result).toBe(-8.8);
38+
})
39+
});
40+
41+
describe('subtract', () => {
42+
test('with two integers', () => {
43+
const calculator = new Calculator();
44+
calculator.execute('subtract 5 3');
45+
expect(calculator.result).toBe(2);
46+
});
47+
48+
test('with two floats', () => {
49+
const calculator = new Calculator();
50+
calculator.execute('subtract 5.5 3.3');
51+
expect(calculator.result).toBe(2.2);
52+
});
53+
54+
test('with two negative numbers', () => {
55+
const calculator = new Calculator();
56+
calculator.execute('subtract -5 -3');
57+
expect(calculator.result).toBe(-2);
58+
});
59+
60+
test('with first number negative', () => {
61+
const calculator = new Calculator();
62+
calculator.execute('subtract -5 3');
63+
expect(calculator.result).toBe(-8);
64+
});
65+
66+
test('with second number negative', () => {
67+
const calculator = new Calculator();
68+
calculator.execute('subtract 5 -3');
69+
expect(calculator.result).toBe(8);
70+
})
71+
72+
test('with two negative floats', () => {
73+
const calculator = new Calculator();
74+
calculator.execute('subtract -5.5 -3.3');
75+
expect(calculator.result).toBe(-2.2);
76+
})
77+
});
78+
79+
describe('multiply', () => {
80+
test('with two integers', () => {
81+
const calculator = new Calculator();
82+
calculator.execute('multiply 5 3');
83+
expect(calculator.result).toBe(15);
84+
});
85+
86+
test('with two floats', () => {
87+
const calculator = new Calculator();
88+
calculator.execute('multiply 5.5 3.3');
89+
expect(calculator.result).toBe(18.15);
90+
});
91+
92+
test('with two negative numbers', () => {
93+
const calculator = new Calculator();
94+
calculator.execute('multiply -5 -3');
95+
expect(calculator.result).toBe(15);
96+
});
97+
98+
test('with first number negative', () => {
99+
const calculator = new Calculator();
100+
calculator.execute('multiply -5 3');
101+
expect(calculator.result).toBe(-15);
102+
});
103+
104+
test('with second number negative', () => {
105+
const calculator = new Calculator();
106+
calculator.execute('multiply 5 -3');
107+
expect(calculator.result).toBe(-15);
108+
})
109+
110+
test('with two negative floats', () => {
111+
const calculator = new Calculator();
112+
calculator.execute('multiply -5.5 -3.3');
113+
expect(calculator.result).toBe(18.15);
114+
})
115+
});
116+
117+
describe('divide', () => {
118+
test('with two integers', () => {
119+
const calculator = new Calculator();
120+
calculator.execute('divide 5 3');
121+
expect(calculator.result).toBeCloseTo(1.6666666666666667);
122+
});
123+
124+
test('with two floats', () => {
125+
const calculator = new Calculator();
126+
calculator.execute('divide 5.5 3.3');
127+
expect(calculator.result).toBeCloseTo(1.6666666666666667);
128+
});
129+
130+
test('with two negative numbers', () => {
131+
const calculator = new Calculator();
132+
calculator.execute('divide -5 -3');
133+
expect(calculator.result).toBeCloseTo(1.6666666666666667);
134+
});
135+
136+
test('with first number negative', () => {
137+
const calculator = new Calculator();
138+
calculator.execute('divide -5 3');
139+
expect(calculator.result).toBeCloseTo(-1.6666666666666667);
140+
});
141+
142+
test('with second number negative', () => {
143+
const calculator = new Calculator();
144+
calculator.execute('divide 5 -3');
145+
expect(calculator.result).toBeCloseTo(-1.6666666666666667);
146+
})
147+
148+
test('with two negative floats', () => {
149+
const calculator = new Calculator();
150+
calculator.execute('divide -5.5 -3.3');
151+
expect(calculator.result).toBeCloseTo(1.6666666666666667);
152+
})
153+
});
154+
155+
describe('multiple operations', () => {
156+
test('adds using initial value', () => {
157+
const calculator = new Calculator(5);
158+
calculator.execute('add 5');
159+
calculator.execute('add 3');
160+
expect(calculator.result).toBe(13);
161+
});
162+
163+
test('adds ignoring initial value', () => {
164+
const calculator = new Calculator(5);
165+
calculator.execute('add 5 3');
166+
calculator.execute('add 5');
167+
expect(calculator.result).toBe(13);
168+
});
169+
170+
test('subtracts using initial value', () => {
171+
const calculator = new Calculator(7);
172+
calculator.execute('subtract 5');
173+
calculator.execute('subtract 3');
174+
expect(calculator.result).toBe(-1);
175+
});
176+
177+
test('subtracts ignoring initial value', () => {
178+
const calculator = new Calculator(7);
179+
calculator.execute('subtract 5 3');
180+
calculator.execute('subtract 5');
181+
expect(calculator.result).toBe(-3);
182+
});
183+
184+
test('multiplies using initial value', () => {
185+
const calculator = new Calculator(2);
186+
calculator.execute('multiply 5');
187+
calculator.execute('multiply 3');
188+
expect(calculator.result).toBe(30);
189+
});
190+
191+
test('multiplies ignoring initial value', () => {
192+
const calculator = new Calculator(2);
193+
calculator.execute('multiply 5 3');
194+
calculator.execute('multiply 5');
195+
expect(calculator.result).toBe(75);
196+
});
197+
198+
test('divides using initial value', () => {
199+
const calculator = new Calculator(100);
200+
calculator.execute('divide 5');
201+
calculator.execute('divide 3');
202+
expect(calculator.result).toBeCloseTo(6.666666666666667);
203+
});
204+
205+
test('divides ignoring initial value', () => {
206+
const calculator = new Calculator(100);
207+
calculator.execute('divide 10 5');
208+
calculator.execute('divide 3');
209+
expect(calculator.result).toBeCloseTo(0.666666666666667);
210+
});
211+
});
212+
});
213+
}

0 commit comments

Comments
 (0)