Created
December 5, 2010 23:40
-
-
Save fixermark/729607 to your computer and use it in GitHub Desktop.
Role builder test case: is this a hack?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#role_builder.rb | |
require 'json' | |
require 'sinatra/base' | |
module Sinatra | |
module RoleBuilder | |
class BadParameterException < Exception | |
end | |
def self.registered(app) | |
app.set :roles, [{"description"=>"TODO: Fill this in", "commands"=>[] }] | |
end | |
# Adds a command to the role, and registers the | |
# params are | |
# -name: Name of the command | |
# -description: COmmand's description (optional) | |
# -arugments: Arguments to the command, which is a list of tuples (name, | |
# description). Description is optional. | |
# -return: Description of the return value, if any. Optional. | |
# -usage: HTTP usage description. A triple of | |
# -- method ("get", "post", etc.) | |
# -- path ("/object/${my_object}") | |
# -- data ("${my_data}") | |
def add_command(params) | |
name = params[:name] || | |
(raise BadParameterException, "Name parameter is required.") | |
description = params[:description] | |
arguments = params[:arguments] || [] | |
return_info = params[:return] | |
usage = params[:usage] || | |
(raise BadParameterException, "Must specify usage for #{name}") | |
new_command = {"name" => (name.downcase)} | |
if description | |
new_command["description"] = description | |
end | |
new_command["arguments"]=[] | |
arguments.each do |arg| | |
argument = {"name" => arg[0].downcase} | |
if arg.length > 1 | |
argument["description"] = arg[1] | |
end | |
new_command["arguments"] << argument | |
end | |
if return_info | |
new_command["return"] = {"description" => return_info} | |
end | |
new_command["usage"] = { | |
"method" => usage[0], | |
"path" => usage[1], | |
"data" => usage[2] | |
} | |
roles[0]["commands"] << new_command | |
end | |
def get_roles() | |
JSON.dump roles | |
end | |
end | |
register RoleBuilder | |
end | |
#tc_role_builder.rb | |
require 'rubygems' | |
require 'role_builder' | |
require 'test/unit' | |
require 'rack/test' | |
require 'json' | |
ENV['RACK_ENV'] = 'test' | |
class TestRoleBuilder < Test::Unit::TestCase | |
def app | |
Sinatra::Application | |
end | |
def setup | |
app.set :roles, [{"description"=>"","commands"=>[]}] | |
@default_usage = [ | |
"get", | |
"/path", | |
"data"] | |
end | |
def test_add_command_fails_on_badparams | |
assert_raise Sinatra::RoleBuilder::BadParameterException do | |
app.add_command :usage => "Test" | |
end | |
assert_raise Sinatra::RoleBuilder::BadParameterException do | |
app.add_command :name => "Test" | |
end | |
end | |
def test_add_command_adds_to_roles | |
app.add_command( | |
:name => "test", | |
:description => "My test data.", | |
:arguments => [["arg1","An argument"],["arg2"]], | |
:return => "Test return.", | |
:usage => ["get", | |
"/path/to/test", | |
"my_data"]) | |
result = JSON.parse(app.get_roles) | |
command = result[0]["commands"][0] | |
assert_equal command["name"], "test" | |
assert_equal command["description"], "My test data." | |
assert_equal command["arguments"][0]["name"], "arg1" | |
assert_equal command["arguments"][0]["description"], "An argument" | |
assert_equal command["arguments"][1]["name"], "arg2" | |
assert_equal command["return"]["description"], "Test return." | |
assert_equal command["usage"]["method"], "get" | |
assert_equal command["usage"]["path"], "/path/to/test" | |
assert_equal command["usage"]["data"], "my_data" | |
end | |
def test_identifier_case_insensitivity | |
app.add_command( | |
:name => "My command", | |
:arguments => [["Cap"]], | |
:usage => ["get", | |
"/path", | |
"data"]) | |
result = JSON.parse(app.get_roles) | |
assert_equal "my command", result[0]["commands"][0]["name"] | |
assert_equal "cap", result[0]["commands"][0]["arguments"][0]["name"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment