github.com/searKing/golang/go@v1.2.117/path/path_test.go (about) 1 package path_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/searKing/golang/go/path" 8 ) 9 10 type RelTests struct { 11 root, path, want string 12 } 13 14 var reltests = []RelTests{ 15 {"a/b", "a/b", "."}, 16 {"a/b/.", "a/b", "."}, 17 {"a/b", "a/b/.", "."}, 18 {"./a/b", "a/b", "."}, 19 {"a/b", "./a/b", "."}, 20 {"ab/cd", "ab/cde", "../cde"}, 21 {"ab/cd", "ab/c", "../c"}, 22 {"a/b", "a/b/c/d", "c/d"}, 23 {"a/b", "a/b/../c", "../c"}, 24 {"a/b/../c", "a/b", "../b"}, 25 {"a/b/c", "a/c/d", "../../c/d"}, 26 {"a/b", "c/d", "../../c/d"}, 27 {"a/b/c/d", "a/b", "../.."}, 28 {"a/b/c/d", "a/b/", "../.."}, 29 {"a/b/c/d/", "a/b", "../.."}, 30 {"a/b/c/d/", "a/b/", "../.."}, 31 {"../../a/b", "../../a/b/c/d", "c/d"}, 32 {"/a/b", "/a/b", "."}, 33 {"/a/b/.", "/a/b", "."}, 34 {"/a/b", "/a/b/.", "."}, 35 {"/ab/cd", "/ab/cde", "../cde"}, 36 {"/ab/cd", "/ab/c", "../c"}, 37 {"/a/b", "/a/b/c/d", "c/d"}, 38 {"/a/b", "/a/b/../c", "../c"}, 39 {"/a/b/../c", "/a/b", "../b"}, 40 {"/a/b/c", "/a/c/d", "../../c/d"}, 41 {"/a/b", "/c/d", "../../c/d"}, 42 {"/a/b/c/d", "/a/b", "../.."}, 43 {"/a/b/c/d", "/a/b/", "../.."}, 44 {"/a/b/c/d/", "/a/b", "../.."}, 45 {"/a/b/c/d/", "/a/b/", "../.."}, 46 {"/../../a/b", "/../../a/b/c/d", "c/d"}, 47 {".", "a/b", "a/b"}, 48 {".", "..", ".."}, 49 50 // can't do purely lexically 51 {"..", ".", "err"}, 52 {"..", "a", "err"}, 53 {"../..", "..", "err"}, 54 {"a", "/a", "err"}, 55 {"/a", "a", "err"}, 56 } 57 58 func TestRel(t *testing.T) { 59 tests := append([]RelTests{}, reltests...) 60 for _, test := range tests { 61 got, err := path.Rel(test.root, test.path) 62 if test.want == "err" { 63 if err == nil { 64 t.Errorf("Rel(%q, %q)=%q, want error", test.root, test.path, got) 65 } 66 continue 67 } 68 if err != nil { 69 t.Errorf("Rel(%q, %q): want %q, got error: %s", test.root, test.path, test.want, err) 70 } 71 if got != test.want { 72 t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path, got, test.want) 73 } 74 } 75 } 76 77 func TestResolveReference(t *testing.T) { 78 table := []struct { 79 FromBase, ToBase, FromPath, ToPath string 80 }{ 81 { 82 FromBase: "/data/fruits", 83 ToBase: "/data/animals", 84 FromPath: "apple", 85 ToPath: "/data/animals/apple", 86 }, 87 { 88 FromBase: "/data/fruits", 89 ToBase: "/data/animals", 90 FromPath: "/data/fruits/apple", 91 ToPath: "/data/animals/apple", 92 }, 93 { 94 FromBase: "/data/fruits", 95 ToBase: "/data/animals", 96 FromPath: "./apple", 97 ToPath: "/data/animals/apple", 98 }, 99 { 100 FromBase: "/data/fruits", 101 ToBase: "/data/animals", 102 FromPath: "/data/stars/moon", 103 ToPath: "/data/stars/moon", 104 }, 105 { 106 FromBase: "./data/fruits", 107 ToBase: "/data/animals", 108 FromPath: "/data/stars/moon", 109 ToPath: "/data/animals/data/stars/moon", 110 }, 111 } 112 for i, test := range table { 113 toPath := path.ResolveReference(test.FromPath, test.FromBase, test.ToBase) 114 if !strings.EqualFold(toPath, test.ToPath) { 115 t.Errorf("#%d. got %q, want %q", i, toPath, test.ToPath) 116 } 117 } 118 }