Skip to content

Commit

Permalink
supports first half of Slim 0.3 - reusing instances
Browse files Browse the repository at this point in the history
  • Loading branch information
dougbradbury committed Feb 1, 2011
1 parent fe175c8 commit cc7b482
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .gitignore
@@ -1,7 +1,7 @@
.rakeTasks .rakeTasks
RubySlim.iml *.iml
RubySlim.ipr *.ipr
RubySlim.iws *.iws
.DS_Store .DS_Store
.idea .idea


2 changes: 1 addition & 1 deletion lib/ruby_slim.rb
Expand Up @@ -18,7 +18,7 @@ def run(port)
end end


def serve_ruby_slim(socket) def serve_ruby_slim(socket)
socket.puts("Slim -- V0.2"); socket.puts("Slim -- V0.3");
said_bye = false said_bye = false
while !said_bye while !said_bye
length = socket.read(6).to_i length = socket.read(6).to_i
Expand Down
3 changes: 3 additions & 0 deletions lib/slim_helper_library.rb
@@ -0,0 +1,3 @@
class SlimHelperLibrary

end
26 changes: 19 additions & 7 deletions lib/statement_executor.rb
Expand Up @@ -17,10 +17,14 @@ def library?(instance_name)


def create(instance_name, class_name, constructor_arguments) def create(instance_name, class_name, constructor_arguments)
begin begin
instance = construct_instance(replace_symbol(class_name), replace_symbols(constructor_arguments)) instance = replace_symbol(class_name)
if library?(instance_name) if instance.is_a?(String)
@libraries << instance instance = construct_instance(instance, replace_symbols(constructor_arguments))
if library?(instance_name)
@libraries << instance
end
end end

@instances[instance_name] = instance @instances[instance_name] = instance
"OK" "OK"
rescue SlimError => e rescue SlimError => e
Expand Down Expand Up @@ -96,7 +100,8 @@ def to_file_name(module_name)
end end


def send_message_to_instance(instance, method, args) def send_message_to_instance(instance, method, args)
instance.send(method, *replace_tables_with_hashes(replace_symbols(args))) symbols = replace_symbols(args)
instance.send(method, *replace_tables_with_hashes(symbols))
end end


def call(instance_name, method_name, *args) def call(instance_name, method_name, *args)
Expand Down Expand Up @@ -131,11 +136,18 @@ def get_symbol(name)
@symbols[name] @symbols[name]
end end


def acquire_symbol(symbol_text)
symbol = get_symbol(symbol_text[1..-1])
symbol = symbol_text if symbol.nil?
symbol
end

def replace_symbol(item) def replace_symbol(item)
match = item.match(/\A\$\w*\z/)
return acquire_symbol(match[0]) if match

item.gsub(/\$\w*/) do |match| item.gsub(/\$\w*/) do |match|
symbol = get_symbol(match[1..-1]) acquire_symbol(match)
symbol = match if symbol.nil?
symbol
end end
end end


Expand Down
37 changes: 35 additions & 2 deletions lib/test_module/test_slim.rb
@@ -1,3 +1,5 @@
require 'ostruct'

module TestModule module TestModule
class SystemUnderTest class SystemUnderTest
def sut_method def sut_method
Expand All @@ -7,12 +9,15 @@ def sut_method


class TestSlim class TestSlim
attr_reader :sut attr_reader :sut
def initialize attr_accessor :string
def initialize(generation = 0)
@generation = generation
@sut = SystemUnderTest.new @sut = SystemUnderTest.new
@string = "string"
end end


def return_string def return_string
"string" @string
end end


def returnString #Should not ever be called. def returnString #Should not ever be called.
Expand Down Expand Up @@ -47,5 +52,33 @@ def utf8
"Espa\357\277\275ol" "Espa\357\277\275ol"
end end


def create_test_slim_with_string(string)
slim = TestSlim.new(@generation + 1)
slim.string = string
slim
end

def new_with_string(string)
s = TestSlim.new
s.string = string
s
end

def echo_object(method, string)
OpenStruct.new(method.to_sym => string)
end

def call_on(method, object)
object.send(method.to_sym)
end

# def is_same(other)
# self === other
# end
#
# def get_string_from_other other
# other.get_string_arg
# end

end end
end end
11 changes: 0 additions & 11 deletions rubyslim.iml

This file was deleted.

4 changes: 2 additions & 2 deletions spec/instance_creation_spec.rb
Expand Up @@ -29,8 +29,8 @@
end end


it "can't create an instance with the wrong number of arguments" do it "can't create an instance with the wrong number of arguments" do
result = @caller.create("x", "TestModule::TestSlim", ["noSuchArgument"]) result = @caller.create("x", "TestModule::TestSlim", ["1", "noSuchArgument"])
result.should include(Statement::EXCEPTION_TAG + "message:<<COULD_NOT_INVOKE_CONSTRUCTOR TestModule::TestSlim[1]>>") result.should include(Statement::EXCEPTION_TAG + "message:<<COULD_NOT_INVOKE_CONSTRUCTOR TestModule::TestSlim[2]>>")
end end


it "can't create an instance if there is no class" do it "can't create an instance if there is no class" do
Expand Down
13 changes: 13 additions & 0 deletions spec/list_executor_spec.rb
Expand Up @@ -172,4 +172,17 @@ def check_results expectations
check_results "id2"=>"OK" check_results "id2"=>"OK"
end end


it "can use a fixture method that returns a fixture object" do
add_statement "id1", "callAndAssign", "object", "test_slim", "echo_object", "let_me_see", "Boogaloo"
add_statement "id2", "call", "test_slim", "call_on", "let_me_see", "$object"
check_results "id2" => "Boogaloo"
end

it "can use an instance that was stored as a symbol" do
add_statement "id1", "callAndAssign", "test_slim_instance", "test_slim", "create_test_slim_with_string", "Boogaloo"
add_statement "m2", "make", "test_slim", "$test_slim_instance"
check_results "m2" => "OK"

end

end end
14 changes: 14 additions & 0 deletions spec/slim_helper_library_spec.rb
@@ -0,0 +1,14 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
require 'slim_helper_library'

describe SlimHelperLibrary do
before() do
@helper = SlimHelperLibrary.new
end

it "should push a fixture" do
pending

end

end
6 changes: 6 additions & 0 deletions spec/statement_executor_spec.rb
Expand Up @@ -23,4 +23,10 @@
proc = proc {@executor.require_class("MyModule::MyClass")} proc = proc {@executor.require_class("MyModule::MyClass")}
proc.should raise_error(SlimError, /message:<<COULD_NOT_INVOKE_CONSTRUCTOR MyModule::MyClass failed to find in/) proc.should raise_error(SlimError, /message:<<COULD_NOT_INVOKE_CONSTRUCTOR MyModule::MyClass failed to find in/)
end end

it "can handle symbols whose values are objects" do
@executor.set_symbol("foo", OpenStruct.new(:foo => "bar"))
@executor.get_symbol("foo").foo.should == "bar"
@executor.replace_symbol("$foo").foo.should == "bar"
end
end end

0 comments on commit cc7b482

Please sign in to comment.