rubyslim-unofficial 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +2 -0
  3. data/README.md +187 -0
  4. data/Rakefile +21 -0
  5. data/bin/rubyslim +10 -0
  6. data/lib/rubyslim/list_deserializer.rb +69 -0
  7. data/lib/rubyslim/list_executor.rb +12 -0
  8. data/lib/rubyslim/list_serializer.rb +45 -0
  9. data/lib/rubyslim/ruby_slim.rb +61 -0
  10. data/lib/rubyslim/slim_error.rb +3 -0
  11. data/lib/rubyslim/slim_helper_library.rb +23 -0
  12. data/lib/rubyslim/socket_service.rb +53 -0
  13. data/lib/rubyslim/statement.rb +79 -0
  14. data/lib/rubyslim/statement_executor.rb +174 -0
  15. data/lib/rubyslim/table_to_hash_converter.rb +32 -0
  16. data/lib/test_module/library_new.rb +13 -0
  17. data/lib/test_module/library_old.rb +12 -0
  18. data/lib/test_module/should_not_find_test_slim_in_here/test_slim.rb +7 -0
  19. data/lib/test_module/simple_script.rb +9 -0
  20. data/lib/test_module/test_chain.rb +5 -0
  21. data/lib/test_module/test_slim.rb +86 -0
  22. data/lib/test_module/test_slim_with_arguments.rb +23 -0
  23. data/lib/test_module/test_slim_with_no_sut.rb +5 -0
  24. data/rubyslim-unofficial.gemspec +25 -0
  25. data/spec/instance_creation_spec.rb +40 -0
  26. data/spec/it8f_spec.rb +8 -0
  27. data/spec/list_deserializer_spec.rb +66 -0
  28. data/spec/list_executor_spec.rb +187 -0
  29. data/spec/list_serialzer_spec.rb +38 -0
  30. data/spec/method_invocation_spec.rb +127 -0
  31. data/spec/slim_helper_library_spec.rb +80 -0
  32. data/spec/socket_service_spec.rb +98 -0
  33. data/spec/spec_helper.rb +6 -0
  34. data/spec/statement_executor_spec.rb +50 -0
  35. data/spec/statement_spec.rb +17 -0
  36. data/spec/table_to_hash_converter_spec.rb +31 -0
  37. metadata +132 -0
@@ -0,0 +1,187 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require "statement_executor"
3
+ require "list_executor"
4
+
5
+ describe ListExecutor do
6
+ before do
7
+ @executor = ListExecutor.new
8
+ @statements = []
9
+ @table = "<table><tr><td>name</td><td>bob</td></tr><tr><td>addr</td><td>here</td></tr></table>"
10
+ add_statement "i1", "import", "TestModule"
11
+ add_statement "m1", "make", "test_slim", "TestSlim"
12
+ end
13
+
14
+ def get_result(id, result_list)
15
+ pairs_to_map(result_list)[id]
16
+ end
17
+
18
+ def pairs_to_map(pairs)
19
+ map = {}
20
+ pairs.each {|pair| map[pair[0]] = pair[1]}
21
+ map
22
+ end
23
+
24
+ def add_statement(*args)
25
+ @statements << args
26
+ end
27
+
28
+ def check_results expectations
29
+ results = @executor.execute(@statements)
30
+ expectations.each_pair {|id, expected|
31
+ get_result(id, results).should == expected
32
+ }
33
+ end
34
+
35
+ it "can respond with OK to import" do
36
+ check_results "i1"=>"OK"
37
+ end
38
+
39
+ it "can't execute an invalid operation" do
40
+ add_statement "inv1", "invalidOperation"
41
+ results = @executor.execute(@statements)
42
+ get_result("inv1", results).should include(Statement::EXCEPTION_TAG+"message:<<INVALID_STATEMENT: [\"inv1\", \"invalidOperation\"].")
43
+ end
44
+
45
+ it "can't execute a malformed instruction" do
46
+ add_statement "id", "call", "notEnoughArguments"
47
+ message = "message:<<MALFORMED_INSTRUCTION [\"id\", \"call\", \"notEnoughArguments\"].>>"
48
+ results = @executor.execute(@statements)
49
+ get_result("id", results).should include(Statement::EXCEPTION_TAG+message)
50
+ end
51
+
52
+ it "can't call a method on an instance that doesn't exist" do
53
+ add_statement "id", "call", "no_such_instance", "no_such_method"
54
+ results = @executor.execute(@statements)
55
+ get_result("id", results).should include(Statement::EXCEPTION_TAG+"message:<<NO_INSTANCE no_such_instance>>")
56
+ end
57
+
58
+ it "should respond to an empty set of instructions with an empty set of results" do
59
+ @executor.execute([]).length.should == 0
60
+ end
61
+
62
+ it "can make an instance given a fully qualified name in dot format" do
63
+ @executor.execute([["m1", "make", "instance", "testModule.TestSlim"]]).should == [["m1", "OK"]]
64
+ end
65
+
66
+ it "can call a simple method in ruby form" do
67
+ add_statement "id", "call", "test_slim", "return_string"
68
+
69
+ check_results "m1" => "OK", "id"=>"string"
70
+ end
71
+
72
+ it "can call a simple method in ruby form" do
73
+ add_statement "id", "call", "test_slim", "utf8"
74
+
75
+ check_results "m1" => "OK", "id"=>"Espa\357\277\275ol"
76
+ end
77
+
78
+
79
+ it "can call a simple method in FitNesse form" do
80
+ add_statement "id", "call", "test_slim", "returnString"
81
+
82
+ check_results "m1"=>"OK", "id"=>"string"
83
+ end
84
+
85
+ it "will allow later imports to take precendence over early imports" do
86
+ @statements.insert(0, ["i2", "import", "TestModule.ShouldNotFindTestSlimInHere"])
87
+ add_statement "id", "call", "test_slim", "return_string"
88
+ check_results "m1"=>"OK", "id"=>"string"
89
+ end
90
+
91
+ it "can pass arguments to constructor" do
92
+ add_statement "m2", "make", "test_slim_2", "TestSlimWithArguments", "3"
93
+ add_statement "c1", "call", "test_slim_2", "arg"
94
+
95
+ check_results "m2"=>"OK", "c1"=>"3"
96
+ end
97
+
98
+ it "can pass tables to constructor" do
99
+ add_statement "m2", "make", "test_slim_2", "TestSlimWithArguments", @table
100
+ add_statement "c1", "call", "test_slim_2", "name"
101
+ add_statement "c2", "call", "test_slim_2", "addr"
102
+
103
+ check_results "m2"=>"OK", "c1"=>"bob", "c2"=>"here"
104
+ end
105
+
106
+ it "can pass tables to functions" do
107
+ add_statement "m2", "make", "test_slim_2", "TestSlimWithArguments", "nil"
108
+ add_statement "c0", "call", "test_slim_2", "set_arg", @table
109
+ add_statement "c1", "call", "test_slim_2", "name"
110
+ add_statement "c2", "call", "test_slim_2", "addr"
111
+
112
+ check_results "m2"=>"OK", "c1"=>"bob", "c2"=>"here"
113
+ end
114
+
115
+ it "can call a function more than once" do
116
+ add_statement "c1", "call", "test_slim", "add", "x", "y"
117
+ add_statement "c2", "call", "test_slim", "add", "a", "b"
118
+
119
+ check_results "c1" => "xy", "c2" => "ab"
120
+ end
121
+
122
+ it "can assign the return value to a symbol" do
123
+ add_statement "id1", "callAndAssign", "v", "test_slim", "add", "x", "y"
124
+ add_statement "id2", "call", "test_slim", "echo", "$v"
125
+ check_results "id1" => "xy", "id2" => "xy"
126
+ end
127
+
128
+ it "can replace multiple symbols in a single argument" do
129
+ add_statement "id1", "callAndAssign", "v1", "test_slim", "echo", "Bob"
130
+ add_statement "id2", "callAndAssign", "v2", "test_slim", "echo", "Martin"
131
+ add_statement "id3", "call", "test_slim", "echo", "name: $v1 $v2"
132
+ check_results "id3" => "name: Bob Martin"
133
+ end
134
+
135
+ it "should ignore '$' if what follows is not a symbol" do
136
+ add_statement "id3", "call", "test_slim", "echo", "$v1"
137
+ check_results "id3" => "$v1"
138
+ end
139
+
140
+ it "can pass and return a list" do
141
+ l = ["1", "2"]
142
+ add_statement "id", "call", "test_slim", "echo", l
143
+ check_results "id"=> l
144
+ end
145
+
146
+ it "can pass a symbol in a list" do
147
+ add_statement "id1", "callAndAssign", "v", "test_slim", "echo", "x"
148
+ add_statement "id2", "call", "test_slim", "echo", ["$v"]
149
+ check_results "id2" => ["x"]
150
+ end
151
+
152
+ it "can return null" do
153
+ add_statement "id", "call", "test_slim", "null"
154
+ check_results "id" => nil
155
+ end
156
+
157
+ it "can survive executing a syntax error" do
158
+ add_statement "id", "call", "test_slim", "syntax_error"
159
+ results = @executor.execute(@statements)
160
+ get_result("id", results).should include(Statement::EXCEPTION_TAG)
161
+ end
162
+
163
+ it "can make a fixture from the name in a symbol" do
164
+ add_statement "id1", "callAndAssign", "test_system", "test_slim", "echo", "TestChain"
165
+ add_statement "id2", "make", "fixture_instance1", "$test_system"
166
+ check_results "id2"=>"OK"
167
+ end
168
+
169
+ it "can make a fixture from a concatonated symbol" do
170
+ add_statement "id1", "callAndAssign", "test_system", "test_slim", "echo", "Chain"
171
+ add_statement "id2", "make", "fixture_instance1", "Test$test_system"
172
+ check_results "id2"=>"OK"
173
+ end
174
+
175
+ it "can use a fixture method that returns a fixture object" do
176
+ add_statement "id1", "callAndAssign", "object", "test_slim", "echo_object", "let_me_see", "Boogaloo"
177
+ add_statement "id2", "call", "test_slim", "call_on", "let_me_see", "$object"
178
+ check_results "id2" => "Boogaloo"
179
+ end
180
+
181
+ it "can use an instance that was stored as a symbol" do
182
+ add_statement "id1", "callAndAssign", "test_slim_instance", "test_slim", "create_test_slim_with_string", "Boogaloo"
183
+ add_statement "m2", "make", "test_slim", "$test_slim_instance"
184
+ check_results "m2" => "OK"
185
+ end
186
+
187
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require "list_serializer"
3
+
4
+ describe ListSerializer do
5
+ it "can serialize and empty list" do
6
+ ListSerializer.serialize([]).should == "[000000:]"
7
+ end
8
+
9
+ it "can serialize a one item list" do
10
+ ListSerializer.serialize(["hello"]).should == "[000001:000005:hello:]"
11
+ end
12
+
13
+ it "can serialize a two item list" do
14
+ ListSerializer.serialize(["hello", "world"]).should == "[000002:000005:hello:000005:world:]"
15
+ end
16
+
17
+ it "can serialize a nested list" do
18
+ ListSerializer.serialize([["element"]]).should == "[000001:000024:[000001:000007:element:]:]"
19
+ end
20
+
21
+ it "can serialize a list with a non-string" do
22
+ ListSerializer.serialize([1]).should == "[000001:000001:1:]"
23
+ end
24
+
25
+ it "can serialize a null element" do
26
+ ListSerializer.serialize([nil]).should == "[000001:000004:null:]"
27
+ end
28
+
29
+ it "can serialize a string with multibyte chars" do
30
+ ListSerializer.serialize(["Köln"]).should == "[000001:000004:Köln:]"
31
+ end
32
+
33
+ it "can serialize a string with UTF8" do
34
+ ListSerializer.serialize(["Español"]).should == "[000001:000007:Espa\xc3\xb1ol:]"
35
+ end
36
+
37
+
38
+ end
@@ -0,0 +1,127 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require "statement_executor"
3
+
4
+ describe StatementExecutor do
5
+ before do
6
+ @executor = StatementExecutor.new
7
+ end
8
+ context "Simple Method Invocations" do
9
+ before do
10
+ @executor.create("test_slim", "TestModule::TestSlim", [])
11
+ @test_slim = @executor.instance("test_slim")
12
+ end
13
+
14
+ it "can call a method with no arguments" do
15
+ @test_slim.should_receive(:no_args).with()
16
+ @executor.call("test_slim", "no_args")
17
+ end
18
+
19
+ it "can't call a method that doesn't exist" do
20
+ result = @executor.call("test_slim", "no_such_method")
21
+ result.should include(Statement::EXCEPTION_TAG + "message:<<NO_METHOD_IN_CLASS no_such_method[0] TestModule::TestSlim.>>")
22
+ end
23
+
24
+ it "can call a method that returns a value" do
25
+ @test_slim.should_receive(:return_value).and_return("arg")
26
+ @executor.call("test_slim", "return_value").should == "arg"
27
+ end
28
+
29
+ it "can call a method that returns a value" do
30
+ @test_slim.should_receive(:return_value).and_return("Español")
31
+ val = @executor.call("test_slim", "return_value")
32
+ val.should == "Español"
33
+ val.jlength.should == 7
34
+ end
35
+
36
+
37
+ it "can call a method that takes an argument" do
38
+ @test_slim.should_receive(:one_arg).with("arg")
39
+ @executor.call("test_slim", "one_arg", "arg")
40
+ end
41
+
42
+ it "can't call a method on an instance that doesn't exist" do
43
+ result = @executor.call("no_such_instance", "no_such_method")
44
+ result.should include(Statement::EXCEPTION_TAG + "message:<<NO_INSTANCE no_such_instance>>")
45
+ end
46
+
47
+ it "can replace symbol expressions with their values" do
48
+ @executor.set_symbol("v", "bob")
49
+ @executor.call("test_slim", "echo", "hi $v.").should == "hi bob."
50
+ end
51
+
52
+ it "can call a method on the @sut" do
53
+ @test_slim.sut.should_receive(:sut_method).with()
54
+ @executor.call("test_slim", "sut_method")
55
+ end
56
+
57
+ it "should call attributes on sut" do
58
+ @executor.call("test_slim", "set_attribute", "a")
59
+ @executor.call("test_slim", "attribute").should == "a"
60
+ end
61
+ end
62
+
63
+ context "Method invocations using fixture with no sut" do
64
+ before do
65
+ @executor.create("test_slim", "TestModule::TestSlimWithNoSut", []);
66
+ @test_slim = @executor.instance("test_slim")
67
+ end
68
+
69
+ it "can't call method that doesn't exist if no 'sut' exists" do
70
+ result = @executor.call("test_slim", "no_such_method")
71
+ result.should include(Statement::EXCEPTION_TAG + "message:<<NO_METHOD_IN_CLASS no_such_method[0] TestModule::TestSlimWithNoSut.>>")
72
+ end
73
+ end
74
+
75
+ context "Method invocations when library instances have been created." do
76
+ before do
77
+ @executor.create("library_old", "TestModule::LibraryOld", [])
78
+ @executor.create("library_new", "TestModule::LibraryNew", [])
79
+ @library_old = @executor.instance("library_old")
80
+ @library_new = @executor.instance("library_new")
81
+ @executor.create("test_slim", "TestModule::TestSlim", [])
82
+ @test_slim = @executor.instance("test_slim")
83
+ end
84
+
85
+ it "should throw normal exception if no such method is found." do
86
+ result = @executor.call("test_slim", "no_such_method")
87
+ result.should include(Statement::EXCEPTION_TAG + "message:<<NO_METHOD_IN_CLASS no_such_method[0] TestModule::TestSlim.>>")
88
+ end
89
+
90
+ it "should still call normal methods in fixture" do
91
+ @test_slim.should_receive(:no_args).with()
92
+ @executor.call("test_slim", "no_args")
93
+ end
94
+
95
+ it "should still call methods on the sut" do
96
+ @test_slim.sut.should_receive(:sut_method).with()
97
+ @executor.call("test_slim", "sut_method")
98
+ end
99
+
100
+ it "should call a specific method on library_old" do
101
+ @library_old.should_receive(:method_on_library_old).with()
102
+ @executor.call("test_slim", "method_on_library_old")
103
+ end
104
+
105
+ it "should call a specific method on library_new" do
106
+ @library_new.should_receive(:method_on_library_new).with()
107
+ @executor.call("test_slim", "method_on_library_new")
108
+ end
109
+
110
+ it "should call method on library_new but not on library_old" do
111
+ @library_new.should_receive(:a_method).with()
112
+ @library_old.should_not_receive(:a_method).with()
113
+ @executor.call("test_slim", "a_method")
114
+ end
115
+
116
+ it "should call built-in library methods" do
117
+ @executor.call("test_slim", "push_fixture").should == nil
118
+ @executor.call("test_slim", "pop_fixture").should == nil
119
+ end
120
+
121
+ it "should translate getters and setters" do
122
+ @executor.call("test_slim", "set_lib_attribute", "lemon")
123
+ @executor.call("test_slim", "lib_attribute").should == "lemon"
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require 'slim_helper_library'
3
+ require 'statement_executor'
4
+
5
+ SLIM_HELPER_LIBRARY_INSTANCE_NAME = "SlimHelperLibrary"
6
+ ACTOR_INSTANCE_NAME = "scriptTableActor"
7
+
8
+ describe SlimHelperLibrary do
9
+ before() do
10
+ @executor = StatementExecutor.new
11
+ @executor.add_module("TestModule")
12
+ @executor.create(ACTOR_INSTANCE_NAME, "TestSlim", ["0"]).should == "OK"
13
+ @instance = @executor.instance(ACTOR_INSTANCE_NAME)
14
+ @helper = SlimHelperLibrary.new(@executor)
15
+ end
16
+
17
+ it "can get current scriptTableActor" do
18
+ @helper.get_fixture.should be @instance
19
+ end
20
+
21
+ it "can push and pop" do
22
+ @helper.push_fixture
23
+ @executor.create(ACTOR_INSTANCE_NAME, "TestSlim", ["1"])
24
+ @helper.get_fixture.should_not be @instance
25
+ @helper.pop_fixture
26
+ @helper.get_fixture.should be @instance
27
+ end
28
+
29
+ it "can push and pop many" do
30
+ @helper.push_fixture
31
+ @executor.create(ACTOR_INSTANCE_NAME, "TestChain", []).should == "OK"
32
+ one = @executor.instance(ACTOR_INSTANCE_NAME)
33
+ one.should_not be_nil
34
+ @helper.push_fixture
35
+
36
+ @executor.create(ACTOR_INSTANCE_NAME, "SimpleScript", [])
37
+ two = @executor.instance(ACTOR_INSTANCE_NAME)
38
+ one.should_not be two
39
+ @helper.get_fixture.should be two
40
+
41
+ @helper.pop_fixture
42
+ @helper.get_fixture.should be one
43
+
44
+ @helper.pop_fixture
45
+ @helper.get_fixture.should be @instance
46
+
47
+ end
48
+
49
+ end
50
+
51
+
52
+ #@Test
53
+ #public void testSlimHelperLibraryIsStoredInSlimExecutor() throws Exception {
54
+ # Object helperLibrary = caller.getInstance(SLIM_HELPER_LIBRARY_INSTANCE_NAME);
55
+ # assertTrue(helperLibrary instanceof SlimHelperLibrary);
56
+ #}
57
+ #
58
+ #@Test
59
+ #public void testSlimHelperLibraryHasStatementExecutor() throws Exception {
60
+ # SlimHelperLibrary helperLibrary = (SlimHelperLibrary) caller.getInstance(SLIM_HELPER_LIBRARY_INSTANCE_NAME);
61
+ # assertSame(caller, helperLibrary.getStatementExecutor());
62
+ #}
63
+ #
64
+ #@Test
65
+ #public void testSlimHelperLibraryCanPushAndPopFixture() throws Exception {
66
+ # SlimHelperLibrary helperLibrary = (SlimHelperLibrary) caller.getInstance(SLIM_HELPER_LIBRARY_INSTANCE_NAME);
67
+ # Object response = caller.create(ACTOR_INSTANCE_NAME, getTestClassName(), new Object[0]);
68
+ # Object firstActor = caller.getInstance(ACTOR_INSTANCE_NAME);
69
+ #
70
+ # helperLibrary.pushFixture();
71
+ #
72
+ # response = caller.create(ACTOR_INSTANCE_NAME, getTestClassName(), new Object[] {"1"});
73
+ # assertEquals("OK", response);
74
+ # assertNotSame(firstActor, caller.getInstance(ACTOR_INSTANCE_NAME));
75
+ #
76
+ # helperLibrary.popFixture();
77
+ #
78
+ # assertSame(firstActor, caller.getInstance(ACTOR_INSTANCE_NAME));
79
+ #}
80
+ #
@@ -0,0 +1,98 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
3
+ require 'socket_service'
4
+
5
+ class SocketServiceTest < Test::Unit::TestCase
6
+ def setup
7
+ @port = 12345
8
+ @ss = SocketService.new()
9
+ @connections = 0
10
+ end
11
+
12
+ def testOneConnection
13
+ @ss.serve(@port) {@connections += 1}
14
+ connect(@port)
15
+ @ss.close()
16
+ assert_equal(1, @connections)
17
+ end
18
+
19
+ def testManyConnections
20
+ @ss.serve(@port) {@connections += 1}
21
+ 10.times {connect(@port)}
22
+ @ss.close()
23
+ assert_equal(10, @connections)
24
+ assert_equal(0, @ss.pendingSessions)
25
+ end
26
+
27
+ def testSocketSend
28
+ @ss.serve(@port) do |serverSocket|
29
+ serverSocket.write("hi")
30
+ end
31
+
32
+ clientSocket = TCPSocket.open("localhost", @port)
33
+ answer = clientSocket.gets
34
+ clientSocket.close
35
+ assert_equal("hi", answer)
36
+ @ss.close()
37
+ end
38
+
39
+ # TEST FREEZES!!!
40
+ # We should not be able to keep the service alive by hitting
41
+ # it with connections after we close it?
42
+ def _testCantKeepAliveByConnectingAfterClose
43
+ #set up a service that waits for a message and then dies.
44
+ @ss.serve(@port) do |serverSocket|
45
+ message = serverSocket.gets
46
+ end
47
+
48
+ #s1 is a connection to that service.
49
+ s1 = TCPSocket.open("localhost", @port)
50
+ sleep(0.1)
51
+
52
+ #now start closing the server in a separate thread. It cannot
53
+ #finish closing until s1 completes.
54
+ Thread.start {@ss.close}
55
+ sleep(0.1)
56
+
57
+ #try to connect to the dying server.
58
+ s2=nil
59
+ Thread.start {s2 = TCPSocket.open("localhost", @port)}
60
+ sleep(0.1)
61
+ assert_equal(nil, s2, "shouldn't have connected")
62
+ assert_not_equal(nil, s1, "Should have connected")
63
+
64
+ #Complete the s1 session.
65
+ s1.write("testCloseRaceCondition");
66
+ s1.close
67
+
68
+ #collect the pending s2 connection
69
+ testThread = Thread.current
70
+ @ss.serve(@port) {testThread.wakeup}
71
+ Thread.stop
72
+ assert_not_equal(nil, s2)
73
+ s2.close
74
+ @ss.close
75
+ end
76
+
77
+ def testSessionCount
78
+ @ss.serve(@port) do |serverSocket|
79
+ message = serverSocket.gets
80
+ end
81
+
82
+ s1 = nil;
83
+ Thread.start {s1 = TCPSocket.open("localhost", @port)}
84
+ sleep(0.2)
85
+ assert_equal(1, @ss.pendingSessions);
86
+ s1.write("testSessionCount");
87
+ s1.close
88
+ sleep(0.2)
89
+ assert_equal(0, @ss.pendingSessions)
90
+ @ss.close
91
+ end
92
+
93
+ def connect(port)
94
+ s = TCPSocket.open("localhost", @port)
95
+ sleep(0.1)
96
+ s.close
97
+ end
98
+ end
@@ -0,0 +1,6 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) + "/../lib/rubyslim")
2
+ require "rubygems"
3
+ require "spec"
4
+ require 'jcode'
5
+ $KCODE="UTF8"
6
+ require 'timeout'
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require "statement_executor"
3
+
4
+ describe StatementExecutor do
5
+ before do
6
+ @executor = StatementExecutor.new
7
+ end
8
+
9
+ it "can split class names" do
10
+ @executor.split_class_name("a::b::c").should == ["a", "b", "c"]
11
+ end
12
+
13
+ it "can convert module names to file names" do
14
+ @executor.to_file_name("MyModuleName").should == "my_module_name"
15
+ end
16
+
17
+ it "can build the path name to a class" do
18
+ @executor.make_path_to_class("ModuleOne::ModuleTwo::MyClass").should == "module_one/module_two/my_class"
19
+ end
20
+
21
+ it "can require a class" do
22
+ @executor.add_module("MyModule")
23
+ proc = proc {@executor.require_class("MyModule::MyClass")}
24
+ proc.should raise_error(SlimError, /message:<<COULD_NOT_INVOKE_CONSTRUCTOR MyModule::MyClass failed to find in/)
25
+ end
26
+
27
+ it "can handle symbols whose values are objects" do
28
+ @executor.set_symbol("foo", OpenStruct.new(:foo => "bar"))
29
+ @executor.get_symbol("foo").foo.should == "bar"
30
+ @executor.replace_symbol("$foo").foo.should == "bar"
31
+ end
32
+
33
+ describe "accessor translation" do
34
+ class TestInstance
35
+ attr_accessor :foo
36
+ end
37
+
38
+ before(:each) do
39
+ @instance = TestInstance.new
40
+ @executor.set_instance("test_instance", @instance)
41
+ end
42
+
43
+ it "should translate setters" do
44
+ @executor.call("test_instance", "set_foo", "123")
45
+ @instance.foo.should == "123"
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require "statement"
3
+
4
+ describe Statement do
5
+ before do
6
+ @statement = Statement.new("")
7
+ end
8
+
9
+ it "can translate slim class names to ruby class names" do
10
+ @statement.slim_to_ruby_class("myPackage.MyClass").should == "MyPackage::MyClass"
11
+ @statement.slim_to_ruby_class("this.that::theOther").should == "This::That::TheOther"
12
+ end
13
+
14
+ it "can translate slim method names to ruby method names" do
15
+ @statement.slim_to_ruby_method("myMethod").should == "my_method"
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require 'table_to_hash_converter'
3
+
4
+ def shouldNotChange(string)
5
+ TableToHashConverter.convert(string).should == string
6
+ end
7
+
8
+ describe TableToHashConverter do
9
+ [
10
+ ["some string", "not a table"],
11
+ ["<table>blah", "incomplete table"],
12
+ ["<table><tr><td>hi</td></tr></table>", "too few columns"],
13
+ ["<table><tr><td>hi</td><td>med</td><td>lo</td></tr></table>", "too many columns"]
14
+ ].each do |string, reason|
15
+ it "#{reason}: should not change '#{string}'" do
16
+ shouldNotChange(string)
17
+ end
18
+ end
19
+ end
20
+
21
+ describe TableToHashConverter do
22
+ [
23
+ ["<table><tr><td>name</td><td>bob</td></tr></table>", {:name=>"bob"}],
24
+ [" <table> <tr> <td> name </td> <td> bob </td> </tr> </table> ", {:name=>"bob"}],
25
+ ["<table><tr><td>name</td><td>bob</td></tr><tr><td>addr</td><td>here</td></tr></table>", {:name=>'bob', :addr=>'here'}]
26
+ ].each do |table, hash|
27
+ it "should match #{table} to #{hash}" do
28
+ TableToHashConverter.convert(table).should == hash
29
+ end
30
+ end
31
+ end