internal/suggest/suggest_test.go

v1.40.1
gitbay/internal/suggest/suggest_test.go history · blame · raw

100 lines · 3393 bytes

  1package suggest
  2
  3import (
  4	"reflect"
  5	"strings"
  6	"testing"
  7)
  8
  9func TestParse(t *testing.T) {
 10	cases := []struct {
 11		name  string
 12		body  string
 13		lines []string
 14		found bool
 15		err   string
 16	}{
 17		{"none", "just prose\n```go\nx\n```\n", nil, false, ""},
 18		{"one line", "try this\n```suggestion\nreturn nil\n```\n", []string{"return nil"}, true, ""},
 19		{"several", "```suggestion\na\n\tb\n```", []string{"a", "\tb"}, true, ""},
 20		{"deletion", "drop it\n```suggestion\n```\n", []string{}, true, ""},
 21		{"blank line", "```suggestion\n\n```\n", []string{""}, true, ""},
 22		{"crlf body", "x\r\n```suggestion\r\nnew\r\n```\r\n", []string{"new"}, true, ""},
 23		{"longer fence", "````suggestion\n```\n````\n", []string{"```"}, true, ""},
 24		{"unclosed", "```suggestion\nnew\n", nil, false, "not closed"},
 25		{"two", "```suggestion\na\n```\n```suggestion\nb\n```\n", nil, false, "one suggestion"},
 26	}
 27	for _, c := range cases {
 28		lines, found, err := Parse(c.body)
 29		if c.err != "" {
 30			if err == nil || !strings.Contains(err.Error(), c.err) {
 31				t.Errorf("%s: err = %v, want %q", c.name, err, c.err)
 32			}
 33			continue
 34		}
 35		if err != nil || found != c.found || (c.found && !reflect.DeepEqual(lines, c.lines)) {
 36			t.Errorf("%s: Parse = %q %v %v, want %q %v", c.name, lines, found, err, c.lines, c.found)
 37		}
 38	}
 39}
 40
 41func TestStrip(t *testing.T) {
 42	if got := Strip("use this\n```suggestion\nx\n```\nthanks"); got != "use this\nthanks" {
 43		t.Errorf("Strip = %q", got)
 44	}
 45	if got := Strip("```suggestion\nx\n```"); got != "" {
 46		t.Errorf("Strip of a bare block = %q", got)
 47	}
 48}
 49
 50func TestTextRoundTrip(t *testing.T) {
 51	for _, lines := range [][]string{nil, {""}, {"a"}, {"a", "", "b"}} {
 52		if got := FromText(Text(lines)); !reflect.DeepEqual(got, lines) && !(len(got) == 0 && len(lines) == 0) {
 53			t.Errorf("FromText(Text(%q)) = %q", lines, got)
 54		}
 55	}
 56}
 57
 58func TestApply(t *testing.T) {
 59	cases := []struct {
 60		name       string
 61		content    string
 62		start, end int
 63		repl       []string
 64		want       string
 65		err        string
 66	}{
 67		{"one line", "a\nb\nc\n", 2, 2, []string{"B"}, "a\nB\nc\n", ""},
 68		{"range to more", "a\nb\nc\nd\n", 2, 3, []string{"x", "y", "z"}, "a\nx\ny\nz\nd\n", ""},
 69		{"range to fewer", "a\nb\nc\nd\n", 1, 3, []string{"x"}, "x\nd\n", ""},
 70		{"deletion", "a\nb\nc\n", 2, 2, nil, "a\nc\n", ""},
 71		{"delete all", "a\nb\n", 1, 2, nil, "", ""},
 72		{"eof no newline", "a\nb", 2, 2, []string{"B", "C"}, "a\nB\nC", ""},
 73		{"eof with newline", "a\nb\n", 2, 2, []string{"B"}, "a\nB\n", ""},
 74		{"crlf", "a\r\nb\r\nc\r\n", 2, 2, []string{"x", "y"}, "a\r\nx\r\ny\r\nc\r\n", ""},
 75		{"crlf eof no newline", "a\r\nb", 2, 2, []string{"x", "y"}, "a\r\nx\r\ny", ""},
 76		{"past eof", "a\nb\n", 2, 3, []string{"x"}, "", "has 2 lines"},
 77		{"bad range", "a\n", 2, 1, nil, "", "bad line range"},
 78	}
 79	for _, c := range cases {
 80		got, err := Apply([]byte(c.content), c.start, c.end, c.repl)
 81		if c.err != "" {
 82			if err == nil || !strings.Contains(err.Error(), c.err) {
 83				t.Errorf("%s: err = %v, want %q", c.name, err, c.err)
 84			}
 85			continue
 86		}
 87		if err != nil || string(got) != c.want {
 88			t.Errorf("%s: Apply = %q, %v; want %q", c.name, got, err, c.want)
 89		}
 90	}
 91}
 92
 93func TestRange(t *testing.T) {
 94	if got, ok := Range([]byte("a\r\nb\nc"), 2, 3); !ok || string(got) != "b\nc" {
 95		t.Errorf("Range = %q %v", got, ok)
 96	}
 97	if _, ok := Range([]byte("a\n"), 1, 2); ok {
 98		t.Error("Range past the end reported ok")
 99	}
100}