-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter_test.go
More file actions
73 lines (63 loc) · 1.6 KB
/
printer_test.go
File metadata and controls
73 lines (63 loc) · 1.6 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"time"
)
var repo = repository{
Name: "testrepo",
FullName: "testuser/testrepo",
Description: "this is test repository",
Owner: "testuser",
Stars: 7777,
URL: "http://www.google.com/",
PushedAt: time.Now(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
func TestIOPrinter(t *testing.T) {
buf := new(bytes.Buffer)
p := &ioprinter{w: buf, params: "name,stars"}
if err := p.Print(repo); err != nil {
t.Fatal(err)
}
exp := fmt.Sprintf("%s %d\n", repo.Name, repo.Stars)
if buf.String() != exp {
t.Fatalf("unxpected value. expected: %q actual: %q", exp, buf.String())
}
}
func TestJsonPrinter(t *testing.T) {
buf := new(bytes.Buffer)
p := &jsonprinter{w: buf}
if err := p.Print(repo); err != nil {
t.Fatal(err)
}
b, err := json.Marshal(repo)
if err != nil {
t.Fatal(err)
}
exp := string(b) + "\n" // printer adds `\n` to end of line.
if buf.String() != exp {
t.Fatalf("unxpected value. expected: %q actual: %q", exp, buf.String())
}
}
func TestMultiPrinter(t *testing.T) {
b1 := new(bytes.Buffer)
b2 := new(bytes.Buffer)
bp1 := &ioprinter{w: b1, params: "name"}
bp2 := &ioprinter{w: b2, params: "stars"}
mp := multiprinter{[]printer{bp1, bp2}}
if err := mp.Print(repo); err != nil {
t.Fatal(err)
}
exp := fmt.Sprintf("%s\n", repo.Name)
if b1.String() != exp {
t.Fatalf("unexpected value, expected: %q actual: %q", exp, b1.String())
}
exp = fmt.Sprintf("%d\n", repo.Stars)
if b2.String() != exp {
t.Fatalf("unexpected value. expected %q, actual: %q", exp, b2.String())
}
}