Created
March 25, 2013 21:53
-
-
Save ichabodcole/5241159 to your computer and use it in GitHub Desktop.
Robot Name Warm-up
This file contains 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 Robot | |
def initialize | |
@count = 0; | |
@name = generate_name | |
@mac = generate_name + ":" + generate_name | |
@created_at = Time.now | |
@reset_at = 0 | |
end | |
def generate_name | |
prefix = ("A".."Z").to_a.sample(2).join | |
suffix = (100..999).to_a.sample(1).join | |
prefix + suffix | |
end | |
def mac | |
@count += 1 | |
@mac | |
end | |
def name | |
@count += 1 | |
@name | |
end | |
def instruction_count | |
@count | |
end | |
def reset | |
@count += 1 | |
@name = generate_name | |
end | |
def timers | |
"#{@reset_at.to_s} seconds since reset, #{@created_at.to_s} seconds since creation" | |
end | |
end | |
robot = Robot.new | |
robot.timers |
This file contains 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/spec' | |
require 'minitest/autorun' | |
require './robot_name' | |
describe 'Robot' do | |
it "should return the same value for every name call" do | |
robo = Robot.new | |
robo.name.must_equal robo.name | |
end | |
it "should have a unique mac address that does not reset" do | |
robot = Robot.new | |
original_mac = robot.mac | |
robot.reset | |
new_mac = robot.mac | |
original_mac.must_equal new_mac | |
end | |
it "should return a unique Robot name" do | |
robot1 = Robot.new | |
robot2 = Robot.new | |
robot1.name.wont_equal robot2.name | |
end | |
it "should return a new name after reset" do | |
robot = Robot.new | |
original_name = robot.name | |
robot.reset | |
new_name = robot.name | |
original_name.wont_equal new_name | |
end | |
it "should count the number of operations" do | |
robot = Robot.new | |
robot.name | |
robot.name | |
robot.reset | |
robot.mac | |
robot.name | |
robot.instruction_count.must_equal 5 | |
end | |
it "should return a timestamp" do | |
cur_time = "0 seconds since reset, #{Time.now.to_s} seconds since creation" | |
robot = Robot.new | |
robot.timers.must_equal cur_time | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment