Skip to content

Commit

Permalink
tests: supplement issue-40 testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
yeqown committed Nov 4, 2021
1 parent b4b1ed2 commit 1ee68af
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions qrcode_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package qrcode

import (
"log"
"crypto/md5"
"encoding/hex"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -176,16 +179,34 @@ func Test_New_WithBorderWidth(t *testing.T) {
// https://github.com/yeqown/go-qrcode/issues/40
func Test_Issue40(t *testing.T) {
qrc, err := New("https://baidu.com/")
if err != nil {
panic(err)
}

require.NoError(t, err)
err = qrc.Save("./testdata/issue40_1.png")
require.NoError(t, err)
err = qrc.Save("./testdata/issue40_2.png")
require.NoError(t, err)

h1, err := hashFile("./testdata/issue40_1.png")
require.NoError(t, err)
h2, err := hashFile("./testdata/issue40_2.png")
require.NoError(t, err)
t.Logf("hash1=%s, hash2=%s", h1, h2)
assert.Equal(t, h1, h2)
}

func hashFile(filename string) (string, error) {
h := md5.New()

fd1, err := os.Open(filename)
if err != nil {
log.Println(err)
return "", err
}
err = qrc.Save("./testdata/issue40_2.png")
bytes, err := io.ReadAll(fd1)
if err != nil {
log.Println(err)
return "", err
}
if _, err = h.Write(bytes); err != nil {
return "", err
}

return hex.EncodeToString(h.Sum(nil)), nil
}

0 comments on commit 1ee68af

Please sign in to comment.