diff --git a/lua/.gitignore b/lua/.gitignore new file mode 100644 index 00000000..07715532 --- /dev/null +++ b/lua/.gitignore @@ -0,0 +1,4 @@ +/luarocks +/lua +/lua_modules +/.luarocks diff --git a/lua/README.md b/lua/README.md new file mode 100644 index 00000000..84fb857e --- /dev/null +++ b/lua/README.md @@ -0,0 +1,46 @@ +# Gilded Rose starting position in Lua + +I assume you have installed [Lua](https://lua.org/start.html) on your system (version >= 5.1). + +## Install + +If you want to use [LuaRocks](https://luarocks.org/) there is a config file that should install the dependencies nicely: + +```sh +$ luarocks install --only-deps gildedrose-dev-1.rockspec +``` + +## Run unit tests from the command line + +The tests use the [Busted](https://github.com/lunarmodules/busted) tool. + +```sh +$ busted +``` + +For coverage you'll need to install [LuaCov](https://github.com/lunarmodules/luacov) + +```sh +$ busted --coverage +$ luacov # generate the report +``` + +## Run the TextTest fixture on the command line + +```sh +$ lua src/main.lua 10 +``` + +## 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 executable in [config.gr](../texttests/config.gr). +Uncomment this line to use it: + +``` +executable:${TEXTTEST_HOME}/lua/texttest +``` + +The TextTest fixture use the `./texttest` executable as a hack to make the `require` work. +Lua does not have relative `require` and I did not find a simple way to implement them so the test need to run from the `./lua` directory. +I will improve this in the future if I learn how to do it better :) diff --git a/lua/gildedrose-dev-1.rockspec b/lua/gildedrose-dev-1.rockspec new file mode 100644 index 00000000..6d47bd7b --- /dev/null +++ b/lua/gildedrose-dev-1.rockspec @@ -0,0 +1,19 @@ +rockspec_format = "3.0" +package = "gildedrose" +version = "dev-1" +source = { + url = "https://github.com/emilybache/GildedRose-Refactoring-Kata.git" +} +description = { + license = "MIT" +} +dependencies = { + "lua >= 5.1, < 5.5" +} +test_dependencies = { + "busted >= 2.2" +} +test = { + type = "command", + command = "busted" +} diff --git a/lua/spec/gilded_rose_spec.lua b/lua/spec/gilded_rose_spec.lua new file mode 100644 index 00000000..10ddbd4f --- /dev/null +++ b/lua/spec/gilded_rose_spec.lua @@ -0,0 +1,10 @@ +local Item = require("src/item") +local GildedRose = require("src/gilded_rose") + +describe("Gilded Rose", function() + it("should foo", function() + local gilded_rose = GildedRose:new({ Item:new("foo", 0, 0) }) + local items = gilded_rose:updateQuality() + assert.same("fixme", items[1].name) + end) +end) diff --git a/lua/src/gilded_rose.lua b/lua/src/gilded_rose.lua new file mode 100644 index 00000000..473382aa --- /dev/null +++ b/lua/src/gilded_rose.lua @@ -0,0 +1,63 @@ +local GildedRose = {} + +function GildedRose:new(items) + return { + items = items, + updateQuality = self.updateQuality, + } +end + +function GildedRose:updateQuality() + for i, _ in pairs(self.items) do + if self.items[i].name ~= "Aged Brie" and + self.items[i].name ~= "Backstage passes to a TAFKAL80ETC concert" then + if self.items[i].quality > 0 then + if self.items[i].name ~= "Sulfuras, Hand of Ragnaros" then + self.items[i].quality = self.items[i].quality - 1 + end + end + else + if self.items[i].quality < 50 then + self.items[i].quality = self.items[i].quality + 1 + + if self.items[i].name == "Backstage passes to a TAFKAL80ETC concert" then + if self.items[i].sell_in < 11 then + if self.items[i].quality < 50 then + self.items[i].quality = self.items[i].quality + 1 + end + end + if self.items[i].sell_in < 6 then + if self.items[i].quality < 50 then + self.items[i].quality = self.items[i].quality + 1 + end + end + end + end + end + + if self.items[i].name ~= "Sulfuras, Hand of Ragnaros" then + self.items[i].sell_in = self.items[i].sell_in - 1 + end + + if self.items[i].sell_in < 0 then + if self.items[i].name ~= "Aged Brie" then + if self.items[i].name ~= "Backstage passes to a TAFKAL80ETC concert" then + if self.items[i].quality > 0 then + if self.items[i].name ~= "Sulfuras, Hand of Ragnaros" then + self.items[i].quality = self.items[i].quality - 1 + end + end + else + self.items[i].quality = self.items[i].quality - self.items[i].quality + end + else + if self.items[i].quality < 50 then + self.items[i].quality = self.items[i].quality + 1 + end + end + end + end + return self.items +end + +return GildedRose diff --git a/lua/src/item.lua b/lua/src/item.lua new file mode 100644 index 00000000..a6a3ce19 --- /dev/null +++ b/lua/src/item.lua @@ -0,0 +1,7 @@ +local Item = {} + +function Item:new(name, sell_in, quality) + return { name = name, sell_in = sell_in, quality = quality } +end + +return Item diff --git a/lua/src/main.lua b/lua/src/main.lua new file mode 100644 index 00000000..94dcdafb --- /dev/null +++ b/lua/src/main.lua @@ -0,0 +1,30 @@ +local Item = require("src/item") +local GildedRose = require("src/gilded_rose") + +local items = { + Item:new("+5 Dexterity Vest", 10, 20), + Item:new("Aged Brie", 2, 0), + Item:new("Elixir of the Mongoose", 5, 7), + Item:new("Sulfuras, Hand of Ragnaros", 0, 80), + Item:new("Sulfuras, Hand of Ragnaros", -1, 80), + Item:new("Backstage passes to a TAFKAL80ETC concert", 15, 20), + Item:new("Backstage passes to a TAFKAL80ETC concert", 10, 49), + Item:new("Backstage passes to a TAFKAL80ETC concert", 5, 49), + + -- This Conjured item does not work properly yet + Item:new("Conjured Mana Cake", 3, 6), +}; + +local days = arg[1] or 2; +local gilded_rose = GildedRose:new(items); + +print("OMGHAI!"); +for day = 0, days do + print("-------- day " .. day .. " --------"); + print("name, sellIn, quality"); + for _, item in pairs(items) do + print(item.name .. ", " .. item.sell_in .. ", " .. item.quality); + end + gilded_rose:updateQuality(); + print("") +end diff --git a/lua/texttest b/lua/texttest new file mode 100755 index 00000000..9cbed34b --- /dev/null +++ b/lua/texttest @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +cd ${TEXTTEST_HOME}/lua +exec ./lua src/main.lua "$@" diff --git a/texttests/config.gr b/texttests/config.gr index 9c707ffc..8716d11d 100755 --- a/texttests/config.gr +++ b/texttests/config.gr @@ -17,6 +17,9 @@ diff_program:meld # Settings for the zig version #executable:${TEXTTEST_HOME}/zig/zig-out/bin/zig +# Settings for the lua version +#executable:${TEXTTEST_HOME}/lua/texttest + # Settings for the Java version using Gradle wrapped in a python script #executable:${TEXTTEST_HOME}/Java/texttest_rig.py #interpreter:python