add Lua lang

This commit is contained in:
Auclair Emmanuel 2025-09-01 11:26:48 +02:00 committed by Peter Kofler
parent e000f865fe
commit d8f30fa8d3
9 changed files with 186 additions and 0 deletions

4
lua/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/luarocks
/lua
/lua_modules
/.luarocks

46
lua/README.md Normal file
View File

@ -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 :)

View File

@ -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"
}

View File

@ -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)

63
lua/src/gilded_rose.lua Normal file
View File

@ -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

7
lua/src/item.lua Normal file
View File

@ -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

30
lua/src/main.lua Normal file
View File

@ -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

4
lua/texttest Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
cd ${TEXTTEST_HOME}/lua
exec ./lua src/main.lua "$@"

View File

@ -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