PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/TDDBC_Yokohama2_PCUnit/Ruby/doc/ruby/ruby-1.9.2/test/win32ole/test_win32ole.rb

https://bitbucket.org/aetos/tddbc_yokohama2
Ruby | 516 lines | 460 code | 46 blank | 10 comment | 14 complexity | f5b1caa969155176883d625fc88806fc MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, AGPL-3.0, 0BSD, Unlicense
  1. #
  2. begin
  3. require 'win32ole'
  4. rescue LoadError
  5. end
  6. require 'test/unit'
  7. if defined?(WIN32OLE)
  8. module CONST1
  9. end
  10. module CONST2
  11. end
  12. module TestCaseForDict
  13. def test_convert_bignum
  14. @dict1.add("a", 9999999999)
  15. @dict1.add("b", 999999999)
  16. @dict1.add("c", @dict1.item("b") * 10 + 9)
  17. assert_equal(9999999999, @dict1.item("a"))
  18. assert_equal(9999999999, @dict1.item("c"))
  19. end
  20. def test_add
  21. @dict1.add("a", 1000)
  22. assert_equal(1000, @dict1.item("a"))
  23. end
  24. def test_setproperty_equal_ended
  25. @dict1.compareMode = 1
  26. @dict1.add("one", 1)
  27. assert_equal(1, @dict1.item("ONE"))
  28. @dict2.add("one", 1)
  29. assert_nil(@dict2.item("ONE"))
  30. assert_equal(1, @dict2.item("one"))
  31. end
  32. def test_non_exist_property
  33. assert_raise(WIN32OLERuntimeError) {
  34. @dict1.unknown_property = 1
  35. }
  36. end
  37. def test_raise_message
  38. exc = assert_raise(WIN32OLERuntimeError) {
  39. @dict1.add
  40. }
  41. assert_match(/^\(in OLE method `add': \)/, exc.message) #`
  42. exc = assert_raise(WIN32OLERuntimeError) {
  43. @dict1._invoke(1, [], [])
  44. }
  45. assert_match(/^\(in OLE method `<dispatch id:1>': \)/, exc.message) #`
  46. exc = assert_raise(WIN32OLERuntimeError) {
  47. @dict1.compareMode = -1
  48. }
  49. assert_match(/^\(in setting property `compareMode': \)/, exc.message) #`
  50. end
  51. def test_no_method_error
  52. exc = assert_raise(NoMethodError) {
  53. @dict1.non_exist_method
  54. }
  55. assert_match(/non_exist_method/, exc.message)
  56. end
  57. def test_ole_methods
  58. methods = @dict1.ole_methods
  59. mnames = methods.collect {|m|
  60. m.name
  61. }
  62. assert(mnames.include?("Add"))
  63. end
  64. def test_ole_func_methods
  65. methods = @dict1.ole_func_methods
  66. mnames = methods.collect {|m|
  67. m.name
  68. }
  69. assert(mnames.include?("Add"))
  70. end
  71. def test_ole_put_methods
  72. methods = @dict1.ole_put_methods
  73. mnames = methods.collect {|m|
  74. m.name
  75. }
  76. assert(mnames.include?("CompareMode"))
  77. end
  78. def test_ole_get_methods
  79. methods = @dict1.ole_get_methods
  80. mnames = methods.collect {|m|
  81. m.name
  82. }
  83. assert(mnames.include?("Count"))
  84. end
  85. def test_ole_mehtod_help
  86. minfo = @dict1.ole_method_help("Add")
  87. assert_equal(2, minfo.size_params)
  88. end
  89. def test_ole_typelib
  90. tlib = @dict1.ole_typelib
  91. assert_equal("Microsoft Scripting Runtime", tlib.name);
  92. end
  93. def test_each
  94. @dict1.add("one", 1)
  95. @dict1.add("two", 2)
  96. i = 0
  97. @dict1.keys.each do |item|
  98. i += 1
  99. end
  100. assert_equal(2, i)
  101. end
  102. def test_bracket
  103. @dict1.add("foo", "FOO")
  104. assert_equal("FOO", @dict1.item("foo"))
  105. assert_equal("FOO", @dict1["foo"])
  106. end
  107. def test_bracket_equal
  108. @dict1.add("foo", "FOO")
  109. @dict1["foo"] = "BAR"
  110. assert_equal("BAR", @dict1["foo"])
  111. end
  112. def test_bracket_with_numkey
  113. @dict1.add(1, "ONE")
  114. @dict1.add(2, "two")
  115. assert_equal("ONE", @dict1[1])
  116. @dict1[2] = "TWO"
  117. assert_equal("TWO", @dict1[2])
  118. end
  119. def test_invoke_with_array
  120. @dict1.add("ary1", [1,2,3])
  121. assert_equal([1,2,3], @dict1["ary1"])
  122. @dict1.add("ary2", [[1,2,"a"], [3,4,"b"]])
  123. assert_equal([[1,2,"a"], [3,4,"b"]], @dict1["ary2"])
  124. @dict1.add("ary3", [[[1]]])
  125. assert_equal([[[1]]], @dict1["ary3"])
  126. @dict1.add("ary4", [[[1], [2], [3]], [[4], [5], [6]]])
  127. assert_equal([[[1],[2], [3]], [[4], [5], [6]]], @dict1["ary4"])
  128. end
  129. end
  130. class TestWin32OLE < Test::Unit::TestCase
  131. include TestCaseForDict
  132. def setup
  133. @dict1 = WIN32OLE.new('Scripting.Dictionary')
  134. @dict2 = WIN32OLE.new('Scripting.Dictionary')
  135. end
  136. def test_s_new
  137. assert_instance_of(WIN32OLE, @dict1)
  138. assert_instance_of(WIN32OLE, @dict2)
  139. end
  140. def test_s_new_exc
  141. assert_raise(TypeError) {
  142. WIN32OLE.new(1)
  143. }
  144. assert_raise(TypeError) {
  145. WIN32OLE.new("Scripting.Dictionary", 1)
  146. }
  147. end
  148. def test_s_new_DCOM
  149. rshell = WIN32OLE.new("Shell.Application")
  150. assert_instance_of(WIN32OLE, rshell)
  151. end
  152. def test_s_new_from_clsid
  153. shell = WIN32OLE.new("{13709620-C279-11CE-A49E-444553540000}")
  154. assert_instance_of(WIN32OLE, shell)
  155. exc = assert_raise(WIN32OLERuntimeError) {
  156. WIN32OLE.new("{000}")
  157. }
  158. assert_match(/unknown OLE server: `\{000\}'/, exc.message) #`
  159. end
  160. def test_s_connect
  161. obj = WIN32OLE.connect("winmgmts:")
  162. assert_instance_of(WIN32OLE, obj)
  163. end
  164. def test_s_connect_exc
  165. assert_raise(TypeError) {
  166. WIN32OLE.connect(1)
  167. }
  168. end
  169. def test_invoke_accept_symbol_hash_key
  170. fso = WIN32OLE.new('Scripting.FileSystemObject')
  171. afolder = fso.getFolder(".")
  172. bfolder = fso.getFolder({"FolderPath" => "."})
  173. cfolder = fso.getFolder({:FolderPath => "."})
  174. assert_equal(afolder.path, bfolder.path)
  175. assert_equal(afolder.path, cfolder.path)
  176. fso = nil
  177. end
  178. def test_setproperty
  179. installer = WIN32OLE.new("WindowsInstaller.Installer")
  180. record = installer.CreateRecord(2)
  181. # this is the way to set property with argument in Win32OLE.
  182. record.setproperty( "StringData", 1, 'dddd')
  183. assert_equal('dddd', record.StringData(1))
  184. end
  185. def test_ole_type
  186. fso = WIN32OLE.new('Scripting.FileSystemObject')
  187. tobj = fso.ole_type
  188. assert_match(/^IFileSystem/, tobj.name)
  189. end
  190. def test_ole_obj_help
  191. fso = WIN32OLE.new('Scripting.FileSystemObject')
  192. tobj = fso.ole_obj_help
  193. assert_match(/^IFileSystem/, tobj.name)
  194. end
  195. def test_invoke_hash_key_non_str_sym
  196. fso = WIN32OLE.new('Scripting.FileSystemObject')
  197. begin
  198. bfolder = fso.getFolder({1 => "."})
  199. assert(false)
  200. rescue TypeError
  201. assert(true)
  202. end
  203. fso = nil
  204. end
  205. def test_get_win32ole_object
  206. shell = WIN32OLE.new('Shell.Application')
  207. folder = shell.nameSpace(0)
  208. assert_instance_of(WIN32OLE, folder)
  209. end
  210. def test_invoke_accept_multi_hash_key
  211. shell = WIN32OLE.new('Shell.Application')
  212. folder = shell.nameSpace(0)
  213. item = folder.items.item(0)
  214. name = folder.getDetailsOf(item, 0)
  215. assert_equal(item.name, name)
  216. name = folder.getDetailsOf({:vItem => item, :iColumn => 0})
  217. assert_equal(item.name, name)
  218. name = folder.getDetailsOf({"vItem" => item, :iColumn => 0})
  219. assert_equal(item.name, name)
  220. end
  221. def test_ole_invoke_with_named_arg_last
  222. shell = WIN32OLE.new('Shell.Application')
  223. folder = shell.nameSpace(0)
  224. item = folder.items.item(0)
  225. name = folder.getDetailsOf(item, {:iColumn => 0})
  226. assert_equal(item.name, name)
  227. end
  228. def test__invoke
  229. shell=WIN32OLE.new('Shell.Application')
  230. assert_equal(shell.NameSpace(0).title, shell._invoke(0x60020002, [0], [WIN32OLE::VARIANT::VT_VARIANT]).title)
  231. end
  232. def test_ole_query_interface
  233. shell=WIN32OLE.new('Shell.Application')
  234. assert_raise(ArgumentError) {
  235. shell2 = shell.ole_query_interface
  236. }
  237. shell2 = shell.ole_query_interface('{A4C6892C-3BA9-11D2-9DEA-00C04FB16162}')
  238. assert_instance_of(WIN32OLE, shell2)
  239. end
  240. def test_ole_respond_to
  241. fso = WIN32OLE.new('Scripting.FileSystemObject')
  242. assert(fso.ole_respond_to?('getFolder'))
  243. assert(fso.ole_respond_to?('GETFOLDER'))
  244. assert(fso.ole_respond_to?(:getFolder))
  245. assert(!fso.ole_respond_to?('XXXXX'))
  246. assert_raise(TypeError) {
  247. assert_raise(fso.ole_respond_to?(1))
  248. }
  249. end
  250. def test_invoke
  251. fso = WIN32OLE.new('Scripting.FileSystemObject')
  252. assert(fso.invoke(:getFolder, "."))
  253. assert(fso.invoke('getFolder', "."))
  254. end
  255. def test_s_const_load
  256. assert(!defined?(CONST1::SsfWINDOWS))
  257. shell=WIN32OLE.new('Shell.Application')
  258. WIN32OLE.const_load(shell, CONST1)
  259. assert_equal(36, CONST1::SsfWINDOWS)
  260. assert(!defined?(CONST2::SsfWINDOWS))
  261. WIN32OLE.const_load("Microsoft Shell Controls And Automation", CONST2)
  262. assert_equal(36, CONST2::SsfWINDOWS)
  263. end
  264. def test_s_create_guid
  265. guid = WIN32OLE.create_guid
  266. assert_match(/^\{[A-Z0-9]{8}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{12}/,
  267. guid)
  268. end
  269. #
  270. # WIN32OLE.codepage is initialized according to Encoding.default_external.
  271. #
  272. # def test_s_codepage
  273. # assert_equal(WIN32OLE::CP_ACP, WIN32OLE.codepage)
  274. # end
  275. def test_s_codepage_set
  276. cp = WIN32OLE.codepage
  277. WIN32OLE.codepage = WIN32OLE::CP_UTF8
  278. assert_equal(WIN32OLE::CP_UTF8, WIN32OLE.codepage)
  279. WIN32OLE.codepage = cp
  280. end
  281. def test_s_codepage_changed
  282. cp = WIN32OLE.codepage
  283. fso = WIN32OLE.new("Scripting.FileSystemObject")
  284. fname = fso.getTempName
  285. begin
  286. obj = WIN32OLE_VARIANT.new([0x3042].pack("U*").force_encoding("UTF-8"))
  287. WIN32OLE.codepage = WIN32OLE::CP_UTF8
  288. assert_equal("\xE3\x81\x82".force_encoding("CP65001"), obj.value)
  289. begin
  290. WIN32OLE.codepage = 932 # Windows-31J
  291. rescue WIN32OLERuntimeError
  292. end
  293. if (WIN32OLE.codepage == 932)
  294. assert_equal("\x82\xA0".force_encoding("CP932"), obj.value)
  295. end
  296. begin
  297. WIN32OLE.codepage = 20932 # MS EUC-JP
  298. rescue WIN32OLERuntimeError
  299. end
  300. if (WIN32OLE.codepage == 20932)
  301. assert_equal("\xA4\xA2".force_encoding("CP20932"), obj.value)
  302. end
  303. WIN32OLE.codepage = cp
  304. file = fso.opentextfile(fname, 2, true)
  305. begin
  306. file.write [0x3042].pack("U*").force_encoding("UTF-8")
  307. ensure
  308. file.close
  309. end
  310. str = ""
  311. open(fname, "r:ascii-8bit") {|ifs|
  312. str = ifs.read
  313. }
  314. assert_equal("\202\240", str)
  315. # This test fail if codepage 20932 (euc) is not installed.
  316. begin
  317. WIN32OLE.codepage = 20932
  318. rescue WIN32OLERuntimeError
  319. end
  320. if (WIN32OLE.codepage == 20932)
  321. WIN32OLE.codepage = cp
  322. file = fso.opentextfile(fname, 2, true)
  323. begin
  324. file.write [164, 162].pack("c*").force_encoding("EUC-JP")
  325. ensure
  326. file.close
  327. end
  328. open(fname, "r:ascii-8bit") {|ifs|
  329. str = ifs.read
  330. }
  331. assert_equal("\202\240", str)
  332. end
  333. ensure
  334. WIN32OLE.codepage = cp
  335. if (File.exist?(fname))
  336. File.unlink(fname)
  337. end
  338. end
  339. end
  340. def test_cp51932
  341. cp = WIN32OLE.codepage
  342. begin
  343. obj = WIN32OLE_VARIANT.new([0x3042].pack("U*").force_encoding("UTF-8"))
  344. begin
  345. WIN32OLE.codepage = 51932
  346. rescue
  347. end
  348. if WIN32OLE.codepage == 51932
  349. assert_equal("\xA4\xA2".force_encoding("CP51932"), obj.value)
  350. end
  351. ensure
  352. WIN32OLE.codepage = cp
  353. end
  354. end
  355. def test_s_locale
  356. assert_equal(WIN32OLE::LOCALE_SYSTEM_DEFAULT, WIN32OLE.locale)
  357. end
  358. def test_s_locale_set
  359. begin
  360. begin
  361. WIN32OLE.locale = 1041
  362. rescue WIN32OLERuntimeError
  363. STDERR.puts("\n#{__FILE__}:#{__LINE__}:#{self.class.name}.test_s_locale_set is skipped(Japanese locale is not installed)")
  364. return
  365. end
  366. assert_equal(1041, WIN32OLE.locale)
  367. WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT
  368. assert_raise(WIN32OLERuntimeError) {
  369. WIN32OLE.locale = 111
  370. }
  371. assert_equal(WIN32OLE::LOCALE_SYSTEM_DEFAULT, WIN32OLE.locale)
  372. ensure
  373. WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT
  374. end
  375. end
  376. def test_s_locale_change
  377. begin
  378. begin
  379. WIN32OLE.locale = 0x0411
  380. rescue WIN32OLERuntimeError
  381. end
  382. if WIN32OLE.locale == 0x0411
  383. obj = WIN32OLE_VARIANT.new("\\100,000", WIN32OLE::VARIANT::VT_CY)
  384. assert_equal("100000", obj.value)
  385. assert_raise(WIN32OLERuntimeError) {
  386. obj = WIN32OLE_VARIANT.new("$100.000", WIN32OLE::VARIANT::VT_CY)
  387. }
  388. else
  389. STDERR.puts("\n#{__FILE__}:#{__LINE__}:#{self.class.name}.test_s_locale_change is skipped(Japanese locale is not installed)")
  390. end
  391. begin
  392. WIN32OLE.locale = 1033
  393. rescue WIN32OLERuntimeError
  394. end
  395. if WIN32OLE.locale == 1033
  396. obj = WIN32OLE_VARIANT.new("$100,000", WIN32OLE::VARIANT::VT_CY)
  397. assert_equal("100000", obj.value)
  398. else
  399. STDERR.puts("\n#{__FILE__}:#{__LINE__}:#{self.class.name}.test_s_locale_change is skipped(US English locale is not installed)")
  400. end
  401. ensure
  402. WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT
  403. end
  404. end
  405. def test_const_CP_ACP
  406. assert_equal(0, WIN32OLE::CP_ACP)
  407. end
  408. def test_const_CP_OEMCP
  409. assert_equal(1, WIN32OLE::CP_OEMCP)
  410. end
  411. def test_const_CP_MACCP
  412. assert_equal(2, WIN32OLE::CP_MACCP)
  413. end
  414. def test_const_CP_THREAD_ACP
  415. assert_equal(3, WIN32OLE::CP_THREAD_ACP)
  416. end
  417. def test_const_CP_SYMBOL
  418. assert_equal(42, WIN32OLE::CP_SYMBOL)
  419. end
  420. def test_const_CP_UTF7
  421. assert_equal(65000, WIN32OLE::CP_UTF7)
  422. end
  423. def test_const_CP_UTF8
  424. assert_equal(65001, WIN32OLE::CP_UTF8)
  425. end
  426. def test_const_LOCALE_SYSTEM_DEFAULT
  427. assert_equal(0x0800, WIN32OLE::LOCALE_SYSTEM_DEFAULT);
  428. end
  429. def test_const_LOCALE_USER_DEFAULT
  430. assert_equal(0x0400, WIN32OLE::LOCALE_USER_DEFAULT);
  431. end
  432. end
  433. # test of subclass of WIN32OLE
  434. class MyDict < WIN32OLE
  435. def MyDict.new
  436. super('Scripting.Dictionary')
  437. end
  438. end
  439. class TestMyDict < Test::Unit::TestCase
  440. include TestCaseForDict
  441. def setup
  442. @dict1 = MyDict.new
  443. @dict2 = MyDict.new
  444. end
  445. def test_s_new
  446. assert_instance_of(MyDict, @dict1)
  447. assert_instance_of(MyDict, @dict2)
  448. end
  449. end
  450. end