(* path_unittest.ml - Don Yang (uguu.org) 04/16/06 *) (* Test Path.replace_separator *) let test_str_a = "a/b\\c" in let test_str_b = "a\\b/c" in Path.replace_separator test_str_a; Path.replace_separator test_str_b; if test_str_a <> test_str_b then failwith "Path.replace_separator";; (* Test Path.normalize *) let test_cases = [ ("", ""); (".", ""); ("..", ".."); ("...", "..."); ("a", "a"); ("./a", "a"); ("././a", "a"); ("a/", "a"); ("a//", "a"); ("a//b", "a/b"); ("a///b", "a/b"); ("a///b//", "a/b"); ("a/./b", "a/b"); ("a/././b", "a/b"); ("a/../b", "b"); ("a/../b/../c", "c"); ("a/.../b/.../c", "a/.../b/.../c"); ("a/./../b", "b"); ("a/.././b", "b"); ("a.b", "a.b"); ("a/.b", "a/.b"); ("a./b", "a./b"); ("a..b", "a..b"); ("a../b", "a../b"); ("a./.b", "a./.b"); ("a/..b", "a/..b"); ("/", "/"); ("/a", "/a"); ("/.", "/"); ("/a/b", "/a/b"); ("/a/..", "/"); ];; List.iter (fun item -> Path.replace_separator (fst item); Path.replace_separator (snd item)) test_cases;; List.iter (fun item -> if Path.normalize (fst item) <> (snd item) then failwith ("Path.normalize(" ^ (fst item) ^ ") failed")) test_cases;; (* Test Path.relative *) let test_cases = [ (("a", "a"), ""); (("a", "b"), "../b"); (("a/b", "c/d"), "../../c/d"); (("a/b/c", "a/b/d"), "../d"); (("a/b/c/d", "a/b/d/c"), "../../d/c"); (("a/a/a/b", "a/b"), "../../../b"); (("a/b", "a/a/a/b"), "../a/a/b"); (("a", "a/b"), "b"); (("a/b", "a"), ".."); ];; List.iter (fun item -> Path.replace_separator (fst (fst item)); Path.replace_separator (snd (fst item)); Path.replace_separator (snd item)) test_cases;; List.iter (fun item -> if Path.relative (fst (fst item)) (snd (fst item)) <> (snd item) then failwith ("Path.relative(" ^ (fst (fst item)) ^ ", " ^ (snd (fst item)) ^ ") failed")) test_cases;; (* Test Path.replace_root *) if (Path.replace_root "a" "b" ("a" ^ Path.separator_str ^ "c")) <> ("b" ^ Path.separator_str ^ "c") then failwith "Path.replace_root";; (* Test Path.pattern_to_regexp *) let test_cases = [ ("", ("", Path.SINGLE_FILE)); ("a", ("a", Path.SINGLE_FILE)); ("a+", ("a\\+", Path.SINGLE_FILE)); ("+a", ("\\+a", Path.SINGLE_FILE)); ("a?", ("a\\?", Path.SINGLE_FILE)); ("?a", ("\\?a", Path.SINGLE_FILE)); ("a[", ("a\\[", Path.SINGLE_FILE)); ("[a", ("\\[a", Path.SINGLE_FILE)); ("a]", ("a\\]", Path.SINGLE_FILE)); ("]a", ("\\]a", Path.SINGLE_FILE)); ("a$", ("a\\$", Path.SINGLE_FILE)); ("$a", ("\\$a", Path.SINGLE_FILE)); ("a^", ("a\\^", Path.SINGLE_FILE)); ("^a", ("\\^a", Path.SINGLE_FILE)); ("a.", ("a\\.", Path.SINGLE_FILE)); (".a", ("\\.a", Path.SINGLE_FILE)); ("a..", ("a\\.\\.", Path.SINGLE_FILE)); ("..a", ("\\.\\.a", Path.SINGLE_FILE)); ("a...", ("a.*", Path.SUB_DIR)); ("...a", (".*a", Path.SUB_DIR)); ("a*", ("a[^" ^ Path.separator_str ^ "]*", Path.BASE_DIR)); ("*a", ("[^" ^ Path.separator_str ^ "]*a", Path.BASE_DIR)); ("+.", ("\\+\\.", Path.SINGLE_FILE)); (".+", ("\\.\\+", Path.SINGLE_FILE)); ("+..", ("\\+\\.\\.", Path.SINGLE_FILE)); ("..+", ("\\.\\.\\+", Path.SINGLE_FILE)); ("+...", ("\\+.*", Path.SUB_DIR)); ("...+", (".*\\+", Path.SUB_DIR)); ("+*", ("\\+[^" ^ Path.separator_str ^ "]*", Path.BASE_DIR)); ("*+", ("[^" ^ Path.separator_str ^ "]*\\+", Path.BASE_DIR)); ("...a*", (".*a[^" ^ Path.separator_str ^ "]*", Path.SUB_DIR)); ("*a...", ("[^" ^ Path.separator_str ^ "]*a.*", Path.SUB_DIR)); ("...+*", (".*\\+[^" ^ Path.separator_str ^ "]*", Path.SUB_DIR)); ("*+...", ("[^" ^ Path.separator_str ^ "]*\\+.*", Path.SUB_DIR)); ] in List.iter (fun item -> if Path.pattern_to_regexp (fst item) <> (snd item) then failwith ("Path.pattern_to_regexp(" ^ (fst item) ^ ") failed"); if Path.pattern_to_regexp ("x" ^ (fst item)) <> ("x" ^ (fst (snd item)), snd (snd item)) then failwith ("Path.pattern_to_regexp(x" ^ (fst item) ^ ") failed"); if Path.pattern_to_regexp ((fst item) ^ "x") <> ((fst (snd item)) ^ "x", snd (snd item)) then failwith ("Path.pattern_to_regexp(" ^ (fst item) ^ "x) failed")) test_cases;; print_string "PASS\n";;