Skip to content

Commit e39cfd9

Browse files
test: refactor TestTopics function for improved structure and readability
1 parent 51de086 commit e39cfd9

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

internal/agrupamento_de_dados/topic_test.go

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,88 @@ package agrupamento_de_dados
33
import (
44
"os"
55
"testing"
6-
)
76

8-
func init() {
9-
rootDir = "./"
10-
}
7+
"github.com/fabianoflorentino/aprendago/pkg/topic"
8+
)
119

1210
func TestTopics(t *testing.T) {
13-
oldStdout := os.Stdout
14-
defer func() { os.Stdout = oldStdout }()
11+
os.Setenv("GOENV", "test")
1512

16-
nullFile, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
17-
if err != nil {
18-
t.Fatalf("failed to open null device: %v", err)
13+
tt := topic.New()
14+
15+
list := []string{
16+
sliceLiteralComposta,
17+
sliceForRange,
18+
sliceFatiandoOuDeletando,
19+
sliceAnexando,
20+
sliceMake,
21+
sliceMultiDimensional,
22+
sliceSurpresaArraySubjacente,
23+
mapsIntroducao,
24+
mapsRangeEDeletando,
1925
}
20-
defer nullFile.Close()
2126

22-
os.Stdout = nullFile
27+
t.Run("TestRootDirValidation", func(t *testing.T) {
28+
if rootDir != "internal/agrupamento_de_dados" {
29+
t.Errorf("Expected rootDir to be 'internal/agrupamento_de_dados', got '%s'", rootDir)
30+
}
31+
})
2332

24-
Topics()
33+
t.Run("TestListOfTopicsWithValidInput", func(t *testing.T) {
34+
expectedLength := 9
35+
topics := tt.ListOfTopics(list, expectedLength)
36+
if len(topics) != expectedLength {
37+
t.Errorf("Expected %d topics, got %d", expectedLength, len(topics))
38+
}
2539

26-
if err != nil {
27-
t.Errorf("Topics() failed: %v", err)
28-
}
40+
expectedTopics := []string{
41+
sliceLiteralComposta,
42+
sliceForRange,
43+
sliceFatiandoOuDeletando,
44+
sliceAnexando,
45+
sliceMake,
46+
sliceMultiDimensional,
47+
sliceSurpresaArraySubjacente,
48+
mapsIntroducao,
49+
mapsRangeEDeletando,
50+
}
51+
52+
for i, topic := range expectedTopics {
53+
if topics[i] != topic {
54+
t.Errorf("Expected topic at index %d to be %s, got %s", i, topic, topics[i])
55+
}
56+
}
57+
})
58+
59+
t.Run("TestListOfTopicsWithLimitGreaterThanListSize", func(t *testing.T) {
60+
limit := 10
61+
topics := tt.ListOfTopics(list, limit)
62+
if len(topics) != len(list) {
63+
t.Errorf("Expected %d topics, got %d", len(list), len(topics))
64+
}
65+
})
66+
67+
t.Run("TestListOfTopicsWithZeroLimit", func(t *testing.T) {
68+
limit := 0
69+
topics := tt.ListOfTopics(list, limit)
70+
if len(topics) != 0 {
71+
t.Errorf("Expected 0 topics, got %d", len(topics))
72+
}
73+
})
74+
75+
t.Run("TestListOfTopicsWithNegativeLimit", func(t *testing.T) {
76+
limit := -5
77+
topics := tt.ListOfTopics(list, limit)
78+
if len(topics) != 0 {
79+
t.Errorf("Expected 0 topics, got %d", len(topics))
80+
}
81+
})
82+
83+
t.Run("TestListOfTopicsWithEmptyList", func(t *testing.T) {
84+
emptyList := []string{}
85+
topics := tt.ListOfTopics(emptyList, 5)
86+
if len(topics) != 0 {
87+
t.Errorf("Expected 0 topics for empty list, got %d", len(topics))
88+
}
89+
})
2990
}

0 commit comments

Comments
 (0)