...
1package yaupdrepo
2
3import (
4 "bytes"
5 "strings"
6 "testing"
7
8 "golang.conradwood.net/go-easyops/utils"
9)
10
11func TestPosFinderLine2(t *testing.T) {
12 testval := `
13line1
14line2
15line3
16line4
17`
18 new_testval := `
19line1
20line2
21line2b
22line3
23line4
24`
25
26 ct := []byte(testval)
27 nct := []byte(new_testval)
28 pf := NewPositionFinder(ct)
29 x := pf.Content()
30 if !bytes.Equal(x, ct) {
31 t.Fatalf("content mismatch before any modification")
32 }
33 if !pf.find_line_containing("line2") {
34 t.Fatalf("failed to find pattern")
35 }
36 pf.add_line("line2b")
37 expect_content(t, nct, pf)
38}
39
40func TestPosFinderLine0(t *testing.T) {
41 testval := `
42line1
43line2
44line3
45line4
46`
47 new_testval := `
48line0
49line1
50line2
51line3
52line4
53`
54
55 ct := []byte(testval)
56 nct := []byte(new_testval)
57 pf := NewPositionFinder(ct)
58 x := pf.Content()
59 if !bytes.Equal(x, ct) {
60 t.Fatalf("content mismatch before any modification")
61 }
62 pf.add_line("line0")
63 expect_content(t, nct, pf)
64}
65
66func TestPosFinderLine5(t *testing.T) {
67 testval := `
68line1
69line2
70line3
71line4
72`
73 new_testval := `
74line1
75line2
76line3
77line4
78line4b
79`
80
81 ct := []byte(testval)
82 nct := []byte(new_testval)
83 pf := NewPositionFinder(ct)
84 x := pf.Content()
85 if !bytes.Equal(x, ct) {
86 t.Fatalf("content mismatch before any modification")
87 }
88 if !pf.find_line_containing("line4") {
89 t.Fatalf("failed to find pattern")
90 }
91 pf.add_line("line4b")
92 expect_content(t, nct, pf)
93}
94
95func TestPosFinderMultiLine(t *testing.T) {
96 testval := `
97line1
98line2
99line3
100line4
101`
102 new_testval := `
103line1
104line2
105line2b
106line3
107line4
108`
109
110 ct := []byte(testval)
111 nct := []byte(new_testval)
112 pf := NewPositionFinder(ct)
113 x := pf.Content()
114 if !bytes.Equal(x, ct) {
115 t.Fatalf("content mismatch before any modification")
116 }
117 pf.find_line_containing("line")
118 pf.find_line_containing("line")
119 pf.add_line("line2b")
120 expect_content(t, nct, pf)
121}
122
123func expect_content(t *testing.T, ct []byte, pf *positionFinder) {
124 if bytes.Equal(ct, pf.Content()) {
125 return
126 }
127 lines_expected := strings.Split(string(ct), "\n")
128 lines_got := strings.Split(string(pf.Content()), "\n")
129 max_lines := len(lines_expected)
130 if len(lines_got) > max_lines {
131 max_lines = len(lines_got)
132 }
133 ta := &utils.Table{}
134 ta.AddHeaders("got", "expected")
135 for i := 0; i < max_lines; i++ {
136 l1 := ""
137 l2 := ""
138 if i < len(lines_got) {
139 l1 = lines_got[i]
140 }
141 if i < len(lines_expected) {
142 l2 = lines_expected[i]
143 }
144 ta.AddString(l1)
145 ta.AddString(l2)
146 ta.NewRow()
147 }
148 t.Errorf("modification produced incorrect result")
149 t.Logf("\n" + ta.ToPrettyString())
150 // t.Logf("New Content:\n%s\n", string(pf.Content()))
151}
View as plain text