Created
December 29, 2018 00:42
-
-
Save kejadlen/9251104a05c5044e212d39d690182951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "yaml" | |
require "minitest/autorun" | |
require "minitest/pride" | |
class TestStatement < Minitest::Test | |
def statement(invoice, plays) | |
total_amount = 0 | |
volume_credits = 0 | |
result = "Statement for #{invoice.fetch("customer")}\n" | |
format = ->(currency) { "$#{currency.to_s.reverse.chars.each_slice(3).map(&:join).join(?,).reverse}.00" } | |
invoice.fetch("performances").each do |perf| | |
play = plays.fetch(perf.fetch("playID")) | |
this_amount = 0 | |
case play.fetch("type") | |
when "tragedy" | |
this_amount = 40000 | |
this_amount += 1000 * (perf.fetch("audience") - 30) if perf.fetch("audience") > 30 | |
when "comedy" | |
this_amount = 30000 | |
this_amount += 10000 + 500 * (perf.fetch("audience") - 20) if perf.fetch("audience") > 20 | |
this_amount += 300 * perf.fetch("audience") | |
else | |
fail "Unknown type: #{play.fetch("type")}" | |
end | |
# add volume credits | |
volume_credits += [perf.fetch("audience") - 30, 0].max | |
# add extra credit for every ten comedy attendees | |
volume_credits += perf.fetch("audience") / 5 if play.fetch("type") == "comedy" | |
# print line for this order | |
result << " #{play.fetch("name")}: #{format[this_amount / 100]} (#{perf.fetch("audience")} seats)\n" | |
total_amount += this_amount | |
end | |
result << "Amount owed is #{format[total_amount / 100]}\n" | |
result << "You earned #{volume_credits} credits\n" | |
result | |
end | |
def test_statement | |
plays = YAML.load(<<~PLAYS) | |
hamlet: | |
name: Hamlet | |
type: tragedy | |
as-like: | |
name: As You Like It | |
type: comedy | |
othello: | |
name: Othello | |
type: tragedy | |
PLAYS | |
invoices = YAML.load(<<~INVOICES) | |
- customer: BigCo | |
performances: | |
- playID: hamlet | |
audience: 55 | |
- playID: as-like | |
audience: 35 | |
- playID: othello | |
audience: 40 | |
INVOICES | |
assert_equal <<~EXPECTED, statement(invoices.first, plays) | |
Statement for BigCo | |
Hamlet: $650.00 (55 seats) | |
As You Like It: $580.00 (35 seats) | |
Othello: $500.00 (40 seats) | |
Amount owed is $1,730.00 | |
You earned 47 credits | |
EXPECTED | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment