package git

import "testing"

func TestUnquoteGitPath(t *testing.T) {
	cases := []struct{ name, in, want string }{
		{"plain", "src/main.go", "src/main.go"},
		{"space_no_quotes", "a b/c.txt", "a b/c.txt"},
		{"unicode_octal", `"public/assets/img/valentine\342\200\231s/x.png"`, "public/assets/img/valentine’s/x.png"},
		{"quoted_space", `"with space/a b.txt"`, "with space/a b.txt"},
		{"escaped_tab", "\"tab\\tinside.txt\"", "tab\tinside.txt"},
		{"escaped_quote", "\"quote\\\".txt\"", "quote\".txt"},
		{"escaped_backslash", "\"back\\\\slash.txt\"", "back\\slash.txt"},
	}
	for _, c := range cases {
		if got := unquoteGitPath(c.in); got != c.want {
			t.Errorf("%s: unquoteGitPath(%q) = %q, want %q", c.name, c.in, got, c.want)
		}
	}
}

func TestParseNameStatusUnquotesSpecialCharPath(t *testing.T) {
	// git diff --name-status output for an added file whose name has a unicode
	// apostrophe (U+2019 -> UTF-8 E2 80 99 -> octal \342\200\231), C-quoted by git.
	out := "A\t\"public/valentine\\342\\200\\231s/x.png\""
	changes, err := ParseNameStatus(out)
	if err != nil {
		t.Fatalf("ParseNameStatus: %v", err)
	}
	if len(changes) != 1 {
		t.Fatalf("want 1 change, got %d", len(changes))
	}
	want := "public/valentine’s/x.png"
	if changes[0].Path != want {
		t.Errorf("Path = %q, want %q", changes[0].Path, want)
	}
}
