Skip to content

Commit

Permalink
When hashWidget html tables are passed as arguments, they are convert…
Browse files Browse the repository at this point in the history
…ed to hashes
  • Loading branch information
unclebob committed Nov 23, 2009
1 parent a8d601c commit 6f5650e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 9 deletions.
12 changes: 10 additions & 2 deletions lib/statement_executor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "slim_error"
require "statement"
require "table_to_hash_converter"

class StatementExecutor
def initialize
Expand Down Expand Up @@ -33,10 +34,17 @@ def split_class_name(class_name)
class_name.split(/\:\:/)
end

def replace_tables_with_hashes(constructor_arguments)
args = constructor_arguments.map do |arg|
TableToHashConverter.convert arg
end
return args
end

def construct(class_name, constructor_arguments)
class_object = get_class(class_name)
begin
class_object.new(*constructor_arguments)
class_object.new(*replace_tables_with_hashes(constructor_arguments))
rescue ArgumentError => e
raise SlimError.new("message:<<COULD_NOT_INVOKE_CONSTRUCTOR #{class_name}[#{constructor_arguments.length}]>>")
end
Expand Down Expand Up @@ -83,7 +91,7 @@ def call(instance_name, method_name, *args)
raise SlimError.new("message:<<NO_INSTANCE #{instance_name}>>") if instance.nil?
method = method_name.to_sym
raise SlimError.new("message:<<NO_METHOD_IN_CLASS #{method}[#{args.length}] #{instance.class.name}.>>") if !instance.respond_to?(method)
instance.send(method, *replace_symbols(args))
instance.send(method, *replace_tables_with_hashes(replace_symbols(args)))
rescue SlimError => e
Statement::EXCEPTION_TAG + e.to_s
end
Expand Down
32 changes: 32 additions & 0 deletions lib/table_to_hash_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rexml/document'

class TableToHashConverter
def self.convert(string)
begin
doc = REXML::Document.new(string.strip)
return html_to_hash(doc)
rescue
return string
end
end

def self.html_to_hash(doc)
table = doc.elements['table']
raise ArgumentError if table.nil?
create_hash_from_rows(table)
end

def self.create_hash_from_rows(rows)
hash = {}
rows.elements.each('tr') do |row|
add_row_to_hash(hash, row)
end
hash
end

def self.add_row_to_hash(hash, row)
columns = row.get_elements('td')
raise ArgumentError if columns.size != 2
hash[columns[0].text.strip.to_sym] = columns[1].text.strip
end
end
12 changes: 12 additions & 0 deletions lib/test_module/test_slim_with_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@ def initialize(arg)
def arg
@arg
end

def name
@arg[:name]
end

def addr
@arg[:addr]
end

def set_arg(hash)
@arg = hash
end
end
end
32 changes: 25 additions & 7 deletions spec/list_executor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
before do
@executor = ListExecutor.new
@statements = []
@table = "<table><tr><td>name</td><td>bob</td></tr><tr><td>addr</td><td>here</td></tr></table>"
add_statement "i1", "import", "TestModule"
add_statement "m1", "make", "test_slim", "TestSlim"
end
Expand Down Expand Up @@ -65,33 +66,50 @@ def check_results expectations
it "can call a simple method in ruby form" do
add_statement "id", "call", "test_slim", "return_string"

check_results "m1" => "OK","id"=>"string"
check_results "m1" => "OK", "id"=>"string"
end

it "can call a simple method in ruby form" do
add_statement "id", "call", "test_slim", "utf8"

check_results "m1" => "OK","id"=>"Espa\357\277\275ol"
check_results "m1" => "OK", "id"=>"Espa\357\277\275ol"
end


it "can call a simple method in FitNesse form" do
add_statement "id", "call", "test_slim", "returnString"

check_results "m1"=>"OK","id"=>"string"
check_results "m1"=>"OK", "id"=>"string"
end

it "will allow later imports to take precendence over early imports" do
@statements.insert(0, ["i2", "import", "TestModule.ShouldNotFindTestSlimInHere"])
add_statement "id", "call", "test_slim", "return_string"
check_results "m1"=>"OK","id"=>"string"
check_results "m1"=>"OK", "id"=>"string"
end

it "can pass arguments to constructor" do
add_statement "m2", "make", "test_slim_2", "TestSlimWithArguments", "3"
add_statement "c1", "call", "test_slim_2", "arg"

check_results "m2"=>"OK","c1"=>"3"
check_results "m2"=>"OK", "c1"=>"3"
end

it "can pass tables to constructor" do
add_statement "m2", "make", "test_slim_2", "TestSlimWithArguments", @table
add_statement "c1", "call", "test_slim_2", "name"
add_statement "c2", "call", "test_slim_2", "addr"

check_results "m2"=>"OK", "c1"=>"bob", "c2"=>"here"
end

it "can pass tables to functions" do
add_statement "m2", "make", "test_slim_2", "TestSlimWithArguments", "nil"
add_statement "c0", "call", "test_slim_2", "set_arg", @table
add_statement "c1", "call", "test_slim_2", "name"
add_statement "c2", "call", "test_slim_2", "addr"

check_results "m2"=>"OK", "c1"=>"bob", "c2"=>"here"
end

it "can call a function more than once" do
Expand All @@ -113,7 +131,7 @@ def check_results expectations
add_statement "id3", "call", "test_slim", "echo", "name: $v1 $v2"
check_results "id3" => "name: Bob Martin"
end

it "should ignore '$' if what follows is not a symbol" do
add_statement "id3", "call", "test_slim", "echo", "$v1"
check_results "id3" => "$v1"
Expand All @@ -135,7 +153,7 @@ def check_results expectations
add_statement "id", "call", "test_slim", "null"
check_results "id" => nil
end

it "can survive executing a syntax error" do
add_statement "id", "call", "test_slim", "syntax_error"
results = @executor.execute(@statements)
Expand Down
31 changes: 31 additions & 0 deletions spec/table_to_hash_converter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
require 'table_to_hash_converter'

def shouldNotChange(string)
TableToHashConverter.convert(string).should == string
end

describe TableToHashConverter do
[
["some string", "not a table"],
["<table>blah", "incomplete table"],
["<table><tr><td>hi</td></tr></table>", "too few columns"],
["<table><tr><td>hi</td><td>med</td><td>lo</td></tr></table>", "too many columns"]
].each do |string, reason|
it "#{reason}: should not change '#{string}'" do
shouldNotChange(string)
end
end
end

describe TableToHashConverter do
[
["<table><tr><td>name</td><td>bob</td></tr></table>", {:name=>"bob"}],
[" <table> <tr> <td> name </td> <td> bob </td> </tr> </table> ", {:name=>"bob"}],
["<table><tr><td>name</td><td>bob</td></tr><tr><td>addr</td><td>here</td></tr></table>", {:name=>'bob', :addr=>'here'}]
].each do |table, hash|
it "should match #{table} to #{hash}" do
TableToHashConverter.convert(table).should == hash
end
end
end

0 comments on commit 6f5650e

Please sign in to comment.