Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove recover and add testing #10

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions captcha_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package captcha

import (
"image/color"
"math/rand"
"strconv"
"testing"
Expand Down Expand Up @@ -60,3 +61,14 @@ func TestCreateByMathByOption(t *testing.T) {
require.GreaterOrEqual(t, result, int(minMath)-int(maxMath))
require.LessOrEqual(t, result, int(maxMath)*2)
}

func TestCreateCaptcha(t *testing.T) {
opt := getOptionByText(OptionText{
BackgroundColor: color.Black,
})
text := opt.randomText()
capt, err := opt.createCaptcha(text)

require.NotEmpty(t, capt)
require.NoError(t, err)
}
6 changes: 0 additions & 6 deletions draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package captcha

import (
"fmt"
"log"
"math/rand"

"github.com/reu98/go-svg-captcha/fonts"
"github.com/tdewolff/canvas"
)

func (opt *option) drawText(text string) (string, error) {
defer func() {
if err := recover(); err != nil {
log.Fatalln(err)
}
}()

fillColorMin := fillColorMinDefault
fillColorMax := fillColorMaxDefault
Expand Down
19 changes: 19 additions & 0 deletions draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,22 @@ func TestDrawTextWithoutDefault(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, data)
}

func TestDrawTextWithError(t *testing.T) {
opt := getOptionByText(OptionText{
FontPath: "font.ttf",
})
text := opt.randomText()
data, err := opt.drawText(text)

require.Empty(t, data)
require.Error(t, err)
}

func TestLoadFontWithFontPathInvalid(t *testing.T) {
fontPath := "font.ttf"
font, err := loadFont(fontPath)

require.Nil(t, font)
require.Error(t, err)
}
47 changes: 47 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,50 @@ func TestGetOptionByMath(t *testing.T) {
require.Equal(t, data.mathMax, mathMaxDefault)
require.Contains(t, []mathOperator{MathOperatorMinus, MathOperatorPlus}, data.mathOperator)
}

func TestGetOptionByTextWithOption(t *testing.T) {
option := OptionText{
Size: 6,
Width: 200,
Height: 100,
FontSize: 18,
Curve: 3,
IgnoreCharacters: "90",
CharactersPreset: "1234567890",
BackgroundColor: color.Black,
}
opt := getOptionByText(option)

require.Equal(t, opt.size, uint8(6))
require.Equal(t, opt.width, uint16(200))
require.Equal(t, opt.height, uint16(100))
require.Equal(t, opt.fontSize, uint(18)*uint(ratioFontSize))
require.Equal(t, opt.curve, uint8(3))
require.Equal(t, opt.ignoreCharacters, "90")
require.Equal(t, opt.charactersPreset, "1234567890")
require.True(t, opt.isColor)
require.Equal(t, opt.backgroundColor, color.Black)
}

func TestGetOptionByMathWithOption(t *testing.T) {
option := OptionMath{
Width: 200,
Height: 100,
FontSize: 18,
Curve: 3,
BackgroundColor: color.Black,
MathOperator: MathOperatorPlus,
MathMin: 10,
MathMax: 99,
}
opt := getOptionByMath(option)

require.Equal(t, opt.width, uint16(200))
require.Equal(t, opt.height, uint16(100))
require.Equal(t, opt.fontSize, uint(18)*uint(ratioFontSize))
require.True(t, opt.isColor)
require.Equal(t, opt.backgroundColor, color.Black)
require.Equal(t, opt.mathMin, uint8(10))
require.Equal(t, opt.mathMax, uint16(99))
require.Equal(t, opt.mathOperator, MathOperatorPlus)
}
Loading