From fd35bf5be1a853fba810e61fe650909b66052643 Mon Sep 17 00:00:00 2001 From: vincelee888 Date: Fri, 3 Sep 2021 16:27:25 +0100 Subject: [PATCH] adds golden master test --- TypeScript/build/.gitkeep | 0 TypeScript/package.json | 6 ++-- TypeScript/test/checkGoldenMaster.spec.ts | 26 +++++++++++++++++ TypeScript/test/generateGoldenMaster.ts | 7 +++++ TypeScript/test/generateReport.ts | 34 ++++++++++++++++++++++ TypeScript/test/gilded-rose.spec.ts | 10 +++---- TypeScript/test/golden-master-text-test.ts | 30 +++---------------- 7 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 TypeScript/build/.gitkeep create mode 100644 TypeScript/test/checkGoldenMaster.spec.ts create mode 100644 TypeScript/test/generateGoldenMaster.ts create mode 100644 TypeScript/test/generateReport.ts diff --git a/TypeScript/build/.gitkeep b/TypeScript/build/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/TypeScript/package.json b/TypeScript/package.json index 6bca69d9..3e41fe59 100644 --- a/TypeScript/package.json +++ b/TypeScript/package.json @@ -6,7 +6,9 @@ "precompile": "rimraf app/**/*.js test/**/*.js", "compile": "tsc", "pretest": "rimraf app/**/*.js test/**/*.js", - "test": "nyc mocha" + "test": "nyc mocha", + "test:gm": "nyc mocha -g 'gilded rose golden master'", + "create-master": "ts-node ./test/generateGoldenMaster.ts" }, "license": "MIT", "private": true, @@ -19,7 +21,7 @@ "nyc": "~15.1.0", "rimraf": "~3.0.2", "source-map-support": "0.5.19", - "ts-node": "~9.1.1", + "ts-node": "^10.2.1", "typescript": "~4.1.3" }, "nyc": { diff --git a/TypeScript/test/checkGoldenMaster.spec.ts b/TypeScript/test/checkGoldenMaster.spec.ts new file mode 100644 index 00000000..75eb0202 --- /dev/null +++ b/TypeScript/test/checkGoldenMaster.spec.ts @@ -0,0 +1,26 @@ +import * as fs from "fs"; +import {generateReport, getItems, goldenMasterFilename} from "./generateReport"; +import {GildedRose} from "../app/gilded-rose"; +import { expect } from "chai"; + +function getGoldenMaster() { + try { + return fs.readFileSync(goldenMasterFilename).toString().split('\n') + }catch { + throw new Error('No golden master file generated; run `yarn create-master` or `npm run create-master`') + } +} + +describe('gilded rose golden master', () => { + it('should match golden master', () => { + const goldenMaster = getGoldenMaster(); + + const newReport = generateReport(new GildedRose(getItems())) + + expect(newReport.length).to.equal(goldenMaster.length) + + goldenMaster.forEach((line, index) => { + expect(newReport[index]).to.equal(line) + }) + }) +}) \ No newline at end of file diff --git a/TypeScript/test/generateGoldenMaster.ts b/TypeScript/test/generateGoldenMaster.ts new file mode 100644 index 00000000..cacb7aaa --- /dev/null +++ b/TypeScript/test/generateGoldenMaster.ts @@ -0,0 +1,7 @@ +import * as fs from "fs"; +import {generateReport, getItems, goldenMasterFilename} from "./generateReport"; +import {GildedRose} from "../app/gilded-rose"; + + +const report = generateReport(new GildedRose(getItems())) +fs.writeFileSync(goldenMasterFilename, report.join('\n')) \ No newline at end of file diff --git a/TypeScript/test/generateReport.ts b/TypeScript/test/generateReport.ts new file mode 100644 index 00000000..fa35a977 --- /dev/null +++ b/TypeScript/test/generateReport.ts @@ -0,0 +1,34 @@ +import {GildedRose, Item} from "../app/gilded-rose"; + +export const goldenMasterFilename = './build/goldenMaster.txt'; + +export const getItems = () => ([ + new Item("+5 Dexterity Vest", 10, 20), // + new Item("Aged Brie", 2, 0), // + new Item("Elixir of the Mongoose", 5, 7), // + new Item("Sulfuras, Hand of Ragnaros", 0, 80), // + new Item("Sulfuras, Hand of Ragnaros", -1, 80), + new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), + new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), + new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), + // this conjured item does not work properly yet + new Item("Conjured Mana Cake", 3, 6) +]); + +export function generateReport(gildedRose: GildedRose) { + const outputLines: string[] = [] + + const days: number = 2; + + for (let i = 0; i < days; i++) { + outputLines.push("-------- day " + i + " --------"); + outputLines.push("name, sellIn, quality"); + gildedRose.items.forEach(element => { + outputLines.push(element.name + ' ' + element.sellIn + ' ' + element.quality); + }); + outputLines.push(); + gildedRose.updateQuality(); + } + + return outputLines +} \ No newline at end of file diff --git a/TypeScript/test/gilded-rose.spec.ts b/TypeScript/test/gilded-rose.spec.ts index 0c192caf..c8dadac1 100644 --- a/TypeScript/test/gilded-rose.spec.ts +++ b/TypeScript/test/gilded-rose.spec.ts @@ -3,10 +3,10 @@ import { Item, GildedRose } from '../app/gilded-rose'; describe('Gilded Rose', function () { - it('should foo', function() { - const gildedRose = new GildedRose([ new Item('foo', 0, 0) ]); - const items = gildedRose.updateQuality(); - expect(items[0].name).to.equal('fixme'); - }); + // it('should foo', function() { + // const gildedRose = new GildedRose([ new Item('foo', 0, 0) ]); + // const items = gildedRose.updateQuality(); + // expect(items[0].name).to.equal('fixme'); + // }); }); diff --git a/TypeScript/test/golden-master-text-test.ts b/TypeScript/test/golden-master-text-test.ts index 96b60e41..d5a003c5 100644 --- a/TypeScript/test/golden-master-text-test.ts +++ b/TypeScript/test/golden-master-text-test.ts @@ -1,27 +1,5 @@ -import { Item, GildedRose } from '../app/gilded-rose'; +import {generateReport, getItems} from "./generateReport"; +import {GildedRose} from "../app/gilded-rose"; -const items = [ - new Item("+5 Dexterity Vest", 10, 20), // - new Item("Aged Brie", 2, 0), // - new Item("Elixir of the Mongoose", 5, 7), // - new Item("Sulfuras, Hand of Ragnaros", 0, 80), // - new Item("Sulfuras, Hand of Ragnaros", -1, 80), - new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), - new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), - new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), - // this conjured item does not work properly yet - new Item("Conjured Mana Cake", 3, 6)]; - - -const gildedRose = new GildedRose(items); -var days: number = 2; -for (let i = 0; i < days; i++) { - console.log("-------- day " + i + " --------"); - console.log("name, sellIn, quality"); - items.forEach(element => { - console.log(element.name + ' ' + element.sellIn + ' ' + element.quality); - - }); - console.log(); - gildedRose.updateQuality(); -} \ No newline at end of file +const outputLines = generateReport(new GildedRose(getItems())); +console.log(outputLines.join('\n')) \ No newline at end of file