/pack/bundle/opt/fzf/src/terminal_test.go

https://bitbucket.org/skeept/dotvim · Go · 139 lines · 93 code · 31 blank · 15 comment · 5 complexity · a5c325be00a2df09dd51175efee1de8d MD5 · raw file

  1. package fzf
  2. import (
  3. "regexp"
  4. "testing"
  5. "github.com/junegunn/fzf/src/util"
  6. )
  7. func newItem(str string) *Item {
  8. bytes := []byte(str)
  9. trimmed, _, _ := extractColor(str, nil, nil)
  10. return &Item{origText: &bytes, text: util.ToChars([]byte(trimmed))}
  11. }
  12. func TestReplacePlaceholder(t *testing.T) {
  13. item1 := newItem(" foo'bar \x1b[31mbaz\x1b[m")
  14. items1 := []*Item{item1, item1}
  15. items2 := []*Item{
  16. newItem("foo'bar \x1b[31mbaz\x1b[m"),
  17. newItem("foo'bar \x1b[31mbaz\x1b[m"),
  18. newItem("FOO'BAR \x1b[31mBAZ\x1b[m")}
  19. delim := "'"
  20. var regex *regexp.Regexp
  21. var result string
  22. check := func(expected string) {
  23. if result != expected {
  24. t.Errorf("expected: %s, actual: %s", expected, result)
  25. }
  26. }
  27. printsep := "\n"
  28. // {}, preserve ansi
  29. result = replacePlaceholder("echo {}", false, Delimiter{}, printsep, false, "query", items1)
  30. check("echo ' foo'\\''bar \x1b[31mbaz\x1b[m'")
  31. // {}, strip ansi
  32. result = replacePlaceholder("echo {}", true, Delimiter{}, printsep, false, "query", items1)
  33. check("echo ' foo'\\''bar baz'")
  34. // {}, with multiple items
  35. result = replacePlaceholder("echo {}", true, Delimiter{}, printsep, false, "query", items2)
  36. check("echo 'foo'\\''bar baz'")
  37. // {..}, strip leading whitespaces, preserve ansi
  38. result = replacePlaceholder("echo {..}", false, Delimiter{}, printsep, false, "query", items1)
  39. check("echo 'foo'\\''bar \x1b[31mbaz\x1b[m'")
  40. // {..}, strip leading whitespaces, strip ansi
  41. result = replacePlaceholder("echo {..}", true, Delimiter{}, printsep, false, "query", items1)
  42. check("echo 'foo'\\''bar baz'")
  43. // {q}
  44. result = replacePlaceholder("echo {} {q}", true, Delimiter{}, printsep, false, "query", items1)
  45. check("echo ' foo'\\''bar baz' 'query'")
  46. // {q}, multiple items
  47. result = replacePlaceholder("echo {+}{q}{+}", true, Delimiter{}, printsep, false, "query 'string'", items2)
  48. check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ''query '\\''string'\\''''foo'\\''bar baz' 'FOO'\\''BAR BAZ'")
  49. result = replacePlaceholder("echo {}{q}{}", true, Delimiter{}, printsep, false, "query 'string'", items2)
  50. check("echo 'foo'\\''bar baz''query '\\''string'\\''''foo'\\''bar baz'")
  51. result = replacePlaceholder("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, false, "query", items1)
  52. check("echo 'foo'\\''bar'/'baz'/'bazfoo'\\''bar'/'baz'/'foo'\\''bar'/' foo'\\''bar baz'/'foo'\\''bar baz'/{n.t}/{}/{1}/{q}/''")
  53. result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, false, "query", items2)
  54. check("echo 'foo'\\''bar'/'baz'/'baz'/'foo'\\''bar'/'foo'\\''bar baz'/{n.t}/{}/{1}/{q}/''")
  55. result = replacePlaceholder("echo {+1}/{+2}/{+-1}/{+-2}/{+..}/{n.t}/\\{}/\\{1}/\\{q}/{+3}", true, Delimiter{}, printsep, false, "query", items2)
  56. check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")
  57. // forcePlus
  58. result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, true, "query", items2)
  59. check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")
  60. // Whitespace preserving flag with "'" delimiter
  61. result = replacePlaceholder("echo {s1}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
  62. check("echo ' foo'")
  63. result = replacePlaceholder("echo {s2}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
  64. check("echo 'bar baz'")
  65. result = replacePlaceholder("echo {s}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
  66. check("echo ' foo'\\''bar baz'")
  67. result = replacePlaceholder("echo {s..}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
  68. check("echo ' foo'\\''bar baz'")
  69. // Whitespace preserving flag with regex delimiter
  70. regex = regexp.MustCompile(`\w+`)
  71. result = replacePlaceholder("echo {s1}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
  72. check("echo ' '")
  73. result = replacePlaceholder("echo {s2}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
  74. check("echo ''\\'''")
  75. result = replacePlaceholder("echo {s3}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
  76. check("echo ' '")
  77. // No match
  78. result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, printsep, false, "query", []*Item{nil, nil})
  79. check("echo /")
  80. // No match, but with selections
  81. result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, printsep, false, "query", []*Item{nil, item1})
  82. check("echo /' foo'\\''bar baz'")
  83. // String delimiter
  84. result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
  85. check("echo ' foo'\\''bar baz'/'foo'/'bar baz'")
  86. // Regex delimiter
  87. regex = regexp.MustCompile("[oa]+")
  88. // foo'bar baz
  89. result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
  90. check("echo ' foo'\\''bar baz'/'f'/'r b'/''\\''bar b'")
  91. }
  92. func TestQuoteEntryCmd(t *testing.T) {
  93. tests := map[string]string{
  94. `"`: `^"\^"^"`,
  95. `\`: `^"\\^"`,
  96. `\"`: `^"\\\^"^"`,
  97. `"\\\"`: `^"\^"\\\\\\\^"^"`,
  98. `&|<>()@^%!`: `^"^&^|^<^>^(^)^@^^^%^!^"`,
  99. `%USERPROFILE%`: `^"^%USERPROFILE^%^"`,
  100. `C:\Program Files (x86)\`: `^"C:\\Program Files ^(x86^)\\^"`,
  101. }
  102. for input, expected := range tests {
  103. escaped := quoteEntryCmd(input)
  104. if escaped != expected {
  105. t.Errorf("Input: %s, expected: %s, actual %s", input, expected, escaped)
  106. }
  107. }
  108. }