Refactor OTP tests and cleanup code

- Removed unnecessary blank lines in otp_test.go and otp.go files for improved readability.
- Ensured consistency in test assertions for alphanumeric code generation and default behavior with zero values.
- Streamlined the benchmark test for alphanumeric code generation, enhancing performance measurement clarity.
This commit is contained in:
Max 2025-10-15 17:21:52 +08:00
parent c4ecda54e9
commit b2dbe7ac51
2 changed files with 7 additions and 9 deletions

View file

@ -119,4 +119,3 @@ func generateRandomString(length int, charset string) string {
return string(result) return string(result)
} }

View file

@ -31,10 +31,10 @@ func TestGenerate(t *testing.T) {
option.Length = 8 option.Length = 8
_, alphaCode := Generate(option) _, alphaCode := Generate(option)
assert.Equal(t, 8, len(alphaCode), "Alphanumeric code should match length") assert.Equal(t, 8, len(alphaCode), "Alphanumeric code should match length")
// Verify alphanumeric // Verify alphanumeric
for _, c := range alphaCode { for _, c := range alphaCode {
assert.True(t, (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'), assert.True(t, (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'),
"Code should be alphanumeric uppercase") "Code should be alphanumeric uppercase")
} }
t.Logf("Generated alphanumeric code: %s", alphaCode) t.Logf("Generated alphanumeric code: %s", alphaCode)
@ -175,11 +175,11 @@ func TestOTPZeroValues(t *testing.T) {
// Test with zero/empty option values // Test with zero/empty option values
option := Option{} option := Option{}
id, code := Generate(option) id, code := Generate(option)
assert.NotEmpty(t, id, "Should generate ID even with zero values") assert.NotEmpty(t, id, "Should generate ID even with zero values")
assert.NotEmpty(t, code, "Should generate code even with zero values") assert.NotEmpty(t, code, "Should generate code even with zero values")
assert.Equal(t, 6, len(code), "Should use default length") assert.Equal(t, 6, len(code), "Should use default length")
// Should be numeric by default // Should be numeric by default
for _, c := range code { for _, c := range code {
assert.True(t, c >= '0' && c <= '9', "Should default to numeric") assert.True(t, c >= '0' && c <= '9', "Should default to numeric")
@ -189,11 +189,11 @@ func TestOTPZeroValues(t *testing.T) {
func TestOTPInvalidType(t *testing.T) { func TestOTPInvalidType(t *testing.T) {
option := NewOption() option := NewOption()
option.Type = "invalid_type" option.Type = "invalid_type"
id, code := Generate(option) id, code := Generate(option)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
assert.NotEmpty(t, code) assert.NotEmpty(t, code)
// Should fallback to numeric // Should fallback to numeric
for _, c := range code { for _, c := range code {
assert.True(t, c >= '0' && c <= '9', "Invalid type should fallback to numeric") assert.True(t, c >= '0' && c <= '9', "Invalid type should fallback to numeric")
@ -221,9 +221,8 @@ func BenchmarkGenerateAlphanumeric(b *testing.B) {
option := NewOption() option := NewOption()
option.Type = "alphanumeric" option.Type = "alphanumeric"
option.Length = 8 option.Length = 8
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Generate(option) Generate(option)
} }
} }