main_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package hyperlinked_grep
  3. import (
  4. "fmt"
  5. "kitty/tools/utils/shlex"
  6. "testing"
  7. "github.com/google/go-cmp/cmp"
  8. )
  9. var _ = fmt.Print
  10. func TestRgArgParsing(t *testing.T) {
  11. if RgExe() == "rg" {
  12. t.Skip("Skipping as rg not found in PATH")
  13. }
  14. check_failure := func(args ...string) {
  15. _, _, _, err := parse_args(args...)
  16. if err == nil {
  17. t.Fatalf("No error when parsing: %#v", args)
  18. }
  19. }
  20. check_failure("--kitten", "xyz")
  21. check_failure("--kitten", "xyz=1")
  22. check_kitten_opts := func(matching, context, headers bool, args ...string) {
  23. _, _, kitten_opts, err := parse_args(args...)
  24. if err != nil {
  25. t.Fatalf("error when parsing: %#v: %s", args, err)
  26. }
  27. if matching != kitten_opts.matching_lines {
  28. t.Fatalf("Matching lines not correct for: %#v", args)
  29. }
  30. if context != kitten_opts.context_lines {
  31. t.Fatalf("Context lines not correct for: %#v", args)
  32. }
  33. if headers != kitten_opts.file_headers {
  34. t.Fatalf("File headers not correct for: %#v", args)
  35. }
  36. }
  37. check_kitten_opts(true, true, true)
  38. check_kitten_opts(false, false, false, "--kitten", "hyperlink=none")
  39. check_kitten_opts(false, false, true, "--kitten", "hyperlink=none", "--count", "--kitten=hyperlink=file_headers")
  40. check_kitten_opts(false, false, true, "--kitten", "hyperlink=none,file_headers")
  41. check_kitten_opts = func(with_filename, heading, line_number bool, args ...string) {
  42. _, _, kitten_opts, err := parse_args(args...)
  43. if err != nil {
  44. t.Fatalf("error when parsing: %#v: %s", args, err)
  45. }
  46. if with_filename != kitten_opts.with_filename {
  47. t.Fatalf("with_filename not correct for: %#v", args)
  48. }
  49. if heading != kitten_opts.heading {
  50. t.Fatalf("heading not correct for: %#v", args)
  51. }
  52. if line_number != kitten_opts.line_number {
  53. t.Fatalf("line_number not correct for: %#v", args)
  54. }
  55. }
  56. check_kitten_opts(true, true, true)
  57. check_kitten_opts(true, false, true, "--no-heading")
  58. check_kitten_opts(true, true, true, "--no-heading", "--pretty")
  59. check_kitten_opts(true, true, true, "--no-heading", "--heading")
  60. check_args := func(args, expected string) {
  61. a, err := shlex.Split(args)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. _, actual, _, err := parse_args(a...)
  66. if err != nil {
  67. t.Fatalf("error when parsing: %#v: %s", args, err)
  68. }
  69. ex, err := shlex.Split(expected)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if diff := cmp.Diff(ex, actual); diff != "" {
  74. t.Fatalf("args not correct for %s\n%s", args, diff)
  75. }
  76. }
  77. check_args("--count --max-depth 10 --XxX yyy abcd", "--count --max-depth 10 --XxX yyy abcd")
  78. check_args("--max-depth=10 --kitten hyperlink=none abcd", "--max-depth=10 abcd")
  79. check_args("-m 10 abcd", "--max-count 10 abcd")
  80. check_args("-nm 10 abcd", "-n --max-count 10 abcd")
  81. check_args("-mn 10 abcd", "-n --max-count 10 abcd")
  82. }