PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/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--
  4class TEST_MICROSOFT_PATH
  5   -- Test for MICROSOFT_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_join_invalidates_cache
 63         tear_down
 64      end
 65
 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         create drive.make_from_string("c:")
 78         create drive_root.make_from_string("c:\")
 79         create drive_path.make_from_string("c:\path")
 80         create drive2_root.make_from_string("d:\")
 81         sys.set_environment_variable("HOME", "\home\dmoisset")
 82         path_for_tests := bd.current_working_directory.out
 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, drive, drive_root, drive_path, drive2_root: MICROSOFT_PATH_NAME
 98
 99   mkpath (s: STRING): MICROSOFT_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: MICROSOFT_PATH_NAME
117         --  MISSING exists, same_file, expand_shellouts
118
119   test_count
120      do
121         assert_integers_equal("empty", 0, empty.count)
122         assert_integers_equal("single", 1, single.count)
123         assert_integers_equal("root", 0, root.count)
124         assert_integers_equal("absolute", 2, absolute.count)
125         assert_integers_equal("multi_slash", 2, multi_slash.count)
126         assert_integers_equal("final_slash", 3, final_slash.count)
127         assert_integers_equal("simple", 4, simple.count)
128      end
129
130   test_last
131      do
132         assert_equal("single", "xyz.png", single.last)
133         assert_equal("absolute", "y.txt", absolute.last)
134         assert_equal("multi_slash", "b", multi_slash.last)
135         assert_equal("final_slash", "", final_slash.last)
136         assert_equal("simple", "d.", simple.last)
137      end
138
139   test_extension
140      do
141         assert_equal("single", ".png", single.extension)
142         assert_equal("absolute", ".txt", absolute.extension)
143         assert_equal("multi_slash", "", multi_slash.extension)
144         assert_equal("final_slash", "", final_slash.extension)
145         assert_equal("simple", ".", simple.extension)
146      end
147
148   test_is_absolute
149      do
150         assert("empty", not empty.is_absolute)
151         assert("single", not single.is_absolute)
152         assert("root", root.is_absolute)
153         assert("absolute", absolute.is_absolute)
154         assert("multi_slash", not multi_slash.is_absolute)
155         assert("final_slash", not final_slash.is_absolute)
156         assert("simple", not simple.is_absolute)
157      end
158
159   test_plus
160      do
161         assert_equal("empty_empty", "", (empty + empty).to_string)
162         assert_equal("empty_simple", simple.to_string, (empty + simple).to_string)
163         assert_equal("empty_absolute", absolute.to_string, (empty + absolute).to_string)
164         assert_equal("simple_absolute", absolute.to_string, (simple + absolute).to_string)
165         assert_equal("simple_simple", "a\b\ccc\d.\a\b\ccc\d.", (simple + simple).to_string)
166         assert_equal("simple_empty", "a\b\ccc\d.", (simple + empty).to_string)
167         -- Interesting cases of join with drives
168         assert_equal("drive_absolute", "c:\xxx.d\y.txt", (drive + absolute).to_string)
169         assert_equal("driveroot_absolute", "c:\xxx.d\y.txt", (drive_root + absolute).to_string)
170         assert_equal("drivepath_absolute", absolute.to_string, (drive_path + absolute).to_string)
171         assert_equal("drive_drive2", drive2_root.to_string, (drive + drive2_root).to_string)
172         assert_equal("driveroot_drive2", drive2_root.to_string, (drive_root + drive2_root).to_string)
173         -- Simpler cases of drive+something
174         assert_equal("drivepath_single", "c:\path\xyz.png", (drive_path + single).to_string)
175      end
176
177   test_to_absolute
178      local
179         bd: BASIC_DIRECTORY
180      do
181         bd.change_current_working_directory("/usr/bin")
182         empty.to_absolute
183         assert_equal("empty", "\usr\bin", empty.to_string)
184         single.to_absolute
185         assert_equal("single", "\usr\bin\xyz.png", single.to_string)
186         root.to_absolute
187         assert_equal("root", "\", root.to_string)
188         absolute.to_absolute
189         assert_equal("absolute", "\xxx.d\y.txt", absolute.to_string)
190         multi_slash.to_absolute
191         assert_equal("multi_slash", "\usr\bin\a\b", multi_slash.to_string)
192         final_slash.to_absolute
193         assert_equal("final_slash", "\usr\bin\a.d\b", final_slash.to_string)
194         simple.to_absolute
195         assert_equal("simple", "\usr\bin\a\b\ccc\d.", simple.to_string)
196      end
197
198   test_normalize
199      local
200         p: MICROSOFT_PATH_NAME
201      do
202         empty.normalize
203         assert_equal("empty", ".", empty.to_string)
204         multi_slash.normalize
205         assert_equal("multi_slash", "a\b", multi_slash.to_string)
206         final_slash.normalize
207         assert_equal("final_slash", "a.d\b", final_slash.to_string)
208         simple.normalize
209         assert_equal("simple", "a\b\ccc\d.", simple.to_string)
210         p := mkpath("\\server\share")
211         p.normalize
212         assert_equal("unc_path", "\\server\share", p.to_string)
213         p := mkpath("\..\x\y")
214         p.normalize
215         assert_equal("parent_root", "\x\y", p.to_string)
216         p := mkpath("..\z\..\x\y")
217         p.normalize
218         assert_equal("parent_after", "..\x\y", p.to_string)
219         p := mkpath("\x\y\..")
220         p.normalize
221         assert_equal("parent_last", "\x", p.to_string)
222         p := mkpath(".\y")
223         p.normalize
224         assert_equal("this_first", "y", p.to_string)
225         p := mkpath("..\.\..\x\y")
226         p.normalize
227         assert_equal("this_middle", "..\..\x\y", p.to_string)
228         p := mkpath("x\y\.")
229         p.normalize
230         assert_equal("this_last", "x\y", p.to_string)
231         p := mkpath("\.")
232         p.normalize
233         assert_equal("root_this", "\", p.to_string)
234         p := mkpath("c:")
235         p.normalize
236         assert_equal("drive_only", "c:.", p.to_string)
237      end
238
239   test_is_normalized
240      do
241         assert("empty", not empty.is_normalized)
242         assert("single", single.is_normalized)
243         assert("root", root.is_normalized)
244         assert("absolute", absolute.is_normalized)
245         assert("multi_slash", not multi_slash.is_normalized)
246         assert("final_slash", not final_slash.is_normalized)
247         assert("simple", simple.is_normalized)
248         assert("unix_slash", not mkpath("/usr/bin").is_normalized)
249         assert("double_root", mkpath("\\").is_normalized)
250         assert("triple_root", mkpath("\\\").is_normalized)
251         assert("drive_double_root", not mkpath("c:\\").is_normalized)
252         assert("parent_root", not mkpath("\..\x/y").is_normalized)
253         assert("parent_first", mkpath("..\..\x\y").is_normalized)
254         assert("parent_after", not mkpath("..\z\..\x\y").is_normalized)
255         assert("parent_only", mkpath("..\..").is_normalized)
256         assert("parent_last", not mkpath("\x\y\..").is_normalized)
257         assert("this_first", not mkpath("./y").is_normalized)
258         assert("this_middle", not mkpath("..\.\..\x\y").is_normalized)
259         assert("this_last", not mkpath("x\y\.").is_normalized)
260         assert("this_only", mkpath(".").is_normalized)
261         assert("root_this", not mkpath("\.").is_normalized)
262      end
263
264   test_remove_last
265      do
266         single.remove_last
267         assert_equal("single", "", single.to_string)
268         absolute.remove_last
269         assert_equal("absolute", "\xxx.d", absolute.to_string)
270         multi_slash.remove_last
271         assert_equal("multi_slash", "a", multi_slash.to_string)
272         final_slash.remove_last
273         assert_equal("final_slash", "a.d\b", final_slash.to_string)
274         simple.remove_last
275         assert_equal("simple", "a\b\ccc", simple.to_string)
276         drive_path.remove_last
277         assert_equal("drive_path", "c:\", drive_path.to_string)
278      end
279
280   test_add_last
281      do
282         assert_equal("empty", "foo", (empty / "foo").to_string)
283         assert_equal("single", "xyz.png\foo", (single / "foo").to_string)
284         assert_equal("root", "\foo", (root / "foo").to_string)
285         assert_equal("absolute", "\xxx.d\y.txt\foo", (absolute / "foo").to_string)
286         assert_equal("multi_slash", "a\\\\b\foo", (multi_slash / "foo").to_string)
287         assert_equal("final_slash", "a.d\b\foo", (final_slash / "foo").to_string)
288         assert_equal("simple", "a\b\ccc\d.\foo", (simple / "foo").to_string)
289      end
290
291   test_expand_user
292      local
293         p: MICROSOFT_PATH_NAME; sys: SYSTEM
294      do
295         p := mkpath("\usr\local\bin~")
296         p.expand_user
297         assert_equal("non_initial_tilde", "\usr\local\bin~", p.to_string)
298         p := mkpath("~\bin")
299         p.expand_user
300         assert_equal("home_subdir", "\home\dmoisset\bin", p.to_string)
301         p := mkpath("~")
302         p.expand_user
303         assert_equal("home_only", "\home\dmoisset", p.to_string)
304         sys.set_environment_variable("HOME", "\boot")
305         p := mkpath("~\vmlinuz")
306         p.expand_user
307         assert_equal("home_from_$HOME", "\boot\vmlinuz", p.to_string)
308      end
309   --   test_expand_explicit_user
310   --           do
311   --                   crash -- not done yet
312   --           end
313   --   test_common_prefix
314   --           do
315   --                   assert_equal ("empty", "", (mkpath("").common_prefix(mkpath(""))).to_string)
316   --                   assert_equal ("root", "/", (mkpath("/bin").common_prefix(mkpath("/usr/bin"))).to_string)
317   --                   assert_equal ("partial", "/b", (mkpath("/bin").common_prefix(mkpath("/boot"))).to_string)
318   --                   assert_equal ("substring", "/usr/bin", (mkpath("/usr/bin/X11R6").common_prefix(mkpath("/usr/bin"))).to_string)
319   --                   assert_equal ("complete", "/usr/bin/", (mkpath("/usr/bin/X11R6").common_prefix(mkpath("/usr/bin/foo"))).to_string)
320   --           end
321
322   test_expand_variables
323      local
324         p1, p2: MICROSOFT_PATH_NAME; sys: SYSTEM
325      do
326         p1 := mkpath("~\foo")
327         p2 := mkpath("$HOME\foo")
328         p1.expand_user
329         p2.expand_variables
330         assert_equal("expand_simple", p1.to_string, p2.to_string)
331         p2 := mkpath("${HOME}\foo")
332         p2.expand_variables
333         assert_equal("expand_bracketed", p1.to_string, p2.to_string)
334         p1 := mkpath("~")
335         p2 := mkpath("$HOME")
336         p1.expand_user
337         p2.expand_variables
338         assert_equal("expand_alone", p1.to_string, p2.to_string)
339         p2 := mkpath("x$foo${bar}y")
340         sys.set_environment_variable("foo", "11\11")
341         sys.set_environment_variable("bar", "22\22")
342         p2.expand_variables
343         assert_equal("expand_several", "x11\1122\22y", p2.to_string)
344         p2 := mkpath("x$foo${bar}y")
345         sys.set_environment_variable("foo", "$bar")
346         sys.set_environment_variable("bar", "$foo$bar")
347         p2.expand_variables
348         assert_equal("expand_nonnrecursive", "x$bar$foo$bary", p2.to_string)
349         p2 := mkpath("$doesnotexist${this_variable_is_fake}")
350         p2.expand_variables
351         assert_equal("non_existant", "$doesnotexist${this_variable_is_fake}", p2.to_string)
352         p2 := mkpath("3$.")
353         p2.expand_variables
354         assert_equal("single_dollar", "3$.", p2.to_string)
355         p2 := mkpath("${HOME")
356         p2.expand_variables
357         assert_equal("incomplete", "${HOME", p2.to_string)
358         p2 := mkpath("${")
359         p2.expand_variables
360         assert_equal("incomplete2", "${", p2.to_string)
361      end
362
363feature {ANY} -- Bugfix tests
364   test_join_invalidates_cache
365      local
366         p1, p2: MICROSOFT_PATH_NAME
367      do
368         create p1.make_from_string("c:\x")
369         create p2.make_from_string("y")
370         p1.join(p2)
371         assert_equal("ok", "c:\x\y", p1.to_string)
372      end
373
374end -- class TEST_MICROSOFT_PATH
375--
376-- ------------------------------------------------------------------------------------------------------------------------------
377-- Copyright notice below. Please read.
378--
379-- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
380-- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
381-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
382-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
383-- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
384-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
385--
386-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.       - University of Nancy 1 - FRANCE
387-- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
388--
389-- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
390--
391-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
392-- ------------------------------------------------------------------------------------------------------------------------------