@@ -0,0 +1,32 @@
1+package fsext
2+
3+import (
4+ "path/filepath"
5+ "testing"
6+
7+ "github.com/stretchr/testify/require"
8+)
9+
10+func TestDirTrimUnicode(t *testing.T) {
11+ sep := string(filepath.Separator)
12+
13+ // An abbreviated component keeps its first character.
14+ require.Equal(t,
15+ filepath.Join("~", "...", "p", "file"),
16+ DirTrim(sep+"home"+sep+"user"+sep+"project"+sep+"file", 2),
17+ )
18+
19+ // A multi-byte (e.g. CJK) component must keep its first whole character,
20+ // not its first byte, which would render a wrong character.
21+ require.Equal(t,
22+ filepath.Join("~", "...", "项", "file"),
23+ DirTrim(sep+"home"+sep+"项目"+sep+"file", 2),
24+ )
25+
26+ // A grapheme cluster spanning multiple runes (a base letter plus a
27+ // combining accent) must be kept whole, not split after the base rune.
28+ require.Equal(t,
29+ filepath.Join("~", "...", "e\u0301", "file"),
30+ DirTrim(sep+"home"+sep+"e\u0301cole"+sep+"file", 2),
31+ )
32+}