fork of https://github.com/sourcegraph/zoekt
1package web
2
3import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7)
8
9func TestAddLineNumbers(t *testing.T) {
10 tests := []struct {
11 name string
12 content string
13 lineNum int
14 isBefore bool
15 want []lineMatch
16 }{
17 {
18 name: "empty content",
19 content: "",
20 lineNum: 10,
21 isBefore: true,
22 want: nil,
23 },
24 {
25 name: "single line before",
26 content: "hello world",
27 lineNum: 10,
28 isBefore: true,
29 want: []lineMatch{
30 {LineNum: 9, Content: "hello world"},
31 },
32 },
33 {
34 name: "single line after",
35 content: "hello world",
36 lineNum: 10,
37 isBefore: false,
38 want: []lineMatch{
39 {LineNum: 11, Content: "hello world"},
40 },
41 },
42 {
43 name: "multiple lines before",
44 content: "first line\nsecond line\nthird line",
45 lineNum: 10,
46 isBefore: true,
47 want: []lineMatch{
48 {LineNum: 7, Content: "first line"},
49 {LineNum: 8, Content: "second line"},
50 {LineNum: 9, Content: "third line"},
51 },
52 },
53 {
54 name: "multiple lines after",
55 content: "first line\nsecond line\nthird line",
56 lineNum: 10,
57 isBefore: false,
58 want: []lineMatch{
59 {LineNum: 11, Content: "first line"},
60 {LineNum: 12, Content: "second line"},
61 {LineNum: 13, Content: "third line"},
62 },
63 },
64 {
65 name: "content with empty lines before",
66 content: "first line\n\nthird line",
67 lineNum: 10,
68 isBefore: true,
69 want: []lineMatch{
70 {LineNum: 7, Content: "first line"},
71 {LineNum: 8, Content: ""},
72 {LineNum: 9, Content: "third line"},
73 },
74 },
75 {
76 name: "content with empty lines after",
77 content: "first line\n\nthird line",
78 lineNum: 10,
79 isBefore: false,
80 want: []lineMatch{
81 {LineNum: 11, Content: "first line"},
82 {LineNum: 12, Content: ""},
83 {LineNum: 13, Content: "third line"},
84 },
85 },
86 }
87
88 for _, tt := range tests {
89 t.Run(tt.name, func(t *testing.T) {
90 got := AddLineNumbers(tt.content, tt.lineNum, tt.isBefore)
91 if diff := cmp.Diff(tt.want, got); diff != "" {
92 t.Errorf("AddLineNumbers() mismatch (-want +got):\n%s", diff)
93 }
94 })
95 }
96}