WPSTools plugins mode activated

This commit is contained in:
erwanlr
2013-01-17 13:08:01 +01:00
parent 1d7923c7b7
commit d9fd20c6fe
17 changed files with 749 additions and 251 deletions

View File

@@ -0,0 +1,153 @@
require "spec_helper"
describe CustomOptionParser do
let(:parser) { CustomOptionParser.new }
describe "#new" do
end
describe "::option_to_symbol" do
after :each do
if @exception
expect { CustomOptionParser::option_to_symbol(@option) }.to raise_error(@exception)
else
CustomOptionParser::option_to_symbol(@option).should === @expected
end
end
context "without REQUIRED or OPTIONAL arguments" do
context "with short option" do
it "should return :test" do
@option = ["-t", "--test", "Testing"]
@expected = :test
end
it "should :its_a_long_option" do
@option = ["-l", "--its-a-long-option", "Testing '-' replacement"]
@expected = :its_a_long_option
end
end
context "without short option" do
it "should return :long" do
@option = ["--long", "The method should find the option name ('long')"]
@expected = :long
end
it "should return :long_option" do
@option = ["--long-option", "No short !"]
@expected = :long_option
end
end
context "without long option" do
it "should raise an arror" do
@option = ["-v", "The long option is missing there"]
@exception = "Could not find the option name for [\"-v\", \"The long option is missing there\"]"
end
it "should raise an error" do
@option = ["The long option is missing there"]
@exception = "Could not find the option name for [\"The long option is missing there\"]"
end
end
context "with multiple long option names (like alias)" do
it "should return :check_long and not :cl" do
@option = ["--check-long", "--cl"]
@expected = :check_long
end
end
end
context "with REQUIRED or OPTIONAL arguments" do
it "should removed the OPTIONAL argument" do
@option = ["-p", "--page [PAGE_NUMBER]"]
@expected = :page
end
it "should removed the REQUIRED argument" do
@option = ["--url TARGET_URL"]
@expected = :url
end
end
end
describe "#add_option" do
context "exception throwing if" do
after :each do
expect { parser.add_option(@option) }.to raise_error(@exception)
end
it "argument passed is not an Array" do
@option = "a simple String"
@exception = "The option must be an array, String supplied : 'a simple String'"
end
it "option name is already used" do
@option = ["-v", "--verbose", "Verbose mode"]
parser.add_option(@option)
@exception = "The option verbose is already used !"
end
end
it "should have had 2 symbols (:verbose, :url) to @symbols_used" do
parser.add_option(["-v", "--verbose"])
parser.add_option(["--url TARGET_URL"])
parser.symbols_used.sort.should === [:url, :verbose]
end
context "parsing" do
before :each do
parser.add_option(["-u", "--url TARGET_URL", "Set the target url"])
end
it "should raise an error if an unknown option is supplied" do
expect { parser.parse!(["--verbose"]) }.to raise_error(OptionParser::InvalidOption)
end
it "should raise an error if an option require an argument which is not supplied" do
expect { parser.parse!(["--url"]) }.to raise_error(OptionParser::MissingArgument)
end
it "should retrieve the correct argument" do
parser.parse!(["-u", "iam_the_target"])
parser.results.should === { :url => "iam_the_target" }
end
end
end
describe "#add" do
it "should raise an error if the argument is not an Array or Array(Array)" do
expect { parser.add("Hello") }.to raise_error("Options must be at least an Array, or an Array(Array). String supplied")
end
before :each do
parser.add(["-u", "--url TARGET_URL"])
end
context "single option" do
it "should add the :url option, and retrieve the correct argument" do
parser.symbols_used.should === [ :url ]
parser.results(["-u", "target.com"]).should === { :url => "target.com" }
end
end
context "multiple options" do
it "should add 2 options, and retrieve the correct arguments" do
parser.add([
["-v", "--verbose"],
["--test [TEST_NUMBER]"]
])
parser.symbols_used.sort.should === [:test, :url, :verbose]
parser.results(["-u", "wp.com", "-v", "--test"]).should === { :test => nil, :url => "wp.com", :verbose => true }
end
end
end
end

View File

@@ -0,0 +1,46 @@
require 'spec_helper'
describe Plugin do
subject(:plugin) { Plugin.new }
describe "#new" do
context "with some infos" do
subject(:plugin) { Plugin.new(infos) }
let(:infos) { {:author => "John"} }
its(:author) { should === infos[:author] }
end
end
describe "#run" do
it "should raise a NotImplementedError" do
expect { plugin.run }.to raise_error(NotImplementedError)
end
end
describe "#register_options" do
after :each do
if @exception
expect { plugin.register_options(*@options) }.to raise_error(@exception)
else
plugin.register_options(*@options)
plugin.registered_options.sort.should === @expected.sort
end
end
context "when an option is not an Array" do
it "should raise an error" do
@options = [["-v", "--verbose", "It's a valid option"], "Not a valid one"]
@exception = "Each option must be an array, String supplied"
end
end
context "when options are Arrays" do
it "should register the options" do
@options = [["-v", "--verbose", "Verbose mode"], ["-u", "--url TARGET_URL"]]
@expected = *@options
end
end
end
end

View File

@@ -0,0 +1,95 @@
require "spec_helper"
class TestPlugin < Plugin
def initialize
register_options(["-u", "--url"])
end
end
class AnotherPlugin < Plugin
def initialize
super(:author => "John")
# No Options
end
end
describe Plugins do
subject(:plugins) { Plugins.new }
let(:test_plugin) { TestPlugin.new }
let(:another_plugin) { AnotherPlugin.new }
describe "#new" do
context "without argument" do
its(:option_parser) { should be_a CustomOptionParser }
it "should be an Array" do
plugins.should be_an Array
end
end
context "with an option_parser argument" do
subject(:plugin) { Plugins.new(CustomOptionParser.new("the banner")) }
its(:option_parser) { should be_a CustomOptionParser }
its("option_parser.banner") { should === "the banner" }
it "should raise an eror if the parser is not an instance of CustomOptionParser" do
expect { Plugins.new(OptionParser.new) }.to raise_error("The parser must be an instance of CustomOptionParser, OptionParser supplied")
end
end
end
describe "#register_plugin" do
after :each do
if @exception
expect { plugins.register_plugin(@plugin) }.to raise_error(@exception)
else
plugins.register_plugin(@plugin)
plugins.should include(@plugin)
plugins.should === @expected
end
end
context "when the argument supplied is not an instance of Plugin" do
it "should raise an error" do
@plugin = "I'am a String"
@exception = "The argument must be an instance of Plugin, String supplied"
end
end
it "should register the plugin" do
@plugin = TestPlugin.new
@expected = [@plugin]
end
it "should register 2 plugins (the order is important)" do
plugins.register_plugin(test_plugin)
@plugin = another_plugin
@expected = [test_plugin, @plugin]
end
end
describe "#register" do
after :each do
plugins.register(*@plugins_to_register)
@plugins_to_register.each do |plugin|
plugins.should include(plugin)
end
# For the correct order
plugins.should === @plugins_to_register
end
it "should register 1 plugin" do
@plugins_to_register = [test_plugin]
end
it "should register 2 plugins" do
@plugins_to_register = [another_plugin, test_plugin]
end
end
end

View File

@@ -0,0 +1,3 @@
require File.expand_path(File.dirname(__FILE__) + "/../../wpstools_helper")
# TODO

View File

@@ -0,0 +1,3 @@
require File.expand_path(File.dirname(__FILE__) + "/../../wpstools_helper")
# TODO