File: edit_test.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (162 lines) | stat: -rw-r--r-- 3,797 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2012 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package file

import (
	"os"
	"testing"

	"github.com/tredoe/osutil/sh"
)

func TestCreate(t *testing.T) {
	if err := CreateString(TEMP_FILE, `
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu 
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
culpa qui officia deserunt mollit anim id est laborum.
`); err != nil {
		t.Fatal(err)
	}

	out, err := sh.Runf("wc -l %s", TEMP_FILE)
	if err != nil {
		t.Fatal(err)
	}
	if out[0] != '7' {
		t.Fatalf("got %q lines, want 7", out[0])
	}
}

func TestEdit(t *testing.T) {
	line := "I've heard that the night is all magic.\n"

	e, err := NewEdit(TEMP_FILE)
	if err != nil {
		t.Fatal(err)
	}
	/*defer func() {
		if err = os.Remove(TEMP_FILE); err != nil {
			t.Error(err)
		}
	}()*/
	defer func() {
		if err = e.Close(); err != nil {
			t.Error(err)
		}
	}()

	// The backup should be created.
	if _, err = os.Stat(TEMP_BACKUP); err != nil {
		t.Error(err)
	}
	defer func() {
		if err = os.Remove(TEMP_BACKUP); err != nil {
			t.Error(err)
		}
	}()

	// Append
	if err = e.AppendString("\n" + line); err != nil {
		t.Error(err)
	} else {
		if out, _ := sh.Run("tail -n1 " + TEMP_FILE); string(out) != line {
			t.Errorf("Append => got %q, want %q", out, line)
		}
	}

	/*// Insert
	if err = e.InsertString(line); err != nil {
		t.Error(err)
	} else {
		if out, _, _ := sh.Run("head -n1 " + TEMP_FILE); out != line {
			t.Errorf("Insert => got %q, want %q", out, line)
		}
	}*/

	// Replace
	repl := []Replacer{
		{"dolor", "DOL_"},
		{"labor", "LABOR_"},
	}
	resul := "3\n"

	if err = e.Replace(repl); err != nil {
		t.Error(err)
	} else {
		if out, _ := sh.Runf("grep -c %s %s", repl[1].Replace, TEMP_FILE); string(out) != resul {
			t.Errorf("Replace (%s) => got %v, want %v", repl[1].Replace, out, resul)
		}
	}

	repl = []Replacer{
		{"DOL_", "dOlOr"},
		{"LABOR_", "lAbOr"},
	}
	resul = "1\n"

	if err = e.ReplaceN(repl, 1); err != nil {
		t.Error(err)
	} else {
		for i := 0; i <= 1; i++ {
			if out, _ := sh.Runf("grep -c %s %s", repl[i].Replace, TEMP_FILE); string(out) != resul {
				t.Errorf("Replace (%s) => got %v, want %v", repl[i].Replace, out, resul)
			}
		}
	}

	// ReplaceAtLine
	replAt := []ReplacerAtLine{
		{"LABOR", "o", "OO"},
	}
	resul = "2\n"

	if err = e.ReplaceAtLine(replAt); err != nil {
		t.Error(err)
	} else {
		if out, _ := sh.Run("grep -c OO " + TEMP_FILE); string(out) != resul {
			t.Errorf("ReplaceAtLine => got %v, want %v", out, resul)
		}
	}

	replAt = []ReplacerAtLine{
		{"heard", "a", "AA"},
	}
	resul = "1\n"

	if err = e.ReplaceAtLineN(replAt, 2); err != nil {
		t.Error(err)
	} else {
		if out, _ := sh.Runf("tail -n1 %s | grep -c A", TEMP_FILE); string(out) != resul {
			t.Errorf("ReplaceAtLineN => got %v, want %v", out, resul)
		}
	}

	// Comment
	resul = "2\n"

	if err = e.Comment([]string{"night", "quis"}); err != nil {
		t.Error(err)
	} else {
		if out, _ := sh.Runf("grep -c %s %s", e.CommentChar, TEMP_FILE); string(out) != resul {
			t.Errorf("Comment => got %v, want %v", out, resul)
		}
	}

	// CommentOut
	resul = "0\n"

	if err = e.CommentOut([]string{"night", "quis"}); err != nil {
		t.Error(err)
	} else {
		if out, _ := sh.Runf("grep -c %s %s", e.CommentChar, TEMP_FILE); string(out) != resul {
			t.Errorf("CommentOut => got %v, want %v", out, resul)
		}
	}
}