PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_nameutils.rb

https://github.com/wanabe/bitclust
Ruby | 391 lines | 354 code | 31 blank | 6 comment | 0 complexity | 5e97a9350ac958244b39617468d0ce92 MD5 | raw file
  1. require 'test/unit'
  2. require 'bitclust/nameutils'
  3. class TestNameUtils < Test::Unit::TestCase
  4. include BitClust::NameUtils
  5. data("_builtin" => [true, "_builtin"],
  6. "fileutils" => [true, "fileutils"],
  7. "socket" => [true, "socket"],
  8. "open-uri" => [true, "open-uri"],
  9. "net/http" => [true, "net/http"],
  10. "racc/cparse" => [true, "racc/cparse"],
  11. "test/unit/testcase" => [true, "test/unit/testcase"],
  12. "empty string" => [false, ""],
  13. "following space" => [false, "fileutils "],
  14. "leading space" => [false, " fileutils"],
  15. "split by space" => [false, "file utils"],
  16. "following new line" => [false, "fileutils\n"],
  17. "folowing tab" => [false, "fileutils\t"],
  18. "with extension .rb" => [false, "fileutils.rb"],
  19. "CamelCase with extension .rb" => [false, "English.rb"],
  20. "with extension .so" => [false, "socket.so"],
  21. "sub library with extension .rb" => [false, "net/http.rb"],
  22. "sub library with extension .so" => [false, "racc/cparse.so"])
  23. def test_libname?(data)
  24. expected, target = data
  25. assert_equal(expected, libname?(target))
  26. end
  27. data("_builtin" => ["_builtin", "_builtin"],
  28. "fileutils" => ["fileutils", "fileutils"],
  29. "socket" => ["socket", "socket"],
  30. "English" => ["English", "English"],
  31. "open-uri" => ["open=2duri", "open-uri"],
  32. "net/http" => ["net.http", "net/http"],
  33. "racc/cparse" => ["racc.cparse", "racc/cparse"],
  34. "test/unit/testcase" => ["test.unit.testcase", "test/unit/testcase"])
  35. def test_libname2id(data)
  36. expected, target = data
  37. assert_equal(expected, libname2id(target))
  38. end
  39. data("_builtin" => ["_builtin", "_builtin"],
  40. "fileutils" => ["fileutils", "fileutils"],
  41. "socket" => ["socket", "socket"],
  42. "English" => ["English", "English"],
  43. "open=2duri" => ["open-uri", "open=2duri"],
  44. "net.http" => ["net/http", "net.http"],
  45. "racc.cparse" => ["racc/cparse", "racc.cparse"],
  46. "test.unit.testcase" => ["test/unit/testcase", "test.unit.testcase"])
  47. def test_libid2name(data)
  48. expected, target = data
  49. assert_equal(expected, libid2name(target))
  50. end
  51. data("fatal" => [true, "fatal"],
  52. "Array" => [true, "Array"],
  53. "String" => [true, "String"],
  54. "Net::HTTP" => [true, "Net::HTTP"],
  55. "Test::Unit::TestCase" => [true, "Test::Unit::TestCase"],
  56. "ARGF.class" => [true, "ARGF.class"],
  57. "Complex::compatible" => [true, "Complex::compatible"],
  58. "empty string" => [false, ""],
  59. "following space" => [false, "Array "],
  60. "leading space" => [false, " Array"],
  61. "split by space" => [false, "Test Case"],
  62. "following new line" => [false, "TestCase\n"],
  63. "leading tab" => [false, "\tTestCase"],
  64. "small case" => [false, "string"],
  65. "camelCase" => [false, "stringScanner"],
  66. "libname" => [false, "net/http"],
  67. "libid" => [false, "net.http"],
  68. "libname with '-'" => [false, "open-uri"])
  69. def test_classname?(data)
  70. expected, target = data
  71. assert_equal(expected, classname?(target))
  72. end
  73. data("Array" => ["Array", "Array"],
  74. "String" => ["String", "String"],
  75. "Net::HTTP" => ["Net=HTTP", "Net::HTTP"],
  76. "Test::Unit::TestCase" => ["Test=Unit=TestCase", "Test::Unit::TestCase"],
  77. "ARGF.class" => ["ARGF.class", "ARGF.class"],
  78. "Complex::compatible" => ["Complex=compatible", "Complex::compatible"])
  79. def test_classname2id(data)
  80. expected, target = data
  81. assert_equal(expected, classname2id(target))
  82. end
  83. data("Array" => ["Array", "Array"],
  84. "String" => ["String", "String"],
  85. "Net=HTTP" => ["Net::HTTP", "Net=HTTP"],
  86. "Test=Unit=TestCase" => ["Test::Unit::TestCase", "Test=Unit=TestCase"],
  87. "ARGF.class" => ["ARGF.class", "ARGF.class"],
  88. "Complex=compatible" => ["Complex::compatible", "Complex=compatible"])
  89. def test_classid2name(data)
  90. expected, target = data
  91. assert_equal(expected, classid2name(target))
  92. end
  93. data("String#index" => [true, "String#index"],
  94. "CGI#accept" => [true, "CGI#accept"],
  95. "Net::HTTP#ca_path" => [true, "Net::HTTP#ca_path"],
  96. "FileUtils.#cp" => [true, "FileUtils.#cp"],
  97. "ARGF.class#path" => [true, "ARGF.class#path"],
  98. "ARGF.class" => [false, "ARGF.class"])
  99. def test_method_spec?(data)
  100. expected, target = data
  101. assert_equal(expected, method_spec?(target))
  102. end
  103. data("String/i.index._builtin" => ["String#index", "String/i.index._builtin"],
  104. "CGI/i.accept.cgi" => ["CGI#accept", "CGI/i.accept.cgi"],
  105. "Net=HTTP/i.ca_path.net.http" => ["Net::HTTP#ca_path", "Net=HTTP/i.ca_path.net.http"],
  106. "FileUtils/m.cp.fileutils" => ["FileUtils.#cp", "FileUtils/m.cp.fileutils"],
  107. "ARGF.class/i.filename.ARGF" => ["ARGF.class#filename", "ARGF.class/i.filename.ARGF"])
  108. def test_methodid2spec(data)
  109. expected, target = data
  110. assert_equal(expected, methodid2specstring(target))
  111. end
  112. data("String/i.index._builtin" => ["_builtin", "String/i.index._builtin"],
  113. "CGI/i.accept.cgi" => ["cgi", "CGI/i.accept.cgi"],
  114. "Net=HTTP/i.ca_path.net.http" => ["net.http", "Net=HTTP/i.ca_path.net.http"],
  115. "OpenURI/m.open.open=2duri" => ["open=2duri", "OpenURI/m.open.open=2duri"])
  116. def test_methodid2libid(data)
  117. expected, target = data
  118. assert_equal(expected, methodid2libid(target))
  119. end
  120. data("String/i.index._builtin" => ["String", "String/i.index._builtin"],
  121. "CGI/i.accept.cgi" => ["CGI", "CGI/i.accept.cgi"],
  122. "Net=HTTP/i.ca_path.net.http" => ["Net=HTTP", "Net=HTTP/i.ca_path.net.http"])
  123. def test_methodid2classid(data)
  124. expected, target = data
  125. assert_equal(expected, methodid2classid(target))
  126. end
  127. data("String/i.index._builtin" => [:instance_method, "String/i.index._builtin"],
  128. "CGI/i.accept.cgi" => [:instance_method, "CGI/i.accept.cgi"],
  129. "Net=HTTP/i.ca_path.net.http" => [:instance_method, "Net=HTTP/i.ca_path.net.http"],
  130. "String/s.new._builtin" => [:singleton_method, "String/s.new._builtin"])
  131. def test_methodid2typename(data)
  132. expected, target = data
  133. assert_equal(expected, methodid2typename(target))
  134. end
  135. data("String/i.index._builtin" => ["index", "String/i.index._builtin"],
  136. "CGI/i.accept.cgi" => ["accept", "CGI/i.accept.cgi"],
  137. "Net=HTTP/i.ca_path.net.http" => ["ca_path", "Net=HTTP/i.ca_path.net.http"])
  138. def test_methodid2mname(data)
  139. expected, target = data
  140. assert_equal(expected, methodid2mname(target))
  141. end
  142. data("index" => [true, "index"],
  143. "accept" => [true, "accept"],
  144. "get" => [true, "get"],
  145. "Array" => [true, "Array"],
  146. "getIndex" => [true, "getIndex"],
  147. "PROXY" => [true, "PROXY"],
  148. "HTTP_PROXY" => [true, "HTTP_PROXY"],
  149. "gsub!" => [true, "gsub!"],
  150. "empty? " => [true, "empty?"],
  151. "instance_eval" => [true, "instance_eval"],
  152. "__send" => [true, "__send"],
  153. "__send__" => [true, "__send__"],
  154. "__send!" => [true, "__send!"],
  155. "+" => [true, "+"],
  156. "-" => [true, "-"],
  157. "*" => [true, "*"],
  158. "/" => [true, "/"],
  159. "&" => [true, "&"],
  160. "|" => [true, "|"],
  161. "^" => [true, "^"],
  162. "`" => [true, "`"],
  163. ">>" => [true, ">>"],
  164. "<<" => [true, "<<"],
  165. "+@" => [true, "+@"],
  166. "-@" => [true, "-@"],
  167. "!" => [true, "!"],
  168. "!@" => [true, "!@"],
  169. "~" => [true, "~"],
  170. "**" => [true, "**"],
  171. "<" => [true, "<"],
  172. ">" => [true, ">"],
  173. "<=" => [true, "<="],
  174. ">=" => [true, ">="],
  175. "==" => [true, "=="],
  176. "===" => [true, "==="],
  177. "=~" => [true, "=~"],
  178. "[]" => [true, "[]"],
  179. "[]=" => [true, "[]="],
  180. "" => [false, ""],
  181. "!=" => [true, "!="],
  182. "!~" => [true, "!~"],
  183. "&&" => [false, "&&"],
  184. "||" => [false, "||"],
  185. "++" => [false, "++"],
  186. ">>>" => [false, ">>>"],
  187. "***" => [false, "***"],
  188. "====" => [false, "===="],
  189. "#accept" => [false, "#accept"],
  190. ".new" => [false, ".new"],
  191. ".#cp" => [false, ".#cp"],
  192. "$gvar" => [false, "$gvar"],
  193. "CGI#accept" => [false, "CGI#accept"],
  194. "String.new" => [false, "String.new"],
  195. "Net::HTTP.get" => [false, "Net::HTTP.get"],
  196. "Net::HTTP.new" => [false, "Net::HTTP.new"])
  197. def test_methodname?(data)
  198. expected, target = data
  199. assert_equal(expected, methodname?(target))
  200. end
  201. def test_build_method_id
  202. assert_equal "String/i.index._builtin",
  203. build_method_id("_builtin", "String", :instance_method, "index")
  204. end
  205. # library private
  206. #def test_split_method_id
  207. # assert_equal ["String", "i", "index", "_builtin"],
  208. # split_method_id("String/i.index._builtin")
  209. #end
  210. data(:instance_method => [true, :instance_method],
  211. :singleton_method => [true, :singleton_method],
  212. :module_function => [true, :module_function],
  213. :constant => [true, :constant],
  214. :special_variable => [true, :special_variable],
  215. :instance_eval => [false, :instance_eval],
  216. :instance => [false, :instance],
  217. :singleton => [false, :singleton],
  218. "i" => [false, "i"],
  219. "s" => [false, "s"],
  220. :i => [false, :i],
  221. :s => [false, :s])
  222. def test_typename?(data)
  223. expected, target = data
  224. assert_equal(expected, typename?(target))
  225. end
  226. data do
  227. data_set = {}
  228. typemarks = [".", "#", ".#", "$", "::"]
  229. typemarks.each do |mark|
  230. data_set[mark] = [true, mark]
  231. end
  232. #marks = (0..255).map {|a| (0..255).map {|b| a.chr + b.chr } }.flatten
  233. marks = (0..127).map {|a| (0..127).map {|b| a.chr + b.chr } }.flatten
  234. (marks - typemarks).each do |m|
  235. data_set[m] = [false, m]
  236. end
  237. data_set
  238. end
  239. def test_typemark?(data)
  240. expected, target = data
  241. assert_equal(expected, typemark?(target))
  242. end
  243. data do
  244. data_set = {}
  245. typechars = %w( i s m c v )
  246. typechars.each do |char|
  247. data_set[char] = [true, char]
  248. end
  249. ((0..255).map {|b| b.chr } - typechars).each do |char|
  250. data_set[char] = [false, char]
  251. end
  252. data_set
  253. end
  254. def test_typechar?(data)
  255. expected, target = data
  256. assert_equal(expected, typechar?(target))
  257. end
  258. data(:singleton_method => ["s", :singleton_method],
  259. :instance_method => ["i", :instance_method],
  260. :module_function => ["m", :module_function],
  261. :constant => ["c", :constant],
  262. :special_variable => ["v", :special_variable])
  263. def test_typename2char(data)
  264. expected, target = data
  265. assert_equal(expected, typename2char(target))
  266. end
  267. data("s" => [:singleton_method, "s"],
  268. "i" => [:instance_method, "i"],
  269. "m" => [:module_function, "m"],
  270. "c" => [:constant, "c"],
  271. "v" => [:special_variable, "v"])
  272. def test_typechar2name(data)
  273. expected, target = data
  274. assert_equal(expected, typechar2name(target))
  275. end
  276. data("." => ["s", "."],
  277. "#" => ["i", "#"],
  278. ".#" => ["m", ".#"],
  279. "::" => ["c", "::"],
  280. "$" => ["v", "$"])
  281. def test_typemark2char(data)
  282. expected, target = data
  283. assert_equal(expected, typemark2char(target))
  284. end
  285. data("s" => [".", "s"],
  286. "i" => ["#", "i"],
  287. "m" => [".#", "m"],
  288. "c" => ["::", "c"],
  289. "v" => ["$", "v"])
  290. def test_typechar2mark(data)
  291. expected, target = data
  292. assert_equal(expected, typechar2mark(target))
  293. end
  294. data("Array" => ["Array", "Array"],
  295. "String" => ["String", "String"],
  296. "index" => ["index", "index"],
  297. "*" => ["=2a", "*"],
  298. "**" => ["=2a=2a", "**"],
  299. "open-uri" => ["open=2duri", "open-uri"],
  300. "net.http" => ["net=2ehttp", "net.http"])
  301. def test_encodename_url(data)
  302. expected, target = data
  303. assert_equal(expected, encodename_url(target))
  304. end
  305. data("Array" => ["Array", "Array"],
  306. "String" => ["String", "String"],
  307. "index" => ["index", "index"],
  308. "=2a" => ["*", "=2a"],
  309. "=2a=2a" => ["**", "=2a=2a"],
  310. "open=2duri" => ["open-uri", "open=2duri"],
  311. "net=2ehttp" => ["net.http", "net=2ehttp"])
  312. def test_decodename_url(data)
  313. expected, target = data
  314. assert_equal(expected, decodename_url(target))
  315. end
  316. data("Array" => ["-array", "Array"],
  317. "String" => ["-string", "String"],
  318. "CGI" => ["-c-g-i", "CGI"],
  319. "=2a" => ["=2a", "=2a"],
  320. "=2a=2a" => ["=2a=2a", "=2a=2a"],
  321. "open=2duri" => ["open=2duri", "open=2duri"],
  322. "Net=HTTP" => ["-net=-h-t-t-p", "Net=HTTP"])
  323. def test_encodeid(data)
  324. expected, target = data
  325. assert_equal(expected, encodeid(target))
  326. end
  327. data("-array" => ["Array", "-array"],
  328. "-string" => ["String", "-string"],
  329. "-c-g-i" => ["CGI", "-c-g-i"],
  330. "=2a" => ["=2a", "=2a"],
  331. "=2a=2a" => ["=2a=2a", "=2a=2a"],
  332. "open=2duri" => ["open=2duri", "open=2duri"],
  333. "-net=-h-t-t-p" => ["Net=HTTP", "-net=-h-t-t-p"])
  334. def test_decodeid(data)
  335. expected, target = data
  336. assert_equal(expected, decodeid(target))
  337. end
  338. data("Array" => ["-array", "Array"],
  339. "String" => ["-string", "String"],
  340. "index" => ["index", "index"],
  341. "*" => ["=2a", "*"],
  342. "**" => ["=2a=2a", "**"],
  343. "open-uri" => ["open=2duri", "open-uri"],
  344. "net.http" => ["net=2ehttp", "net.http"])
  345. def test_encodename_fs(data)
  346. expected, target = data
  347. assert_equal(expected, encodename_fs(target))
  348. end
  349. data("-array" => ["Array", "-array"],
  350. "-string" => ["String", "-string"],
  351. "index" => ["index", "index"],
  352. "=2a" => ["*", "=2a"],
  353. "=2a=2a" => ["**", "=2a=2a"],
  354. "open=2duri" => ["open-uri", "open=2duri"],
  355. "net=2ehttp" => ["net.http", "net=2ehttp"])
  356. def test_decodename_fs(data)
  357. expected, target = data
  358. assert_equal(expected, decodename_fs(target))
  359. end
  360. end