-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
100 lines (92 loc) · 2.27 KB
/
Copy pathexample_test.go
File metadata and controls
100 lines (92 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package applypatch_test
import (
"fmt"
"strings"
"testing"
"github.com/fgn/applypatch"
)
func ExampleApply() {
input := "def greet():\n print(\"Hi\")\n"
diff := "@@ def greet():\n- print(\"Hi\")\n+ print(\"Hello\")"
output, err := applypatch.Apply(input, diff)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(output)
// Output:
// def greet():
// print("Hello")
}
func ExampleMode_Apply() {
// Strict mode rejects a diff that only matches when whitespace is
// ignored; Codex mode applies it like the Codex CLI would.
input := "total \n"
diff := "-total\n+sum"
_, err := applypatch.Strict.Apply(input, diff)
fmt.Println("strict:", err)
output, _ := applypatch.Codex.Apply(input, diff)
fmt.Printf("codex: %q\n", output)
// Output:
// strict: target must match exactly once
// codex: "sum\n"
}
func ExampleApplyFiles() {
files := map[string]string{"greet.py": "print(\"Hi\")\n"}
patch := `*** Begin Patch
*** Add File: README.md
+# Greeter
*** Update File: greet.py
@@
-print("Hi")
+print("Hello")
*** End Patch`
output, err := applypatch.Codex.ApplyFiles(files, patch)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%q\n%q\n", output["README.md"], output["greet.py"])
// Output:
// "# Greeter\n"
// "print(\"Hello\")\n"
}
func ExampleCodexTool() {
tool := applypatch.CodexTool()
fmt.Println(tool.Type, tool.Name, tool.Format.Type, tool.Format.Syntax)
// Output: custom apply_patch grammar lark
}
func ExampleApplyBatch() {
input := "alpha\nbeta\ngamma\n"
output, err := applypatch.ApplyBatch(input, []string{"-alpha\n+ALPHA", "-gamma\n+GAMMA"})
if err != nil {
fmt.Println(err)
return
}
fmt.Print(output)
// Output:
// ALPHA
// beta
// GAMMA
}
// BenchmarkParseLargePatch guards against quadratic parsing of long updates.
func BenchmarkParseLargePatch(b *testing.B) {
var patch strings.Builder
patch.WriteString("*** Begin Patch\n*** Add File: big.txt\n")
for range 20000 {
patch.WriteString("+a line of added text\n")
}
patch.WriteString("*** Update File: other.txt\n@@\n")
for range 20000 {
patch.WriteString(" a line of context\n")
}
patch.WriteString("*** End Patch")
text := patch.String()
b.SetBytes(int64(len(text)))
for b.Loop() {
_, err := applypatch.Parse(text)
if err != nil {
b.Fatal(err)
}
}
}