public
Description: Johnson wraps JavaScript in a loving Ruby embrace.
Homepage: http://github.com/jbarnette/johnson/wikis
Clone URL: git://github.com/jbarnette/johnson.git
THEY ALL FUCKING WORK
tenderlove (author)
Fri May 30 16:59:45 -0700 2008
commit  5bcd3531d7a01d190bc99130ae1dd44964b800eb
tree    2724315216ef6dcc00fe49f4f4f086a8584e46a9
parent  4507bf52e909971dd1184256e0cfba3429ac3d3a
...
3
4
5
6
7
 
 
8
9
10
11
12
 
13
14
 
15
16
17
...
3
4
5
 
 
6
7
8
9
10
11
 
12
13
 
14
15
16
17
0
@@ -3,15 +3,15 @@ require File.expand_path(File.join(File.dirname(__FILE__), "/../helper"))
0
 module Johnson
0
   class BrowserTest < Johnson::TestCase
0
     def setup
0
-      @context = Johnson::Context.new
0
-      @context.evaluate('Johnson.require("johnson/browser");')
0
+      @runtime = Johnson::Runtime.new
0
+      @runtime.evaluate('Johnson.require("johnson/browser");')
0
     end
0
 
0
     def test_set_location_returns_location
0
       filename = "file://#{File.expand_path(__FILE__)}"
0
-      @context.evaluate("window.location = '#{filename}'")
0
+      @runtime.evaluate("window.location = '#{filename}'")
0
       uri = URI.parse(filename)
0
-      assert_equal(uri.to_s, @context.evaluate('window.location').to_s)
0
+      assert_equal(uri.to_s, @runtime.evaluate('window.location').to_s)
0
     end
0
   end
0
 end
...
4
5
6
7
 
8
9
10
11
12
 
 
13
14
15
16
17
18
 
 
 
19
20
21
22
23
24
25
26
 
 
 
27
28
29
30
31
32
 
33
34
35
...
4
5
6
 
7
8
9
10
 
 
11
12
13
14
15
 
 
 
16
17
18
19
20
21
22
23
 
 
 
24
25
26
27
28
29
30
31
 
32
33
34
35
0
@@ -4,32 +4,32 @@ module Johnson
0
   module Conversions
0
     class ArrayTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
       
0
       def test_array_index_get
0
-        @context[:list] = [1, 2, 3, 4]
0
-        assert_equal(1, @context.evaluate("list[0]"))
0
+        @runtime[:list] = [1, 2, 3, 4]
0
+        assert_equal(1, @runtime.evaluate("list[0]"))
0
       end
0
 
0
       def test_array_index_set
0
-        @context[:list] = []
0
-        @context.evaluate("list[0] = 42")
0
-        assert_equal(42, @context[:list][0])
0
+        @runtime[:list] = []
0
+        @runtime.evaluate("list[0] = 42")
0
+        assert_equal(42, @runtime[:list][0])
0
       end
0
 
0
       def test_array_works_with_for_in
0
         list = [1, 2, 3, 4]
0
 
0
-        @context['alert'] = lambda { |x| p x }
0
-        @context['list'] = list
0
-        @context.evaluate("
0
+        @runtime['alert'] = lambda { |x| p x }
0
+        @runtime['list'] = list
0
+        @runtime.evaluate("
0
           var new_list = [];
0
           for(x in list) {
0
             new_list.push(x + 1);
0
           }
0
         ")
0
-        assert_equal(list.map { |x| x + 1}, @context['new_list'].to_a)
0
+        assert_equal(list.map { |x| x + 1}, @runtime['new_list'].to_a)
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
12
 
 
13
14
15
16
17
 
 
18
19
20
...
4
5
6
 
7
8
9
10
 
 
11
12
13
14
15
 
 
16
17
18
19
20
0
@@ -4,17 +4,17 @@ module Johnson
0
   module Conversions
0
     class BooleanTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
       
0
       def test_truthiness
0
-        @context[:v] = true
0
-        assert_same(true, @context.evaluate("v === true"))
0
+        @runtime[:v] = true
0
+        assert_same(true, @runtime.evaluate("v === true"))
0
       end
0
 
0
       def test_dirty_lies
0
-        @context[:v] = false
0
-        assert_same(false, @context.evaluate("v === true"))
0
+        @runtime[:v] = false
0
+        assert_same(false, @runtime.evaluate("v === true"))
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
 
12
13
14
15
16
17
 
 
18
19
20
21
22
 
 
23
24
25
...
30
31
32
33
 
34
35
36
...
4
5
6
 
7
8
9
10
 
11
12
13
14
15
 
 
16
17
18
19
20
 
 
21
22
23
24
25
...
30
31
32
 
33
34
35
36
0
@@ -4,22 +4,22 @@ module Johnson
0
   module Conversions
0
     class CallableTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
       
0
       def test_proc_works_in_jsland
0
-        @context[:squared] = Proc.new { |x| x * x }
0
+        @runtime[:squared] = Proc.new { |x| x * x }
0
         assert_js_equal(4, "squared(2)")
0
       end
0
       
0
       def test_procs_roundtrip
0
-        @context[:k] = k = lambda { |x| x }
0
-        assert_same(k, @context.evaluate("k"))
0
+        @runtime[:k] = k = lambda { |x| x }
0
+        assert_same(k, @runtime.evaluate("k"))
0
       end
0
       
0
       def test_proc_js_function_proxy_gets_reused
0
-        @context[:k] = k = lambda { |x| x }
0
-        @context[:kk] = k
0
+        @runtime[:k] = k = lambda { |x| x }
0
+        @runtime[:kk] = k
0
         assert_js("k === kk")
0
       end
0
       
0
@@ -30,7 +30,7 @@ module Johnson
0
       end
0
       
0
       def test_anything_with_a_call_method_can_be_called_as_a_method
0
-        @context[:c] = CallableThing.new
0
+        @runtime[:c] = CallableThing.new
0
         assert_js_equal("foo", "c()")
0
       end
0
     end
...
4
5
6
7
 
8
9
10
11
12
13
14
 
 
 
15
16
17
...
4
5
6
 
7
8
9
10
11
 
 
 
12
13
14
15
16
17
0
@@ -4,14 +4,14 @@ module Johnson
0
   module Conversions
0
     class FileTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
 
0
       def test_read_file
0
         File.open(__FILE__, 'rb') { |f|
0
-          @context[:foo] = f
0
-          assert_equal(f, @context.evaluate("foo"))
0
-          assert_equal(File.read(__FILE__), @context.evaluate("foo.read()"))
0
+          @runtime[:foo] = f
0
+          assert_equal(f, @runtime.evaluate("foo"))
0
+          assert_equal(File.read(__FILE__), @runtime.evaluate("foo.read()"))
0
         }
0
       end
0
     end
...
4
5
6
7
 
8
9
10
11
12
 
 
13
14
15
16
 
17
18
19
20
 
21
22
23
...
4
5
6
 
7
8
9
10
 
 
11
12
13
14
15
 
16
17
18
19
 
20
21
22
23
0
@@ -4,20 +4,20 @@ module Johnson
0
   module Conversions
0
     class NilTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
       
0
       def test_ruby_nil_is_js_null
0
-        @context[:v] = nil
0
-        assert_equal(true, @context.evaluate("v == null"))
0
+        @runtime[:v] = nil
0
+        assert_equal(true, @runtime.evaluate("v == null"))
0
       end
0
 
0
       def test_js_null_is_ruby_nil
0
-        assert_nil(@context.evaluate("null"))
0
+        assert_nil(@runtime.evaluate("null"))
0
       end
0
 
0
       def test_js_undefined_is_ruby_nil
0
-        assert_nil(@context.evaluate("undefined"))
0
+        assert_nil(@runtime.evaluate("undefined"))
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
 
12
13
14
15
 
16
17
18
19
 
20
21
22
23
24
25
 
26
27
 
28
29
30
31
 
32
33
34
 
35
36
37
...
4
5
6
 
7
8
9
10
 
11
12
13
14
 
15
16
17
18
 
19
20
21
22
23
24
 
25
26
 
27
28
29
30
 
31
32
33
 
34
35
36
37
0
@@ -4,34 +4,34 @@ module Johnson
0
   module Conversions
0
     class NumberTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
       
0
       def test_ruby_fixnum_in_js
0
-        @context[:v] = 42
0
+        @runtime[:v] = 42
0
         
0
         assert_js("v == 42")
0
         assert_js_equal(42, "v")
0
-        assert_equal(42, @context[:v])
0
+        assert_equal(42, @runtime[:v])
0
       end
0
       
0
       def test_js_fixnum_in_ruby
0
-        fix = @context.evaluate("42")
0
+        fix = @runtime.evaluate("42")
0
         assert_equal(42, fix)
0
         assert_kind_of(Fixnum, fix)
0
       end
0
 
0
       def test_ruby_float_in_js
0
-        @context[:pi] = pi = 3.141592654
0
+        @runtime[:pi] = pi = 3.141592654
0
         assert_js_equal(pi, "pi")
0
-        assert_in_delta(pi, @context.evaluate("pi"), 2 ** -20)
0
+        assert_in_delta(pi, @runtime.evaluate("pi"), 2 ** -20)
0
       end
0
       
0
       def test_ruby_bignum_in_js
0
-        @context[:big] = big = 2 ** 200        
0
+        @runtime[:big] = big = 2 ** 200        
0
         
0
         assert_js_equal(big, "big")
0
-        assert_kind_of(Float, @context.evaluate("big"))
0
+        assert_kind_of(Float, @runtime.evaluate("big"))
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
12
13
14
15
16
 
 
 
 
 
 
17
18
19
20
21
 
 
22
23
24
 
 
25
26
27
...
4
5
6
 
7
8
9
10
 
 
 
 
 
 
11
12
13
14
15
16
17
18
19
 
 
20
21
22
 
 
23
24
25
26
27
0
@@ -4,24 +4,24 @@ module Johnson
0
   module Conversions
0
     class RegexpTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
 
0
       def test_regex_converts
0
-        @context[:x] = /aaron/
0
-        @context[:y] = /john/i
0
-        assert @context.evaluate('"aaron".match(x)')
0
-        assert !@context.evaluate('"Aaron".match(x)')
0
-        assert @context.evaluate('"john".match(y)')
0
-        assert @context.evaluate('"John".match(y)')
0
+        @runtime[:x] = /aaron/
0
+        @runtime[:y] = /john/i
0
+        assert @runtime.evaluate('"aaron".match(x)')
0
+        assert !@runtime.evaluate('"Aaron".match(x)')
0
+        assert @runtime.evaluate('"john".match(y)')
0
+        assert @runtime.evaluate('"John".match(y)')
0
       end
0
 
0
       def test_regex_roundtrips
0
-        @context[:x] = /aaron/
0
-        assert_equal(/aaron/, @context.evaluate('x'))
0
+        @runtime[:x] = /aaron/
0
+        assert_equal(/aaron/, @runtime.evaluate('x'))
0
 
0
-        @context[:x] = /aaron/m
0
-        assert_equal(/aaron/m, @context.evaluate('x'))
0
+        @runtime[:x] = /aaron/m
0
+        assert_equal(/aaron/m, @runtime.evaluate('x'))
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
 
12
13
14
15
16
 
17
18
19
20
21
 
 
22
23
24
25
26
 
 
27
28
29
...
4
5
6
 
7
8
9
10
 
11
12
13
14
15
 
16
17
18
19
 
 
20
21
22
23
24
 
 
25
26
27
28
29
0
@@ -4,26 +4,26 @@ module Johnson
0
   module Conversions
0
     class StringTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
       
0
       def test_ruby_string_in_js
0
-        @context[:v] = "foo"
0
+        @runtime[:v] = "foo"
0
         assert_js("'foo' == v")
0
       end
0
 
0
       def test_js_string_in_ruby
0
-        assert_equal("foo", @context.evaluate("'foo'"))
0
+        assert_equal("foo", @runtime.evaluate("'foo'"))
0
       end
0
 
0
       def test_roundtrip
0
-        @context[:v] = v = "hola"
0
-        assert_equal(v, @context.evaluate("v"))
0
+        @runtime[:v] = v = "hola"
0
+        assert_equal(v, @runtime.evaluate("v"))
0
       end
0
       
0
       def test_strings_are_copies
0
-        @context[:v] = v = "hola"
0
-        assert_not_same(v, @context.evaluate("v"))
0
+        @runtime[:v] = v = "hola"
0
+        assert_not_same(v, @runtime.evaluate("v"))
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
12
13
14
15
 
 
 
16
17
18
...
4
5
6
 
7
8
9
10
11
12
 
 
 
13
14
15
16
17
18
0
@@ -4,15 +4,15 @@ module Johnson
0
   module Conversions
0
     class StructTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
 
0
       def test_use_struct
0
         f = Struct.new(:phil_collins).new
0
         f.phil_collins = 'awesome'
0
-        @context[:foo] = f
0
-        assert_equal(f, @context.evaluate('foo'))
0
-        assert_equal('awesome', @context.evaluate('foo.phil_collins'))
0
+        @runtime[:foo] = f
0
+        assert_equal(f, @runtime.evaluate('foo'))
0
+        assert_equal('awesome', @runtime.evaluate('foo.phil_collins'))
0
       end
0
     end
0
   end
...
4
5
6
7
 
8
9
10
11
12
 
 
13
14
 
15
16
17
18
19
 
 
20
21
22
...
4
5
6
 
7
8
9
10
 
 
11
12
13
 
14
15
16
17
 
 
18
19
20
21
22
0
@@ -4,19 +4,19 @@ module Johnson
0
   module Conversions
0
     class SymbolTest < Johnson::TestCase
0
       def setup
0
-        @context = Johnson::Context.new
0
+        @runtime = Johnson::Runtime.new
0
       end
0
 
0
       def test_symbols_are_interned
0
-        @context[:v] = :symbol
0
-        @context[:x] = :symbol
0
+        @runtime[:v] = :symbol
0
+        @runtime[:x] = :symbol
0
 
0
-        assert(@context.evaluate("v !== null && v === x"))
0
+        assert(@runtime.evaluate("v !== null && v === x"))
0
       end
0
 
0
       def test_ruby_symbol_roundtrips
0
-        @context[:v] = :foo
0
-        assert_equal(:foo, @context.evaluate("v"))
0
+        @runtime[:v] = :foo
0
+        assert_equal(:foo, @runtime.evaluate("v"))
0
       end
0
     end
0
   end
...
12
13
14
 
 
 
 
 
 
 
 
 
 
 
15
16
17
...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -12,6 +12,17 @@ module Johnson
0
         @runtime['thread'] = thread
0
         assert_js_equal(false, "thread.send('alive?')")
0
       end
0
+
0
+      def test_new_js_thread
0
+        @runtime.evaluate('function testing() { Ruby.sleep(10); }')
0
+        @runtime.evaluate('new Ruby.Thread(function() { testing(); })')
0
+      end
0
+
0
+      def test_js_thread_read_file
0
+        @runtime['filename'] = File.expand_path(__FILE__)
0
+        @runtime.evaluate('function testing() { Ruby.File.read(filename); }')
0
+        @runtime.evaluate('new Ruby.Thread(function() { testing(); })')
0
+      end
0
     end
0
   end
0
 end

Comments