jm

Odin for scripts: small packages and a runner, on core: only
Log | Files | Refs | README

path_test.odin (2793B)


      1 package path
      2 
      3 import "core:os"
      4 import "core:strings"
      5 import "core:testing"
      6 
      7 @(test)
      8 expand_home :: proc(t: ^testing.T) {
      9 	h := home(context.temp_allocator)
     10 	testing.expect(t, len(h) > 0, "home must resolve")
     11 	testing.expect_value(t, expand("~", context.temp_allocator), h)
     12 	testing.expect(t, strings.has_prefix(expand("~/x", context.temp_allocator), h))
     13 	testing.expect(t, strings.has_suffix(expand("~/x", context.temp_allocator), "x"))
     14 	testing.expect_value(t, expand("a/../b", context.temp_allocator), "b")
     15 }
     16 
     17 @(test)
     18 files_roundtrip :: proc(t: ^testing.T) {
     19 	root, err := temp_dir("jm-path-test-", context.temp_allocator)
     20 	testing.expect_value(t, err, nil)
     21 	defer remove_all(root)
     22 
     23 	nested := join(root, "a", "b", allocator = context.temp_allocator)
     24 	testing.expect_value(t, mkdirs(nested), nil)
     25 	testing.expect_value(t, mkdirs(nested), nil) // existing is fine
     26 	testing.expect(t, is_dir(nested))
     27 
     28 	file := join(nested, "f.txt", allocator = context.temp_allocator)
     29 	testing.expect_value(t, write(file, "one\r\ntwo\n"), nil)
     30 	testing.expect_value(t, append_file(file, "three\n"), nil)
     31 
     32 	text, rerr := read(file, context.temp_allocator)
     33 	testing.expect_value(t, rerr, nil)
     34 	testing.expect_value(t, text, "one\r\ntwo\nthree\n")
     35 
     36 	ls, lerr := read_lines(file, context.temp_allocator)
     37 	testing.expect_value(t, lerr, nil)
     38 	testing.expect_value(t, len(ls), 3)
     39 	if len(ls) == 3 {
     40 		testing.expect_value(t, ls[0], "one")
     41 		testing.expect_value(t, ls[2], "three")
     42 	}
     43 
     44 	other := join(root, "z.txt", allocator = context.temp_allocator)
     45 	testing.expect_value(t, write(other, "z"), nil)
     46 	names, nerr := list(root, context.temp_allocator)
     47 	testing.expect_value(t, nerr, nil)
     48 	testing.expect_value(t, len(names), 2)
     49 	if len(names) == 2 {
     50 		testing.expect_value(t, names[0], "a")
     51 		testing.expect_value(t, names[1], "z.txt")
     52 	}
     53 
     54 	files, werr := walk(root, context.temp_allocator)
     55 	testing.expect_value(t, werr, nil)
     56 	testing.expect_value(t, len(files), 2)
     57 	if len(files) == 2 {
     58 		testing.expect(t, strings.has_suffix(files[0], "f.txt"))
     59 		testing.expect(t, strings.has_suffix(files[1], "z.txt"))
     60 	}
     61 
     62 	_, missing := read(join(root, "missing", allocator = context.temp_allocator), context.temp_allocator)
     63 	testing.expect(t, missing != nil)
     64 	testing.expect(t, missing == os.General_Error.Not_Exist)
     65 }
     66 
     67 @(test)
     68 same_paths :: proc(t: ^testing.T) {
     69 	testing.expect(t, same("a/b/../c", "a/c"))
     70 	testing.expect(t, !same("a/c", "a/d"))
     71 	when ODIN_OS == .Windows || ODIN_OS == .Darwin {
     72 		testing.expect(t, same("A/C", "a/c"))
     73 	}
     74 }
     75 
     76 @(test)
     77 app_dirs_exist :: proc(t: ^testing.T) {
     78 	d, err := cache_dir("jm-path-test", context.temp_allocator)
     79 	testing.expect_value(t, err, nil)
     80 	testing.expect(t, is_dir(d))
     81 	testing.expect(t, strings.has_suffix(d, "jm-path-test"), "cache_dir must use the app name")
     82 	remove_all(d)
     83 }