github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/pathiterator_test.go (about) 1 // 2 // Copyright 2022 The AVFS authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package avfs_test 18 19 import ( 20 "testing" 21 22 "github.com/avfs/avfs" 23 "github.com/avfs/avfs/vfs/memfs" 24 ) 25 26 // TestPathIterator tests PathIterator methods. 27 func TestPathIterator(t *testing.T) { 28 vfs := memfs.New() 29 30 t.Run("PathIterator", func(t *testing.T) { 31 cases := []struct { 32 path string 33 parts []string 34 osType avfs.OSType 35 }{ 36 {path: `C:\`, parts: nil, osType: avfs.OsWindows}, 37 {path: `C:\Users`, parts: []string{"Users"}, osType: avfs.OsWindows}, 38 {path: `c:\नमस्ते\दुनिया`, parts: []string{"नमस्ते", "दुनिया"}, osType: avfs.OsWindows}, 39 40 {path: "/", parts: nil, osType: avfs.OsLinux}, 41 {path: "/a", parts: []string{"a"}, osType: avfs.OsLinux}, 42 {path: "/b/c/d", parts: []string{"b", "c", "d"}, osType: avfs.OsLinux}, 43 {path: "/नमस्ते/दुनिया", parts: []string{"नमस्ते", "दुनिया"}, osType: avfs.OsLinux}, 44 } 45 46 for _, c := range cases { 47 if c.osType != avfs.CurrentOSType() { 48 continue 49 } 50 51 pi := avfs.NewPathIterator(vfs, c.path) 52 i := 0 53 54 for ; pi.Next(); i++ { 55 if i >= len(c.parts) { 56 continue 57 } 58 59 if pi.Part() != c.parts[i] { 60 t.Errorf("%s : want part %d to be %s, got %s", c.path, i, c.parts[i], pi.Part()) 61 } 62 63 wantLeft := pi.Path()[:pi.Start()] 64 if pi.Left() != wantLeft { 65 t.Errorf("%s : want left %d to be %s, got %s", c.path, i, wantLeft, pi.Left()) 66 } 67 68 wantLeftPart := pi.Path()[:pi.End()] 69 if pi.LeftPart() != wantLeftPart { 70 t.Errorf("%s : want left %d to be %s, got %s", c.path, i, wantLeftPart, pi.LeftPart()) 71 } 72 73 wantRight := pi.Path()[pi.End():] 74 if pi.Right() != wantRight { 75 t.Errorf("%s : want right %d to be %s, got %s", c.path, i, wantRight, pi.Right()) 76 } 77 78 wantRightPart := pi.Path()[pi.Start():] 79 if pi.RightPart() != wantRightPart { 80 t.Errorf("%s : want right %d to be %s, got %s", c.path, i, wantRightPart, pi.RightPart()) 81 } 82 83 wantIsLast := i == (len(c.parts) - 1) 84 if pi.IsLast() != wantIsLast { 85 t.Errorf("%s : want IsLast %d to be %t, got %t", c.path, i, wantIsLast, pi.IsLast()) 86 } 87 } 88 89 if i != len(c.parts) { 90 t.Errorf("%s : want %d parts, got %d parts", pi.Path(), len(c.parts), i) 91 } 92 93 if c.osType == avfs.OsWindows { 94 if pi.VolumeNameLen() == 0 || pi.VolumeName() == "" { 95 t.Errorf("%s : want VolumeName != '', got ''", pi.Path()) 96 } 97 } else { 98 if pi.VolumeNameLen() != 0 || pi.VolumeName() != "" { 99 t.Errorf("%s : want VolumeName == '', got %s ", pi.Path(), pi.VolumeName()) 100 } 101 } 102 } 103 }) 104 105 t.Run("ReplacePart", func(t *testing.T) { 106 cases := []struct { 107 path string 108 part string 109 newPart string 110 newPath string 111 reset bool 112 osType avfs.OSType 113 }{ 114 { 115 path: `c:\path`, part: `path`, newPart: `..\..\..`, 116 newPath: `c:\`, reset: true, osType: avfs.OsWindows, 117 }, 118 { 119 path: `c:\an\absolute\path`, part: `absolute`, newPart: `c:\just\another`, 120 newPath: `c:\just\another\path`, reset: true, osType: avfs.OsWindows, 121 }, 122 { 123 path: `c:\a\random\path`, part: `random`, newPart: `very\long`, 124 newPath: `c:\a\very\long\path`, reset: false, osType: avfs.OsWindows, 125 }, 126 { 127 path: "/a/very/very/long/path", part: "long", newPart: "/a", 128 newPath: "/a/path", reset: true, osType: avfs.OsLinux, 129 }, 130 { 131 path: "/path", part: "path", newPart: "../../..", 132 newPath: "/", reset: true, osType: avfs.OsLinux, 133 }, 134 { 135 path: "/a/relative/path", part: "relative", newPart: "../../..", 136 newPath: "/path", reset: true, osType: avfs.OsLinux, 137 }, 138 { 139 path: "/an/absolute/path", part: "random", newPart: "/just/another", 140 newPath: "/just/another/path", reset: true, osType: avfs.OsLinux, 141 }, 142 { 143 path: "/a/relative/path", part: "relative", newPart: "very/long", 144 newPath: "/a/very/long/path", reset: false, osType: avfs.OsLinux, 145 }, 146 } 147 148 for _, c := range cases { 149 if c.osType != avfs.CurrentOSType() { 150 continue 151 } 152 153 pi := avfs.NewPathIterator(vfs, c.path) 154 for pi.Next() { 155 if pi.Part() == c.part { 156 reset := pi.ReplacePart(c.newPart) 157 158 if pi.Path() != c.newPath { 159 t.Errorf("%s : want new path to be %s, got %s", c.path, c.newPath, pi.Path()) 160 } 161 162 if reset != c.reset { 163 t.Errorf("%s : want Reset to be %t, got %t", c.path, c.reset, reset) 164 } 165 166 break 167 } 168 } 169 } 170 }) 171 }