Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_path.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 fun test_path(p: Path)
16 do
17 print "* {p}"
18 print "simplified: {p.simplified}"
19 print "filename: {p.filename}"
20 print "dir: {p.dir}"
21 var e = p.exists
22 print "exists: {e}"
23 print " error? {p.last_error or else "nope"}"
24 var s = p.stat
25 if s == null then
26 print "stat: nope"
27 else
28 print "stat: is_dir={s.is_dir} is_reg={s.is_reg}"
29 end
30 print " error? {p.last_error or else "nope"}"
31 var stream = p.open_ro
32 print "open: {stream.path or else "-"}"
33 print " error? {p.last_error or else "nope"}"
34 print "first_line: {stream.read_line}"
35 stream.close
36 print " error? {stream.last_error or else "nope"}"
37 var txt = p.read_all
38 print "content: {txt.length} chars"
39 print " error? {p.last_error or else "nope"}"
40 var bin = p.read_all_bytes
41 print "content: {bin.length} bytes"
42 print " error? {p.last_error or else "nope"}"
43 var lines = p.read_lines
44 print "content: {lines.length} lines"
45 print " error? {p.last_error or else "nope"}"
46
47 var files = p.files
48 print "content: {files.length} files"
49 print " error? {p.last_error or else "nope"}"
50 end
51
52 if args.length != 1 then
53 print "require the name of the output directory"
54 return
55 end
56
57 var base = args.first
58 base.mkdir
59 test_path(base.to_path)
60
61 var f = (base/"file.txt").to_path
62 test_path(f)
63
64 "hello world!".write_to_file(f.to_s)
65 test_path(f)
66
67 var dn = base / "dir"
68 var d = dn.to_path
69 dn.mkdir
70 "goodby world!".write_to_file(dn/"file.txt")
71 test_path(d)