diff --git a/lib/fakerbot/cli.rb b/lib/fakerbot/cli.rb index 16d8408..2efebdd 100644 --- a/lib/fakerbot/cli.rb +++ b/lib/fakerbot/cli.rb @@ -26,7 +26,7 @@ def list(*) if options[:help] invoke :help, ['list'] else - Fakerbot::Commands::List.new(options).execute + FakerBot::Commands::List.new(options).execute end end diff --git a/lib/fakerbot/commands/list.rb b/lib/fakerbot/commands/list.rb index d79f8dd..994cf74 100644 --- a/lib/fakerbot/commands/list.rb +++ b/lib/fakerbot/commands/list.rb @@ -2,7 +2,7 @@ require_relative '../command' -module Fakerbot +module FakerBot module Commands class List < FakerBot::Command def initialize(options) diff --git a/lib/fakerbot/commands/search.rb b/lib/fakerbot/commands/search.rb index a36222c..d959a56 100644 --- a/lib/fakerbot/commands/search.rb +++ b/lib/fakerbot/commands/search.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'fakerbot/bot' +require_relative '../command' module FakerBot module Commands @@ -9,15 +10,15 @@ def initialize(options) @options = options end - def execute(input) - render FakerBot::Bot.find(input) + def execute(input, output: $stdout) + render FakerBot::Bot.find(input), output end private - def render(result) + def render(result, output) return not_found if result.empty? - super + super(result, output) end def not_found diff --git a/spec/integration/search_spec.rb b/spec/integration/search_spec.rb new file mode 100644 index 0000000..c9c8aac --- /dev/null +++ b/spec/integration/search_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +RSpec.describe '`fakerbot search` command', type: :cli do + it 'executes `fakerbot help search` command successfully' do + output = `fakerbot help search` + expected_output = <<~OUT + Usage: + fakerbot search [Faker] + + Options: + -h, [--help], [--no-help] # Display usage information + -v, [--verbose], [--no-verbose] # Display Faker constants methods with examples + + Search Faker method(s) + OUT + + expect(output).to eq(expected_output) + end + + context 'when search query exists' do + it 'returns results' do + output = `fakerbot search name` + expect(output).to match(/Faker::/) + expect(output).to match(/└──/) + end + end + + context 'when search query does not exist' do + it 'returns a not found message' do + output = `fakerbot search asdasdhk` + expect(output).to match(/Sorry, we couldn't find a match/) + end + end +end diff --git a/spec/unit/list_spec.rb b/spec/unit/list_spec.rb index b019e65..363bfd6 100644 --- a/spec/unit/list_spec.rb +++ b/spec/unit/list_spec.rb @@ -1,9 +1,9 @@ require 'fakerbot/commands/list' -RSpec.describe Fakerbot::Commands::List do +RSpec.describe FakerBot::Commands::List do let(:output) { StringIO.new } let(:options) { {} } - let(:command) { Fakerbot::Commands::List.new(options) } + let(:command) { FakerBot::Commands::List.new(options) } before do command.execute(output: output) diff --git a/spec/unit/search_spec.rb b/spec/unit/search_spec.rb new file mode 100644 index 0000000..a86e936 --- /dev/null +++ b/spec/unit/search_spec.rb @@ -0,0 +1,28 @@ +require 'fakerbot/commands/search' + +RSpec.describe FakerBot::Commands::Search do + let(:output) { StringIO.new } + let(:options) { {} } + let(:command) { described_class.new(options) } + + context 'when query object exists' do + before do + command.execute('image', output: output) + end + + it 'returns results' do + expect(output.string).to match(/Faker/) + expect(output.string.lines.size).to be_positive + end + end + + context 'when query object does not exist' do + before do + command.execute('hasjdhaksjd', output: output) + end + + it 'returns nil' do + expect(output.string).to be_empty + end + end +end