Skip to content

Instantly share code, notes, and snippets.

@dainmiller
Last active April 13, 2020 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dainmiller/935e61205c2668d5a10293923365b872 to your computer and use it in GitHub Desktop.
Save dainmiller/935e61205c2668d5a10293923365b872 to your computer and use it in GitHub Desktop.
TDD Example
require 'minitest/autorun'
require 'json'
require 'httparty'
class Movie
CHILDRENS = "0"
REGULAR = "1"
NEW_RELEASE = "2"
attr_accessor :price_code, :title
def initialize(title:, price_code:)
@title = title
@price_code = price_code
end
def price_for_week
{
"0" => 3,
"1" => 10,
"2" => 20
}.fetch(@price_code)
end
end
class Rental
attr_accessor :movie, :days_rented
def initialize(movie:, days_rented:)
@movie = movie
@days_rented = days_rented
end
def price_per_day
@movie.price_for_week / 7
end
end
class Customer
@@rentals = []
def initialize(name:, rental:)
@name = name
@@rentals << rental
end
def rentals
@@rentals
end
end
class Statement
attr_accessor :total_owed
def initialize(customer:, rental:, movie:)
@customer = customer
@rental = rental
@movie = movie
end
def tally_total_owed
@total_owed = @customer.rentals.map { |rental|
rental.price_per_day * rental.days_rented
}.inject(:+)
self
end
end
class Dollar
attr_reader :amount
def initialize original
@original = original
end
def times multiplier
@amount = [@original, multiplier].inject(:*)
self
end
end
describe "Dollar" do
it "multiplication" do
five = Dollar.new(5)
multiplied = five.times(2)
assert_equal(10, multiplied.amount)
end
it "multiplication without modifying original" do
five = Dollar.new(5)
multiplied = five.times(2)
assert_equal(10, multiplied.amount)
multi = five.times(3)
assert_equal(15, multi.amount)
end
end
describe "Statement" do
it "calculates total owed for users who rented movies" do
movie = Movie.new(
title: 'john wick 3',
price_code: "2"
)
rental = Rental.new(
movie: movie,
days_rented: 12
)
customer = Customer.new(
name: 'Dain Miller',
rental: rental
)
statement = Statement.new(
customer: customer,
rental: rental,
movie: movie
).tally_total_owed
assert_equal(24, statement.total_owed)
end
end
module Clients
module Cacheable
def cache data
File.open('data.json', 'w') { |file| file.write(data) }
end
def file_exists?
not File.read('data.json').empty?
end
end
module Parseable
def parse data
JSON.parse data
end
end
module Fetchable
def fetch url
HTTParty.get(url).body
end
end
class Github
include Fetchable, Cacheable, Parseable
def initialize(org:)
cache fetch "https://api.github.com/orgs/#{org}/repos" unless file_exists?
@org ||= parse File.read('data.json')
end
def repos
@org
end
end
end
describe "GithubClient" do
it "returns the repos for a specific org passed in" do
client = Clients::Github.new(org: 'opensourcecourses')
assert_equal(1, client.repos.count)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment