diff --git a/erlang/.gitignore b/erlang/.gitignore index 088157b2..566e0d74 100644 --- a/erlang/.gitignore +++ b/erlang/.gitignore @@ -1,2 +1,21 @@ -/_build -/*.txt \ No newline at end of file +.rebar3 +_* +.eunit +*.o +*.beam +*.plt +*.swp +*.swo +.erlang.cookie +ebin +log +erl_crash.dump +.rebar +logs +_build +*.iml +rebar3.crashdump +*~ +*.iml +.idea + diff --git a/erlang/README.md b/erlang/README.md index 2f8af532..b442c1ce 100644 --- a/erlang/README.md +++ b/erlang/README.md @@ -1,11 +1,26 @@ -# Erlang +GildedRose +===== -## Prerequisites +You will need to install [erlang](https://www.erlang.org/), and [rebar3](https://github.com/erlang/rebar3). I recommend following the instructions from JetBrains: [Getting Started with Erlang](https://www.jetbrains.com/help/idea/getting-started-with-erlang.html). -- Erlang/OTP 20 or later +When you open this project with IntelliJ I recommend that you do -## Running Tests + File | New | Project from existing sources -``` -./rebar3 eunit -``` +Then be sure to select "Erlang" as the project type, and configure the rebar3 location correctly. + +Build +----- + + $ rebar3 compile + +Run Tests +--------- + + $ rebar3 eunit + +TextTest Fixture +---------------- +To run for 30 days: + + $ update_quality_main.escript 30 \ No newline at end of file diff --git a/erlang/include/gilded_rose.hrl b/erlang/include/GildedRose.hrl similarity index 100% rename from erlang/include/gilded_rose.hrl rename to erlang/include/GildedRose.hrl diff --git a/erlang/include/TextTestFixture.hrl b/erlang/include/TextTestFixture.hrl new file mode 100644 index 00000000..84fed9d6 --- /dev/null +++ b/erlang/include/TextTestFixture.hrl @@ -0,0 +1 @@ +-include("GildedRose.hrl"). diff --git a/erlang/rebar.config b/erlang/rebar.config index 60a00f5d..f618f3e4 100644 --- a/erlang/rebar.config +++ b/erlang/rebar.config @@ -1,2 +1,2 @@ -{erl_opts, [warnings_as_errors]}. -{eunit_opts, [verbose]}. +{erl_opts, [debug_info]}. +{deps, []}. \ No newline at end of file diff --git a/erlang/rebar.lock b/erlang/rebar.lock new file mode 100644 index 00000000..57afcca0 --- /dev/null +++ b/erlang/rebar.lock @@ -0,0 +1 @@ +[]. diff --git a/erlang/rebar3 b/erlang/rebar3 deleted file mode 100755 index f0b1ba78..00000000 Binary files a/erlang/rebar3 and /dev/null differ diff --git a/erlang/src/GildedRose.app.src b/erlang/src/GildedRose.app.src new file mode 100644 index 00000000..57b3c7af --- /dev/null +++ b/erlang/src/GildedRose.app.src @@ -0,0 +1,14 @@ +{application, 'GildedRose', + [{description, "An OTP library"}, + {vsn, "0.1.0"}, + {registered, []}, + {applications, + [kernel, + stdlib + ]}, + {env,[]}, + {modules, []}, + + {licenses, ["Apache 2.0"]}, + {links, []} + ]}. diff --git a/erlang/src/GildedRose.erl b/erlang/src/GildedRose.erl new file mode 100644 index 00000000..5288065b --- /dev/null +++ b/erlang/src/GildedRose.erl @@ -0,0 +1,87 @@ +-module('GildedRose'). + +-include("GildedRose.hrl"). + +-export([ + update_quality/1 +]). + +-spec update_quality([#item{}]) -> [#item{}]. +update_quality(Items) -> + lists:map(fun update_item/1, Items). + +-spec update_item(#item{}) -> #item{}. +update_item(Item = #item{name = Name}) -> + Item1 = if + Name /= "Aged Brie" andalso Name /= "Backstage passes to a TAFKAL80ETC concert" -> + if + Item#item.quality > 0 -> + if + Name /= "Sulfuras, Hand of Ragnaros" -> + Item#item{quality = Item#item.quality - 1}; + true -> + Item + end; + true -> + Item + end; + true -> + if + Item#item.quality < 50 -> + Item2 = Item#item{quality = Item#item.quality + 1}, + if + Name == "Backstage passes to a TAFKAL80ETC concert" -> + Item3 = if + Item2#item.sell_in < 11 -> + if + Item2#item.quality < 50 -> + Item2#item{quality = Item2#item.quality + 1}; + true -> Item2 + end; + true -> Item2 + end, + if + Item3#item.sell_in < 6 -> + if + Item3#item.quality < 50 -> + Item3#item{quality = Item3#item.quality + 1}; + true -> Item3 + end; + true -> Item3 + end; + true -> Item2 + end; + true -> Item + end + end, + Item4 = if + Name /= "Sulfuras, Hand of Ragnaros" -> + Item1#item{sell_in = Item1#item.sell_in - 1}; + true -> Item1 + end, + if + Item4#item.sell_in < 0 -> + if + Name /= "Aged Brie" -> + if + Name /= "Backstage passes to a TAFKAL80ETC concert" -> + if + Item4#item.quality > 0 -> + if + Name /= "Sulfuras, Hand of Ragnaros" -> + Item4#item{quality = Item4#item.quality - 1}; + true -> Item4 + end; + true -> Item4 + end; + true -> Item4#item{quality = Item4#item.quality - Item4#item.quality} + end; + true -> + if + Item4#item.quality < 50 -> + Item4#item{quality = Item4#item.quality + 1}; + true -> Item4 + end + end; + true -> Item4 + end. diff --git a/erlang/src/TextTestFixture.erl b/erlang/src/TextTestFixture.erl new file mode 100644 index 00000000..bdbf510b --- /dev/null +++ b/erlang/src/TextTestFixture.erl @@ -0,0 +1,20 @@ +-module('TextTestFixture'). + +-include("GildedRose.hrl"). + +-export([print_update_quality/2]). + +print_one_day(Day, Items) -> + io:format("~n-------- day ~p --------~n", [Day]), + io:format("name, sellIn, quality~n", []), + lists:foreach(fun(#item{name = Name, sell_in = SellIn, quality = Quality}) -> + io:format("~s, ~p, ~p~n", [Name, SellIn, Quality]) + end, Items). + +print_update_quality(Days, Items) -> + io:format("OMGHAI!"), + lists:foreach( + fun(Day) -> print_one_day(Day, Items), + 'GildedRose':update_quality(Items) + end, lists:seq(0, Days - 1)). + diff --git a/erlang/src/gilded_rose.app.src b/erlang/src/gilded_rose.app.src deleted file mode 100644 index 4c47019a..00000000 --- a/erlang/src/gilded_rose.app.src +++ /dev/null @@ -1 +0,0 @@ -{application, gilded_rose, [{vsn, "0.1"}]}. diff --git a/erlang/src/gilded_rose.erl b/erlang/src/gilded_rose.erl deleted file mode 100644 index 98b08e95..00000000 --- a/erlang/src/gilded_rose.erl +++ /dev/null @@ -1,87 +0,0 @@ --module(gilded_rose). - --include("gilded_rose.hrl"). - --export([ - update_quality/1 -]). - --spec update_quality([#item{}]) -> [#item{}]. -update_quality(Items) -> - lists:map(fun update_item/1, Items). - --spec update_item(#item{}) -> #item{}. -update_item(Item = #item{name = Name}) -> - Item1 = if - Name /= "Aged Brie" andalso Name /= "Backstage passes to a TAFKAL80ETC concert" -> - if - Item#item.quality > 0 -> - if - Name /= "Sulfuras, Hand of Ragnaros" -> - Item#item{quality = Item#item.quality - 1}; - true -> - Item - end; - true -> - Item - end; - true -> - if - Item#item.quality < 50 -> - Item2 = Item#item{quality = Item#item.quality + 1}, - if - Name == "Backstage passes to a TAFKAL80ETC concert" -> - Item3 = if - Item2#item.sell_in < 11 -> - if - Item2#item.quality < 50 -> - Item2#item{quality = Item2#item.quality + 1}; - true -> Item2 - end; - true -> Item2 - end, - if - Item3#item.sell_in < 6 -> - if - Item3#item.quality < 50 -> - Item3#item{quality = Item3#item.quality + 1}; - true -> Item3 - end; - true -> Item3 - end; - true -> Item2 - end; - true -> Item - end - end, - Item4 = if - Name /= "Sulfuras, Hand of Ragnaros" -> - Item1#item{sell_in = Item1#item.sell_in - 1}; - true -> Item1 - end, - if - Item4#item.sell_in < 0 -> - if - Name /= "Aged Brie" -> - if - Name /= "Backstage passes to a TAFKAL80ETC concert" -> - if - Item4#item.quality > 0 -> - if - Name /= "Sulfuras, Hand of Ragnaros" -> - Item4#item{quality = Item4#item.quality - 1}; - true -> Item4 - end; - true -> Item4 - end; - true -> Item4#item{quality = Item4#item.quality - Item4#item.quality} - end; - true -> - if - Item4#item.quality < 50 -> - Item4#item{quality = Item4#item.quality + 1}; - true -> Item4 - end - end; - true -> Item4 - end. diff --git a/erlang/test/GildedRoseTest.erl b/erlang/test/GildedRoseTest.erl new file mode 100644 index 00000000..83c3e0a8 --- /dev/null +++ b/erlang/test/GildedRoseTest.erl @@ -0,0 +1,9 @@ +-module('GildedRoseTest'). + +-include_lib("eunit/include/eunit.hrl"). + +-include("GildedRose.hrl"). + +update_quality_test_() -> [ + {"foo", ?_assertMatch([#item{name= "FixMe"}], 'GildedRose':update_quality([#item{name = "Foo", sell_in = 0, quality = 0}]))} +]. diff --git a/erlang/test/gilded_rose_tests.erl b/erlang/test/gilded_rose_tests.erl deleted file mode 100644 index a05c7f52..00000000 --- a/erlang/test/gilded_rose_tests.erl +++ /dev/null @@ -1,44 +0,0 @@ --module(gilded_rose_tests). - --include("gilded_rose.hrl"). --include_lib("eunit/include/eunit.hrl"). - --define(ITEMS, [ - #item{name = "+5 Dexterity Vest", sell_in = 10, quality = 20}, - #item{name = "Aged Brie", sell_in = 2, quality = 0}, - #item{name = "Elixir of the Mongoose", sell_in = 5, quality = 7}, - #item{name = "Sulfuras, Hand of Ragnaros", sell_in = 0, quality = 80}, - #item{name = "Sulfuras, Hand of Ragnaros", sell_in = -1, quality = 80}, - #item{name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 15, quality = 20}, - #item{name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 10, quality = 49}, - #item{name = "Backstage passes to a TAFKAL80ETC concert", sell_in = 5, quality = 49}, - #item{name = "Conjured Mana Cake", sell_in = 3, quality = 6} -]). - -golden_master_test() -> - Days = 30, - {ok, IoDevice} = file:open("actual_output.txt", [write]), - try - lists:foldl(fun(Day, Items) -> - io:format(IoDevice, "-------- day ~p --------~n", [Day]), - io:format(IoDevice, "name, sellIn, quality~n", []), - lists:foreach(fun(#item{name = Name, sell_in = SellIn, quality = Quality}) -> - io:format(IoDevice, "~s, ~p, ~p~n", [Name, SellIn, Quality]) - end, Items), - io:nl(IoDevice), - gilded_rose:update_quality(Items) - end, ?ITEMS, lists:seq(0, Days - 1)) - after - file:close(IoDevice) - end, - case file:read_file("expected_output.txt") of - {ok, ExpectedFile} -> - {ok, ActualFile} = file:read_file("actual_output.txt"), - ?assertEqual(ExpectedFile, ActualFile); - {error, Reason} -> - ?debugFmt("Could not read file 'expected_output.txt': ~p", [Reason]) - end. - -update_quality_test_() -> [ - {"first test", ?_assertMatch([#item{}], gilded_rose:update_quality([#item{}]))} -].