Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

RubySource Demo for Test Driving to Distraction

View student_file.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
class StudentLine
 
def initialize(line)
@line = line
end
 
def full_name
first_name << " " << second_name
end
 
def age
@line[28..29]
end
 
def gender
@line[30..-1].strip
end
 
private
 
def first_name
@line[14..27].strip
end
 
def second_name
@line[0..13].strip
end
 
end
 
class StudentFile
 
attr_reader :class_name
def initialize(lines, output=STDOUT)
@class_name = lines.shift.strip
@footer = lines.pop
@lines = []
 
lines.each do |line|
@lines << StudentLine.new(line) if line.length > 33
end
 
@output = output
end
 
def self.parse(filename, output=STDOUT)
f = File.new(filename)
self.new(f.readlines, output)
end
 
def valid_file?
(line_count == expected_line_count) && !line_count.zero?
end
 
def print
if self.valid_file?
@output.print formatted_output
else
@output.print "INVALID FILE"
end
end
 
private
 
def formatted_output
[class_name.upcase, separator, header, separator, students, separator].join("\n")
end
 
def header
"| STUDENT | AGE | GENDER |"
end
 
def separator
"+-----------------------------+-----+--------------+"
end
 
def students
row = []
@lines.each do |line|
row << ("| " << line.full_name.ljust(28) << "| " << line.age.ljust(4) << "| " << line.gender.ljust(13) << "|")
end
row.join("\n")
end
 
def line_count
@lines.length
end
 
def expected_line_count
@footer.to_i
end
end
View student_file.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
require 'minitest/spec'
require 'minitest/autorun'
require 'stringio'
 
require './student_file'
 
describe StudentLine do
 
before do
@student = StudentLine.new("RABBIT JESSICA 25FEMALE")
end
 
it "will return the name of the student" do
@student.full_name.must_equal "JESSICA RABBIT"
end
 
it "will return the age of the student" do
@student.age.must_equal "25"
end
 
it "should return the gender" do
@student.gender.must_equal "FEMALE"
end
 
end
 
describe StudentFile do
 
describe "parse" do
it "will return an instance of StudentFile" do
StudentFile.parse('test_file.txt').must_be_kind_of(StudentFile)
end
end
 
it "will return the correct class name" do
valid_file.class_name.must_equal "Class Name"
end
 
it "will return true if the file is valid" do
valid_file.valid_file?.must_be :==, true
end
 
it "will return false if the file is invalid" do
invalid_file.valid_file?.must_be :==, false
end
 
describe "print" do
before do
@out = StringIO.new
end
 
it "will print pipe seperated classmate info" do
valid_file(@out).print
@out.string.must_be :==, expected_output.strip
end
 
it "will display error if file is invalid" do
invalid_file(@out).print
@out.string.must_be :==, "INVALID FILE"
end
end
 
end
 
def valid_file(op=STDOUT)
StudentFile.new(["Class Name\n", "RABBIT JESSICA 25FEMALE\n", "001\n"], op)
end
 
def invalid_file(op=STDOUT)
StudentFile.new(["Class Name"], op)
end
 
def expected_output
<<OUTPUT
CLASS NAME
+-----------------------------+-----+--------------+
| STUDENT | AGE | GENDER |
+-----------------------------+-----+--------------+
| JESSICA RABBIT | 25 | FEMALE |
+-----------------------------+-----+--------------+
OUTPUT
end
View student_file.rb
1 2 3 4 5
Test Unit 101
WIGGUM RALF 13MALE
COYOTE WYLIE 72MALE
RABBIT JESSICA 25FEMALE
003
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.