-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from AnsarIbrahim/Unit-Testing
Unit testing
- Loading branch information
Showing
12 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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 |
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
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 |
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
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 |
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
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 |
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
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 |
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
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 |
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
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 |