From 7f643b2568127b8ef97f7224a55ee936151e232d Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sun, 3 Apr 2022 17:22:25 +0300 Subject: [PATCH 1/8] rspec init in hw_04 --- HW04/Dmitry_Susha/.rspec | 1 + HW04/Dmitry_Susha/spec/spec_helper.rb | 107 ++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 HW04/Dmitry_Susha/.rspec create mode 100644 HW04/Dmitry_Susha/spec/spec_helper.rb diff --git a/HW04/Dmitry_Susha/.rspec b/HW04/Dmitry_Susha/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/HW04/Dmitry_Susha/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/HW04/Dmitry_Susha/spec/spec_helper.rb b/HW04/Dmitry_Susha/spec/spec_helper.rb new file mode 100644 index 00000000..ccd6f13d --- /dev/null +++ b/HW04/Dmitry_Susha/spec/spec_helper.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require_relative '../lib/file_manager' +require_relative '../lib/homework_observable' +require_relative '../lib/homework' +require_relative '../lib/mentor' +require_relative '../lib/notification' +require_relative '../lib/student' +require_relative '../lib/user' +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # This setting enables warnings. It's recommended, but in some cases may + # # be too noisy due to issues in dependencies. + # config.warnings = true + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end From 6a04a987647554dc6bae7c160cd1d1aa2d381868 Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sun, 10 Apr 2022 17:12:55 +0300 Subject: [PATCH 2/8] add file_manager_spec --- HW04/Dmitry_Susha/spec/file_manager_spec.rb | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 HW04/Dmitry_Susha/spec/file_manager_spec.rb diff --git a/HW04/Dmitry_Susha/spec/file_manager_spec.rb b/HW04/Dmitry_Susha/spec/file_manager_spec.rb new file mode 100644 index 00000000..dfe4c600 --- /dev/null +++ b/HW04/Dmitry_Susha/spec/file_manager_spec.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +RSpec.describe FileManager do + let(:foo_class) { Class.new { include FileManager } } + let(:dir) { 'spec/helpers' } + let(:file_name) { 'test.txt' } + + describe '.create_file' do + context 'when succes create file' do + after do + File.delete('spec/helpers/test.txt') + end + + it 'file created' do + expect(foo_class.new.create_file(dir, file_name)).to be_a_kind_of(File) + end + end + end + + describe '.add_data' do + context 'when succes add data in file' do + before do + File.open('spec/helpers/test.txt', 'a+') + 3.times do + foo_class.new.add_data(dir, file_name, '!') + end + end + + after do + File.delete('spec/helpers/test.txt') + end + + it 'Each line have data' do + File.open("#{dir}/#{file_name}", 'r').each do |line| + expect(line.chomp).to eq('!') + end + end + end + + context 'when data empty' do + before do + File.open('spec/helpers/test.txt', 'a+') + 3.times do + foo_class.new.add_data(dir, file_name, '') + end + end + + after do + File.delete('spec/helpers/test.txt') + end + + it 'Each line empty' do + File.open("#{dir}/#{file_name}", 'r').each do |line| + expect(line.chomp).to eq('') + end + end + end + end + + describe '.read_dir' do + context 'when there is a file' do + before do + File.open('spec/helpers/test.txt', 'a+') + end + + after do + File.delete('spec/helpers/test.txt') + end + + it 'show file name' do + expect(foo_class.new.read_dir(dir)).to include('test.txt') + end + end + + context 'when the directory is empty' do + it 'show array without files names' do + expect(foo_class.new.read_dir(dir)).to eq(['..', '.']) + end + end + end +end From 918a482e023777ecf1bca04f60f0d509c6e615f2 Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sun, 10 Apr 2022 21:07:15 +0300 Subject: [PATCH 3/8] add mentor spec --- HW04/Dmitry_Susha/spec/mentor_spec.rb | 104 ++++++++++++++++++ HW04/Dmitry_Susha/spec/spec_helper.rb | 1 + .../shared_example_for_update.rb | 22 ++++ HW04/Dmitry_Susha/spec/user_spec.rb | 17 +++ 4 files changed, 144 insertions(+) create mode 100644 HW04/Dmitry_Susha/spec/mentor_spec.rb create mode 100644 HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb create mode 100644 HW04/Dmitry_Susha/spec/user_spec.rb diff --git a/HW04/Dmitry_Susha/spec/mentor_spec.rb b/HW04/Dmitry_Susha/spec/mentor_spec.rb new file mode 100644 index 00000000..3881b005 --- /dev/null +++ b/HW04/Dmitry_Susha/spec/mentor_spec.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +RSpec.describe Mentor do + subject { described_class.new(name: 'Ivan', surname: 'Ivanov') } + + let(:student) { Student.new(name: 'Petr', surname: 'Petrov') } + let(:homework) { Homework.new(title: 'title', description: 'fake', student: student) } + + describe '#add_homework' do + before do + allow(Homework).to receive(:new).with( + title: 'title', + description: 'fake', + student: student + ).and_return('homework') + end + + it 'create new homework' do + expect(subject.add_homework(title: 'title', description: 'fake', student: student)).to eq('homework') + end + + it 'wait Homework.new call' do + subject.add_homework(title: 'title', description: 'fake', student: student) + expect(Homework).to have_received(:new).with(title: 'title', description: 'fake', student: student) + end + end + + describe '#invite_student_to_work' do + let(:mentor) { instance_double(described_class) } + + before do + allow(mentor).to receive(:add_notification).with(homework.student) + allow(mentor).to receive(:invite_student_to_work).with(homework).and_return(mentor.add_notification(homework.student)) + end + + it 'wait #add_notification' do + mentor.invite_student_to_work(homework) + expect(mentor).to have_received(:add_notification).with(homework.student) + end + end + + describe '#subscribe_to' do + before do + allow(homework).to receive(:add_mentor_observer) + end + + it 'wait homework.add_mentor_observer call' do + subject.subscribe_to!(homework) + expect(homework).to have_received(:add_mentor_observer) + end + end + + describe '#reject_to_work!' do + context "with remarks = 'bad'" do + before do + allow(homework).to receive(:reject!).with(subject, 'bad') + end + + it 'wait homework.reject! call with remarks = bad' do + subject.reject_to_work!(homework, 'bad') + expect(homework).to have_received(:reject!).with(subject, 'bad') + end + end + + context 'without remarks' do + before do + allow(homework).to receive(:reject!).with(subject, '') + end + + it 'wait homework.reject! call' do + subject.reject_to_work!(homework) + expect(homework).to have_received(:reject!).with(subject, '') + end + end + end + + describe '#accept!' do + context 'with default grade' do + before do + allow(homework).to receive(:accept!).with(5) + end + + it 'wait homework.reject! call' do + subject.accept!(homework) + expect(homework).to have_received(:accept!).with(5) + end + end + + context 'with grade = 4' do + before do + allow(homework).to receive(:accept!).with(4) + end + + it 'wait homework.reject! call' do + subject.accept!(homework, 4) + expect(homework).to have_received(:accept!).with(4) + end + end + end + + describe '#update' do + include_examples 'update' + end +end diff --git a/HW04/Dmitry_Susha/spec/spec_helper.rb b/HW04/Dmitry_Susha/spec/spec_helper.rb index ccd6f13d..a8fc78b1 100644 --- a/HW04/Dmitry_Susha/spec/spec_helper.rb +++ b/HW04/Dmitry_Susha/spec/spec_helper.rb @@ -7,6 +7,7 @@ require_relative '../lib/notification' require_relative '../lib/student' require_relative '../lib/user' +Dir['./spec/support/**/*.rb'].sort.each { |f| require f } # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause diff --git a/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb b/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb new file mode 100644 index 00000000..8ef5fbbb --- /dev/null +++ b/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'update' do + describe '#update' do + let(:student) { Student.new(name: 'Petr', surname: 'Petrov') } + let(:homework) { Homework.new(title: 'title', description: 'fake', student: student) } + let(:user) { instance_double(described_class) } + let(:notification_message) do + "#{homework.student.name} #{homework.student.surname} #{homework.title} - #{homework.state}" + end + + before do + allow(user).to receive(:add_notification).with(user, notification_message) + allow(user).to receive(:update).with(homework).and_return(user.add_notification(user, notification_message)) + end + + it 'wait #add_notification with arguments' do + user.update(homework) + expect(user).to have_received(:add_notification).with(user, notification_message) + end + end +end diff --git a/HW04/Dmitry_Susha/spec/user_spec.rb b/HW04/Dmitry_Susha/spec/user_spec.rb new file mode 100644 index 00000000..9b58df65 --- /dev/null +++ b/HW04/Dmitry_Susha/spec/user_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.describe User do + subject { described_class.new(name: 'Ivan', surname: 'Ivanov') } + + describe '#name' do + it 'user has name' do + expect(subject.name).to eq('Ivan') + end + end + + describe '#surname' do + it 'user has surname' do + expect(subject.surname).to eq('Ivanov') + end + end +end From e6055df22bb5ac1bc5a1e757d2b496c9c4656331 Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sun, 10 Apr 2022 22:06:52 +0300 Subject: [PATCH 4/8] add student_spec --- HW04/Dmitry_Susha/lib/student.rb | 2 +- HW04/Dmitry_Susha/spec/student_spec.rb | 78 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 HW04/Dmitry_Susha/spec/student_spec.rb diff --git a/HW04/Dmitry_Susha/lib/student.rb b/HW04/Dmitry_Susha/lib/student.rb index 35ab4abd..bd20db9f 100644 --- a/HW04/Dmitry_Susha/lib/student.rb +++ b/HW04/Dmitry_Susha/lib/student.rb @@ -14,7 +14,7 @@ def to_work!(homework) homework.to_work! end - def add_answer!(homework, answer) + def add_answer!(homework, answer = '') homework.add_answer!(answer) end diff --git a/HW04/Dmitry_Susha/spec/student_spec.rb b/HW04/Dmitry_Susha/spec/student_spec.rb new file mode 100644 index 00000000..d7c1821b --- /dev/null +++ b/HW04/Dmitry_Susha/spec/student_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +RSpec.describe Student do + subject { described_class.new(name: 'Petr', surname: 'Petrov') } + + let(:homework) { Homework.new(title: 'title', description: 'fake', student: subject) } + + describe '#homeworks' do + let(:file_name1) { 'homework.txt' } + let(:user) { instance_double(described_class) } + + before do + allow(user).to receive(:homeworks).and_return([file_name1]) + end + + it 'return file' do + expect(user.homeworks).to eq([file_name1]) + end + end + + describe '#to_work!' do + before do + allow(homework).to receive(:add_student_observer) + allow(homework).to receive(:to_work!) + end + + it 'wait homework.add_student_observer call' do + subject.to_work!(homework) + expect(homework).to have_received(:add_student_observer) + end + + it 'wait homework.to_work! call' do + subject.to_work!(homework) + expect(homework).to have_received(:to_work!) + end + end + + describe '#add_answer!' do + context "with answer = 'i dont know'" do + before do + allow(homework).to receive(:add_answer!).with('i dont know') + end + + it 'wait homework.reject! call with answer = i dont know' do + subject.add_answer!(homework, 'i dont know') + expect(homework).to have_received(:add_answer!).with('i dont know') + end + end + + context 'without answer' do + before do + allow(homework).to receive(:add_answer!) + end + + it 'wait homework.reject! call' do + subject.add_answer!(homework) + expect(homework).to have_received(:add_answer!) + end + end + end + + describe '#to_check!' do + context "with answer = 'i dont know'" do + before do + allow(homework).to receive(:to_check!) + end + + it 'wait homework.reject! call with answer = i dont know' do + subject.to_check!(homework) + expect(homework).to have_received(:to_check!) + end + end + end + + describe '#update' do + include_examples 'update' + end +end From 7bdaedae30d0b8d10756d83b539f3c5e77bc4268 Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sun, 10 Apr 2022 23:52:02 +0300 Subject: [PATCH 5/8] add homework_spec and some refactoring --- HW04/Dmitry_Susha/spec/homework_spec.rb | 114 ++++++++++++++++++ HW04/Dmitry_Susha/spec/mentor_spec.rb | 14 +-- HW04/Dmitry_Susha/spec/student_spec.rb | 10 +- .../shared_example_for_update.rb | 10 +- 4 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 HW04/Dmitry_Susha/spec/homework_spec.rb diff --git a/HW04/Dmitry_Susha/spec/homework_spec.rb b/HW04/Dmitry_Susha/spec/homework_spec.rb new file mode 100644 index 00000000..db4816dd --- /dev/null +++ b/HW04/Dmitry_Susha/spec/homework_spec.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +RSpec.describe Homework do + subject { described_class.new(title: 'title', description: 'fake', student: student) } + + let(:student) { Student.new(name: 'Petr', surname: 'Petrov') } + + describe '#title' do + it 'homework has title' do + expect(subject.title).to eq('title') + end + end + + describe '#description' do + it 'homework has description' do + expect(subject.description).to eq('fake') + end + end + + describe '#student' do + it 'homework has student' do + expect(subject.student).to eq(student) + end + end + + describe '#state' do + it 'homework has state Created' do + expect(subject.state).to eq('Created') + end + end + + describe '#add_homework_file' do + let(:homework) { described_class.new(title: 'title', description: 'fake', student: student) } + let(:homework_data) { 'data' } + + before do + allow(homework).to receive(:add_data) + end + + it 'call #add_data' do + homework.add_homework_file + expect(homework).to have_received(:add_data) + end + end + + describe '#to_work!' do + let(:homework) { described_class.new(title: 'title', description: 'fake', student: student) } + + before do + allow(homework).to receive_messages(update_state_in_file: 'foo', notify_mentors: 'bar') + end + + it 'state = In work ' do + homework.to_work! + expect(homework.state).to eq('In work') + end + end + + describe '#to_check!' do + let(:homework) { described_class.new(title: 'title', description: 'fake', student: student) } + + before do + allow(homework).to receive_messages(update_state_in_file: 'foo', notify_mentors: 'bar') + end + + it 'state = Pending rewiev' do + homework.to_check! + expect(homework.state).to eq('Pending rewiev') + end + end + + describe '#reject!' do + let(:homework) { described_class.new(title: 'title', description: 'fake', student: student) } + let(:mentor) { 'mentor' } + let(:remarks) { 'remarks' } + + before do + allow(homework).to receive_messages(update_state_in_file: 'foo', notify_students: 'bar', add_remarks: 'baz') + end + + it 'state = Rejected' do + homework.reject!(mentor, remarks) + expect(homework.state).to eq('Rejected') + end + end + + describe '#accept!' do + let(:homework) { described_class.new(title: 'title', description: 'fake', student: student) } + let(:grade) { 5 } + + before do + allow(homework).to receive_messages(update_state_in_file: 'foo', notify_students: 'bar', add_grade: 'baz') + end + + it 'state = Passed' do + homework.accept!(grade) + expect(homework.state).to eq('Passed') + end + end + + describe '#add_answer' do + let(:homework) { described_class.new(title: 'title', description: 'fake', student: student) } + let(:answer) { 'answer' } + + before do + allow(homework).to receive(:add_data) + end + + it 'call #add_data' do + homework.add_answer!(answer) + expect(homework).to have_received(:add_data) + end + end +end diff --git a/HW04/Dmitry_Susha/spec/mentor_spec.rb b/HW04/Dmitry_Susha/spec/mentor_spec.rb index 3881b005..f7888ba8 100644 --- a/HW04/Dmitry_Susha/spec/mentor_spec.rb +++ b/HW04/Dmitry_Susha/spec/mentor_spec.rb @@ -19,7 +19,7 @@ expect(subject.add_homework(title: 'title', description: 'fake', student: student)).to eq('homework') end - it 'wait Homework.new call' do + it 'call Homework.new call' do subject.add_homework(title: 'title', description: 'fake', student: student) expect(Homework).to have_received(:new).with(title: 'title', description: 'fake', student: student) end @@ -33,7 +33,7 @@ allow(mentor).to receive(:invite_student_to_work).with(homework).and_return(mentor.add_notification(homework.student)) end - it 'wait #add_notification' do + it 'call #add_notification' do mentor.invite_student_to_work(homework) expect(mentor).to have_received(:add_notification).with(homework.student) end @@ -44,7 +44,7 @@ allow(homework).to receive(:add_mentor_observer) end - it 'wait homework.add_mentor_observer call' do + it 'call homework.add_mentor_observer' do subject.subscribe_to!(homework) expect(homework).to have_received(:add_mentor_observer) end @@ -56,7 +56,7 @@ allow(homework).to receive(:reject!).with(subject, 'bad') end - it 'wait homework.reject! call with remarks = bad' do + it 'call homework.reject!' do subject.reject_to_work!(homework, 'bad') expect(homework).to have_received(:reject!).with(subject, 'bad') end @@ -67,7 +67,7 @@ allow(homework).to receive(:reject!).with(subject, '') end - it 'wait homework.reject! call' do + it 'call homework.reject!' do subject.reject_to_work!(homework) expect(homework).to have_received(:reject!).with(subject, '') end @@ -80,7 +80,7 @@ allow(homework).to receive(:accept!).with(5) end - it 'wait homework.reject! call' do + it 'call homework.reject!' do subject.accept!(homework) expect(homework).to have_received(:accept!).with(5) end @@ -91,7 +91,7 @@ allow(homework).to receive(:accept!).with(4) end - it 'wait homework.reject! call' do + it 'call homework.reject!' do subject.accept!(homework, 4) expect(homework).to have_received(:accept!).with(4) end diff --git a/HW04/Dmitry_Susha/spec/student_spec.rb b/HW04/Dmitry_Susha/spec/student_spec.rb index d7c1821b..65b307f2 100644 --- a/HW04/Dmitry_Susha/spec/student_spec.rb +++ b/HW04/Dmitry_Susha/spec/student_spec.rb @@ -24,12 +24,12 @@ allow(homework).to receive(:to_work!) end - it 'wait homework.add_student_observer call' do + it 'call homework.add_student_observer' do subject.to_work!(homework) expect(homework).to have_received(:add_student_observer) end - it 'wait homework.to_work! call' do + it 'call homework.to_work!' do subject.to_work!(homework) expect(homework).to have_received(:to_work!) end @@ -41,7 +41,7 @@ allow(homework).to receive(:add_answer!).with('i dont know') end - it 'wait homework.reject! call with answer = i dont know' do + it 'call homework.add_answer! call' do subject.add_answer!(homework, 'i dont know') expect(homework).to have_received(:add_answer!).with('i dont know') end @@ -52,7 +52,7 @@ allow(homework).to receive(:add_answer!) end - it 'wait homework.reject! call' do + it 'call homework.add_answer!' do subject.add_answer!(homework) expect(homework).to have_received(:add_answer!) end @@ -65,7 +65,7 @@ allow(homework).to receive(:to_check!) end - it 'wait homework.reject! call with answer = i dont know' do + it 'call homework.to_check!' do subject.to_check!(homework) expect(homework).to have_received(:to_check!) end diff --git a/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb b/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb index 8ef5fbbb..0c25ef62 100644 --- a/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb +++ b/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb @@ -4,19 +4,15 @@ describe '#update' do let(:student) { Student.new(name: 'Petr', surname: 'Petrov') } let(:homework) { Homework.new(title: 'title', description: 'fake', student: student) } - let(:user) { instance_double(described_class) } - let(:notification_message) do - "#{homework.student.name} #{homework.student.surname} #{homework.title} - #{homework.state}" - end + let(:user) { described_class.new(name: 'name', surname: 'surname') } before do - allow(user).to receive(:add_notification).with(user, notification_message) - allow(user).to receive(:update).with(homework).and_return(user.add_notification(user, notification_message)) + allow(user).to receive(:add_notification) end it 'wait #add_notification with arguments' do user.update(homework) - expect(user).to have_received(:add_notification).with(user, notification_message) + expect(user).to have_received(:add_notification) end end end From 3eba5e86e6da28cf1d03dac93a7d395d755ad6e3 Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Mon, 11 Apr 2022 02:34:46 +0300 Subject: [PATCH 6/8] add spec for hw 03 --- HW03/Dmitry_Susha/.rspec | 1 + HW03/Dmitry_Susha/dmitry_susha_hw_03_t_01.rb | 2 + HW03/Dmitry_Susha/dmitry_susha_hw_03_t_02.rb | 5 +- HW03/Dmitry_Susha/dmitry_susha_hw_03_t_03.rb | 2 + HW03/Dmitry_Susha/dmitry_susha_hw_03_t_04.rb | 2 + .../spec/dmitry_susha_hw_03_t_01_spec.rb | 37 ++++++ .../spec/dmitry_susha_hw_03_t_02_spec.rb | 37 ++++++ .../spec/dmitry_susha_hw_03_t_03_spec.rb | 37 ++++++ .../spec/dmitry_susha_hw_03_t_04_spec.rb | 31 ++++++ HW03/Dmitry_Susha/spec/spec_helper.rb | 105 ++++++++++++++++++ 10 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 HW03/Dmitry_Susha/.rspec create mode 100644 HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb create mode 100644 HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb create mode 100644 HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb create mode 100644 HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb create mode 100644 HW03/Dmitry_Susha/spec/spec_helper.rb diff --git a/HW03/Dmitry_Susha/.rspec b/HW03/Dmitry_Susha/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/HW03/Dmitry_Susha/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_01.rb b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_01.rb index c614a151..0ef6fcd9 100644 --- a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_01.rb +++ b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_01.rb @@ -2,6 +2,8 @@ class Homework3 def task1(logs) + return '' unless logs.is_a?(String) + logs.split("\n").select { |string| string.downcase.include?('error') }[0].to_s end end diff --git a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_02.rb b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_02.rb index 835ec157..2ac835e5 100644 --- a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_02.rb +++ b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_02.rb @@ -3,9 +3,10 @@ class Homework3 FORMAT_STRING = %r{^(?:[0-9]{1,3}\.){3}[0-9]{1,3} - - \[\d*/\w*/\d*:\d*:\d*:\d* \+\d*\] "\w* /\w*/\d/\w*} def task2(logs) + return [] unless logs.is_a?(String) + lines = logs.split("\n") - lines.select { |string| check_format?(string) } - .map { |string| make_output(string) } + lines.select { |string| check_format?(string) }.map { |string| make_output(string) } end private diff --git a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_03.rb b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_03.rb index 1da73301..174cd421 100644 --- a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_03.rb +++ b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_03.rb @@ -4,6 +4,8 @@ class Homework3 START_EVENT_LINE = /Calling core with action:/ def task3(logs) + return '0' unless logs.is_a?(String) + lines = logs.split("\n") start_event_lines = lines.select { |string| string.match?(START_EVENT_LINE) } return '0' if start_event_lines.size < 2 diff --git a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_04.rb b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_04.rb index da9c8b55..67690158 100644 --- a/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_04.rb +++ b/HW03/Dmitry_Susha/dmitry_susha_hw_03_t_04.rb @@ -2,6 +2,8 @@ class Homework3 def task4(string) + return { letters: 0, digits: 0 } unless string.is_a?(String) + letters = string.count('a-zA-Z') digits = string.count('0-9') { letters: letters, digits: digits } diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb new file mode 100644 index 00000000..d9d990bd --- /dev/null +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +RSpec.describe Homework3 do + let(:obj) { described_class.new } + + describe 'task 1' do + context 'when text wrong' do + it 'returns an empty string' do + expect(obj.task1('sadhfksjadfhksahdf')).to eq('') + end + end + + context 'when log is array' do + it 'returns an empty string' do + expect(obj.task1([])).to eq('') + end + end + + context 'when log is hash' do + it 'returns an empty string' do + expect(obj.task1({})).to eq('') + end + end + + context 'when log is nil' do + it 'returns an empty string' do + expect(obj.task1(nil)).to eq('') + end + end + + context 'when log is number' do + it 'returns an empty string' do + expect(obj.task1(1)).to eq('') + end + end + end +end diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb new file mode 100644 index 00000000..0efd9534 --- /dev/null +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +RSpec.describe Homework3 do + let(:obj) { described_class.new } + + describe 'task 2' do + context 'when text wrong' do + it 'returns an empty array' do + expect(obj.task2('sadhfksjadfhksahdf')).to eq([]) + end + end + + context 'when log is array' do + it 'returns an empty array' do + expect(obj.task2([])).to eq([]) + end + end + + context 'when log is hash' do + it 'returns an emrpty array' do + expect(obj.task2({})).to eq([]) + end + end + + context 'when log is nil' do + it 'returns an empty array' do + expect(obj.task2(nil)).to eq([]) + end + end + + context 'when log is number' do + it 'returns an empty array' do + expect(obj.task2(1)).to eq([]) + end + end + end +end diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb new file mode 100644 index 00000000..be4f8cad --- /dev/null +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +RSpec.describe Homework3 do + let(:obj) { described_class.new } + + describe 'task 3' do + context 'when text wrong' do + it 'returns "0"' do + expect(obj.task3('sadhfksjadfhksahdf')).to eq('0') + end + end + + context 'when log is array' do + it 'returns "0"' do + expect(obj.task3([])).to eq('0') + end + end + + context 'when log is hash' do + it 'returns "0"' do + expect(obj.task3({})).to eq('0') + end + end + + context 'when log is nil' do + it 'returns "0"' do + expect(obj.task3(nil)).to eq('0') + end + end + + context 'when log is number' do + it 'returns "0"' do + expect(obj.task3(1)).to eq('0') + end + end + end +end diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb new file mode 100644 index 00000000..dad6afdf --- /dev/null +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +RSpec.describe Homework3 do + let(:obj) { described_class.new } + + describe 'task 4' do + context 'when log is array' do + it 'returns { letters: 0, digits: 0 }' do + expect(obj.task4([])).to eq({ letters: 0, digits: 0 }) + end + end + + context 'when log is hash' do + it 'returns { letters: 0, digits: 0 }' do + expect(obj.task4({})).to eq({ letters: 0, digits: 0 }) + end + end + + context 'when log is nil' do + it 'returns { letters: 0, digits: 0 }' do + expect(obj.task4(nil)).to eq({ letters: 0, digits: 0 }) + end + end + + context 'when log is number' do + it 'returns { letters: 0, digits: 0 }' do + expect(obj.task4(1)).to eq({ letters: 0, digits: 0 }) + end + end + end +end diff --git a/HW03/Dmitry_Susha/spec/spec_helper.rb b/HW03/Dmitry_Susha/spec/spec_helper.rb new file mode 100644 index 00000000..977f242c --- /dev/null +++ b/HW03/Dmitry_Susha/spec/spec_helper.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +require_relative '../dmitry_susha_hw_03_t_01' +require_relative '../dmitry_susha_hw_03_t_02' +require_relative '../dmitry_susha_hw_03_t_03' +require_relative '../dmitry_susha_hw_03_t_04' + +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # This setting enables warnings. It's recommended, but in some cases may + # # be too noisy due to issues in dependencies. + # config.warnings = true + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end From 3044e8e0bf8f7ea54e378d06d285a4654a1d608f Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sat, 23 Apr 2022 14:01:31 +0300 Subject: [PATCH 7/8] fix in hw 03 --- .../spec/dmitry_susha_hw_03_t_01_spec.rb | 12 ++++++------ .../spec/dmitry_susha_hw_03_t_02_spec.rb | 16 ++++++++-------- .../spec/dmitry_susha_hw_03_t_03_spec.rb | 12 ++++++------ .../spec/dmitry_susha_hw_03_t_04_spec.rb | 10 +++++----- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb index d9d990bd..76d70c1c 100644 --- a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_01_spec.rb @@ -1,36 +1,36 @@ # frozen_string_literal: true RSpec.describe Homework3 do - let(:obj) { described_class.new } + subject { described_class.new } describe 'task 1' do context 'when text wrong' do it 'returns an empty string' do - expect(obj.task1('sadhfksjadfhksahdf')).to eq('') + expect(subject.task1('sadhfksjadfhksahdf')).to eq('') end end context 'when log is array' do it 'returns an empty string' do - expect(obj.task1([])).to eq('') + expect(subject.task1([])).to eq('') end end context 'when log is hash' do it 'returns an empty string' do - expect(obj.task1({})).to eq('') + expect(subject.task1({})).to eq('') end end context 'when log is nil' do it 'returns an empty string' do - expect(obj.task1(nil)).to eq('') + expect(subject.task1(nil)).to eq('') end end context 'when log is number' do it 'returns an empty string' do - expect(obj.task1(1)).to eq('') + expect(subject.task1(1)).to eq('') end end end diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb index 0efd9534..b6ef4563 100644 --- a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_02_spec.rb @@ -1,36 +1,36 @@ # frozen_string_literal: true RSpec.describe Homework3 do - let(:obj) { described_class.new } + subject { described_class.new } describe 'task 2' do - context 'when text wrong' do + context 'when text is wrong' do it 'returns an empty array' do - expect(obj.task2('sadhfksjadfhksahdf')).to eq([]) + expect(subject.task2('sadhfksjadfhksahdf')).to eq([]) end end context 'when log is array' do it 'returns an empty array' do - expect(obj.task2([])).to eq([]) + expect(subject.task2([])).to eq([]) end end context 'when log is hash' do - it 'returns an emrpty array' do - expect(obj.task2({})).to eq([]) + it 'returns an empty array' do + expect(subject.task2({})).to eq([]) end end context 'when log is nil' do it 'returns an empty array' do - expect(obj.task2(nil)).to eq([]) + expect(subject.task2(nil)).to eq([]) end end context 'when log is number' do it 'returns an empty array' do - expect(obj.task2(1)).to eq([]) + expect(subject.task2(1)).to eq([]) end end end diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb index be4f8cad..d611522d 100644 --- a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_03_spec.rb @@ -1,36 +1,36 @@ # frozen_string_literal: true RSpec.describe Homework3 do - let(:obj) { described_class.new } + subject { described_class.new } describe 'task 3' do context 'when text wrong' do it 'returns "0"' do - expect(obj.task3('sadhfksjadfhksahdf')).to eq('0') + expect(subject.task3('sadhfksjadfhksahdf')).to eq('0') end end context 'when log is array' do it 'returns "0"' do - expect(obj.task3([])).to eq('0') + expect(subject.task3([])).to eq('0') end end context 'when log is hash' do it 'returns "0"' do - expect(obj.task3({})).to eq('0') + expect(subject.task3({})).to eq('0') end end context 'when log is nil' do it 'returns "0"' do - expect(obj.task3(nil)).to eq('0') + expect(subject.task3(nil)).to eq('0') end end context 'when log is number' do it 'returns "0"' do - expect(obj.task3(1)).to eq('0') + expect(subject.task3(1)).to eq('0') end end end diff --git a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb index dad6afdf..eb526080 100644 --- a/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb +++ b/HW03/Dmitry_Susha/spec/dmitry_susha_hw_03_t_04_spec.rb @@ -1,30 +1,30 @@ # frozen_string_literal: true RSpec.describe Homework3 do - let(:obj) { described_class.new } + subject { described_class.new } describe 'task 4' do context 'when log is array' do it 'returns { letters: 0, digits: 0 }' do - expect(obj.task4([])).to eq({ letters: 0, digits: 0 }) + expect(subject.task4([])).to eq({ letters: 0, digits: 0 }) end end context 'when log is hash' do it 'returns { letters: 0, digits: 0 }' do - expect(obj.task4({})).to eq({ letters: 0, digits: 0 }) + expect(subject.task4({})).to eq({ letters: 0, digits: 0 }) end end context 'when log is nil' do it 'returns { letters: 0, digits: 0 }' do - expect(obj.task4(nil)).to eq({ letters: 0, digits: 0 }) + expect(subject.task4(nil)).to eq({ letters: 0, digits: 0 }) end end context 'when log is number' do it 'returns { letters: 0, digits: 0 }' do - expect(obj.task4(1)).to eq({ letters: 0, digits: 0 }) + expect(subject.task4(1)).to eq({ letters: 0, digits: 0 }) end end end From cf3fa870f39b9a6965dd16e0df1d40789a74f926 Mon Sep 17 00:00:00 2001 From: DmitryS1989 Date: Sat, 23 Apr 2022 14:07:09 +0300 Subject: [PATCH 8/8] fix in hw 04 --- HW04/Dmitry_Susha/spec/file_manager_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HW04/Dmitry_Susha/spec/file_manager_spec.rb b/HW04/Dmitry_Susha/spec/file_manager_spec.rb index dfe4c600..1cc46db0 100644 --- a/HW04/Dmitry_Susha/spec/file_manager_spec.rb +++ b/HW04/Dmitry_Susha/spec/file_manager_spec.rb @@ -11,14 +11,14 @@ File.delete('spec/helpers/test.txt') end - it 'file created' do + it 'creates file' do expect(foo_class.new.create_file(dir, file_name)).to be_a_kind_of(File) end end end describe '.add_data' do - context 'when succes add data in file' do + context 'when data present' do before do File.open('spec/helpers/test.txt', 'a+') 3.times do