From d4514e6c9f81f35c24bd28032041cb77dada5076 Mon Sep 17 00:00:00 2001 From: Birame Sene Date: Sun, 29 Dec 2024 12:24:08 +0100 Subject: [PATCH] WIP: adding Deno version --- deno/.editorconfig | 12 +++++ deno/.gitignore | 11 +++++ deno/.mocharc.js | 11 +++++ deno/.nycrc.js | 17 +++++++ deno/README.md | 44 ++++++++++++++++++ deno/app/gilded-rose.ts | 69 ++++++++++++++++++++++++++++ deno/deno.json | 8 ++++ deno/deno.lock | 44 ++++++++++++++++++ deno/main.ts | 8 ++++ deno/main_test.ts | 6 +++ deno/package.json | 35 ++++++++++++++ deno/test/golden-master-text-test.ts | 33 +++++++++++++ deno/test/jest/approvals.spec.ts | 54 ++++++++++++++++++++++ deno/test/jest/gilded-rose.spec.ts | 9 ++++ deno/test/mocha/gilded-rose.spec.ts | 10 ++++ deno/test/vitest/gilded-rose.spec.ts | 9 ++++ deno/texttest_rig.py | 14 ++++++ deno/tsconfig.json | 20 ++++++++ 18 files changed, 414 insertions(+) create mode 100644 deno/.editorconfig create mode 100644 deno/.gitignore create mode 100644 deno/.mocharc.js create mode 100644 deno/.nycrc.js create mode 100644 deno/README.md create mode 100644 deno/app/gilded-rose.ts create mode 100644 deno/deno.json create mode 100644 deno/deno.lock create mode 100644 deno/main.ts create mode 100644 deno/main_test.ts create mode 100644 deno/package.json create mode 100644 deno/test/golden-master-text-test.ts create mode 100644 deno/test/jest/approvals.spec.ts create mode 100644 deno/test/jest/gilded-rose.spec.ts create mode 100644 deno/test/mocha/gilded-rose.spec.ts create mode 100644 deno/test/vitest/gilded-rose.spec.ts create mode 100644 deno/texttest_rig.py create mode 100644 deno/tsconfig.json diff --git a/deno/.editorconfig b/deno/.editorconfig new file mode 100644 index 00000000..4a7ea303 --- /dev/null +++ b/deno/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/deno/.gitignore b/deno/.gitignore new file mode 100644 index 00000000..566b078b --- /dev/null +++ b/deno/.gitignore @@ -0,0 +1,11 @@ +.DS_Store +.idea +node_modules +typings +app/**/*.js +app/**/*.js.map +test/**/*.js +test/**/*.js.map +coverage +.nyc_output +package-lock.json diff --git a/deno/.mocharc.js b/deno/.mocharc.js new file mode 100644 index 00000000..9ae39f77 --- /dev/null +++ b/deno/.mocharc.js @@ -0,0 +1,11 @@ +"use strict"; + +module.exports = { + require: [ + "ts-node/register", + "tsconfig-paths/register", + "source-map-support/register" + ], + recursive: true, + spec: "test/mocha/*.spec.ts" +} diff --git a/deno/.nycrc.js b/deno/.nycrc.js new file mode 100644 index 00000000..3b19d56e --- /dev/null +++ b/deno/.nycrc.js @@ -0,0 +1,17 @@ +module.exports = { + extension: [ + ".ts" + ], + exclude: [ + "**/*.d.ts", + "test/**", + "/*.js" + ], + require: [ + "ts-node/register" + ], + reporter: [ + "html", + "text" + ] +} diff --git a/deno/README.md b/deno/README.md new file mode 100644 index 00000000..c1f40c43 --- /dev/null +++ b/deno/README.md @@ -0,0 +1,44 @@ +# Gilded Rose + +This is the Gilded Rose kata in TypeScript through [Deno](https://deno.com/) for a simplified experience. + +## Getting started + +Install dependencies + +No more dependencies to install. Just go to the next step. + +## Run the unit tests from the Command-Line + +Open a terminal and run the following command: + +```sh +deno test +``` + +To run all tests in watch mode + +```sh +deo test --watch +``` + +## Run the TextTest fixture from the Command-Line + +```sh +deno run test/golden-master-text-test.ts +``` + +Or with number of days as args: +```sh +deno run test/golden-master-text-test.ts 10 +``` + +You should make sure the command shown above works when you execute it in a terminal before trying to use TextTest (see below). + + +## Run the TextTest approval test that comes with this project + +There are instructions in the [TextTest Readme](../texttests/README.md) for setting up TextTest. You will need to specify the Python executable and interpreter in [config.gr](../texttests/config.gr). Uncomment these lines: + + executable:${TEXTTEST_HOME}/python/texttest_fixture.py + interpreter:python diff --git a/deno/app/gilded-rose.ts b/deno/app/gilded-rose.ts new file mode 100644 index 00000000..db58d678 --- /dev/null +++ b/deno/app/gilded-rose.ts @@ -0,0 +1,69 @@ +export class Item { + name: string; + sellIn: number; + quality: number; + + constructor(name, sellIn, quality) { + this.name = name; + this.sellIn = sellIn; + this.quality = quality; + } +} + +export class GildedRose { + items: Array; + + constructor(items = [] as Array) { + this.items = items; + } + + updateQuality() { + for (let i = 0; i < this.items.length; i++) { + if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { + if (this.items[i].quality > 0) { + if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { + this.items[i].quality = this.items[i].quality - 1 + } + } + } else { + if (this.items[i].quality < 50) { + this.items[i].quality = this.items[i].quality + 1 + if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { + if (this.items[i].sellIn < 11) { + if (this.items[i].quality < 50) { + this.items[i].quality = this.items[i].quality + 1 + } + } + if (this.items[i].sellIn < 6) { + if (this.items[i].quality < 50) { + this.items[i].quality = this.items[i].quality + 1 + } + } + } + } + } + if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { + this.items[i].sellIn = this.items[i].sellIn - 1; + } + if (this.items[i].sellIn < 0) { + if (this.items[i].name != 'Aged Brie') { + if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { + if (this.items[i].quality > 0) { + if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { + this.items[i].quality = this.items[i].quality - 1 + } + } + } else { + this.items[i].quality = this.items[i].quality - this.items[i].quality + } + } else { + if (this.items[i].quality < 50) { + this.items[i].quality = this.items[i].quality + 1 + } + } + } + } + + return this.items; + } +} diff --git a/deno/deno.json b/deno/deno.json new file mode 100644 index 00000000..5b320c26 --- /dev/null +++ b/deno/deno.json @@ -0,0 +1,8 @@ +{ + "tasks": { + "dev": "deno run --watch main.ts" + }, + "imports": { + "@std/assert": "jsr:@std/assert@1" + } +} diff --git a/deno/deno.lock b/deno/deno.lock new file mode 100644 index 00000000..be56851a --- /dev/null +++ b/deno/deno.lock @@ -0,0 +1,44 @@ +{ + "version": "4", + "specifiers": { + "jsr:@std/assert@1": "1.0.10", + "jsr:@std/internal@^1.0.5": "1.0.5" + }, + "jsr": { + "@std/assert@1.0.10": { + "integrity": "59b5cbac5bd55459a19045d95cc7c2ff787b4f8527c0dd195078ff6f9481fbb3", + "dependencies": [ + "jsr:@std/internal" + ] + }, + "@std/internal@1.0.5": { + "integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba" + } + }, + "workspace": { + "dependencies": [ + "jsr:@std/assert@1" + ], + "packageJson": { + "dependencies": [ + "npm:@types/chai@^4.2.22", + "npm:@types/jest@^29.4.0", + "npm:@types/mocha@^10.0.1", + "npm:@types/node@^18.14.0", + "npm:@vitest/coverage-istanbul@~0.28.5", + "npm:chai@^4.3.4", + "npm:jest@^29.4.3", + "npm:mocha@^10.2.0", + "npm:nyc@15.1", + "npm:rimraf@^4.1.2", + "npm:source-map-support@~0.5.20", + "npm:ts-jest@^29.0.5", + "npm:ts-node@^10.9.1", + "npm:tsconfig-paths@^4.1.2", + "npm:typescript@^4.4.4", + "npm:vite-tsconfig-paths@^4.0.5", + "npm:vitest@~0.28.5" + ] + } + } +} diff --git a/deno/main.ts b/deno/main.ts new file mode 100644 index 00000000..292ce5f6 --- /dev/null +++ b/deno/main.ts @@ -0,0 +1,8 @@ +export function add(a: number, b: number): number { + return a + b; +} + +// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts +if (import.meta.main) { + console.log("Add 2 + 3 =", add(2, 3)); +} diff --git a/deno/main_test.ts b/deno/main_test.ts new file mode 100644 index 00000000..3d981e9b --- /dev/null +++ b/deno/main_test.ts @@ -0,0 +1,6 @@ +import { assertEquals } from "@std/assert"; +import { add } from "./main.ts"; + +Deno.test(function addTest() { + assertEquals(add(2, 3), 5); +}); diff --git a/deno/package.json b/deno/package.json new file mode 100644 index 00000000..c7c597e3 --- /dev/null +++ b/deno/package.json @@ -0,0 +1,35 @@ +{ + "name": "gilded-rose-kata", + "version": "1.0.0", + "description": "Gilded Rose kata in TypeScript", + "scripts": { + "precompile": "rimraf app/**/*.js test/**/*.js", + "compile": "tsc", + "pretest": "rimraf app/**/*.js test/**/*.js", + "test:jest": "jest", + "test:jest:watch": "jest --watchAll", + "test:mocha": "nyc mocha", + "test:vitest": "vitest --coverage" + }, + "license": "MIT", + "private": true, + "devDependencies": { + "@types/chai": "^4.2.22", + "@types/jest": "^29.4.0", + "@types/mocha": "^10.0.1", + "@types/node": "^18.14.0", + "@vitest/coverage-istanbul": "^0.28.5", + "chai": "^4.3.4", + "jest": "^29.4.3", + "mocha": "^10.2.0", + "nyc": "~15.1.0", + "rimraf": "^4.1.2", + "source-map-support": "^0.5.20", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.1", + "tsconfig-paths": "^4.1.2", + "typescript": "^4.4.4", + "vite-tsconfig-paths": "^4.0.5", + "vitest": "^0.28.5" + } +} diff --git a/deno/test/golden-master-text-test.ts b/deno/test/golden-master-text-test.ts new file mode 100644 index 00000000..a7d8aabc --- /dev/null +++ b/deno/test/golden-master-text-test.ts @@ -0,0 +1,33 @@ +import { GildedRose, Item } from "../app/gilded-rose.ts"; + +console.log("OMGHAI!"); + +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); + +let days: number = 2; +if (process.argv.length > 2) { + days = +process.argv[2]; +} + +for (let i = 0; i < days + 1; 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(); +} diff --git a/deno/test/jest/approvals.spec.ts b/deno/test/jest/approvals.spec.ts new file mode 100644 index 00000000..bb7f45f9 --- /dev/null +++ b/deno/test/jest/approvals.spec.ts @@ -0,0 +1,54 @@ +import { Item, GildedRose } from '@/gilded-rose'; + +/** + * This unit test uses [Jest Snapshot](https://goo.gl/fbAQLP). + * + * There are two test cases here with different styles: + *
  • "foo" is more similar to the unit test from the 'Java' version + *
  • "thirtyDays" is more similar to the TextTest from the 'Java' version + * + * I suggest choosing one style to develop and deleting the other. + */ + +describe('Gilded Rose Approval', () => { + + let gameConsoleOutput: string; + let originalConsoleLog: (message: any) => void; + let originalProcessArgv: string[] + + function gameConsoleLog(msg: string) { + if (msg) { + gameConsoleOutput += msg; + } + gameConsoleOutput += "\n"; + } + + beforeEach(() => { + // prepare capturing console.log to our own gameConsoleLog. + gameConsoleOutput = ""; + originalConsoleLog = console.log; + console.log = gameConsoleLog; + originalProcessArgv = process.argv; + }); + + afterEach(() => { + // reset original console.log + console.log = originalConsoleLog; + process.argv = originalProcessArgv; + }); + + it('should foo', () => { + const gildedRose = new GildedRose([new Item('foo', 0, 0)]); + const items = gildedRose.updateQuality(); + + expect(items).toMatchSnapshot(); + }); + + it('should thirtyDays', () => { + process.argv = ["", " { + it('should foo', () => { + const gildedRose = new GildedRose([new Item('foo', 0, 0)]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toBe('fixme'); + }); +}); diff --git a/deno/test/mocha/gilded-rose.spec.ts b/deno/test/mocha/gilded-rose.spec.ts new file mode 100644 index 00000000..02cd24c5 --- /dev/null +++ b/deno/test/mocha/gilded-rose.spec.ts @@ -0,0 +1,10 @@ +import { expect } from 'chai'; +import { Item, GildedRose } from '@/gilded-rose'; + +describe('Gilded Rose', () => { + it('should foo', () => { + const gildedRose = new GildedRose([new Item('foo', 0, 0)]); + const items = gildedRose.updateQuality(); + expect(items[0].name).to.equal('fixme'); + }); +}); diff --git a/deno/test/vitest/gilded-rose.spec.ts b/deno/test/vitest/gilded-rose.spec.ts new file mode 100644 index 00000000..65330750 --- /dev/null +++ b/deno/test/vitest/gilded-rose.spec.ts @@ -0,0 +1,9 @@ +import { Item, GildedRose } from '@/gilded-rose'; + +describe('Gilded Rose', () => { + it('should foo', () => { + const gildedRose = new GildedRose([new Item('foo', 0, 0)]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toBe('fixme'); + }); +}); diff --git a/deno/texttest_rig.py b/deno/texttest_rig.py new file mode 100644 index 00000000..1aaa8e62 --- /dev/null +++ b/deno/texttest_rig.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +""" +This script uses npx to execute the TextTest Fixture for TypeScript. +It is designed to be used by TextTest and specified in the file 'texttests/config.gr' in this repo. +It is more convenient for TextTest to use since npx needs +several arguments in addition to the one the TextTest fixture needs. +""" +import os +import subprocess +import sys + +args = " ".join(sys.argv[1:]) +TEXTTEST_HOME = os.environ.get("TEXTTEST_HOME", os.getcwd()) +subprocess.run(f"deno run {TEXTTEST_HOME}/TypeScript/test/golden-master-text-test.ts {args}", shell=True) diff --git a/deno/tsconfig.json b/deno/tsconfig.json new file mode 100644 index 00000000..d2922795 --- /dev/null +++ b/deno/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "strict": true, + "noImplicitAny": false, + "sourceMap": true, + "baseUrl": "./", + "resolveJsonModule": true, + "esModuleInterop": true, + "paths": { + "@/*": [ + "app/*" + ] + } + }, + "exclude": [ + "node_modules" + ] +}