diff --git a/school_library/decorators/trimmer_decorator.rb b/school_library/decorators/trimmer_decorator.rb index c7a0657..e9476b1 100644 --- a/school_library/decorators/trimmer_decorator.rb +++ b/school_library/decorators/trimmer_decorator.rb @@ -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 diff --git a/school_library/library/student.rb b/school_library/library/student.rb index 77d20c9..3972cb9 100644 --- a/school_library/library/student.rb +++ b/school_library/library/student.rb @@ -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) diff --git a/school_library/library/teacher.rb b/school_library/library/teacher.rb index ddb447a..02c972b 100644 --- a/school_library/library/teacher.rb +++ b/school_library/library/teacher.rb @@ -10,7 +10,7 @@ def can_use_services? true end - attr_reader :specialization + attr_reader :specialization, :parent_permission private diff --git a/school_library/modules/create_books.rb b/school_library/modules/create_books.rb index 6cf17e7..f297c55 100644 --- a/school_library/modules/create_books.rb +++ b/school_library/modules/create_books.rb @@ -1,4 +1,3 @@ -# create_book.rb require_relative '../library/person' require_relative '../models/book' require_relative '../models/rental' diff --git a/school_library/modules/create_rentals.rb b/school_library/modules/create_rentals.rb index 67f4048..7cfa304 100644 --- a/school_library/modules/create_rentals.rb +++ b/school_library/modules/create_rentals.rb @@ -1,5 +1,3 @@ -# create_rentals.rb - require_relative '../library/person' require_relative '../models/book' require_relative '../models/rental' diff --git a/spec/book_spec.rb b/spec/book_spec.rb new file mode 100644 index 0000000..395df0c --- /dev/null +++ b/spec/book_spec.rb @@ -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 diff --git a/spec/classroom_spec.rb b/spec/classroom_spec.rb new file mode 100644 index 0000000..868d2a2 --- /dev/null +++ b/spec/classroom_spec.rb @@ -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 diff --git a/spec/decorator_spec.rb b/spec/decorator_spec.rb new file mode 100644 index 0000000..810188f --- /dev/null +++ b/spec/decorator_spec.rb @@ -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 diff --git a/spec/person_spec.rb b/spec/person_spec.rb new file mode 100644 index 0000000..345bc69 --- /dev/null +++ b/spec/person_spec.rb @@ -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 diff --git a/spec/rental_spec.rb b/spec/rental_spec.rb new file mode 100644 index 0000000..ffe14c9 --- /dev/null +++ b/spec/rental_spec.rb @@ -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 diff --git a/spec/student_spec.rb b/spec/student_spec.rb new file mode 100644 index 0000000..eba06c9 --- /dev/null +++ b/spec/student_spec.rb @@ -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 diff --git a/spec/teacher_spec.rb b/spec/teacher_spec.rb new file mode 100644 index 0000000..32be803 --- /dev/null +++ b/spec/teacher_spec.rb @@ -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