PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/drillbit/Resources/tests/ruby/ruby.rb

http://github.com/appcelerator/titanium_desktop
Ruby | 343 lines | 279 code | 57 blank | 7 comment | 40 complexity | ad7f2a4b0c2bcf0dbf68328317c8dd3d MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'foo'
  2. $orig_include_path = $LOAD_PATH
  3. builder_path = Titanium.API.getApplication().getResourcesPath() + '/builder/lib'
  4. $LOAD_PATH << builder_path
  5. require 'builder/lib/builder'
  6. require 'markaby/lib/markaby'
  7. require 'ostruct'
  8. require 'rss'
  9. def external_document
  10. document.getElementById 'a'
  11. end
  12. def require_file_module
  13. f = Foo.new
  14. f.bar
  15. end
  16. def require_sub_file_module
  17. f = Foo.new
  18. f.yum
  19. end
  20. def test_window_global
  21. my_global_foo 'suck ass'
  22. end
  23. def what_is_love?
  24. f = Foo.new
  25. f.what_is_love?(my_global_var)
  26. end
  27. def test_document_title
  28. return document.title
  29. end
  30. def test_gem
  31. mab = Markaby::Builder.new
  32. mab.html do
  33. head { title "Boats.com" }
  34. end
  35. mab.to_s
  36. end
  37. def test_type_string
  38. return 'i am string'
  39. end
  40. def test_type_symbol(stringbol)
  41. return stringbol.to_sym
  42. end
  43. def test_type_float
  44. 1.1
  45. end
  46. def test_type_long
  47. 10**100 #<= google
  48. end
  49. def test_type_scientific_notation_large
  50. 1.7e19
  51. end
  52. def test_type_scientific_notation_small
  53. 3.21e-13
  54. end
  55. def test_type_int
  56. 1
  57. end
  58. def test_type_boolean_false
  59. false
  60. end
  61. def test_type_boolean_true
  62. true
  63. end
  64. def test_type_map
  65. {'a'=>'b'}
  66. end
  67. def test_get_symbolly_hash
  68. # We want more than 1/2 of the keys to be symbols
  69. # for new keys to be treated like symbols.
  70. return {
  71. :a => 'a',
  72. :one => 'b',
  73. :two => 'c',
  74. :three => 'd',
  75. :symbol => 'bully',
  76. :another_symbol => 'wooly',
  77. 'not_a_symbol' => 'foo'
  78. }
  79. end
  80. def test_get_less_symbolly_hash
  81. # We want more than 1/2 of the keys to be symbols
  82. # for new keys to be treated like symbols.
  83. return {
  84. 'a' => 'a',
  85. 'one' => 'b',
  86. 'two' => 'c',
  87. 'three' => 'd',
  88. 'four' => 'd',
  89. 'five' => 'd',
  90. :symbol => 'bully',
  91. :another_not_symbol => 'wooly',
  92. 'not_a_symbol' => 'foo'
  93. }
  94. end
  95. def test_symbolly_hash(hash)
  96. if hash.has_key?('fromjs')
  97. return false
  98. elsif hash.has_key?(:fromjs)
  99. return true
  100. else
  101. return false
  102. end
  103. end
  104. def test_less_symbolly_hash(hash)
  105. if hash.has_key?(:fromjs)
  106. return false
  107. elsif hash.has_key?('fromjs')
  108. return true
  109. else
  110. return false
  111. end
  112. end
  113. def test_type_tuple
  114. OpenStruct.new(:one=>2,:two=>3)
  115. end
  116. def test_type_struct
  117. customer = Struct.new(:name,:address)
  118. customer.new("jeff","mtn view")
  119. end
  120. def test_type_nil
  121. nil
  122. end
  123. def test_type_array
  124. [1,2,3]
  125. end
  126. def test_type_function
  127. method(:test_type_array)
  128. end
  129. def test_type_proc
  130. Proc.new { 'hello world' }
  131. end
  132. # tests for conversion from JS into Ruby
  133. def test_js_type_string(t)
  134. return t.class == String
  135. end
  136. def test_js_type_int(t)
  137. return t.class == Fixnum
  138. end
  139. def test_js_type_function(t)
  140. return t.class == Method
  141. end
  142. def test_js_type_float(t)
  143. return t.class == Float
  144. end
  145. def test_js_type_list(t)
  146. return t.class == Array
  147. end
  148. def test_js_type_klist(t)
  149. return t.class.name == 'RubyKList'
  150. end
  151. def test_js_klist_elements(t)
  152. return t[0] == 1 && t[1] == 2 && t[2] == 3
  153. end
  154. def test_js_type_kobject(t)
  155. return t.class.name == 'RubyKObject'
  156. end
  157. def test_js_type_kmethod(t)
  158. return t.class.name == 'RubyKMethod'
  159. end
  160. def test_js_type_dict(t)
  161. return t.class == Hash
  162. end
  163. def test_js_type_true_bool(t)
  164. return t == true
  165. end
  166. def test_js_type_false_bool(t)
  167. return t == false
  168. end
  169. def test_js_type_nil(t)
  170. return t.class == NilClass
  171. end
  172. def test_rubykobject_respond_to(o)
  173. if not(o.respond_to?(:sheep))
  174. return "Oops: Did not respond to sheep"
  175. end
  176. if not(o.respond_to?(:cow))
  177. return "Oops: Did not respond to cow"
  178. end
  179. if not(o.respond_to?(:phillip))
  180. return "Oops: Did not respond to phillip"
  181. end
  182. if o.respond_to?(:undef)
  183. return "Oops: Responded to undef"
  184. end
  185. if o.respond_to?(:bob)
  186. return "Oops: Responded to bob"
  187. end
  188. return ""
  189. end
  190. def test_rubykobject_method_missing_exception(o)
  191. exception = ""
  192. begin
  193. o.method_missing(:blahblah)
  194. exception = ":blahblah did not throw an Exception"
  195. rescue NoMethodError
  196. 1 + 1
  197. rescue
  198. exception = ":blahblah did not throw a NoMethodError"
  199. end
  200. if (exception != "")
  201. return exception
  202. end
  203. exception = ""
  204. begin
  205. o.method_missing(:cow)
  206. rescue NoMethodError
  207. exception = ":cow threw a NoMethodError"
  208. rescue
  209. exception = ":cow threw some unknown error: " + $!
  210. end
  211. return exception
  212. end
  213. def test_rubyklist_length(l, length)
  214. if l.length == length
  215. return ""
  216. else
  217. return "Length should have been " + length + " but was " + l.length
  218. end
  219. # Test invalid argument handling with the length method
  220. exception = ""
  221. begin
  222. l.length("what, what")
  223. exception = "length() did not throw an exception"
  224. rescue ArgumentError
  225. exception = ""
  226. rescue
  227. exception = "length() hrew an unknown exception"
  228. end
  229. end
  230. def test_rubyklist_each(l, *list_items)
  231. length = l.length
  232. exp_length = list_items.length
  233. if length != exp_length
  234. return "Length should have been #{exp_length} but was #{length}"
  235. end
  236. i = 0
  237. l.each { |item|
  238. if (item != list_items[i])
  239. return "Item #{i} should have been #{list_items[i].inspect} but was #{item.inspect}"
  240. end
  241. i = i + 1
  242. }
  243. return ""
  244. end
  245. def get_ruby_proc()
  246. p = Proc.new { |x| x + "foo!" }
  247. return p
  248. end
  249. def get_ruby_proc_arity(arity)
  250. if (arity == 0)
  251. p = Proc.new { "|" }
  252. elsif (arity == 1)
  253. p = Proc.new { |x| x + "|" }
  254. elsif (arity == 2)
  255. p = Proc.new { |x, y| x + "|" + y + "|" }
  256. elsif (arity == 3)
  257. p = Proc.new { |x, y, z| x + "|" + y + "|" + z + "|" }
  258. elsif (arity == -1)
  259. p = Proc.new { |x, *y| x + "|" + y.join("|") + "|" }
  260. elsif (arity == -2)
  261. p = Proc.new { |x, y, *z| x + "|" + y + "|" + z.join("|") + "|" }
  262. end
  263. return p
  264. end
  265. def get_empty_ruby_object()
  266. return Object.new
  267. end
  268. def get_empty_ruby_list()
  269. return []
  270. end
  271. def get_empty_ruby_hash()
  272. #return {}
  273. return Object.new
  274. end
  275. def get_include_path()
  276. return $orig_include_path.join(" : ")
  277. end
  278. def api_is_there()
  279. return defined?(RSS) != nil
  280. end
  281. def test_rb_collect(input)
  282. return input.collect {|x| x * 2}
  283. end
  284. def test_rb_map(input)
  285. return input.map {|x| x * 2}
  286. end