util_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "runtime"
  9. "testing"
  10. )
  11. func TestCommonPrefix(t *testing.T) {
  12. test := func(first, second, expect string) {
  13. t.Helper()
  14. res := CommonPrefix(first, second)
  15. if res != expect {
  16. t.Errorf("Expected %s got %s", expect, res)
  17. }
  18. }
  19. if runtime.GOOS == "windows" {
  20. test(`c:\Audrius\Downloads`, `c:\Audrius\Docs`, `c:\Audrius`)
  21. test(`c:\Audrius\Downloads`, `C:\Audrius\Docs`, ``) // Case differences :(
  22. test(`C:\Audrius-a\Downloads`, `C:\Audrius-b\Docs`, `C:\`)
  23. test(`\\?\C:\Audrius-a\Downloads`, `\\?\C:\Audrius-b\Docs`, `\\?\C:\`)
  24. test(`\\?\C:\Audrius\Downloads`, `\\?\C:\Audrius\Docs`, `\\?\C:\Audrius`)
  25. test(`Audrius-a\Downloads`, `Audrius-b\Docs`, ``)
  26. test(`Audrius\Downloads`, `Audrius\Docs`, `Audrius`)
  27. test(`c:\Audrius\Downloads`, `Audrius\Docs`, ``)
  28. test(`c:\`, `c:\`, `c:\`)
  29. test(`\\?\c:\`, `\\?\c:\`, `\\?\c:\`)
  30. } else {
  31. test(`/Audrius/Downloads`, `/Audrius/Docs`, `/Audrius`)
  32. test(`/Audrius\Downloads`, `/Audrius\Docs`, `/`)
  33. test(`/Audrius-a/Downloads`, `/Audrius-b/Docs`, `/`)
  34. test(`Audrius\Downloads`, `Audrius\Docs`, ``) // Windows separators
  35. test(`Audrius/Downloads`, `Audrius/Docs`, `Audrius`)
  36. test(`Audrius-a\Downloads`, `Audrius-b\Docs`, ``)
  37. test(`/Audrius/Downloads`, `Audrius/Docs`, ``)
  38. test(`/`, `/`, `/`)
  39. }
  40. test(`Audrius`, `Audrius`, `Audrius`)
  41. test(`.`, `.`, `.`)
  42. }