|
| 1 | +package tinystring |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestFilePathBase(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + path string |
| 8 | + want string |
| 9 | + }{ |
| 10 | + {"", "."}, |
| 11 | + {"/", "/"}, |
| 12 | + {"///", "/"}, |
| 13 | + {"a", "a"}, |
| 14 | + {"a/b", "b"}, |
| 15 | + {"a/b/", "b"}, |
| 16 | + {"/a/b/c", "c"}, |
| 17 | + {"/a/b/c/", "c"}, |
| 18 | + {"///a", "a"}, |
| 19 | + {"a//b", "b"}, |
| 20 | + {".", "."}, |
| 21 | + {"..", ".."}, |
| 22 | + {"no/slash_at_end", "slash_at_end"}, |
| 23 | + // file extension cases |
| 24 | + {"file.txt", "file.txt"}, |
| 25 | + {"dir/file.txt", "file.txt"}, |
| 26 | + {"/path/to/archive.tar.gz", "archive.tar.gz"}, |
| 27 | + {"dir/.hidden", ".hidden"}, |
| 28 | + {"a/b.c/d.e", "d.e"}, |
| 29 | + {"/trailing/.bashrc/", ".bashrc"}, |
| 30 | + } |
| 31 | + |
| 32 | + for _, tc := range tests { |
| 33 | + t.Run(tc.path, func(t *testing.T) { |
| 34 | + got := PathBase(tc.path) |
| 35 | + if got != tc.want { |
| 36 | + t.Fatalf("FilePathBase(%q) = %q; want %q", tc.path, got, tc.want) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestPathBaseWindows(t *testing.T) { |
| 43 | + tests := []struct{ path, want string }{ |
| 44 | + {`C:\`, `\`}, |
| 45 | + {`C:\file.exe`, `file.exe`}, |
| 46 | + {`C:\Program Files\App\app.exe`, `app.exe`}, |
| 47 | + {`C:\dir\\sub\file.txt`, `file.txt`}, |
| 48 | + {`\\server\share\file`, `file`}, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tc := range tests { |
| 52 | + t.Run(tc.path, func(t *testing.T) { |
| 53 | + got := PathBase(tc.path) |
| 54 | + if got != tc.want { |
| 55 | + t.Fatalf("PathBase(%q) = %q; want %q", tc.path, got, tc.want) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
0 commit comments