互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

5 years ago
  1. package utils
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "testing"
  5. )
  6. func TestQrcode2Base64(t *testing.T) {
  7. Convey("测试生成二维码并把二维码变成base64编码", t, func() {
  8. urlPath := "https://www.baidu.com"
  9. code, err := Qrcode2Base64(urlPath)
  10. So(err, ShouldBeNil)
  11. So(code, ShouldStartWith, "data:image/png;base64,")
  12. })
  13. }
  14. func TestFloat64CusDecimal(t *testing.T) {
  15. Convey("测试把float64精度截断", t, func() {
  16. f1 := 2000.22202
  17. f := Float64CusDecimal(f1, 2)
  18. So(f, ShouldEqual, 2000.22)
  19. f2 := 5000.4
  20. f = Float64CusDecimal(f2, 1)
  21. So(f, ShouldEqual, f2)
  22. f3 := 5000.509
  23. f = Float64CusDecimal(f3, 2)
  24. So(f, ShouldNotEqual, 5000.50)
  25. })
  26. }
  27. func TestGif2Base64(t *testing.T) {
  28. Convey("测试把gif编码为base64", t, func() {
  29. urlPath := "http://photocdn.sohu.com/20150721/mp23627612_1437451852870_2.gif"
  30. gif, err := Gif2Base64(urlPath)
  31. So(err, ShouldEqual, nil)
  32. So(gif, ShouldStartWith, "data:image/gif;base64")
  33. urlPath = "https://www.baidu.com"
  34. gif, err = Gif2Base64(urlPath)
  35. So(err, ShouldBeNil)
  36. So(urlPath, ShouldEqual, urlPath)
  37. urlPath = "xxxx.gif"
  38. gif, err = Gif2Base64(urlPath)
  39. So(err, ShouldNotEqual, nil)
  40. })
  41. }
  42. func TestPathExists(t *testing.T) {
  43. Convey("测试文件是否存在", t, func() {
  44. // 存在状态
  45. path := "utils.go"
  46. exist, err := PathExists(path)
  47. So(err, ShouldEqual, nil)
  48. So(exist, ShouldBeTrue)
  49. // 不存在状态
  50. path = "utils.+"
  51. exist, err = PathExists(path)
  52. So(err, ShouldBeNil)
  53. So(exist, ShouldBeFalse)
  54. })
  55. }
  56. func TestCountDownFormat(t *testing.T) {
  57. Convey("测试倒计时格式", t, func() {
  58. num := 12000
  59. str := CountDownFormat(num)
  60. So(str, ShouldEqual, "3:20:0")
  61. })
  62. }