example_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. /*
  4. Amendments Thu, 2017-December-14:
  5. - test integration (go test -v)
  6. - idiomatic go code
  7. */
  8. package qrcode
  9. import (
  10. "fmt"
  11. "image/color"
  12. "os"
  13. "testing"
  14. )
  15. func TestExampleEncode(t *testing.T) {
  16. if png, err := Encode("https://example.org", Medium, 256); err != nil {
  17. t.Errorf("Error: %s", err.Error())
  18. } else {
  19. fmt.Printf("PNG is %d bytes long", len(png))
  20. }
  21. }
  22. func TestExampleWriteFile(t *testing.T) {
  23. filename := "example.png"
  24. if err := WriteFile("https://example.org", Medium, 256, filename); err != nil {
  25. if err = os.Remove(filename); err != nil {
  26. t.Errorf("Error: %s", err.Error())
  27. }
  28. }
  29. }
  30. func TestExampleEncodeWithColourAndWithoutBorder(t *testing.T) {
  31. q, err := New("https://example.org", Medium)
  32. if err != nil {
  33. t.Errorf("Error: %s", err)
  34. return
  35. }
  36. // Optionally, disable the QR Code border.
  37. q.DisableBorder = true
  38. // Optionally, set the colours.
  39. q.ForegroundColor = color.RGBA{R: 0x33, G: 0x33, B: 0x66, A: 0xff}
  40. q.BackgroundColor = color.RGBA{R: 0xef, G: 0xef, B: 0xef, A: 0xff}
  41. err = q.WriteFile(256, "example2.png")
  42. if err != nil {
  43. t.Errorf("Error: %s", err)
  44. return
  45. }
  46. }