random_test.TestGenerator_GenerateSecretCode   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 61
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 42
nop 1
dl 0
loc 61
rs 4.8
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like random_test.TestGenerator_GenerateSecretCode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package random_test
2
3
import (
4
	"errors"
5
	"strings"
6
	"testing"
7
8
	"github.com/memnix/memnix-rest/pkg/random"
9
)
10
11
func TestGenerator_GenerateSecretCode(t *testing.T) {
12
	generator := random.GetRandomGeneratorInstance()
13
14
	testCases := []struct {
15
		expectedErr error
16
		name        string
17
		n           int
18
		expected    int
19
		hasErr      bool
20
	}{
21
		{
22
			name:        "GenerateSecretCodeWithLength10",
23
			n:           10,
24
			expected:    10,
25
			expectedErr: nil,
26
			hasErr:      false,
27
		},
28
		{
29
			name:        "GenerateSecretCodeWithLength20",
30
			n:           20,
31
			expected:    20,
32
			expectedErr: nil,
33
			hasErr:      false,
34
		},
35
		{
36
			name:        "GenerateSecretCodeWithLength0",
37
			n:           0,
38
			expected:    0,
39
			expectedErr: nil,
40
			hasErr:      false,
41
		},
42
		{
43
			name:        "GenerateSecretCodeWithLengthNegative",
44
			n:           -1,
45
			expected:    0,
46
			expectedErr: errors.New("invalid length"),
47
			hasErr:      true,
48
		},
49
	}
50
51
	for _, tc := range testCases {
52
		t.Run(tc.name, func(t *testing.T) {
53
			code, err := generator.GenerateSecretCode(tc.n)
54
			if tc.hasErr && err == nil {
55
				t.Fatalf("Expected error, but got nil")
56
			}
57
58
			if !tc.hasErr && err != nil {
59
				t.Fatalf("Unexpected error: %v", err)
60
			}
61
62
			if tc.hasErr && err != nil && err.Error() != tc.expectedErr.Error() {
63
				t.Fatalf("Expected error to be %v, but got %v", tc.expectedErr, err)
64
			}
65
66
			if len(code) != tc.expected {
67
				t.Errorf("Expected code length to be %d, but got %d", tc.expected, len(code))
68
			}
69
70
			if !isValidCode(code) {
71
				t.Errorf("Generated code is not valid: %s", code)
72
			}
73
		})
74
	}
75
}
76
77
func isValidCode(code string) bool {
78
	// Define the valid characters for the secret code
79
	validChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
80
81
	// Check if each character in the code is a valid character
82
	for _, c := range code {
83
		if !strings.ContainsRune(validChars, c) {
84
			return false
85
		}
86
	}
87
88
	return true
89
}
90
91
func FuzzGenerateSecretCode(f *testing.F) {
92
	generator := random.GetRandomGeneratorInstance()
93
	f.Add(uint(1000))
94
	f.Fuzz(func(t *testing.T, size uint) {
95
		code, err := generator.GenerateSecretCode(int(size))
96
		if err != nil {
97
			t.Fatalf("Unexpected error: %v", err)
98
		}
99
100
		if len(code) != int(size) {
101
			t.Errorf("Expected code length to be %d, but got %d", size, len(code))
102
		}
103
104
		if !isValidCode(code) {
105
			t.Errorf("Generated code is not valid: %s", code)
106
		}
107
	})
108
}
109