Skip to content

Commit

Permalink
Merge pull request #9 from AnsarIbrahim/Unit-Testing
Browse files Browse the repository at this point in the history
Unit testing
  • Loading branch information
AnsarIbrahim authored Sep 7, 2023
2 parents 57851b2 + 545095c commit e094690
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 5 deletions.
2 changes: 1 addition & 1 deletion school_library/decorators/trimmer_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
class TrimmerDecorator < Decorator
def correct_name
name = @nameable.correct_name
name.length > 10 ? name[0..9] : name
name[0..9]
end
end
1 change: 1 addition & 0 deletions school_library/library/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Student < Person
attr_accessor :classroom
attr_reader :parent_permission

def initialize(age, name: 'Unknown', parent_permission: true)
super(generate_random_id, name: name, age: age, parent_permission: parent_permission)
Expand Down
2 changes: 1 addition & 1 deletion school_library/library/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def can_use_services?
true
end

attr_reader :specialization
attr_reader :specialization, :parent_permission

private

Expand Down
1 change: 0 additions & 1 deletion school_library/modules/create_books.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# create_book.rb
require_relative '../library/person'
require_relative '../models/book'
require_relative '../models/rental'
Expand Down
2 changes: 0 additions & 2 deletions school_library/modules/create_rentals.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# create_rentals.rb

require_relative '../library/person'
require_relative '../models/book'
require_relative '../models/rental'
Expand Down
33 changes: 33 additions & 0 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative '../school_library/models/book'

describe Book do
before :each do
@book = Book.new('My Book', 'Jane Doe')
end

describe '#new' do
it 'takes two parameters and returns a Book object' do
expect(@book).to be_an_instance_of Book
end
end

describe '#title' do
it 'returns the correct title' do
expect(@book.title).to eql 'My Book'
end
end

describe '#author' do
it 'returns the correct author' do
expect(@book.author).to eql 'Jane Doe'
end
end

describe '#add_rental' do
it 'adds a rental to the rentals array' do
rental = double('Rental')
@book.add_rental(rental)
expect(@book.rentals).to include(rental)
end
end
end
28 changes: 28 additions & 0 deletions spec/classroom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative '../school_library/models/classroom'
require_relative '../school_library/library/student'

describe Classroom do
before :each do
@my_classroom = Classroom.new('7E Upper')
end

describe '#new' do
it 'creates a Classroom instance given the label' do
expect(@my_classroom).to be_an_instance_of Classroom
end
end

describe '#label' do
it 'returns the correct label' do
@my_classroom.label.should eql '7E Upper'
end
end

describe 'Add Student'
it 'accepts new student' do
my_student = Student.new(10, name: 'Jane')
@my_classroom.add_student(my_student)
expect(@my_classroom.students.include?(my_student)).to eql true
expect(@my_classroom.students[0].name).to eql 'Jane'
end
end
23 changes: 23 additions & 0 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative '../school_library/decorators/capitalize_decorator'
require_relative '../school_library/decorators/trimmer_decorator'
require_relative '../school_library/library/person'

describe CapitalizeDecorator do
describe '#should capitalize the name' do
it 'should capitalize the name' do
person = Person.new(1, name: 'maximilianus', age: 25)
capitalized = CapitalizeDecorator.new(person)
expect(capitalized.correct_name).to eql 'Maximilianus'
end
end
end

describe TrimmerDecorator do
describe '#should trim the name' do
it 'should trim the name' do
person = Person.new(1, name: 'maximilianus', age: 25)
trimmed = TrimmerDecorator.new(person).correct_name
expect(trimmed).to eql 'maximilian'
end
end
end
83 changes: 83 additions & 0 deletions spec/person_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require_relative '../school_library/decorators/nameable'
require_relative '../school_library/library/person'

describe Person do
let(:person) { Person.new(1, name: 'Unknown', age: 25, parent_permission: true) }

describe '#initialize' do
it 'takes three parameters and returns a Person object' do
expect(person).to be_an_instance_of Person
end

it 'should assign the correct id' do
expect(person.id).to eql 1
end

it 'should assign the correct name' do
expect(person.name).to eql 'Unknown'
end

it 'should assign the correct age' do
expect(person.age).to eql 25
end

it 'should assign the correct parent_permission' do
expect(person.instance_variable_get(:@parent_permission)).to eql true
end

it 'should initialize an empty rentals array' do
expect(person.rentals).to be_empty
end
end

describe '#of_age?' do
it 'returns true if age is 18 or older' do
person.age = 20
expect(person.send(:of_age?)).to be true
end

it 'returns false if age is under 18' do
person.age = 16
expect(person.send(:of_age?)).to be false
end
end

describe '#can_use_services?' do
it 'returns true if the person is of age' do
person.age = 20
expect(person.can_use_services?).to be true
end

it 'returns true if parent permission is granted' do
person.age = 16
person.instance_variable_set(:@parent_permission, true)
expect(person.can_use_services?).to be true
end

it 'returns false if neither of age nor parent permission' do
person.age = 16
person.instance_variable_set(:@parent_permission, false)
expect(person.can_use_services?).to be false
end
end

describe '#correct_name' do
it 'returns the correct name' do
expect(person.correct_name).to eql 'Unknown'
end
end

describe '#add_rental' do
it 'adds a rental to the rentals array' do
rental = double('Rental')
person.add_rental(rental)
expect(person.rentals).to include(rental)
end
end

describe 'when name is not provided' do
it 'defaults to "Unknown"' do
expect(person.name).to eql 'Unknown'
end
end
end
37 changes: 37 additions & 0 deletions spec/rental_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative '../school_library/models/rental'
require_relative '../school_library/models/book'
require_relative '../school_library/library/student'

describe Rental do
before :each do
my_book = Book.new('My Book', 'Jane Doe')
my_person = Student.new(22, name: 'Rose', parent_permission: true)
@rental = Rental.new('1/1/2024', my_book, my_person)
end

describe '#new' do
it 'takes three parameters and returns a Rental object' do
expect(@rental).to be_an_instance_of Rental
end
end

describe '#date' do
it 'returns the correct rental date' do
expect(@rental.date).to eql '1/1/2024'
end
end

describe '#person' do
it 'assigns the correct person to the rental' do
expect(@rental.person.name).to eql 'Rose'
expect(@rental.person.age).to eql 22
end
end

describe '#book' do
it 'assigns the correct book to the rental' do
expect(@rental.book.title).to eql 'My Book'
expect(@rental.book.author).to eql 'Jane Doe'
end
end
end
44 changes: 44 additions & 0 deletions spec/student_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative '../school_library/library/student'

describe Student do
before :each do
@student = Student.new(25)
@student_without_permission = Student.new(16, name: 'Alice', parent_permission: false)
@student_with_name = Student.new(25, name: 'John')
end

describe '#initialize' do
it 'takes two parameters and returns a Student object' do
expect(@student).to be_an_instance_of Student
end
it 'should assign the correct name' do
expect(@student.name).to eql 'Unknown'
end
it 'should assign the correct age' do
expect(@student.age).to eql 25
end
it 'should assign the correct parent_permission' do
expect(@student.parent_permission).to eql true
end
it 'should override the default parent_permission' do
expect(@student_without_permission.parent_permission).to eql false
end
it 'should override the default name' do
expect(@student_with_name.name).to eql 'John'
end
end

describe '#assign_classroom' do
it 'assigns the correct classroom' do
my_classroom = Classroom.new('7E Upper')
@student.assign_to_classroom(my_classroom)
expect(@student.classroom.label).to eql '7E Upper'
end
end

describe '#play_hooky' do
it 'returns the correct message' do
expect(@student.play_hooky).to eql '¯(ツ)/¯'
end
end
end
40 changes: 40 additions & 0 deletions spec/teacher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative '../school_library/library/teacher'

describe Teacher do
before :each do
@teacher = Teacher.new('Math')
end

describe '#initialize' do
it 'takes two parameters and returns a Teacher object' do
expect(@teacher).to be_an_instance_of Teacher
end
it 'should assign the correct name' do
expect(@teacher.name).to eql 'Unknown'
end
it 'should assign the correct age' do
expect(@teacher.age).to eql nil
end
it 'should assign the correct specialization' do
expect(@teacher.specialization).to eql 'Math'
end
it 'should override the default name' do
@teacher_with_name = Teacher.new('Math', name: 'John')
expect(@teacher_with_name.name).to eql 'John'
end
it 'should override the default age' do
@teacher_with_age = Teacher.new('Math', age: 25)
expect(@teacher_with_age.age).to eql 25
end
it 'should override the default permission' do
@teacher_with_permission = Teacher.new('Math', age: 25, parent_permission: true)
expect(@teacher_with_permission.parent_permission).to eql true
end
end

describe '#can_use_services?' do
it 'returns true' do
expect(@teacher.can_use_services?).to eql true
end
end
end

0 comments on commit e094690

Please sign in to comment.