/test/lib/io/path_name/test_microsoft_path.e

http://github.com/tybor/Liberty · Specman e · 392 lines · 322 code · 25 blank · 45 comment · 0 complexity · 1ee8465e4a1a9a27beb011e5bc78161e MD5 · raw file

  1. -- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries.
  2. -- See the Copyright notice at the end of this file.
  3. --
  4. class TEST_MICROSOFT_PATH
  5. -- Test for MICROSOFT_PATH_NAME (adapted from a test by Daniel Moisset)
  6. insert
  7. EIFFELTEST_TOOLS
  8. rename assert as et_assert,
  9. label_assert as assert
  10. end
  11. create {}
  12. make
  13. feature {}
  14. make
  15. do
  16. set_up
  17. test_count
  18. tear_down
  19. set_up
  20. test_last
  21. tear_down
  22. set_up
  23. test_extension
  24. tear_down
  25. set_up
  26. test_is_absolute
  27. tear_down
  28. set_up
  29. test_plus
  30. tear_down
  31. set_up
  32. test_to_absolute
  33. tear_down
  34. set_up
  35. test_normalize
  36. tear_down
  37. set_up
  38. test_is_normalized
  39. tear_down
  40. set_up
  41. test_remove_last
  42. tear_down
  43. set_up
  44. test_add_last
  45. tear_down
  46. set_up
  47. test_expand_user
  48. tear_down
  49. --set_up
  50. --test_expand_explicit_user
  51. --tear_down
  52. --set_up
  53. --test_common_prefix
  54. --tear_down
  55. set_up
  56. test_expand_variables
  57. tear_down
  58. set_up
  59. test_join_invalidates_cache
  60. tear_down
  61. end
  62. set_up
  63. local
  64. sys: SYSTEM; bd: BASIC_DIRECTORY
  65. do
  66. create empty.make_empty
  67. create single.make_from_string("xyz.png")
  68. create root.make_root
  69. create absolute.make_from_string("\xxx.d\y.txt")
  70. create multi_slash.make_from_string("a\\\\b")
  71. create final_slash.make_from_string("a.d\b\")
  72. create simple.make_from_string("a\b\ccc\d.")
  73. create drive.make_from_string("c:")
  74. create drive_root.make_from_string("c:\")
  75. create drive_path.make_from_string("c:\path")
  76. create drive2_root.make_from_string("d:\")
  77. sys.set_environment_variable("HOME", "\home\dmoisset")
  78. path_for_tests := bd.current_working_directory.out
  79. end
  80. path_for_tests: STRING
  81. -- Path where tests are being run. Used to restore state
  82. -- between tests, not for the tests themselvesl.
  83. tear_down
  84. local
  85. sys: SYSTEM; bd: BASIC_DIRECTORY
  86. do
  87. sys.set_environment_variable("HOME", "\home\dmoisset")
  88. bd.change_current_working_directory(path_for_tests)
  89. end
  90. empty, single, root, absolute, multi_slash, final_slash, simple, drive, drive_root, drive_path, drive2_root: MICROSOFT_PATH_NAME
  91. mkpath (s: STRING): MICROSOFT_PATH_NAME
  92. do
  93. create Result.make_from_string(s)
  94. end
  95. feature {}
  96. assert_integers_equal (name: STRING; i1, i2: INTEGER)
  97. do
  98. assert(name, i1 = i2)
  99. end
  100. assert_equal (name: STRING; o1, o2: STRING)
  101. do
  102. assert(name, o1.is_equal(o2))
  103. end
  104. feature {} -- Tests
  105. path: MICROSOFT_PATH_NAME
  106. -- MISSING exists, same_file, expand_shellouts
  107. test_count
  108. do
  109. assert_integers_equal("empty", 0, empty.count)
  110. assert_integers_equal("single", 1, single.count)
  111. assert_integers_equal("root", 0, root.count)
  112. assert_integers_equal("absolute", 2, absolute.count)
  113. assert_integers_equal("multi_slash", 2, multi_slash.count)
  114. assert_integers_equal("final_slash", 3, final_slash.count)
  115. assert_integers_equal("simple", 4, simple.count)
  116. end
  117. test_last
  118. do
  119. assert_equal("single", "xyz.png", single.last)
  120. assert_equal("absolute", "y.txt", absolute.last)
  121. assert_equal("multi_slash", "b", multi_slash.last)
  122. assert_equal("final_slash", "", final_slash.last)
  123. assert_equal("simple", "d.", simple.last)
  124. end
  125. test_extension
  126. do
  127. assert_equal("single", ".png", single.extension)
  128. assert_equal("absolute", ".txt", absolute.extension)
  129. assert_equal("multi_slash", "", multi_slash.extension)
  130. assert_equal("final_slash", "", final_slash.extension)
  131. assert_equal("simple", ".", simple.extension)
  132. end
  133. test_is_absolute
  134. do
  135. assert("empty", not empty.is_absolute)
  136. assert("single", not single.is_absolute)
  137. assert("root", root.is_absolute)
  138. assert("absolute", absolute.is_absolute)
  139. assert("multi_slash", not multi_slash.is_absolute)
  140. assert("final_slash", not final_slash.is_absolute)
  141. assert("simple", not simple.is_absolute)
  142. end
  143. test_plus
  144. do
  145. assert_equal("empty_empty", "", (empty + empty).to_string)
  146. assert_equal("empty_simple", simple.to_string, (empty + simple).to_string)
  147. assert_equal("empty_absolute", absolute.to_string, (empty + absolute).to_string)
  148. assert_equal("simple_absolute", absolute.to_string, (simple + absolute).to_string)
  149. assert_equal("simple_simple", "a\b\ccc\d.\a\b\ccc\d.", (simple + simple).to_string)
  150. assert_equal("simple_empty", "a\b\ccc\d.", (simple + empty).to_string)
  151. -- Interesting cases of join with drives
  152. assert_equal("drive_absolute", "c:\xxx.d\y.txt", (drive + absolute).to_string)
  153. assert_equal("driveroot_absolute", "c:\xxx.d\y.txt", (drive_root + absolute).to_string)
  154. assert_equal("drivepath_absolute", absolute.to_string, (drive_path + absolute).to_string)
  155. assert_equal("drive_drive2", drive2_root.to_string, (drive + drive2_root).to_string)
  156. assert_equal("driveroot_drive2", drive2_root.to_string, (drive_root + drive2_root).to_string)
  157. -- Simpler cases of drive+something
  158. assert_equal("drivepath_single", "c:\path\xyz.png", (drive_path + single).to_string)
  159. end
  160. test_to_absolute
  161. local
  162. bd: BASIC_DIRECTORY
  163. do
  164. bd.change_current_working_directory("/usr/bin")
  165. empty.to_absolute
  166. assert_equal("empty", "\usr\bin", empty.to_string)
  167. single.to_absolute
  168. assert_equal("single", "\usr\bin\xyz.png", single.to_string)
  169. root.to_absolute
  170. assert_equal("root", "\", root.to_string)
  171. absolute.to_absolute
  172. assert_equal("absolute", "\xxx.d\y.txt", absolute.to_string)
  173. multi_slash.to_absolute
  174. assert_equal("multi_slash", "\usr\bin\a\b", multi_slash.to_string)
  175. final_slash.to_absolute
  176. assert_equal("final_slash", "\usr\bin\a.d\b", final_slash.to_string)
  177. simple.to_absolute
  178. assert_equal("simple", "\usr\bin\a\b\ccc\d.", simple.to_string)
  179. end
  180. test_normalize
  181. local
  182. p: MICROSOFT_PATH_NAME
  183. do
  184. empty.normalize
  185. assert_equal("empty", ".", empty.to_string)
  186. multi_slash.normalize
  187. assert_equal("multi_slash", "a\b", multi_slash.to_string)
  188. final_slash.normalize
  189. assert_equal("final_slash", "a.d\b", final_slash.to_string)
  190. simple.normalize
  191. assert_equal("simple", "a\b\ccc\d.", simple.to_string)
  192. p := mkpath("\\server\share")
  193. p.normalize
  194. assert_equal("unc_path", "\\server\share", p.to_string)
  195. p := mkpath("\..\x\y")
  196. p.normalize
  197. assert_equal("parent_root", "\x\y", p.to_string)
  198. p := mkpath("..\z\..\x\y")
  199. p.normalize
  200. assert_equal("parent_after", "..\x\y", p.to_string)
  201. p := mkpath("\x\y\..")
  202. p.normalize
  203. assert_equal("parent_last", "\x", p.to_string)
  204. p := mkpath(".\y")
  205. p.normalize
  206. assert_equal("this_first", "y", p.to_string)
  207. p := mkpath("..\.\..\x\y")
  208. p.normalize
  209. assert_equal("this_middle", "..\..\x\y", p.to_string)
  210. p := mkpath("x\y\.")
  211. p.normalize
  212. assert_equal("this_last", "x\y", p.to_string)
  213. p := mkpath("\.")
  214. p.normalize
  215. assert_equal("root_this", "\", p.to_string)
  216. p := mkpath("c:")
  217. p.normalize
  218. assert_equal("drive_only", "c:.", p.to_string)
  219. end
  220. test_is_normalized
  221. do
  222. assert("empty", not empty.is_normalized)
  223. assert("single", single.is_normalized)
  224. assert("root", root.is_normalized)
  225. assert("absolute", absolute.is_normalized)
  226. assert("multi_slash", not multi_slash.is_normalized)
  227. assert("final_slash", not final_slash.is_normalized)
  228. assert("simple", simple.is_normalized)
  229. assert("unix_slash", not mkpath("/usr/bin").is_normalized)
  230. assert("double_root", mkpath("\\").is_normalized)
  231. assert("triple_root", mkpath("\\\").is_normalized)
  232. assert("drive_double_root", not mkpath("c:\\").is_normalized)
  233. assert("parent_root", not mkpath("\..\x/y").is_normalized)
  234. assert("parent_first", mkpath("..\..\x\y").is_normalized)
  235. assert("parent_after", not mkpath("..\z\..\x\y").is_normalized)
  236. assert("parent_only", mkpath("..\..").is_normalized)
  237. assert("parent_last", not mkpath("\x\y\..").is_normalized)
  238. assert("this_first", not mkpath("./y").is_normalized)
  239. assert("this_middle", not mkpath("..\.\..\x\y").is_normalized)
  240. assert("this_last", not mkpath("x\y\.").is_normalized)
  241. assert("this_only", mkpath(".").is_normalized)
  242. assert("root_this", not mkpath("\.").is_normalized)
  243. end
  244. test_remove_last
  245. do
  246. single.remove_last
  247. assert_equal("single", "", single.to_string)
  248. absolute.remove_last
  249. assert_equal("absolute", "\xxx.d", absolute.to_string)
  250. multi_slash.remove_last
  251. assert_equal("multi_slash", "a", multi_slash.to_string)
  252. final_slash.remove_last
  253. assert_equal("final_slash", "a.d\b", final_slash.to_string)
  254. simple.remove_last
  255. assert_equal("simple", "a\b\ccc", simple.to_string)
  256. drive_path.remove_last
  257. assert_equal("drive_path", "c:\", drive_path.to_string)
  258. end
  259. test_add_last
  260. do
  261. assert_equal("empty", "foo", (empty / "foo").to_string)
  262. assert_equal("single", "xyz.png\foo", (single / "foo").to_string)
  263. assert_equal("root", "\foo", (root / "foo").to_string)
  264. assert_equal("absolute", "\xxx.d\y.txt\foo", (absolute / "foo").to_string)
  265. assert_equal("multi_slash", "a\\\\b\foo", (multi_slash / "foo").to_string)
  266. assert_equal("final_slash", "a.d\b\foo", (final_slash / "foo").to_string)
  267. assert_equal("simple", "a\b\ccc\d.\foo", (simple / "foo").to_string)
  268. end
  269. test_expand_user
  270. local
  271. p: MICROSOFT_PATH_NAME; sys: SYSTEM
  272. do
  273. p := mkpath("\usr\local\bin~")
  274. p.expand_user
  275. assert_equal("non_initial_tilde", "\usr\local\bin~", p.to_string)
  276. p := mkpath("~\bin")
  277. p.expand_user
  278. assert_equal("home_subdir", "\home\dmoisset\bin", p.to_string)
  279. p := mkpath("~")
  280. p.expand_user
  281. assert_equal("home_only", "\home\dmoisset", p.to_string)
  282. sys.set_environment_variable("HOME", "\boot")
  283. p := mkpath("~\vmlinuz")
  284. p.expand_user
  285. assert_equal("home_from_$HOME", "\boot\vmlinuz", p.to_string)
  286. end
  287. -- test_expand_explicit_user
  288. -- do
  289. -- crash -- not done yet
  290. -- end
  291. -- test_common_prefix
  292. -- do
  293. -- assert_equal ("empty", "", (mkpath("").common_prefix(mkpath(""))).to_string)
  294. -- assert_equal ("root", "/", (mkpath("/bin").common_prefix(mkpath("/usr/bin"))).to_string)
  295. -- assert_equal ("partial", "/b", (mkpath("/bin").common_prefix(mkpath("/boot"))).to_string)
  296. -- assert_equal ("substring", "/usr/bin", (mkpath("/usr/bin/X11R6").common_prefix(mkpath("/usr/bin"))).to_string)
  297. -- assert_equal ("complete", "/usr/bin/", (mkpath("/usr/bin/X11R6").common_prefix(mkpath("/usr/bin/foo"))).to_string)
  298. -- end
  299. test_expand_variables
  300. local
  301. p1, p2: MICROSOFT_PATH_NAME; sys: SYSTEM
  302. do
  303. p1 := mkpath("~\foo")
  304. p2 := mkpath("$HOME\foo")
  305. p1.expand_user
  306. p2.expand_variables
  307. assert_equal("expand_simple", p1.to_string, p2.to_string)
  308. p2 := mkpath("${HOME}\foo")
  309. p2.expand_variables
  310. assert_equal("expand_bracketed", p1.to_string, p2.to_string)
  311. p1 := mkpath("~")
  312. p2 := mkpath("$HOME")
  313. p1.expand_user
  314. p2.expand_variables
  315. assert_equal("expand_alone", p1.to_string, p2.to_string)
  316. p2 := mkpath("x$foo${bar}y")
  317. sys.set_environment_variable("foo", "11\11")
  318. sys.set_environment_variable("bar", "22\22")
  319. p2.expand_variables
  320. assert_equal("expand_several", "x11\1122\22y", p2.to_string)
  321. p2 := mkpath("x$foo${bar}y")
  322. sys.set_environment_variable("foo", "$bar")
  323. sys.set_environment_variable("bar", "$foo$bar")
  324. p2.expand_variables
  325. assert_equal("expand_nonnrecursive", "x$bar$foo$bary", p2.to_string)
  326. p2 := mkpath("$doesnotexist${this_variable_is_fake}")
  327. p2.expand_variables
  328. assert_equal("non_existant", "$doesnotexist${this_variable_is_fake}", p2.to_string)
  329. p2 := mkpath("3$.")
  330. p2.expand_variables
  331. assert_equal("single_dollar", "3$.", p2.to_string)
  332. p2 := mkpath("${HOME")
  333. p2.expand_variables
  334. assert_equal("incomplete", "${HOME", p2.to_string)
  335. p2 := mkpath("${")
  336. p2.expand_variables
  337. assert_equal("incomplete2", "${", p2.to_string)
  338. end
  339. feature {ANY} -- Bugfix tests
  340. test_join_invalidates_cache
  341. local
  342. p1, p2: MICROSOFT_PATH_NAME
  343. do
  344. create p1.make_from_string("c:\x")
  345. create p2.make_from_string("y")
  346. p1.join(p2)
  347. assert_equal("ok", "c:\x\y", p1.to_string)
  348. end
  349. end -- class TEST_MICROSOFT_PATH
  350. --
  351. -- ------------------------------------------------------------------------------------------------------------------------------
  352. -- Copyright notice below. Please read.
  353. --
  354. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  355. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  356. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  357. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  358. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  359. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  360. --
  361. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  362. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  363. --
  364. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  365. --
  366. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  367. -- ------------------------------------------------------------------------------------------------------------------------------