Skip to content

Instantly share code, notes, and snippets.

@AndyStewart
Created March 8, 2015 19:08
Show Gist options
  • Save AndyStewart/452fb181a7da4516aaf5 to your computer and use it in GitHub Desktop.
Save AndyStewart/452fb181a7da4516aaf5 to your computer and use it in GitHub Desktop.
Cheese And Screwdriver
(ns cheesescrewdriver.core-test
(:use [midje.sweet]))
(def today "1/03/2015")
(defn is-expiry-date-today [date]
(= date today))
(defn get-price [product]
(product :price))
(defn expirying-discount [price]
(* price 0.5))
(defn sum-products [products]
(reduce + (map get-price products)))
(defn cheese-price [price expiry-date]
(if (is-expiry-date-today expiry-date) (expirying-discount price) price))
(defn create-screwdriver [name price]
{:name name :price price})
(defn create-cheese [name price expiry]
{:name name :price (cheese-price price expiry) :expiry-date expiry })
(defn apply-bundle-discount [price]
(* price 0.9))
(def crosshead (create-screwdriver "Crosshead" 5.00))
(def flathead (create-screwdriver "Flathead" 15.00))
(def brie (create-cheese "Brie" 5.00 "24/03/2015"))
(def off-cheddar (create-cheese "Cheddar" 10.00 today))
(def cheddar (create-cheese "Cheddar" 10.00 "25/03/2015"))
(defn create-bundle [cheese, screwdriver]
{:name "create-bundle" :price (apply-bundle-discount (sum-products [cheese screwdriver])) :expiry-date (cheese :expiry-date) :type :bundle})
(fact "Basket containing one screwdriver to cart of 5 pound returns 5 pound"
(sum-products [crosshead]) => 5.00)
(fact "Basket containing two screwdrivers to cart of calculates the sum of the products"
(sum-products [crosshead, flathead]) => 20.00)
(fact "Basket containing 1 cheese to cart of calculates the sum of the products"
(sum-products [brie]) => 5.00)
(fact "Basket containing 2 cheese to cart of calculates the sum of the products"
(sum-products [brie cheddar]) => 15.00)
(fact "Basket containing 2 cheese and 1 Screwdriver to cart of calculates the sum of the products"
(sum-products [brie cheddar flathead]) => 30.00)
(fact "Basket containing 1 cheese that is expirying today reduces by 50%"
(sum-products [off-cheddar]) => 5.00)
(fact "Basket containing 1 expirying cheese and 1 non expiring cheese only decreasing expirying cheese"
(sum-products [off-cheddar cheddar]) => 15.00)
(fact "Basket containing screwdriver and cheese bundle gets 10% off"
(sum-products [(create-bundle brie flathead)]) => 18.00)
(fact "Basket containing screwdriver and cheese bundle gets 10% off + sum of other items in baskey"
(sum-products [(create-bundle brie flathead) brie cheddar flathead]) => 48.00)
(fact "Basket containing screwdriver and expirying cheese bundle get 50% off cheese plus an extra gets 10% off"
(sum-products [(create-bundle off-cheddar flathead)]) => 18.00)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment