PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/xruby/compiler/StdlibTest.java

http://xruby.googlecode.com/
Java | 427 lines | 336 code | 88 blank | 3 comment | 0 complexity | 1cf674dfff3f1b9f4cc5a30f45e57172 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.xruby.compiler;
  2. import com.xruby.runtime.lang.RubyRuntime;
  3. /**
  4. * Test if ruby's standand lib work under xruby
  5. */
  6. public class StdlibTest extends CompilerTestCase {
  7. public void setUp() {
  8. RubyRuntime.init(null);
  9. }
  10. public void test_test_unit_assertionfailederror() {
  11. String[] program_texts = {
  12. "require 'test/unit/assertionfailederror'",
  13. "print Test::Unit::AssertionFailedError.superclass",
  14. "begin; raise Test::Unit::AssertionFailedError.new('xxx')\n" +
  15. "rescue =>e; print e.message; end",
  16. };
  17. String[] outputs = {
  18. "",
  19. "StandardError",
  20. "xxx",
  21. };
  22. compile_run_and_compare_output(program_texts, outputs);
  23. }
  24. public void test_test_unit_failure() {
  25. String[] program_texts = {
  26. "require 'test/unit/failure'",
  27. "print Test::Unit::Failure.new('x', 'y', 'z').single_character_display",
  28. "print Test::Unit::Failure.new('x', 'y', 'z').short_display",
  29. "print Test::Unit::Failure.new('x1', [1, 2], 'z1').long_display",
  30. "print Test::Unit::Failure.new('x1', ['y'], 'z1').long_display",
  31. "print Test::Unit::Failure.new('x1', ['y'], 'z1').to_s",
  32. };
  33. String[] outputs = {
  34. "",
  35. "F",
  36. "x: z",
  37. "Failure:\nx1\n [1\n 2]:\nz1",
  38. "Failure:\nx1y:\nz1",
  39. "Failure:\nx1y:\nz1",
  40. };
  41. compile_run_and_compare_output(program_texts, outputs);
  42. }
  43. public void test_test_unit_util_backtracefilter() {
  44. String[] program_texts = {
  45. "print(require('test/unit/util/backtracefilter'))",
  46. "print Test::Unit::Util::BacktraceFilter",
  47. };
  48. String[] outputs = {
  49. "true",
  50. "Test::Unit::Util::BacktraceFilter",
  51. };
  52. compile_run_and_compare_output(program_texts, outputs);
  53. }
  54. public void test_test_unit_util_procwrapper() {
  55. String[] program_texts = {
  56. "print(require('test/unit/util/procwrapper'))",
  57. };
  58. String[] outputs = {
  59. "true",
  60. };
  61. compile_run_and_compare_output(program_texts, outputs);
  62. }
  63. public void test_test_unit_util_observable() {
  64. String[] program_texts = {
  65. "print(require('test/unit/util/observable'))",
  66. };
  67. String[] outputs = {
  68. "true",
  69. };
  70. compile_run_and_compare_output(program_texts, outputs);
  71. }
  72. public void test_test_unit_testresult() {
  73. String[] program_texts = {
  74. "print(require('test/unit/testresult'))",
  75. "print Test::Unit::TestResult.new.to_s",
  76. "print Test::Unit::TestResult.new.passed?",
  77. "print Test::Unit::TestResult.new.failure_count",
  78. "print Test::Unit::TestResult.new.error_count",
  79. "print Test::Unit::TestResult.new.add_run",
  80. "a=Test::Unit::TestResult.new; print a.add_failure('xxx'), a.failure_count",
  81. "a=Test::Unit::TestResult.new; print a.add_error('xxx'), a.add_error('yyy'), a.error_count",
  82. "a=Test::Unit::TestResult.new; print a.add_assertion",
  83. };
  84. String[] outputs = {
  85. "true",
  86. "0 tests, 0 assertions, 0 failures, 0 errors",
  87. "true",
  88. "0",
  89. "0",
  90. "0",
  91. "01",
  92. "002",
  93. "0",
  94. };
  95. compile_run_and_compare_output(program_texts, outputs);
  96. }
  97. public void test_test_unit_error() {
  98. String[] program_texts = {
  99. "print(require('test/unit/error'))",
  100. "print Test::Unit::Error.new('name', 'excepton').single_character_display",
  101. "print Test::Unit::Error.new('excepton', NameError.new('message')).message",
  102. };
  103. String[] outputs = {
  104. "true",
  105. "E",
  106. "NameError: message",
  107. };
  108. compile_run_and_compare_output(program_texts, outputs);
  109. }
  110. public void test_test_unit_testcase() {
  111. String[] program_texts = {
  112. "require('test/unit/testcase')",
  113. "print Test::Unit::TestCase.new('name').size",
  114. };
  115. String[] outputs = {
  116. "",
  117. "1",
  118. };
  119. compile_run_and_compare_output(program_texts, outputs);
  120. }
  121. public void test_test_unit_testsuite() {
  122. String[] program_texts = {
  123. "require('test/unit/testsuite')",
  124. "print Test::Unit::TestSuite.new.to_s",
  125. "print Test::Unit::TestSuite.new('name').to_s",
  126. "print Test::Unit::TestSuite.new.empty?",
  127. "a = Test::Unit::TestSuite.new; a << 1; print a.empty?; a.delete 1; print a.empty?",
  128. "a = Test::Unit::TestSuite.new; a << 'x'; print a.size",
  129. "a = Test::Unit::TestSuite.new; b = Test::Unit::TestSuite.new; a << 1; print a == b",
  130. "a = Test::Unit::TestSuite.new; b = Test::Unit::TestSuite.new; print a == b",
  131. "Test::Unit::TestSuite.new('x').run('z') {|x, y| print x, y}",
  132. };
  133. String[] outputs = {
  134. "",
  135. "Unnamed TestSuite",
  136. "name",
  137. "true",
  138. "falsetrue",
  139. "1",
  140. "false",
  141. "true",
  142. "Test::Unit::TestSuite::STARTEDxTest::Unit::TestSuite::FINISHEDx",
  143. };
  144. compile_run_and_compare_output(program_texts, outputs);
  145. }
  146. public void test_test_unit_collector() {
  147. String[] program_texts = {
  148. "print(require('test/unit/collector'))",
  149. };
  150. String[] outputs = {
  151. "true",
  152. };
  153. compile_run_and_compare_output(program_texts, outputs);
  154. }
  155. public void test_test_unit_assertions() {
  156. String[] program_texts = {
  157. "require('test/unit/assertions')",
  158. "print (include Test::Unit::Assertions)",
  159. "print(build_message('xxx'))",
  160. "print(build_message('xxx', 'yyy'))",
  161. "print AssertionMessage::Template.new(['?']).count",
  162. "print AssertionMessage::Template.new(['?']).result([2])",
  163. "print AssertionMessage::Template.create('<?>').result([3])",
  164. "print AssertionMessage.new('x', '<?>', [1]).add_period('y')",
  165. "print AssertionMessage.new('x', '<?>', [1])",
  166. "print(build_message('xxx', '<?> is not true.', false))",
  167. "assert_block {true}",
  168. };
  169. String[] outputs = {
  170. "",
  171. "Object",
  172. "xxx.",
  173. "xxx.\nyyy",
  174. "1",
  175. "2",
  176. "<3>",
  177. "y.",
  178. "x.\n<1>",
  179. "xxx.\n<false> is not true.",
  180. "",
  181. };
  182. compile_run_and_compare_output(program_texts, outputs);
  183. }
  184. public void test_test_unit_ui_testrunnerutilities() {
  185. String[] program_texts = {
  186. "print(require('test/unit/ui/testrunnerutilities'))",
  187. };
  188. String[] outputs = {
  189. "true",
  190. };
  191. compile_run_and_compare_output(program_texts, outputs);
  192. }
  193. public void test_complex() {
  194. String[] program_texts = {
  195. "require 'complex.rb'",
  196. "print Complex.generic?(1)",
  197. "print Complex.new(0,2)",
  198. "print Complex::I",
  199. };
  200. String[] outputs = {
  201. "",
  202. "true",
  203. "2i",
  204. "1i",
  205. };
  206. compile_run_and_compare_output(program_texts, outputs);
  207. }
  208. public void test_erb() {
  209. String[] program_texts = {
  210. "require 'erb'",
  211. "print ERB::Compiler.new(nil).compile('<%= print 1 %>')",
  212. "ERB.new('<%= print 1 %>').result",
  213. "print ERB.new('<% 3.times do %>1<% end %>').result",
  214. };
  215. String[] outputs = {
  216. "",
  217. "print(( print 1 ).to_s)",
  218. "1",
  219. "111",
  220. };
  221. compile_run_and_compare_output(program_texts, outputs);
  222. }
  223. public void test_shellwords() {
  224. String[] program_texts = {
  225. "require 'shellwords'",
  226. "p Shellwords.shellwords('a b c')",
  227. };
  228. String[] outputs = {
  229. "",
  230. "[\"a\", \"b\", \"c\"]\n",
  231. };
  232. compile_run_and_compare_output(program_texts, outputs);
  233. }
  234. public void test_singleton() {
  235. String[] program_texts = {
  236. "require 'singleton'",
  237. "class TestSingleton; include Singleton; end\n" +
  238. "print(TestSingleton.instance == TestSingleton.instance)",
  239. };
  240. String[] outputs = {
  241. "",
  242. "true",
  243. };
  244. compile_run_and_compare_output(program_texts, outputs);
  245. }
  246. public void test_cgi() {
  247. String[] program_texts = {
  248. "require 'cgi'",
  249. "print CGI::unescape('%27Stop%21%27+said+Fred')",
  250. "p CGI::parse('a=b')",
  251. "print CGI::escapeHTML('Usage: foo \"bar\" <baz>')",
  252. "print CGI::unescapeHTML('Usage: foo &quot;bar&quot; &lt;baz&gt;')",
  253. "print CGI::escapeElement('<BR><A HREF=\"url\"></A>', 'A', 'IMG')",
  254. "print CGI::unescapeElement(CGI::escapeHTML('<BR><A HREF=\"url\"></A>'), 'A', 'IMG')",
  255. };
  256. String[] outputs = {
  257. "",
  258. "'Stop!' said Fred",
  259. "{\"a\"=>[\"b\"]}\n",
  260. "Usage: foo &quot;bar&quot; &lt;baz&gt;",
  261. "Usage: foo \"bar\" <baz>",
  262. "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;",
  263. "&lt;BR&gt;<A HREF=\"url\"></A>",
  264. };
  265. compile_run_and_compare_output(program_texts, outputs);
  266. }
  267. public void test_rational() {
  268. String[] program_texts = {
  269. "require 'rational'",
  270. "print Rational(3,4).to_s",
  271. "print Rational(+7,4).to_i",
  272. "print Rational(-7,4).to_i",
  273. "print Rational(+7,4).to_f",
  274. "print Rational(3,4) / 2",
  275. "print Rational(3,4) / 2.0",
  276. "print Rational(3,4) * 2",
  277. "print Rational(7,4) % Rational(1,2)",
  278. "print Rational(3,4) % 1",
  279. "print Rational(3,4) % Rational(1,7)",
  280. "print Rational(3,4) ** 2",
  281. "print Rational(-3,5).abs",
  282. "print Rational(-3,2) == Rational(-3,2) ",
  283. "print Rational(-3,2) <=> Rational(-3,2) ",
  284. };
  285. String[] outputs = {
  286. "",
  287. "3/4",
  288. "1",
  289. "-2",
  290. "1.75",
  291. "3/8",
  292. "0.375",
  293. "3/2",
  294. "1/4",
  295. "3/4",
  296. "1/28",
  297. "9/16",
  298. "3/5",
  299. "true",
  300. "0",
  301. };
  302. compile_run_and_compare_output(program_texts, outputs);
  303. }
  304. public void test_parsedate() {
  305. String[] program_texts = {
  306. "require 'parsedate'",
  307. "p ParseDate.parsedate('Tuesday, July 5th, 2007, 18:35:20 UTC')",
  308. };
  309. String[] outputs = {
  310. "",
  311. "[2007, 7, 5, 18, 35, 20, \"UTC\", 2]\n",
  312. };
  313. compile_run_and_compare_output(program_texts, outputs);
  314. }
  315. public void test_time() {
  316. String[] program_texts = {
  317. "require 'time'",
  318. "print Time.zone_offset('EDT')",
  319. };
  320. String[] outputs = {
  321. "",
  322. "-14400",
  323. };
  324. compile_run_and_compare_output(program_texts, outputs);
  325. }
  326. public void test_uri() {
  327. String[] program_texts = {
  328. "require 'uri'",
  329. "print URI.escape(\"http://example.com/?a=\\11\\15\")",
  330. "print URI.unescape(\"http://example.com/?a=%09%0D\")",
  331. };
  332. String[] outputs = {
  333. "",
  334. "http://example.com/?a=%09%0D",
  335. "http://example.com/?a=\t\r",
  336. };
  337. compile_run_and_compare_output(program_texts, outputs);
  338. }
  339. }