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
johnson / test / johnson / prelude_test.rb
100644 61 lines (47 sloc) 1.734 kb
1
2
3
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require File.expand_path(File.join(File.dirname(__FILE__), "/../helper"))
 
module Johnson
  class PreludeTest < Johnson::TestCase
    def setup
      @runtime = Johnson::Runtime.new
    end
    
    def test_symbols_are_interned
      assert(@runtime.evaluate("Johnson.symbolize('foo') === Johnson.symbolize('foo')"))
    end
    
    def test_strings_had_a_to_symbol_method
      assert_js_equal(:monkeys, "'monkeys'.toSymbol()")
    end
    
    def test_string_to_symbol_is_not_enumerable
      assert(!@runtime.evaluate(<<-END))
var flag = false;
for (x in "foo") { if (x == 'toSymbol') flag = true }
flag
END
    end
    
    def test_symbol_to_string
      assert_equal("monkey", @runtime.evaluate("Johnson.symbolize('monkey').toString()"))
    end
 
    def test_symbol_inspect
      assert_equal(":monkey", @runtime.evaluate("Johnson.symbolize('monkey').inspect()"))
    end
    
    def test_all_of_ruby_is_available
      assert_raise(Johnson::Error) { @runtime.evaluate("Ruby.Set.new()") }
      
      @runtime.evaluate("Ruby.require('set')")
      assert_kind_of(Set, @runtime.evaluate("Ruby.Set.new()"))
    end
    
    def test_require_an_existing_js_file_without_extension
      assert_js("Johnson.require('johnson/template')")
    end
    
    def test_require_returns_false_the_second_time_around
      assert_js("Johnson.require('johnson/template')")
      assert(!@runtime.evaluate("Johnson.require('johnson/template')"))
    end
    
    def test_missing_requires_throw_LoadError
      assert_js(<<-END)
var flag = false;
try { Johnson.require("johnson/__nonexistent"); }
catch(ex) { flag = true; }
flag;
END
    end
  end
end