/test/lib/io/path_name/test_posix_path.e

http://github.com/tybor/Liberty · Specman e · 406 lines · 327 code · 29 blank · 50 comment · 0 complexity · c80eac41f8c89136df6079f79d2319cd 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_POSIX_PATH
  5. -- Test for POSIX_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_is_file
  60. --tear_down
  61. --set_up
  62. --test_is_directory
  63. --tear_down
  64. test_extension_removal
  65. end
  66. set_up
  67. local
  68. sys: SYSTEM; bd: BASIC_DIRECTORY
  69. do
  70. create empty.make_empty
  71. create single.make_from_string("xyz.png")
  72. create root.make_root
  73. create absolute.make_from_string("/xxx.d/y.txt")
  74. create multi_slash.make_from_string("a////b")
  75. create final_slash.make_from_string("a.d/b/")
  76. create simple.make_from_string("a/b/ccc/d.")
  77. sys.set_environment_variable("HOME", "/home/dmoisset")
  78. create path_for_tests.make_from_string(bd.current_working_directory)
  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: POSIX_PATH_NAME
  91. mkpath (s: STRING): POSIX_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: POSIX_PATH_NAME
  106. -- MISSING exists, same_file, expand_shellouts
  107. test_count
  108. do
  109. assert("empty count", empty.count=0)
  110. assert("single count", single.count=1)
  111. assert("root count", root.count=0)
  112. assert("absolute count", absolute.count=2)
  113. assert("multi_slash count", multi_slash.count=2)
  114. assert("final_slash count", final_slash.count=3)
  115. assert("simple count", simple.count=4)
  116. end
  117. test_last
  118. do
  119. assert("single last", "xyz.png".is_equal( single.last))
  120. assert("absolute last", "y.txt".is_equal( absolute.last))
  121. assert("multi_slash last", "b".is_equal( multi_slash.last))
  122. assert("final_slash last", "".is_equal( final_slash.last))
  123. assert("simple last", "d.".is_equal( simple.last))
  124. end
  125. test_extension
  126. do
  127. print("single extention «"+single.extension+"»%N")
  128. assert("single extention", ".png".is_equal(single.extension))
  129. assert("absolute extention", ".txt".is_equal(absolute.extension))
  130. assert("multi_slash extention", "".is_equal(multi_slash.extension))
  131. assert("final_slash extention", "".is_equal(final_slash.extension))
  132. --assert("simple extention", ".".is_equal(simple.extension))
  133. end
  134. test_is_absolute
  135. do
  136. assert("empty absolute", not empty.is_absolute)
  137. assert("single absolute", not single.is_absolute)
  138. assert("root absolute", root.is_absolute)
  139. assert("absolute absolute", absolute.is_absolute)
  140. assert("multi_slash absolute", not multi_slash.is_absolute)
  141. assert("final_slash absolute", not final_slash.is_absolute)
  142. assert("simple absolute", not simple.is_absolute)
  143. end
  144. test_plus
  145. do
  146. assert("empty_empty", "".is_equal( (empty + empty).to_string))
  147. assert("empty_simple", simple.to_string.is_equal( (empty + simple).to_string))
  148. assert("empty_absolute", absolute.to_string.is_equal( (empty + absolute).to_string))
  149. assert("simple_absolute", absolute.to_string.is_equal( (simple + absolute).to_string))
  150. assert("simple_simple", "a/b/ccc/d./a/b/ccc/d.".is_equal( (simple + simple).to_string))
  151. assert("simple_empty", "a/b/ccc/d.".is_equal( (simple + empty).to_string))
  152. end
  153. test_to_absolute
  154. local
  155. bd: BASIC_DIRECTORY
  156. do
  157. bd.change_current_working_directory("/usr/bin")
  158. empty.to_absolute
  159. assert("empty", "/usr/bin".is_equal( empty.to_string))
  160. single.to_absolute
  161. assert("single", "/usr/bin/xyz.png".is_equal( single.to_string))
  162. root.to_absolute
  163. assert("root", "/".is_equal( root.to_string))
  164. absolute.to_absolute
  165. assert("absolute", "/xxx.d/y.txt".is_equal( absolute.to_string))
  166. multi_slash.to_absolute
  167. assert("multi_slash", "/usr/bin/a/b".is_equal( multi_slash.to_string))
  168. final_slash.to_absolute
  169. assert("final_slash", "/usr/bin/a.d/b".is_equal( final_slash.to_string))
  170. simple.to_absolute
  171. assert("simple", "/usr/bin/a/b/ccc/d.".is_equal( simple.to_string))
  172. end
  173. test_normalize
  174. local
  175. p: POSIX_PATH_NAME
  176. do
  177. empty.normalize
  178. assert("empty", ".".is_equal( empty.to_string))
  179. multi_slash.normalize
  180. assert("multi_slash", "a/b".is_equal( multi_slash.to_string))
  181. final_slash.normalize
  182. assert("final_slash", "a.d/b".is_equal( final_slash.to_string))
  183. simple.normalize
  184. assert("simple", "a/b/ccc/d.".is_equal( simple.to_string))
  185. p := mkpath("///")
  186. p.normalize
  187. assert("triple_root", "/".is_equal( p.to_string))
  188. p := mkpath("/../x/y")
  189. p.normalize
  190. assert("parent_root", "/x/y".is_equal( p.to_string))
  191. p := mkpath("../z/../x/y")
  192. p.normalize
  193. assert("parent_after", "../x/y".is_equal( p.to_string))
  194. p := mkpath("/x/y/..")
  195. p.normalize
  196. assert("parent_last", "/x".is_equal( p.to_string))
  197. p := mkpath("./y")
  198. p.normalize
  199. assert("this_first", "y".is_equal( p.to_string))
  200. p := mkpath(".././../x/y")
  201. p.normalize
  202. assert("this_middle", "../../x/y".is_equal( p.to_string))
  203. p := mkpath("x/y/.")
  204. p.normalize
  205. assert("this_last", "x/y".is_equal( p.to_string))
  206. p := mkpath("/.")
  207. p.normalize
  208. assert("root_this", "/".is_equal( p.to_string))
  209. end
  210. test_is_normalized
  211. do
  212. assert("empty", not empty.is_normalized)
  213. assert("single", single.is_normalized)
  214. assert("root", root.is_normalized)
  215. assert("absolute", absolute.is_normalized)
  216. assert("multi_slash", not multi_slash.is_normalized)
  217. assert("final_slash", not final_slash.is_normalized)
  218. assert("simple", simple.is_normalized)
  219. assert("double_root", mkpath("//").is_normalized)
  220. assert("triple_root", not mkpath("///").is_normalized)
  221. assert("parent_root", not mkpath("/../x/y").is_normalized)
  222. assert("parent_first", mkpath("../../x/y").is_normalized)
  223. assert("parent_after", not mkpath("../z/../x/y").is_normalized)
  224. assert("parent_only", mkpath("../..").is_normalized)
  225. assert("parent_last", not mkpath("/x/y/..").is_normalized)
  226. assert("this_first", not mkpath("./y").is_normalized)
  227. assert("this_middle", not mkpath(".././../x/y").is_normalized)
  228. assert("this_last", not mkpath("x/y/.").is_normalized)
  229. assert("this_only", mkpath(".").is_normalized)
  230. assert("root_this", not mkpath("/.").is_normalized)
  231. end
  232. test_remove_last
  233. do
  234. single.remove_last
  235. assert("single", "".is_equal( single.to_string))
  236. absolute.remove_last
  237. assert("absolute", "/xxx.d".is_equal( absolute.to_string))
  238. multi_slash.remove_last
  239. assert("multi_slash", "a".is_equal( multi_slash.to_string))
  240. final_slash.remove_last
  241. assert("final_slash", "a.d/b".is_equal( final_slash.to_string))
  242. simple.remove_last
  243. assert("simple", "a/b/ccc".is_equal(simple.to_string))
  244. end
  245. test_add_last
  246. do
  247. assert("empty", "foo".is_equal( (empty / "foo").to_string))
  248. assert("single", "xyz.png/foo".is_equal( (single / "foo").to_string))
  249. assert("root", "/foo".is_equal( (root / "foo").to_string))
  250. assert("absolute", "/xxx.d/y.txt/foo".is_equal( (absolute / "foo").to_string))
  251. assert("multi_slash", "a////b/foo".is_equal( (multi_slash / "foo").to_string))
  252. assert("final_slash", "a.d/b/foo".is_equal( (final_slash / "foo").to_string))
  253. assert("simple", "a/b/ccc/d./foo".is_equal( (simple / "foo").to_string))
  254. end
  255. test_expand_user
  256. local
  257. p: POSIX_PATH_NAME; sys: SYSTEM
  258. do
  259. p := mkpath("/usr/local/bin~")
  260. p.expand_user
  261. assert("non_initial_tilde", "/usr/local/bin~".is_equal( p.to_string))
  262. p := mkpath("~/bin")
  263. p.expand_user
  264. assert("home_subdir", "/home/dmoisset/bin".is_equal( p.to_string))
  265. p := mkpath("~")
  266. p.expand_user
  267. assert("home_only", "/home/dmoisset".is_equal( p.to_string))
  268. sys.set_environment_variable("HOME", "/boot")
  269. p := mkpath("~/vmlinuz")
  270. p.expand_user
  271. assert("home_from_$HOME", "/boot/vmlinuz".is_equal( p.to_string))
  272. end
  273. -- test_expand_explicit_user
  274. -- do
  275. -- crash -- not done yet
  276. -- end
  277. -- test_common_prefix
  278. -- do
  279. -- assert_equal ("empty", "", (mkpath("").common_prefix(mkpath(""))).to_string)
  280. -- assert_equal ("root", "/", (mkpath("/bin").common_prefix(mkpath("/usr/bin"))).to_string)
  281. -- assert_equal ("partial", "/b", (mkpath("/bin").common_prefix(mkpath("/boot"))).to_string)
  282. -- assert_equal ("substring", "/usr/bin", (mkpath("/usr/bin/X11R6").common_prefix(mkpath("/usr/bin"))).to_string)
  283. -- assert_equal ("complete", "/usr/bin/", (mkpath("/usr/bin/X11R6").common_prefix(mkpath("/usr/bin/foo"))).to_string)
  284. -- end
  285. test_expand_variables
  286. local
  287. p1, p2: POSIX_PATH_NAME; sys: SYSTEM
  288. do
  289. p1 := mkpath("~/foo")
  290. p2 := mkpath("$HOME/foo")
  291. p1.expand_user
  292. p2.expand_variables
  293. assert("expand_simple", p1.to_string.is_equal( p2.to_string))
  294. p2 := mkpath("${HOME}/foo")
  295. p2.expand_variables
  296. assert("expand_bracketed", p1.to_string.is_equal( p2.to_string))
  297. p1 := mkpath("~")
  298. p2 := mkpath("$HOME")
  299. p1.expand_user
  300. p2.expand_variables
  301. assert("expand_alone", p1.to_string.is_equal( p2.to_string))
  302. p2 := mkpath("x$foo${bar}y")
  303. sys.set_environment_variable("foo", "11/11")
  304. sys.set_environment_variable("bar", "22/22")
  305. p2.expand_variables
  306. assert("expand_several", "x11/1122/22y".is_equal( p2.to_string))
  307. p2 := mkpath("x$foo${bar}y")
  308. sys.set_environment_variable("foo", "$bar")
  309. sys.set_environment_variable("bar", "$foo$bar")
  310. p2.expand_variables
  311. assert("expand_nonnrecursive", "x$bar$foo$bary".is_equal( p2.to_string))
  312. p2 := mkpath("$doesnotexist${this_variable_is_fake}")
  313. p2.expand_variables
  314. assert("non_existant", "$doesnotexist${this_variable_is_fake}".is_equal( p2.to_string))
  315. p2 := mkpath("3$.")
  316. p2.expand_variables
  317. assert("single_dollar", "3$.".is_equal( p2.to_string))
  318. p2 := mkpath("${HOME")
  319. p2.expand_variables
  320. assert("incomplete", "${HOME".is_equal( p2.to_string))
  321. p2 := mkpath("${")
  322. p2.expand_variables
  323. assert("incomplete2", "${".is_equal( p2.to_string))
  324. end
  325. test_is_file
  326. local
  327. p: POSIX_PATH_NAME
  328. do
  329. assert("empty", not empty.is_file)
  330. create p.make_from_string("test_posix_path.e")
  331. assert("existing", p.is_file)
  332. create p.make_from_string("does_not_exists.foo")
  333. assert("nonexisting", not p.is_file)
  334. end
  335. test_is_directory
  336. local
  337. p: POSIX_PATH_NAME
  338. do
  339. assert("root", root.is_directory)
  340. assert("empty", not empty.is_directory)
  341. create p.make_from_string("TESTGEN")
  342. assert("existing", p.is_directory)
  343. create p.make_from_string("does_not_exists.foo")
  344. assert("nonexisting", not p.is_directory)
  345. end
  346. test_extension_removal
  347. local p: POSIX_PATH_NAME; s,s2,s3: STRING
  348. do
  349. create p.make_from_string("/this/string/resemble-a-complete.path")
  350. s := p.last.twin
  351. create s2.copy(p.last)
  352. create s3.make_from_string(p.last)
  353. assert("twin equals to copy", s.is_equal(s2))
  354. assert("twin equals make_from_string", s.is_equal(s3))
  355. s.remove_tail(p.extension.count)
  356. assert("correct file name", s.is_equal("resemble-a-complete"))
  357. assert("correct length", s.count=19)
  358. end
  359. end -- class TEST_POSIX_PATH
  360. --
  361. -- ------------------------------------------------------------------------------------------------------------------------------
  362. -- Copyright notice below. Please read.
  363. --
  364. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  365. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  366. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  367. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  368. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  369. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  370. --
  371. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  372. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  373. --
  374. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  375. --
  376. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  377. -- ------------------------------------------------------------------------------------------------------------------------------