Skip to content

Commit 314ae01

Browse files
test: add unit tests for ListOfTopics method in canais module
1 parent 67d4ec1 commit 314ae01

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

internal/canais/topics_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package canais
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/fabianoflorentino/aprendago/pkg/topic"
8+
)
9+
10+
func TestTopics(t *testing.T) {
11+
os.Setenv("GOENV", "test")
12+
13+
tt := topic.New()
14+
15+
list := []string{
16+
entendendoCanais,
17+
canaisDirecionaisUtilizandoCanais,
18+
rangeEClose,
19+
selectStatement,
20+
commaOkExpression,
21+
convergencia,
22+
divergencia,
23+
context,
24+
}
25+
26+
t.Run("TestRootDirValidation", func(t *testing.T) {
27+
if rootDir != "internal/canais" {
28+
t.Errorf("Expected rootDir to be 'internal/canais', got '%s'", rootDir)
29+
}
30+
})
31+
32+
t.Run("TestListOfTopicsWithValidInput", func(t *testing.T) {
33+
expectedLength := 8
34+
topics := tt.ListOfTopics(list, expectedLength)
35+
if len(topics) != expectedLength {
36+
t.Errorf("Expected %d topics, got %d", expectedLength, len(topics))
37+
}
38+
39+
expectedTopics := []string{
40+
entendendoCanais,
41+
canaisDirecionaisUtilizandoCanais,
42+
rangeEClose,
43+
selectStatement,
44+
commaOkExpression,
45+
convergencia,
46+
divergencia,
47+
context,
48+
}
49+
for i, topic := range expectedTopics {
50+
if topics[i] != topic {
51+
t.Errorf("Expected topic at index %d to be %s, got %s", i, topic, topics[i])
52+
}
53+
}
54+
})
55+
56+
t.Run("TestListOfTopicsWithLimitGreaterThanListSize", func(t *testing.T) {
57+
limit := 10
58+
topics := tt.ListOfTopics(list, limit)
59+
if len(topics) != len(list) {
60+
t.Errorf("Expected %d topics, got %d", len(list), len(topics))
61+
}
62+
})
63+
64+
t.Run("TestListOfTopicsWithZeroLimit", func(t *testing.T) {
65+
limit := 0
66+
topics := tt.ListOfTopics(list, limit)
67+
if len(topics) != 0 {
68+
t.Errorf("Expected 0 topics, got %d", len(topics))
69+
}
70+
})
71+
72+
t.Run("TestListOfTopicsWithNegativeLimit", func(t *testing.T) {
73+
limit := -1
74+
topics := tt.ListOfTopics(list, limit)
75+
if len(topics) != 0 {
76+
t.Errorf("Expected 0 topics for negative limit, got %d", len(topics))
77+
}
78+
})
79+
80+
t.Run("TestListOfTopicsWithEmptyList", func(t *testing.T) {
81+
emptyList := []string{}
82+
topics := tt.ListOfTopics(emptyList, 5)
83+
if len(topics) != 0 {
84+
t.Errorf("Expected 0 topics for empty list, got %d", len(topics))
85+
}
86+
})
87+
}

0 commit comments

Comments
 (0)