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..76d70c1c --- /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 + subject { described_class.new } + + describe 'task 1' do + context 'when text wrong' do + it 'returns an empty string' do + expect(subject.task1('sadhfksjadfhksahdf')).to eq('') + end + end + + context 'when log is array' do + it 'returns an empty string' do + expect(subject.task1([])).to eq('') + end + end + + context 'when log is hash' do + it 'returns an empty string' do + expect(subject.task1({})).to eq('') + end + end + + context 'when log is nil' do + it 'returns an empty string' do + expect(subject.task1(nil)).to eq('') + end + end + + context 'when log is number' do + it 'returns an empty string' do + expect(subject.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..b6ef4563 --- /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 + subject { described_class.new } + + describe 'task 2' do + context 'when text is wrong' do + it 'returns an empty array' do + expect(subject.task2('sadhfksjadfhksahdf')).to eq([]) + end + end + + context 'when log is array' do + it 'returns an empty array' do + expect(subject.task2([])).to eq([]) + end + end + + context 'when log is hash' do + 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(subject.task2(nil)).to eq([]) + end + end + + context 'when log is number' do + it 'returns an empty array' do + expect(subject.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..d611522d --- /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 + subject { described_class.new } + + describe 'task 3' do + context 'when text wrong' do + it 'returns "0"' do + expect(subject.task3('sadhfksjadfhksahdf')).to eq('0') + end + end + + context 'when log is array' do + it 'returns "0"' do + expect(subject.task3([])).to eq('0') + end + end + + context 'when log is hash' do + it 'returns "0"' do + expect(subject.task3({})).to eq('0') + end + end + + context 'when log is nil' do + it 'returns "0"' do + expect(subject.task3(nil)).to eq('0') + end + end + + context 'when log is number' do + it 'returns "0"' do + expect(subject.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..eb526080 --- /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 + subject { described_class.new } + + describe 'task 4' do + context 'when log is array' do + it 'returns { letters: 0, digits: 0 }' do + 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(subject.task4({})).to eq({ letters: 0, digits: 0 }) + end + end + + context 'when log is nil' do + it 'returns { letters: 0, digits: 0 }' do + 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(subject.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 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/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/file_manager_spec.rb b/HW04/Dmitry_Susha/spec/file_manager_spec.rb new file mode 100644 index 00000000..1cc46db0 --- /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 '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 data present' 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 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 new file mode 100644 index 00000000..f7888ba8 --- /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 '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 + 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 'call #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 'call homework.add_mentor_observer' 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 'call homework.reject!' 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 'call homework.reject!' 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 'call homework.reject!' 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 'call homework.reject!' 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 new file mode 100644 index 00000000..a8fc78b1 --- /dev/null +++ b/HW04/Dmitry_Susha/spec/spec_helper.rb @@ -0,0 +1,108 @@ +# 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' +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 +# 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 diff --git a/HW04/Dmitry_Susha/spec/student_spec.rb b/HW04/Dmitry_Susha/spec/student_spec.rb new file mode 100644 index 00000000..65b307f2 --- /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 'call homework.add_student_observer' do + subject.to_work!(homework) + expect(homework).to have_received(:add_student_observer) + end + + it 'call homework.to_work!' 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 '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 + end + + context 'without answer' do + before do + allow(homework).to receive(:add_answer!) + end + + it 'call homework.add_answer!' 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 'call homework.to_check!' do + subject.to_check!(homework) + expect(homework).to have_received(:to_check!) + end + end + end + + describe '#update' do + include_examples 'update' + end +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 new file mode 100644 index 00000000..0c25ef62 --- /dev/null +++ b/HW04/Dmitry_Susha/spec/support/sharred_examples/shared_example_for_update.rb @@ -0,0 +1,18 @@ +# 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) { described_class.new(name: 'name', surname: 'surname') } + + before do + allow(user).to receive(:add_notification) + end + + it 'wait #add_notification with arguments' do + user.update(homework) + expect(user).to have_received(:add_notification) + 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