Debian Codegraph @

writer/standard/writer_test.go

raw
1 package standard
2
3 import (
4 "crypto/md5"
5 "encoding/hex"
6 "image/png"
7 "io"
8 "os"
9 "testing"
10
11 "github.com/yeqown/go-qrcode/v2"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 func Test_New(t *testing.T) {
18 qrc, err := qrcode.New("cost 3+ days to read docs and coding")
19 require.NoError(t, err)
20
21 w, err := New("./testdata/qrtest.jpeg")
22 require.NoError(t, err)
23
24 // save file
25 err = qrc.Save(w)
26 require.NoError(t, err)
27
28 // check file existed
29 _, err = os.Stat("./testdata/qrtest.jpeg")
30 if err != nil {
31 t.Errorf("could not find image file: %v", err)
32 t.Fail()
33 }
34 }
35
36 func Test_New_WithOutputOption_bg_fg_width(t *testing.T) {
37 qrc, err := qrcode.New("Test_New_WithOutputOption_bg_fg_width")
38 require.NoError(t, err)
39
40 w, err := New("./testdata/qrtest_fg_bg.jpeg",
41 WithBgColorRGBHex("#b8de6f"),
42 WithFgColorRGBHex("#01c5c4"),
43 WithQRWidth(20),
44 )
45 require.NoError(t, err)
46
47 // save file
48 err = qrc.Save(w)
49 require.NoError(t, err)
50 }
51
52 func Test_New_WithOutputOption_Logo(t *testing.T) {
53 qrcode.SetDebugMode()
54
55 qrc, err := qrcode.New("Test_New_WithOutputOption_Logo")
56 require.NoError(t, err)
57
58 w, err := New("./testdata/qrtest_logo.jpeg",
59 WithBgColorRGBHex("#b8de6f"),
60 WithFgColorRGBHex("#f1e189"),
61 WithLogoImageFileJPEG("./testdata/logo.jpeg"),
62 //WithLogoImageFilePNG("./testdata/logo.png"), // png required
63 )
64 require.NoError(t, err)
65
66 // save file
67 err = qrc.Save(w)
68 require.NoError(t, err)
69 }
70
71 func Test_New_WithOutputOption_Shape(t *testing.T) {
72 qrc, err := qrcode.New("Test_New_WithOutputOption_Shape")
73 require.NoError(t, err)
74
75 w, err := New("./testdata/qrtest_circle.jpeg",
76 WithBgColorRGBHex("#b8de6f"),
77 WithFgColorRGBHex("#f1e189"),
78 WithCircleShape(),
79 )
80 require.NoError(t, err)
81
82 err = qrc.Save(w)
83 require.NoError(t, err)
84 }
85
86 func Test_New_WithBorderWidth(t *testing.T) {
87 qrc, err := qrcode.New("Test_New_WithOutputOption_Shape")
88 require.NoError(t, err)
89
90 w, err := New("./testdata/qrtest_border_width.jpeg", WithBorderWidth(10, 20, 30, 40))
91
92 // save file
93 err = qrc.Save(w)
94 require.NoError(t, err)
95 }
96
97 // Test_Issue40
98 // https://github.com/yeqown/go-qrcode/issues/40
99 func Test_Issue40(t *testing.T) {
100 qrc, err := qrcode.New("https://yeqown.xyzom/")
101 require.NoError(t, err)
102 w1, err := New("./testdata/issue40_1.png")
103 require.NoError(t, err)
104 err = qrc.Save(w1)
105 require.NoError(t, err)
106
107 w2, err := New("./testdata/issue40_2.png")
108 require.NoError(t, err)
109 err = qrc.Save(w2)
110 require.NoError(t, err)
111
112 h1, err := hashFile("./testdata/issue40_1.png")
113 require.NoError(t, err)
114 h2, err := hashFile("./testdata/issue40_2.png")
115 require.NoError(t, err)
116 t.Logf("hash1=%s, hash2=%s", h1, h2)
117 assert.Equal(t, h1, h2)
118 }
119
120 func hashFile(filename string) (string, error) {
121 h := md5.New()
122
123 fd1, err := os.Open(filename)
124 if err != nil {
125 return "", err
126 }
127 defer fd1.Close()
128
129 bytes, err := io.ReadAll(fd1)
130 if err != nil {
131 return "", err
132 }
133 if _, err = h.Write(bytes); err != nil {
134 return "", err
135 }
136
137 return hex.EncodeToString(h.Sum(nil)), nil
138 }
139
140 func statImage(filename string) (w, h int, err error) {
141 fd, err := os.Open(filename)
142 if err != nil {
143 return 0, 0, err
144 }
145 defer fd.Close()
146
147 img, err := png.Decode(fd)
148 if err != nil {
149 return 0, 0, err
150 }
151
152 rect := img.Bounds()
153 w, h = rect.Dx(), rect.Dy()
154 return
155 }
156
157 func Test_Attribute(t *testing.T) {
158 qrc, err := qrcode.New("https://yeqown.xyzom")
159 require.NoError(t, err)
160
161 w, err := New("./testdata/attr.png",
162 WithBuiltinImageEncoder(PNG_FORMAT),
163 WithQRWidth(13),
164 WithBorderWidth(1, 2, 3, 4),
165 )
166 require.NoError(t, err)
167
168 attr := w.Attribute(qrc.Dimension())
169 t.Logf("attr: %+v", attr)
170
171 err = qrc.Save(w)
172 require.NoError(t, err)
173
174 width, height, err := statImage("./testdata/attr.png")
175 require.NoError(t, err)
176 assert.Equal(t, width, attr.W)
177 assert.Equal(t, height, attr.H)
178 }
179
180 //
181 //func Test_image_draw(t *testing.T) {
182 // m := new(qrcode.Matrix)
183 // // set all 3rd column as black else be white
184 // for x := 0; x < m.Width(); x++ {
185 // _ = m.Set(x, 3, matrix.StateTrue)
186 // }
187 //
188 // fd, err := os.Create("./testdata/default.jpeg")
189 // require.NoError(t, err)
190 // err = drawTo(fd, *m, nil)
191 // require.NoError(t, err)
192 //}
193
194 func Test_writer_WithBgTransparent(t *testing.T) {
195 qrc, err := qrcode.New("https://yeqown.xyzom")
196 require.NoError(t, err)
197
198 w, err := New("./testdata/transparent.png",
199 WithBuiltinImageEncoder(PNG_FORMAT),
200 WithBorderWidth(20),
201 WithBgTransparent(),
202 )
203 require.NoError(t, err)
204
205 err = qrc.Save(w)
206 assert.NoError(t, err)
207 }