Last active
December 27, 2015 08:19
-
-
Save cyril/7295701 to your computer and use it in GitHub Desktop.
Exercices with Ruby specs
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 'minitest/autorun' | |
require_relative 'ascenceur-1' | |
describe Elevator do | |
before do | |
@elevator = Elevator.new | |
end | |
describe "the elevator dashboard" do | |
it "must serve several floors" do | |
@elevator.floors.count.must_be :>=, 2 | |
end | |
it "must wait at the first floor" do | |
@elevator.floor.must_equal 0 | |
end | |
it "must be empty" do | |
@elevator.people.must_be_empty | |
end | |
end | |
end |
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
class Elevator | |
attr_reader :people, :floors, :floor | |
def initialize(floors = 0..4) | |
@floors = floors | |
@floor = floors.first | |
@people = [] | |
end | |
end |
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_relative 'canard-0' | |
require 'minitest/autorun' | |
describe Duck do | |
it "must answer" do | |
Duck.new.speaks.must_equal "Coin-coin" | |
end | |
end |
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
class Duck | |
def speaks | |
'Coin-coin' | |
end | |
end |
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_relative 'canard-1' | |
require 'minitest/autorun' | |
describe "The Law of the Jungle" do | |
before do | |
@donald = Duck.new | |
@picsou = Duck.new | |
@python = Snake.new | |
@jaffar = Snake.new | |
end | |
describe "It's summer, I'm hungry!" do | |
describe Snake do | |
it "is delicious" do | |
@python.eat(@donald).must_equal "Eating a duck..." | |
end | |
it "is delicious" do | |
@python.eat(@picsou).must_equal "Eating a duck..." | |
end | |
it "is delicious" do | |
@python.eat(@jaffar).must_equal "Eating a snake..." | |
end | |
end | |
describe Duck do | |
it "is so bad, impossible to eat this" do | |
@donald.eat(@python).must_equal "Oops, I'm eating a SNAKE? I think it's too much for me!" | |
end | |
it "is bad, but I'm eating" do | |
@donald.eat(@picsou).must_equal "Eating a duck..." | |
end | |
end | |
end | |
end |
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
class Animal | |
def initialize | |
@name = 'Balthazar Picsou' | |
end | |
def name | |
@name | |
end | |
def eat(stuff) | |
"Eating a #{stuff.class.name.downcase}..." | |
end | |
end | |
class Snake < Animal | |
end | |
class Duck < Animal | |
def eat(stuff) | |
if stuff.is_a?(Snake) | |
"Oops, I'm eating a SNAKE? I think it's too much for me!" | |
else | |
super(stuff) | |
end | |
end | |
end |
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_relative 'canard-2' | |
require 'minitest/autorun' | |
describe "Animal behavior" do | |
before do | |
@donald = Duck.new | |
@picsou = Duck.new | |
@python = Snake.new | |
@bob = Pigeon.new | |
end | |
describe Pigeon do | |
it "is dangerous, but no choice" do | |
@donald.eat(@python).must_equal "Oops, I'm eating a SNAKE? I think it's too much for me!" | |
end | |
it "is delicious" do | |
@donald.eat(@bob).must_equal "Eating a pigeon..." | |
end | |
it "is terrific!" do | |
@donald.eat(@picsou).must_equal "Cannibale !!!" | |
end | |
end | |
describe Snake do | |
before do | |
@jaffar = Snake.new | |
end | |
it "is horrible" do | |
@jaffar.eat(@python).must_equal "Eating a snake..." | |
end | |
it "is delicious" do | |
@jaffar.eat(@bob).must_equal "Eating a pigeon..." | |
end | |
it "is sad!" do | |
@jaffar.eat(@picsou).must_equal "Eating a duck..." | |
end | |
it "must be scary" do | |
Snake.voice.must_equal "ZzzzZzzZZzZzz" | |
end | |
it "must sleep a long time" do | |
start_at = Time.now | |
@jaffar.hibernate | |
end_at = Time.now | |
(end_at - start_at).must_be :>=, 2 | |
end | |
end | |
describe Pigeon do | |
before do | |
@bob = Pigeon.new | |
end | |
it "must fly" do | |
@bob.fly.must_equal "I'm flying" | |
end | |
it "must run" do | |
@bob.run.must_equal "I'm running" | |
end | |
it "must sleep" do | |
@bob.sleep.must_equal "I'm sleeping" | |
end | |
it "must jump" do | |
@bob.jump.must_equal "I'm jumping" | |
end | |
end | |
end |
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
class Animal | |
def eat(stuff) | |
"Eating a #{stuff.class.name.downcase}..." | |
end | |
end | |
class Snake < Animal | |
def hibernate | |
sleep(2) | |
end | |
def Snake.voice | |
'ZzzzZzzZZzZzz' | |
end | |
end | |
class Duck < Animal | |
def eat(stuff) | |
if stuff.is_a?(Snake) | |
"Oops, I'm eating a SNAKE? I think it's too much for me!" | |
elsif stuff.is_a?(Duck) | |
"Cannibale !!!" | |
else | |
super(stuff) | |
end | |
end | |
end | |
module Action | |
def fly | |
"I'm flying" | |
end | |
def run | |
"I'm running" | |
end | |
def sleep | |
"I'm sleeping" | |
end | |
def jump | |
"I'm jumping" | |
end | |
end | |
class Pigeon | |
include Action | |
end |
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_relative 'canard-3' | |
require 'minitest/autorun' | |
describe "The Law of the Jungle" do | |
before do | |
@donald = Duck.new | |
@jaffar = Snake.new | |
end | |
describe "It's summer, I'm hungry!" do | |
describe Duck do | |
subject { Duck.new } | |
it "is impossible to eat myself" do | |
subject.eat(subject).must_equal "AiiiE!!!! J'ai failli me manger o.o'" | |
end | |
it "is sad (but I have no choice)" do | |
subject.eat(@donald).must_equal "Ce duck etait tres bon !" | |
end | |
it "is delicious" do | |
subject.eat(@jaffar).must_equal "Ce snake etait tres bon !" | |
end | |
end | |
describe Snake do | |
subject { Snake.new } | |
it "is impossible to eat myself" do | |
subject.eat(subject).must_equal "AiiiE!!!! J'ai failli me manger o.o'" | |
end | |
it "is delicious" do | |
subject.eat(@donald).must_equal "Ce duck etait tres bon !" | |
end | |
it "is sad (but I have no choice)" do | |
subject.eat(@jaffar).must_equal "Ce snake etait tres bon !" | |
end | |
end | |
end | |
end |
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
class Animal | |
def eat(stuff) | |
print "#{self.class}> Miamm... " | |
sleep(0.3) | |
if stuff.object_id == self.object_id | |
"AiiiE!!!! J'ai failli me manger o.o'" | |
else | |
"Ce #{stuff.class.name.downcase} etait tres bon !" | |
end | |
end | |
end | |
class Duck < Animal | |
end | |
class Snake < Animal | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment