This project contains an Elixir library that can be used as a model for managing shop checkouts. A full description of the requirements is available in the TASK.md file.
In order to build and run this project, you need a working Elixir environment. You can follow the Installing Elixir guide to get Elixir running in your system.
The minimum version required by this project is defined in the mix.exs file.
A common problem in every development environment is having multiple projects requiring different versions of the same tools. ASDF is a utility that manages multiple language runtime versions on a per-project basis.
This project has a .tool-versions file that specifies the
elixir version required by the project. ASDF uses this file to set the
specified elixir runtime. After getting
ASDF installed, you can run
asdf install in the root folder of this project to get a working elixir
environment.
Another available option is to avoid messing with your local environment and use a Docker container.
The following command creates an interactive terminal session ready for building this project:
docker run -ti \
--entrypoint /bin/bash \
--mount type=bind,source="$(pwd)",target=/code \
-w /code \
elixir:$(cat .tool-versions | grep elixir | grep -o "[0-9]\.[0-9]")When you have the required elixir environment working, running this project is pretty straightforward. We only have to install the dependencies and we are ready for an interactive session:
$ mix deps.get
...
$ iex -S mix
iex(1)>Then, we can start playing with the library. Here it the result of applying the second test scenario described in TASK.md:
iex(1)> tee = Shop.Product.new("GR1", "Green tea", 311)
%Shop.Product{...}
iex(2)> checkout = Shop.Checkout.new("checkout")
%Shop.Checkout{...}
iex(3)> checkout = Shop.Checkout.add_product(checkout, tee)
%Shop.Checkout{items: [%Shop.Product{...}], ...}
iex(4)> checkout = Shop.Checkout.add_product(checkout, tee)
%Shop.Checkout{items: [%Shop.Product{...}, %Shop.Product{...}], ...}
iex(5)> discount = Shop.Discount.new("CEO Discount", Shop.Discounts.XForY, %{x: 2, y: 1, product: tee.code})
%Shop.Discount{...}
iex(6)> checkout = Shop.Discount.apply(checkout, discount)
%Shop.Checkout{...}
iex(6)> Shop.Checkout.price(checkout)
311You can run mix test on this project to run all the tests available. These
include the test scenarios required by TASK.md, which are defined in
test/shop/discount_test.exs.
Some parts of the code are tested using property testing with PropCheck. test/shop/discounts/bulk_test.exs and test/shop/discounts/x_for_y_test.exs are good examples of this kind of testing.
Other tools like Dialyzer and ExCoveralls are also used by this project.
This project is documented with ex_docs.
You can view a fancy documentation site running this command in the root project folder:
mix deps.get && mix docs
Then open the newly generated file at doc/index.html in your browser.