WIP: adding Deno version

This commit is contained in:
Birame Sene authored and Peter Kofler committed 2025-01-22 18:36:51 +01:00
1 parent 11610e00ba
commit d4514e6c9f
18 files changed
+414

No files matched your search

+12
View File
@@ -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
+11
View File
@@ -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
+11
View File
@@ -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"
}
+17
View File
@@ -0,0 +1,17 @@
module.exports = {
extension: [
".ts"
],
exclude: [
"**/*.d.ts",
"test/**",
"/*.js"
],
require: [
"ts-node/register"
],
reporter: [
"html",
"text"
]
}
+44
View File
@@ -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
+69
View File
@@ -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<Item>;
constructor(items = [] as Array<Item>) {
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;
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}
+44
View File
@@ -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"
]
}
}
}
+8
View File
@@ -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));
}
+6
View File
@@ -0,0 +1,6 @@
import { assertEquals } from "@std/assert";
import { add } from "./main.ts";
Deno.test(function addTest() {
assertEquals(add(2, 3), 5);
});
+35
View File
@@ -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"
}
}
+33
View File
@@ -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();
}
+54
View File
@@ -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:
* <li>"foo" is more similar to the unit test from the 'Java' version
* <li>"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 = ["<node>", "<script", "30"];
require('../golden-master-text-test.ts');
expect(gameConsoleOutput).toMatchSnapshot();
});
});
+9
View File
@@ -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');
});
});
+10
View File
@@ -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');
});
});
+9
View File
@@ -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');
});
});
+14
View File
@@ -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)
+20
View File
@@ -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"
]
}