-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_readlines_test.go
More file actions
45 lines (37 loc) · 1.02 KB
/
t_readlines_test.go
File metadata and controls
45 lines (37 loc) · 1.02 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
package main
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestReadLinesReturnsAllLinesInOrder(t *testing.T) {
tempDir := t.TempDir()
path := filepath.Join(tempDir, "sample.txt")
content := "first\nsecond\nthird\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
got, err := readLines(path)
if err != nil {
t.Fatalf("readLines returned unexpected error: %v", err)
}
want := []string{"first", "second", "third"}
if len(got) != len(want) {
t.Fatalf("line count mismatch: got %d, want %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("line %d mismatch: got %q, want %q", i, got[i], want[i])
}
}
}
func TestReadLinesMissingFileReturnsError(t *testing.T) {
_, err := readLines(filepath.Join(t.TempDir(), "does-not-exist.txt"))
if err == nil {
t.Fatal("expected error for missing file, got nil")
}
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected os.ErrNotExist, got %v", err)
}
}