From b2dbe7ac5171d6ffb2c3a11d5f75e09875fae361 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 15 Oct 2025 17:21:52 +0800 Subject: [PATCH] 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. --- utils/otp/otp.go | 1 - utils/otp/otp_test.go | 15 +++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/utils/otp/otp.go b/utils/otp/otp.go index 669e3e6a..7cc3e8be 100644 --- a/utils/otp/otp.go +++ b/utils/otp/otp.go @@ -119,4 +119,3 @@ func generateRandomString(length int, charset string) string { return string(result) } - diff --git a/utils/otp/otp_test.go b/utils/otp/otp_test.go index 320d6de3..b1de60fb 100644 --- a/utils/otp/otp_test.go +++ b/utils/otp/otp_test.go @@ -31,10 +31,10 @@ func TestGenerate(t *testing.T) { option.Length = 8 _, alphaCode := Generate(option) assert.Equal(t, 8, len(alphaCode), "Alphanumeric code should match length") - + // Verify alphanumeric 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") } t.Logf("Generated alphanumeric code: %s", alphaCode) @@ -175,11 +175,11 @@ func TestOTPZeroValues(t *testing.T) { // Test with zero/empty option values option := Option{} id, code := Generate(option) - + assert.NotEmpty(t, id, "Should generate ID 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") - + // Should be numeric by default for _, c := range code { 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) { option := NewOption() option.Type = "invalid_type" - + id, code := Generate(option) assert.NotEmpty(t, id) assert.NotEmpty(t, code) - + // Should fallback to numeric for _, c := range code { 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.Type = "alphanumeric" option.Length = 8 - + for i := 0; i < b.N; i++ { Generate(option) } } -