forked from alexbrainman/printer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer_test.go
105 lines (94 loc) · 2.01 KB
/
printer_test.go
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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package printer
import (
"encoding/json"
"fmt"
"testing"
)
func TestPrinter(t *testing.T) {
name, err := Default()
if err != nil {
t.Fatalf("Default failed: %v", err)
}
p, err := Open(name)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer p.Close()
err = p.StartDocument("my document", "RAW")
if err != nil {
t.Fatalf("StartDocument failed: %v", err)
}
defer p.EndDocument()
err = p.StartPage()
if err != nil {
t.Fatalf("StartPage failed: %v", err)
}
fmt.Fprintf(p, "Hello %q\n", name)
err = p.EndPage()
if err != nil {
t.Fatalf("EndPage failed: %v", err)
}
}
func TestReadNames(t *testing.T) {
names, err := ReadNames()
if err != nil {
t.Fatalf("ReadNames failed: %v", err)
}
name, err := Default()
if err != nil {
t.Fatalf("Default failed: %v", err)
}
// make sure default printer is listed
for _, v := range names {
if v == name {
return
}
}
t.Fatalf("Default printed %q is not listed amongst printers returned by ReadNames %q", name, names)
}
func TestDriverInfo(t *testing.T) {
name, err := Default()
if err != nil {
t.Fatalf("Default failed: %v", err)
}
p, err := Open(name)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer p.Close()
di, err := p.DriverInfo()
if err != nil {
t.Fatalf("DriverInfo failed: %v", err)
}
t.Logf("%+v", di)
}
func TestJobs(t *testing.T) {
names, err := ReadNames()
if err != nil {
t.Fatalf("ReadNames failed: %v", err)
}
for _, name := range names {
t.Log("Printer Name:", name)
p, err := Open(name)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer p.Close()
pj, err := p.Jobs()
if err != nil {
t.Fatalf("Jobs failed: %v", err)
}
if len(pj) > 0 {
t.Log("Print Jobs:", len(pj))
for _, j := range pj {
b, err := json.MarshalIndent(j, "", " ")
if err == nil && len(b) > 0 {
t.Log(string(b))
}
}
}
}
}