|
1 | 1 | package agrupamento_de_dados |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "testing" |
5 | 6 |
|
6 | | - "github.com/fabianoflorentino/aprendago/pkg/section" |
| 7 | + base "github.com/fabianoflorentino/aprendago/pkg/base_content" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | 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) |
12 | 17 | if err != nil { |
13 | | - t.Errorf("Error creating section: %v", err) |
| 18 | + t.Fatalf("failed to open null device: %v", err) |
14 | 19 | } |
| 20 | + defer nullFile.Close() |
15 | 21 |
|
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 |
29 | 23 |
|
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 | + }) |
34 | 38 |
|
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 | + }) |
48 | 44 |
|
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", |
52 | 70 | } |
53 | | - } |
54 | 71 |
|
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) |
89 | 75 | } |
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 { |
91 | 94 | if option.ExecFunc == nil { |
92 | 95 | t.Errorf("Expected option %v to have a non-nil execution function", i) |
93 | 96 | } |
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() |
97 | 119 | } |
98 | 120 | } |
99 | | - } |
| 121 | + }) |
100 | 122 | } |
0 commit comments