Skip to content

Commit

Permalink
add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
accessd committed May 14, 2016
1 parent 239485b commit 24bcb48
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 44 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
Exclude:
- vendor/**/*
- examples/**/vendor/**/*
- bin/**/*

inherit_from: .rubocop_todo.yml
6 changes: 3 additions & 3 deletions examples/weather/weatherbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class WeatherBot < SlackRubyBot::Bot
desc 'This bot tells you the weather.'

command 'clouds' do
desc "Tells you how many clouds there're above you."
desc 'Tells you how many clouds there\'re above you.'
end

command "What's the weather in <city>?" do
command 'What\'s the weather in <city>?' do
desc 'Tells you the weather in a <city>.'
long_desc "Accurate 10 Day Weather Forecasts for thousands of places around the World.\n" \
"We provide detailed Weather Forecasts over a 10 day period updated four times a day."
'We provide detailed Weather Forecasts over a 10 day period updated four times a day.'
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/slack-ruby-bot/commands/help.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SlackRubyBot
module Commands
class Help < Base
class HelpCommand < Base
help do
title 'help'
desc 'Shows help information.'
Expand All @@ -19,7 +19,6 @@ class Help < Base
end

class << self

private

def general_text
Expand All @@ -36,7 +35,6 @@ def general_text
For more information see https://github.com/dblock/slack-ruby-bot, please.
TEXT
end

end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/slack-ruby-bot/commands/help/attrs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module SlackRubyBot
module Commands
module Help
class Attrs
attr_accessor :command_name, :command_desc, :command_long_desc
attr_reader :class_name, :commands

def initialize(class_name)
@class_name = class_name
@commands = []
end

def title(title)
self.command_name = title
end

def desc(desc)
self.command_desc = desc
end

def long_desc(long_desc)
self.command_long_desc = long_desc
end

def command(title, &block)
@commands << self.class.new(class_name).tap do |k|
k.title(title)
k.instance_eval(&block)
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions lib/slack-ruby-bot/rspec/support/bots_for_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Testing
class WeatherBot < SlackRubyBot::Bot
help do
title 'Weather Bot'
desc 'This bot tells you the weather.'

command 'clouds' do
desc 'Tells you how many clouds there\'re above you.'
end

command '' do
desc 'empty description'
end

command 'command_without_description' do
end

command 'What\'s the weather in <city>?' do
desc 'Tells you the weather in a <city>.'
long_desc "Accurate 10 Day Weather Forecasts for thousands of places around the World.\n" \
'We provide detailed Weather Forecasts over a 10 day period updated four times a day.'
end
end
end
end

module Testing
class HelloCommand < SlackRubyBot::Commands::Base
help do
title 'hello'
desc 'Says hello.'
long_desc 'The long description'
end
end
end
46 changes: 10 additions & 36 deletions lib/slack-ruby-bot/support/commands_helper.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
require 'singleton'
require_relative '../commands/help/attrs'

module SlackRubyBot
class CommandHelpAttrs
attr_accessor :command_name, :command_desc, :command_long_desc
attr_reader :class_name, :commands

def initialize(class_name)
@class_name = class_name
@commands = []
end

def title(title)
self.command_name = title
end

def desc(desc)
self.command_desc = desc
end

def long_desc(long_desc)
self.command_long_desc = long_desc
end

def command(title, &block)
@commands << self.class.new(class_name).tap { |k| k.title(title); k.instance_eval &block }
end
end

class CommandsHelper
include Singleton
attr_reader :commands_help_attrs
Expand All @@ -36,7 +11,7 @@ def initialize
end

def capture_help(class_name, &block)
k = CommandHelpAttrs.new(class_name)
k = Commands::Help::Attrs.new(class_name)
k.instance_eval(&block)
@commands_help_attrs << k
end
Expand All @@ -53,7 +28,7 @@ def other_commands_descs
end

def command_full_desc(name)
unescaped_name = CGI.unescapeHTML(name)
unescaped_name = Slack::Messages::Formatting.unescape(name)
help_attrs = find_command_help_attrs(unescaped_name)
return "There's no command *#{unescaped_name}*" unless help_attrs
return "There's no description for command *#{unescaped_name}*" if help_attrs.command_long_desc.blank?
Expand All @@ -66,24 +41,24 @@ def find_command_help_attrs(name)
help_attrs = commands_help_attrs.find { |k| k.command_name == name }
return help_attrs if help_attrs
commands_help_attrs.each { |k| k.commands.each { |c| return c if c.command_name == name } }
return nil
nil
end

def collect_help_attrs(help_attrs)
help_attrs_with_present_names(help_attrs).map do |help_attrs|
yield(help_attrs)
help_attrs_with_present_names(help_attrs).map do |ha|
yield(ha)
end
end

def collect_name_and_desc(help_attrs)
collect_help_attrs(help_attrs) do |help_attrs|
command_name_and_desc(help_attrs)
collect_help_attrs(help_attrs) do |ha|
command_name_and_desc(ha)
end
end

def command_name_and_desc(help_attrs)
desc = help_attrs.command_desc.present? ? "- #{help_attrs.command_desc}" : ''
"*#{help_attrs.command_name}* #{desc}"
desc = help_attrs.command_desc.present? ? " - #{help_attrs.command_desc}" : ''
"*#{help_attrs.command_name}*#{desc}"
end

def help_attrs_with_present_names(help_attrs)
Expand All @@ -97,6 +72,5 @@ def bot_help_attrs
def other_commands_help_attrs
commands_help_attrs.select { |k| k.class_name.constantize.superclass == SlackRubyBot::Commands::Base }
end

end
end
20 changes: 19 additions & 1 deletion spec/slack-ruby-bot/commands/help_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ def app
SlackRubyBot::App.new
end
it 'help' do
expect(message: "#{SlackRubyBot.config.user} help").to respond_with_slack_message('See https://github.com/dblock/slack-ruby-bot, please.')
message = <<MSG
*Weather Bot* - This bot tells you the weather.
*Commands:*
*clouds* - Tells you how many clouds there're above you.
*command_without_description*
*What's the weather in <city>?* - Tells you the weather in a <city>.
*Other commands:*
*help* - Shows help information.
*hi* - Says hello.
*hello* - Says hello.
For getting description of the command use: *help <command>*
For more information see https://github.com/dblock/slack-ruby-bot, please.
MSG

expect(message: "#{SlackRubyBot.config.user} help").to respond_with_slack_message(message)
end
end
2 changes: 1 addition & 1 deletion spec/slack-ruby-bot/hooks/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
it 'returns only built in command classes' do
expect(built_in_command_classes).to include SlackRubyBot::Commands::Hi
expect(built_in_command_classes).to include SlackRubyBot::Commands::Default
expect(built_in_command_classes).to include SlackRubyBot::Commands::Help
expect(built_in_command_classes).to include SlackRubyBot::Commands::HelpCommand
expect(built_in_command_classes).to_not include SlackRubyBot::Bot
end
it 'does not return unknown command class' do
Expand Down
121 changes: 121 additions & 0 deletions spec/slack-ruby-bot/support/commands_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
require 'spec_helper'

describe SlackRubyBot::CommandsHelper do
let(:bot_class_name) { 'Testing::WeatherBot' }
let(:command_class_name) { 'Testing::HelloCommand' }
let(:commands_helper) { described_class.instance }

describe '#capture_help' do
let(:help_attrs) { commands_helper.commands_help_attrs }
let(:bot_help_attrs) { help_attrs.find { |k| k.class_name == bot_class_name } }
let(:command_help_attrs) { help_attrs.find { |k| k.class_name == command_class_name } }

it 'should save bot class name' do
expect(bot_help_attrs).to be
end

describe 'captures help attributes correctly' do
context 'for command' do
it 'command name' do
expect(command_help_attrs.command_name).to eq('hello')
end

it 'command description' do
expect(command_help_attrs.command_desc).to eq('Says hello.')
end

it 'command long description' do
expect(command_help_attrs.command_long_desc).to eq('The long description')
end
end

context 'for bot' do
it 'name' do
expect(bot_help_attrs.command_name).to eq('Weather Bot')
end

it 'description' do
expect(bot_help_attrs.command_desc).to eq('This bot tells you the weather.')
end

describe 'commands' do
let(:clouds_command) { bot_help_attrs.commands.find { |k| k.command_name == 'clouds' } }
let(:weather_command) { bot_help_attrs.commands.find { |k| k.command_name == "What's the weather in <city>?" } }

it 'command name' do
expect(clouds_command).to be
expect(weather_command).to be
end

it 'command description' do
expect(clouds_command.command_desc).to eq("Tells you how many clouds there're above you.")
expect(weather_command.command_desc).to eq('Tells you the weather in a <city>.')
end

it 'command long description' do
expect(weather_command.command_long_desc).to eq("Accurate 10 Day Weather Forecasts for thousands of places around the World.\n" \
'We provide detailed Weather Forecasts over a 10 day period updated four times a day.')
end
end
end
end
end

describe '#bot_desc_and_commands' do
let(:bot_desc) { commands_helper.bot_desc_and_commands.first }

it 'returns bot name and description' do
expect(bot_desc).to include('*Weather Bot* - This bot tells you the weather.')
end

it 'returns possible commands for bot' do
expect(bot_desc).to include("\n\n*Commands:*\n")
expect(bot_desc).to include("*clouds* - Tells you how many clouds there're above you.\n")
expect(bot_desc).to include("*What's the weather in <city>?* - Tells you the weather in a <city>.")
end

it 'do not return command with empty name' do
expect(bot_desc).to_not include('**')
end

it 'do not show description for command without description' do
expect(bot_desc).to include("*command_without_description*\n")
end
end

describe '#other_commands_descs' do
let(:commands_desc) { commands_helper.other_commands_descs }
let(:hello_command_desc) { commands_desc.find { |desc| desc =~ /\*hello\*/ } }

it 'returns command name and description' do
expect(hello_command_desc).to eq('*hello* - Says hello.')
end
end

describe '#command_full_desc' do
context 'for bot commands' do
it 'returns long description' do
full_desc = commands_helper.command_full_desc("What's the weather in &lt;city&gt;?")
expect(full_desc).to include "Accurate 10 Day Weather Forecasts for thousands of places around the World.\n" \
'We provide detailed Weather Forecasts over a 10 day period updated four times a day.'
end
end

context 'for other commands' do
it 'returns long description' do
full_desc = commands_helper.command_full_desc('hello')
expect(full_desc).to include 'The long description'
end
end

it 'returns correct message if there is no such command' do
full_desc = commands_helper.command_full_desc('deploy')
expect(full_desc).to eq "There's no command *deploy*"
end

it 'returns correct message if there is no long description for the command' do
full_desc = commands_helper.command_full_desc('clouds')
expect(full_desc).to eq "There's no description for command *clouds*"
end
end
end

0 comments on commit 24bcb48

Please sign in to comment.