Skip to content

Instantly share code, notes, and snippets.

@yannis
Last active August 29, 2015 13:57
Show Gist options
  • Save yannis/9827692 to your computer and use it in GitHub Desktop.
Save yannis/9827692 to your computer and use it in GitHub Desktop.
require 'minitest/autorun'
require_relative 'school'
class SchoolTest < MiniTest::Unit::TestCase
def school
@school
end
def setup
@school = School.new
end
def test_an_empty_school
assert_equal({}, school.to_hash)
end
def test_add_student
school.add("Aimee", 2)
assert_equal({2 => ["Aimee"]}, school.to_hash)
end
def test_add_more_students_in_same_class
school.add("Blair", 2)
school.add("James", 2)
school.add("Paul", 2)
assert_equal({2 => ["Blair", "James", "Paul"]}, school.to_hash)
end
def test_add_students_to_different_grades
school.add("Chelsea", 3)
school.add("Logan", 7)
assert_equal({3 => ["Chelsea"], 7 => ["Logan"]}, school.to_hash)
end
def test_get_students_in_a_grade
school.add("Bradley", 5)
school.add("Franklin", 5)
school.add("Jeff", 1)
assert_equal ["Bradley", "Franklin"], school.grade(5)
end
def test_get_students_sorted_in_a_grade
school.add("Franklin", 5)
school.add("Bradley", 5)
school.add("Jeff", 1)
assert_equal ["Bradley", "Franklin"], school.grade(5)
end
def test_get_students_in_a_non_existant_grade
assert_equal [], school.grade(1)
end
def test_sort_school
school.add("Jennifer", 4)
school.add("Kareem", 6)
school.add("Christopher", 4)
school.add("Kyle", 3)
sorted = {
3 => ["Kyle"],
4 => ["Christopher", "Jennifer"],
6 => ["Kareem"]
}
assert_equal sorted, school.to_hash
assert_equal [3, 4, 6], school.to_hash.keys
end
def test_db_add_student_to_grade_after_looking_at_the_db
err = ->{ school.db[6] = ["Matilda"] }.must_raise RuntimeError
err.message.must_match "can't modify frozen Hash"
school.add("Jennifer", 4)
assert_equal school.db, {4=>["Jennifer"]}
assert_equal ["Jennifer"], school.grade(4)
end
def test_database_external_mutation
err = ->{ school.database[6] = ["Matilda"] }.must_raise NoMethodError
err.message.must_match "private method `database' called"
end
def test_sort_doesnt_alter_internal_database
school.add("Bob", 1)
school.add("Alice", 1)
original = { 1 => %w[Bob Alice] }
school.sort # shouldn't impact sort order of school.db
assert_equal original, school.db
end
def test3
school = School.new
school.add "James", 2
x = school.db
err = ->{ x[2] << "Brian" }.must_raise RuntimeError
err.message.must_match "can't modify frozen Array"
assert_equal school.db, {2=>["James"]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment