mirror of
https://github.com/ysoftdevs/Theatrical-Players-Refactoring-Kata.git
synced 2026-06-19 04:59:50 +02:00
initial import
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
**/.idea
|
||||
*.iml
|
||||
@@ -0,0 +1,12 @@
|
||||
Theatrical Players Refactoring Kata
|
||||
====================================
|
||||
|
||||
The first chapter of ['Refactoring' by Martin Fowler, 2nd Edition](https://www.thoughtworks.com/books/refactoring2) contains a worked example of this exercise, in javascript. That chapter is available to download for free. This repo contains the starting point for this exercise in several languages, so you can try it out for yourself.
|
||||
|
||||
What you need to change
|
||||
-----------------------
|
||||
Refactoring is usually driven by a need to make changes. In the book, Fowler adds code to print the statement as HTML in addition to the existing plain text version. He also mentions that the theatrical players want to add new kinds of plays to their repertoire, for example history and pastoral.
|
||||
|
||||
Automated tests
|
||||
---------------
|
||||
In his book Fowler mentions that the first step in refactoring is always the same - to ensure you have a solid set of tests for that section of code. However, Fowler did not include the test code for this example in his book. I have used an 'Approval' testing approach and added soem tests. I find Approval testing to be a powerful technique for rapidly getting existing code under test and to support refactoring. You should review these tests and make sure you understand what they cover and what kinds of refactoring mistakes they would expect to find.
|
||||
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
package-lock.json
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "theatrical-players-refactoring-kata",
|
||||
"jest": {
|
||||
"verbose": true
|
||||
},
|
||||
"version": "1.0.0",
|
||||
"description": "Example from first chapter of 'Refactoring' by Martin Fowler, 2nd Edition (https://www.thoughtworks.com/books/refactoring2)",
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/emilybache/Theatrical-Players-Refactoring-Kata.git"
|
||||
},
|
||||
"keywords": [
|
||||
"kata",
|
||||
"refactor"
|
||||
],
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"bugs": {
|
||||
"url": "https://github.com/emilybache/Theatrical-Players-Refactoring-Kata/issues"
|
||||
},
|
||||
"homepage": "https://github.com/emilybache/Theatrical-Players-Refactoring-Kata",
|
||||
"devDependencies": {
|
||||
"babel": "^5.8.23",
|
||||
"jest": "^24.8.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
|
||||
function statement (invoice, plays) {
|
||||
let totalAmount = 0;
|
||||
let volumeCredits = 0;
|
||||
let result = `Statement for ${invoice.customer}\n`;
|
||||
const format = new Intl.NumberFormat("en-US",
|
||||
{ style: "currency", currency: "USD",
|
||||
minimumFractionDigits: 2 }).format;
|
||||
|
||||
for (let perf of invoice.performances) {
|
||||
const play = plays[perf.playID];
|
||||
let thisAmount = 0;
|
||||
switch (play.type) {
|
||||
case "tragedy":
|
||||
thisAmount = 40000;
|
||||
if (perf.audience > 30) {
|
||||
thisAmount += 1000 * (perf.audience - 30);
|
||||
}
|
||||
break;
|
||||
case "comedy":
|
||||
thisAmount = 30000;
|
||||
if (perf.audience > 20) {
|
||||
thisAmount += 10000 + 500 * (perf.audience - 20);
|
||||
}
|
||||
thisAmount += 300 * perf.audience;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`unknown type: ${play.type}`);
|
||||
}
|
||||
// add volume credits
|
||||
volumeCredits += Math.max(perf.audience - 30, 0);
|
||||
// add extra credit for every ten comedy attendees
|
||||
if ("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);
|
||||
// print line for this order
|
||||
result += ` ${play.name}: ${format(thisAmount/100)} (${perf.audience} seats)\n`;
|
||||
totalAmount += thisAmount;
|
||||
}
|
||||
result += `Amount owed is ${format(totalAmount/100)}\n`;
|
||||
result += `You earned ${volumeCredits} credits\n`;
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = statement;
|
||||
@@ -0,0 +1,11 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`example statement 1`] = `
|
||||
"Statement for BigCo
|
||||
Hamlet: $650.00 (55 seats)
|
||||
As You Like It: $580.00 (35 seats)
|
||||
Othello: $500.00 (40 seats)
|
||||
Amount owed is $1,730.00
|
||||
You earned 47 credits
|
||||
"
|
||||
`;
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"customer": "BigCo",
|
||||
"performances": [
|
||||
{
|
||||
"playID": "hamlet",
|
||||
"audience": 55
|
||||
},
|
||||
{
|
||||
"playID": "as-like",
|
||||
"audience": 35
|
||||
},
|
||||
{
|
||||
"playID": "othello",
|
||||
"audience": 40
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"customer": "BigCoII",
|
||||
"performances": [
|
||||
{
|
||||
"playID": "henry-v",
|
||||
"audience": 53
|
||||
},
|
||||
{
|
||||
"playID": "as-like",
|
||||
"audience": 55
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"henry-v": {"name": "Henry V", "type": "history"},
|
||||
"as-like": {"name": "As You Like It", "type": "pastoral"}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"hamlet": {"name": "Hamlet", "type": "tragedy"},
|
||||
"as-like": {"name": "As You Like It", "type": "comedy"},
|
||||
"othello": {"name": "Othello", "type": "tragedy"}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
const statement = require('../src/statement');
|
||||
const fs=require('fs');
|
||||
|
||||
test('example statement', () => {
|
||||
const invoice = JSON.parse(fs.readFileSync('test/invoice.json', 'utf8'));
|
||||
const plays = JSON.parse(fs.readFileSync('test/plays.json', 'utf8'));
|
||||
expect(statement(invoice, plays)).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('statement with new play types', () => {
|
||||
const invoice = JSON.parse(fs.readFileSync('test/invoice_new_plays.json', 'utf8'));
|
||||
const plays = JSON.parse(fs.readFileSync('test/new_plays.json', 'utf8'));
|
||||
expect(() => {statement(invoice, plays)}).toThrow(/unknown type/);
|
||||
});
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 Emily Bache
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
Reference in New Issue
Block a user