text_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package gocaptcha
  2. import (
  3. "image"
  4. "image/draw"
  5. "math/rand"
  6. "testing"
  7. )
  8. func Test_textDrawer_DrawString(t *testing.T) {
  9. type args struct {
  10. canvas draw.Image
  11. text string
  12. }
  13. tests := []struct {
  14. name string
  15. t *textDrawer
  16. args args
  17. wantErr bool
  18. }{
  19. {
  20. name: "Successful DrawString",
  21. t: &textDrawer{r: rand.New(rand.NewSource(1))},
  22. args: args{
  23. canvas: image.NewRGBA(image.Rect(0, 0, 100, 100)),
  24. text: "Hello, World!",
  25. },
  26. wantErr: false,
  27. },
  28. {
  29. name: "DrawString with empty text",
  30. t: &textDrawer{r: rand.New(rand.NewSource(1))},
  31. args: args{
  32. canvas: image.NewRGBA(image.Rect(0, 0, 100, 100)),
  33. text: "",
  34. },
  35. wantErr: true,
  36. },
  37. {
  38. name: "DrawString with nil canvas",
  39. t: &textDrawer{r: rand.New(rand.NewSource(1))},
  40. args: args{
  41. canvas: nil,
  42. text: "Hello, World!",
  43. },
  44. wantErr: true,
  45. },
  46. }
  47. err := DefaultFontFamily.AddFontPath("./fonts")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. if err := tt.t.DrawString(tt.args.canvas, tt.args.text); (err != nil) != tt.wantErr {
  54. t.Errorf("textDrawer.DrawString() error = %v, wantErr %v", err, tt.wantErr)
  55. }
  56. })
  57. }
  58. }
  59. func Test_twistTextDrawer_DrawString(t *testing.T) {
  60. type args struct {
  61. canvas draw.Image
  62. text string
  63. }
  64. tests := []struct {
  65. name string
  66. t *twistTextDrawer
  67. args args
  68. wantErr bool
  69. }{
  70. {
  71. name: "Successful DrawString",
  72. t: &twistTextDrawer{r: rand.New(rand.NewSource(1))},
  73. args: args{
  74. canvas: image.NewRGBA(image.Rect(0, 0, 100, 100)),
  75. text: "Hello, World!",
  76. },
  77. wantErr: false,
  78. },
  79. {
  80. name: "DrawString with empty text",
  81. t: &twistTextDrawer{r: rand.New(rand.NewSource(1))},
  82. args: args{
  83. canvas: image.NewRGBA(image.Rect(0, 0, 100, 100)),
  84. text: "",
  85. },
  86. wantErr: true,
  87. },
  88. {
  89. name: "DrawString with nil canvas",
  90. t: &twistTextDrawer{r: rand.New(rand.NewSource(1))},
  91. args: args{
  92. canvas: nil,
  93. text: "Hello, World!",
  94. },
  95. wantErr: true,
  96. },
  97. }
  98. err := DefaultFontFamily.AddFontPath("./fonts")
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. for _, tt := range tests {
  103. t.Run(tt.name, func(t *testing.T) {
  104. if err := tt.t.DrawString(tt.args.canvas, tt.args.text); (err != nil) != tt.wantErr {
  105. t.Errorf("twistTextDrawer.DrawString() error = %v, wantErr %v", err, tt.wantErr)
  106. }
  107. })
  108. }
  109. }