Created
October 24, 2011 00:27
-
-
Save bangline/1308123 to your computer and use it in GitHub Desktop.
RubySource Demo for Test Driving to Distraction
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 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 |
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 '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 |
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
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