PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 1ms

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