Skip to content

Commit d07a911

Browse files
Merge pull request #90 from fabianoflorentino/development
Refactor Package Documentation
2 parents 3bcc61f + 93023f3 commit d07a911

31 files changed

+870
-572
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.env
22
.DS_Store
3-
tree.log
3+
**.log
44
log/*.log
55
*.out
6+
**.html

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ Outline do Curso por Capítulo
193193
│   │   ├── menu.go
194194
│   │   ├── menu_test.go
195195
│   │   ├── overview.yml
196-
│   │   ├── topics.go
197-
│   │   └── topics_test.go
196+
│   │   ├── topic_test.go
197+
│   │   └── topics.go
198198
│   ├── aplicacoes
199199
│   │   ├── constants.go
200200
│   │   ├── examples.go
@@ -316,6 +316,9 @@ Outline do Curso por Capítulo
316316
├── log
317317
│   └── development.log
318318
├── pkg
319+
│   ├── base_content
320+
│   │   ├── base_content.go
321+
│   │   └── base_content_test.go
319322
│   ├── format
320323
│   │   ├── helpme.go
321324
│   │   ├── menu_options.go
@@ -340,7 +343,7 @@ Outline do Curso por Capítulo
340343
│   └── trim.go
341344
└── tree.log
342345

343-
44 directories, 123 files
346+
45 directories, 125 files
344347
```
345348

346349
## Licença
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package agrupamento_de_dados
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestHelp(t *testing.T) {
9+
var err error
10+
11+
oldStdout := os.Stdout
12+
defer func() { os.Stdout = oldStdout }()
13+
14+
nullFile, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
15+
if err != nil {
16+
t.Fatalf("failed to open null device: %v", err)
17+
}
18+
defer nullFile.Close()
19+
20+
os.Stdout = nullFile
21+
22+
Help()
23+
24+
if err != nil {
25+
t.Errorf("Help() failed: %v", err)
26+
}
27+
}
Lines changed: 99 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,122 @@
11
package agrupamento_de_dados
22

33
import (
4+
"os"
45
"testing"
56

6-
"github.com/fabianoflorentino/aprendago/pkg/section"
7+
base "github.com/fabianoflorentino/aprendago/pkg/base_content"
78
)
89

910
func TestMenu(t *testing.T) {
10-
// Create a new section with a root directory.
11-
_, err := section.New(rootDir)
11+
var err error
12+
13+
oldStdout := os.Stdout
14+
defer func() { os.Stdout = oldStdout }()
15+
16+
nullFile, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
1217
if err != nil {
13-
t.Errorf("Error creating section: %v", err)
18+
t.Fatalf("failed to open null device: %v", err)
1419
}
20+
defer nullFile.Close()
1521

16-
// Create a slice of MenuOptions.
17-
menuOptions := Menu(
18-
[]string{
19-
flagArray,
20-
flagSliceLiteralComposta,
21-
flagSliceForRange,
22-
flagSliceFatiandoOuDeletando,
23-
flagSliceAnexando,
24-
flagSliceMake,
25-
flagSliceMultiDimensional,
26-
flagSliceSurpresaArraySubjacente,
27-
flagMapsIntroducao,
28-
})
22+
os.Stdout = nullFile
2923

30-
// Check if the menuOptions slice has the expected length.
31-
if len(menuOptions) != 10 {
32-
t.Errorf("Expected menuOptions length of 10, got %v", len(menuOptions))
33-
}
24+
t.Run("TestMenuOptionsLengthAndContent", func(t *testing.T) {
25+
// Create a slice of MenuOptions.
26+
menuOptions := Menu(
27+
[]string{
28+
flagArray,
29+
flagSliceLiteralComposta,
30+
flagSliceForRange,
31+
flagSliceFatiandoOuDeletando,
32+
flagSliceAnexando,
33+
flagSliceMake,
34+
flagSliceMultiDimensional,
35+
flagSliceSurpresaArraySubjacente,
36+
flagMapsIntroducao,
37+
})
3438

35-
// Check if the menuOptions slice has the expected options.
36-
expectedOptions := []string{
37-
"--array",
38-
"--slice-literal-composta",
39-
"--slice-for-range",
40-
"--slice-fatiando-ou-deletando-de-uma-fatia",
41-
"--slice-anexando-a-uma-slice",
42-
"--slice-make",
43-
"--slice-multi-dimensional",
44-
"--slice-a-surpresa-do-array-subjacente",
45-
"--maps-introducao",
46-
"--maps-range-e-deletando",
47-
}
39+
// Check if the menuOptions slice has the expected length.
40+
if len(menuOptions) != 10 {
41+
t.Errorf("Expected menuOptions length of 10, got %v", len(menuOptions))
42+
}
43+
})
4844

49-
for i, option := range menuOptions {
50-
if option.Options != expectedOptions[i] {
51-
t.Errorf("Expected option %v to be %v, got %v", i, expectedOptions[i], option.Options)
45+
t.Run("TestMenuOptionsContent", func(t *testing.T) {
46+
menuOptions := Menu(
47+
[]string{
48+
flagArray,
49+
flagSliceLiteralComposta,
50+
flagSliceForRange,
51+
flagSliceFatiandoOuDeletando,
52+
flagSliceAnexando,
53+
flagSliceMake,
54+
flagSliceMultiDimensional,
55+
flagSliceSurpresaArraySubjacente,
56+
flagMapsIntroducao,
57+
})
58+
59+
expectedOptions := []string{
60+
"--array",
61+
"--slice-literal-composta",
62+
"--slice-for-range",
63+
"--slice-fatiando-ou-deletando-de-uma-fatia",
64+
"--slice-anexando-a-uma-slice",
65+
"--slice-make",
66+
"--slice-multi-dimensional",
67+
"--slice-a-surpresa-do-array-subjacente",
68+
"--maps-introducao",
69+
"--maps-range-e-deletando",
5270
}
53-
}
5471

55-
// Check if the menuOptions slice has the expected execution functions.
56-
for i, option := range menuOptions {
57-
switch i {
58-
case 0:
59-
if option.ExecFunc == nil {
60-
t.Errorf("Expected option %v to have a non-nil execution function", i)
61-
}
62-
case 1:
63-
if option.ExecFunc == nil {
64-
t.Errorf("Expected option %v to have a non-nil execution function", i)
65-
}
66-
case 2:
67-
if option.ExecFunc == nil {
68-
t.Errorf("Expected option %v to have a non-nil execution function", i)
69-
}
70-
case 3:
71-
if option.ExecFunc == nil {
72-
t.Errorf("Expected option %v to have a non-nil execution function", i)
73-
}
74-
case 4:
75-
if option.ExecFunc == nil {
76-
t.Errorf("Expected option %v to have a non-nil execution function", i)
77-
}
78-
case 5:
79-
if option.ExecFunc == nil {
80-
t.Errorf("Expected option %v to have a non-nil execution function", i)
81-
}
82-
case 6:
83-
if option.ExecFunc == nil {
84-
t.Errorf("Expected option %v to have a non-nil execution function", i)
85-
}
86-
case 7:
87-
if option.ExecFunc == nil {
88-
t.Errorf("Expected option %v to have a non-nil execution function", i)
72+
for i, option := range menuOptions {
73+
if option.Options != expectedOptions[i] {
74+
t.Errorf("Expected option %v to be %v, got %v", i, expectedOptions[i], option.Options)
8975
}
90-
case 8:
76+
}
77+
})
78+
79+
t.Run("TestMenuOptionsExecutionFunctions", func(t *testing.T) {
80+
menuOptions := Menu(
81+
[]string{
82+
flagArray,
83+
flagSliceLiteralComposta,
84+
flagSliceForRange,
85+
flagSliceFatiandoOuDeletando,
86+
flagSliceAnexando,
87+
flagSliceMake,
88+
flagSliceMultiDimensional,
89+
flagSliceSurpresaArraySubjacente,
90+
flagMapsIntroducao,
91+
})
92+
93+
for i, option := range menuOptions {
9194
if option.ExecFunc == nil {
9295
t.Errorf("Expected option %v to have a non-nil execution function", i)
9396
}
94-
case 9:
95-
if option.ExecFunc == nil {
96-
t.Errorf("Expected option %v to have a non-nil execution function", i)
97+
}
98+
99+
m := base.New()
100+
newExecFunc := []func(){
101+
func() { m.SectionFormat(array, m.SectionFactory(rootDir)) },
102+
func() { m.SectionFormat(sliceLiteralComposta, m.SectionFactory(rootDir)) },
103+
func() { m.SectionFormat(sliceForRange, m.SectionFactory(rootDir)) },
104+
func() { m.SectionFormat(sliceFatiandoOuDeletando, m.SectionFactory(rootDir)) },
105+
func() { m.SectionFormat(sliceAnexando, m.SectionFactory(rootDir)) },
106+
func() { m.SectionFormat(sliceMake, m.SectionFactory(rootDir)) },
107+
func() { m.SectionFormat(sliceMultiDimensional, m.SectionFactory(rootDir)) },
108+
func() { m.SectionFormat(sliceSurpresaArraySubjacente, m.SectionFactory(rootDir)) },
109+
func() { m.SectionFormat(mapsIntroducao, m.SectionFactory(rootDir)) },
110+
func() { m.SectionFormat(mapsRangeEDeletando, m.SectionFactory(rootDir)) },
111+
}
112+
113+
for i, execFunc := range newExecFunc {
114+
if menuOptions[i].ExecFunc == nil || execFunc == nil {
115+
t.Errorf("Execution function for option %v is nil", i)
116+
} else {
117+
menuOptions[i].ExecFunc()
118+
execFunc()
97119
}
98120
}
99-
}
121+
})
100122
}

internal/agrupamento_de_dados/constants.go renamed to internal/agrupamento_de_dados/static_content.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package agrupamento_de_dados
22

3-
// rootDir represents the relative path to the directory where data grouping topics are stored.
4-
// This constant is used to reference the internal directory structure within the project.
5-
const (
6-
rootDir = "internal/agrupamento_de_dados"
7-
)
8-
93
// The following constants define the names of various topics related to data grouping in Go.
104
// Each constant corresponds to a specific topic and provides a human-readable name for it.
115
const (
@@ -52,3 +46,26 @@ const (
5246
descMapsIntroducao string = "Apresenta o tópico Maps: introdução."
5347
descMapsRangeEDeletando string = "Apresenta o tópico Maps: Range e Deletando."
5448
)
49+
50+
// rootDir represents the relative path to the directory where data grouping topics are stored.
51+
// This constant is used to reference the internal directory structure within the project.
52+
var (
53+
rootDir = "internal/agrupamento_de_dados"
54+
)
55+
56+
// topics is a slice of strings that contains a list of topic identifiers.
57+
// Each identifier represents a specific topic related to data grouping concepts in Go.
58+
var (
59+
topics = []string{
60+
array,
61+
sliceLiteralComposta,
62+
sliceForRange,
63+
sliceFatiandoOuDeletando,
64+
sliceAnexando,
65+
sliceMake,
66+
sliceMultiDimensional,
67+
sliceSurpresaArraySubjacente,
68+
mapsIntroducao,
69+
mapsRangeEDeletando,
70+
}
71+
)

internal/agrupamento_de_dados/topic_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,25 @@ import (
55
"testing"
66
)
77

8-
type MockContentsProvider struct {
9-
CalledWithRootDir string
10-
CalledWithTopics []string
8+
func init() {
9+
rootDir = "./"
1110
}
1211

13-
func (m *MockContentsProvider) TopicsContents(rootDir string, topics []string) {
14-
m.CalledWithRootDir = rootDir
15-
m.CalledWithTopics = topics
16-
}
17-
18-
func TestTopicsAgrupamentoDeDados(t *testing.T) {
19-
os.Setenv("GOENV", "test")
12+
func TestTopics(t *testing.T) {
13+
oldStdout := os.Stdout
14+
defer func() { os.Stdout = oldStdout }()
2015

21-
rootDir := os.Getenv("GOENV")
22-
23-
if rootDir != "test" {
24-
t.Errorf("Expected rootDir to be 'test', got '%s'", rootDir)
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)
2519
}
20+
defer nullFile.Close()
2621

27-
mockProvider := &MockContentsProvider{}
28-
mockProvider.TopicsContents(rootDir, []string{"topic1", "topic2"})
22+
os.Stdout = nullFile
2923

30-
if mockProvider.CalledWithRootDir != rootDir {
31-
t.Errorf("Expected rootDir to be 'test', got '%s'", mockProvider.CalledWithRootDir)
32-
}
24+
Topics()
3325

34-
if len(mockProvider.CalledWithTopics) != 2 {
35-
t.Errorf("Expected 2 topics, got %d", len(mockProvider.CalledWithTopics))
26+
if err != nil {
27+
t.Errorf("Topics() failed: %v", err)
3628
}
3729
}

internal/agrupamento_de_dados/topics.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,13 @@ and display the sections and help information.
88
package agrupamento_de_dados
99

1010
import (
11-
"fmt"
12-
1311
"github.com/fabianoflorentino/aprendago/pkg/topic"
1412
)
1513

1614
// AgrupamentoDeDados prints a header and executes a series of sections related to data grouping in Go.
1715
// Each section is executed by calling the executeSection function with a specific topic name.
1816
// The topics covered include arrays, slices (with various operations), and maps.
1917
func Topics() {
20-
fmt.Printf("\nCapítulo 8: Agrupamento de Dados\n")
21-
22-
list := []string{
23-
array,
24-
sliceLiteralComposta,
25-
sliceForRange,
26-
sliceFatiandoOuDeletando,
27-
sliceAnexando,
28-
sliceMake,
29-
sliceMultiDimensional,
30-
sliceSurpresaArraySubjacente,
31-
mapsIntroducao,
32-
mapsRangeEDeletando,
33-
}
34-
35-
content := topic.New()
36-
content.TopicsContents(rootDir, content.ListOfTopics(list, 10))
18+
t := topic.New()
19+
t.TopicConstructor(rootDir, "Capítulo 8: Agrupamento de Dados", topics, t)
3720
}

0 commit comments

Comments
 (0)