From c359fd755b703488fa5ab0281fa5e4075a6aa997 Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Sat, 14 Sep 2024 11:59:55 +0300 Subject: [PATCH] feat: added tests for all tasks --- spec/spec_02.rb | 16 ++++++++++++++++ spec/spec_03.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 spec/spec_02.rb create mode 100644 spec/spec_03.rb diff --git a/spec/spec_02.rb b/spec/spec_02.rb new file mode 100644 index 0000000..44080ff --- /dev/null +++ b/spec/spec_02.rb @@ -0,0 +1,16 @@ +require_relative '../src/02_user_interface.rb' + +RSpec.describe "Main" do + before do + allow(STDIN).to receive(:gets).and_return("ruby\n") + end + + it "greets the user and checks Ruby as language" do + expect { main() }.to output("Hello my catgirl test_user! \nWhat is your love language?\nruby\nc++\npy\nПодлиза \n").to_stdout + end + + it "handles unknown language input" do + allow(STDIN).to receive(:gets).and_return("java\n") + expect { main() }.to output(/Неизвестный язык: java/).to_stdout + end +end \ No newline at end of file diff --git a/spec/spec_03.rb b/spec/spec_03.rb new file mode 100644 index 0000000..434b534 --- /dev/null +++ b/spec/spec_03.rb @@ -0,0 +1,34 @@ +require_relative '../src/03_fork_02_with_exec_pasted_command.rb' + +RSpec.describe "Main" do + before do + allow(STDIN).to receive(:gets).and_return("ruby\n", "puts 'Hello from Ruby!'\n", "echo 'Hello from shell!'\n") + allow(ARGV).to receive(:[]).with(0).and_return("test_user") + end + + it "greets the user and processes ruby input" do + expect { main() }.to output(/Hello my catgirl test_user! \nWhat is your love language?\nruby\nc++\npy\nПодлиза \n/).to_stdout + end + + it "executes valid ruby command" do + allow(STDIN).to receive(:gets).and_return("ruby\n", "puts 'Hello from Ruby!'\n") + + expect { main() }.to output(/Hello from Ruby!/).to_stdout + end + + it "handles ruby command execution error" do + allow(STDIN).to receive(:gets).and_return("ruby\n", "invalid_ruby_code\n") + + expect { main() }.to output(/Ошибка выполнения команды Ruby: undefined local variable or method `invalid_ruby_code'/).to_stdout + end + + it "executes shell command" do + allow(STDIN).to receive(:gets).and_return("ruby\n", "puts 'Hello from Ruby!'\n", "echo 'Hello from shell!'\n") + expect(main()).to include("Hello from shell!") + end + + it "returns correct output for unknown programming language" do + allow(STDIN).to receive(:gets).and_return("java\n") + expect { main() }.to output(/Неизвестный язык: java/).to_stdout + end +end