Skip to content

Commit 489ca93

Browse files
Merge pull request #69 from fabianoflorentino/development
Development to Main
2 parents aa87abd + 14f7848 commit 489ca93

File tree

2 files changed

+397
-0
lines changed

2 files changed

+397
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
// Package exercicios_ninja_nivel_10 contains tests for the exercises in level 10 of the Ninja Go course.
2+
// These tests are designed to validate the solutions to the exercises by comparing the actual output
3+
// of the functions to the expected output.
4+
package exercicios_ninja_nivel_10
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
"github.com/fabianoflorentino/aprendago/pkg/logger"
11+
"github.com/fabianoflorentino/aprendago/pkg/output"
12+
"github.com/fabianoflorentino/aprendago/pkg/trim"
13+
)
14+
15+
// expectTemplate is a format string used for displaying the expected and actual output
16+
// in a structured way. It helps in comparing the expected result with the actual result
17+
// by showing them side by side in the output.
18+
const (
19+
expectTemplate = "\nwant:\n%s\n\ngot:\n%s\n"
20+
)
21+
22+
// TestResolucaoNaPraticaExercicio1 tests the function ResolucaoNaPraticaExercicio1
23+
// by capturing its output and comparing it to the expected value.
24+
// It fails the test if the output does not contain the expected value or if there is an error capturing the output.
25+
func TestResolucaoNaPraticaExercicio1(t *testing.T) {
26+
output := output.New()
27+
result, err := output.Capture(ResolucaoNaPraticaExercicio1)
28+
if err != nil {
29+
logger.Log("Failed to capture output: %v", err)
30+
t.Fatalf("unexpected error: %v", err)
31+
}
32+
33+
expect := `
34+
Value: 42
35+
`
36+
trim := trim.New()
37+
38+
if !strings.Contains(trim.String(result), trim.String(expect)) {
39+
t.Errorf(expectTemplate, expect, result)
40+
}
41+
}
42+
43+
// TestResolucaoNaPraticaExercicio2 tests the function ResolucaoNaPraticaExercicio2
44+
// by capturing its output and comparing it to the expected result. If the output
45+
// does not match the expected result, the test fails with an error message.
46+
func TestResolucaoNaPraticaExercicio2(t *testing.T) {
47+
output := output.New()
48+
result, err := output.Capture(ResolucaoNaPraticaExercicio2)
49+
if err != nil {
50+
logger.Log("Failed to capture output: %v", err)
51+
t.Fatalf("unexpected error: %v", err)
52+
}
53+
54+
expect := `
55+
Value: 42
56+
--------------------
57+
Type: chan<- int
58+
`
59+
60+
trim := trim.New()
61+
62+
if !strings.Contains(trim.String(result), trim.String(expect)) {
63+
t.Errorf(expectTemplate, expect, result)
64+
}
65+
}
66+
67+
// TestResolucaoNaPraticaExercicio3 tests the function ResolucaoNaPraticaExercicio3
68+
// by capturing its output and comparing it to the expected result. If the output
69+
// does not match the expected result, the test fails with an error message.
70+
func TestResolucaoNaPraticaExercicio3(t *testing.T) {
71+
output := output.New()
72+
result, err := output.Capture(ResolucaoNaPraticaExercicio3)
73+
if err != nil {
74+
logger.Log("Failed to capture output: %v", err)
75+
t.Fatalf("unexpected error: %v", err)
76+
}
77+
78+
expect := `
79+
Value: 0
80+
Value: 1
81+
Value: 2
82+
Value: 3
83+
Value: 4
84+
Value: 5
85+
Value: 6
86+
Value: 7
87+
Value: 8
88+
Value: 9
89+
Value: 10
90+
Value: 11
91+
Value: 12
92+
Value: 13
93+
Value: 14
94+
Value: 15
95+
Value: 16
96+
Value: 17
97+
Value: 18
98+
Value: 19
99+
Value: 20
100+
Value: 21
101+
Value: 22
102+
Value: 23
103+
Value: 24
104+
Value: 25
105+
Value: 26
106+
Value: 27
107+
Value: 28
108+
Value: 29
109+
Value: 30
110+
Value: 31
111+
Value: 32
112+
Value: 33
113+
Value: 34
114+
Value: 35
115+
Value: 36
116+
Value: 37
117+
Value: 38
118+
Value: 39
119+
Value: 40
120+
Value: 41
121+
Value: 42
122+
Value: 43
123+
Value: 44
124+
Value: 45
125+
Value: 46
126+
Value: 47
127+
Value: 48
128+
Value: 49
129+
Value: 50
130+
Value: 51
131+
Value: 52
132+
Value: 53
133+
Value: 54
134+
Value: 55
135+
Value: 56
136+
Value: 57
137+
Value: 58
138+
Value: 59
139+
Value: 60
140+
Value: 61
141+
Value: 62
142+
Value: 63
143+
Value: 64
144+
Value: 65
145+
Value: 66
146+
Value: 67
147+
Value: 68
148+
Value: 69
149+
Value: 70
150+
Value: 71
151+
Value: 72
152+
Value: 73
153+
Value: 74
154+
Value: 75
155+
Value: 76
156+
Value: 77
157+
Value: 78
158+
Value: 79
159+
Value: 80
160+
Value: 81
161+
Value: 82
162+
Value: 83
163+
Value: 84
164+
Value: 85
165+
Value: 86
166+
Value: 87
167+
Value: 88
168+
Value: 89
169+
Value: 90
170+
Value: 91
171+
Value: 92
172+
Value: 93
173+
Value: 94
174+
Value: 95
175+
Value: 96
176+
Value: 97
177+
Value: 98
178+
Value: 99
179+
About to exit...
180+
`
181+
182+
trim := trim.New()
183+
184+
if !strings.Contains(trim.String(result), trim.String(expect)) {
185+
t.Errorf(expectTemplate, expect, result)
186+
}
187+
}
188+
189+
// TestResolucaoNaPraticaExercicio4 tests the function ResolucaoNaPraticaExercicio4
190+
// by capturing its output and comparing it to the expected output.
191+
// It logs and fails the test if there is an error capturing the output
192+
// or if the captured output does not contain the expected string.
193+
func TestResolucaoNaPraticaExercicio4(t *testing.T) {
194+
output := output.New()
195+
result, err := output.Capture(ResolucaoNaPraticaExercicio4)
196+
if err != nil {
197+
logger.Log("Failed to capture output: %v", err)
198+
t.Fatalf("unexpected error: %v", err)
199+
}
200+
201+
expect := `
202+
About to exit...
203+
`
204+
205+
trim := trim.New()
206+
207+
if !strings.Contains(trim.String(result), trim.String(expect)) {
208+
t.Errorf(expectTemplate, expect, result)
209+
}
210+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// Package exercicios_ninja_nivel_9 contains tests and exercises for level 9 of the Ninja Go course.
2+
// This package includes various functions and tests to practice advanced Go programming concepts.
3+
package exercicios_ninja_nivel_9
4+
5+
import (
6+
"strconv"
7+
"strings"
8+
"testing"
9+
10+
"github.com/fabianoflorentino/aprendago/pkg/logger"
11+
"github.com/fabianoflorentino/aprendago/pkg/output"
12+
"github.com/fabianoflorentino/aprendago/pkg/trim"
13+
)
14+
15+
// expectTemplate is a format string used to display the expected and actual values
16+
// in a test output. It helps in comparing the expected result with the actual result
17+
// by formatting them in a readable way.
18+
const (
19+
expectTemplate = "\nwant:\n%s\n\ngot:\n%s\n"
20+
)
21+
22+
// TestResolucaoNaPraticaExercicio1 tests the function ResolucaoNaPraticaExercicio1
23+
// by capturing its output and checking if the number of occurrences of the word "Goroutine"
24+
// matches the expected count. If the output does not match the expected count, the test fails.
25+
func TestResolucaoNaPraticaExercicio1(t *testing.T) {
26+
output := output.New()
27+
result, err := output.Capture(ResolucaoNaPraticaExercicio1)
28+
if err != nil {
29+
logger.Log("Failed to capture output: %v", err)
30+
t.Fatalf("unexpected error: %v", err)
31+
}
32+
33+
expect := 10
34+
35+
if strings.Count(result, "Goroutine") != expect {
36+
t.Errorf(expectTemplate, strconv.Itoa(expect), result)
37+
}
38+
}
39+
40+
// TestResolucaoNaPraticaExercicio2 tests the function ResolucaoNaPraticaExercicio2
41+
// by capturing its output and comparing it to the expected string.
42+
// It fails the test if the output does not contain the expected string.
43+
func TestResolucaoNaPraticaExercicio2(t *testing.T) {
44+
output := output.New()
45+
result, err := output.Capture(ResolucaoNaPraticaExercicio2)
46+
if err != nil {
47+
logger.Log("Failed to capture output: %v", err)
48+
t.Fatalf("unexpected error: %v", err)
49+
}
50+
51+
expect := `
52+
Olá, meu nome é fabiano
53+
`
54+
55+
trim := trim.New()
56+
57+
if !strings.Contains(trim.String(result), trim.String(expect)) {
58+
t.Errorf(expectTemplate, expect, result)
59+
}
60+
}
61+
62+
// TestResolucaoNaPraticaExercicio3 tests the function ResolucaoNaPraticaExercicio3 by capturing its output,
63+
// extracting the expected integer value from the output, and verifying if the value is greater than 50.
64+
// If the value is less than or equal to 50, the test fails with an error message.
65+
func TestResolucaoNaPraticaExercicio3(t *testing.T) {
66+
output := output.New()
67+
result, err := output.Capture(ResolucaoNaPraticaExercicio3)
68+
if err != nil {
69+
logger.Log("Failed to capture output: %v", err)
70+
t.Fatalf("unexpected error: %v", err)
71+
}
72+
73+
expect := strings.Split(result, ":")[1]
74+
trim := trim.New()
75+
76+
expectInt, err := strconv.Atoi(trim.String(expect))
77+
if err != nil {
78+
t.Fatalf("failed to convert expect to int: %v", err)
79+
}
80+
81+
println(expectInt)
82+
83+
if expectInt <= 50 {
84+
t.Errorf(expectTemplate, strconv.Itoa(50), strings.Split(result, ":")[1])
85+
}
86+
}
87+
88+
// TestResolucaoNaPraticaExercicio4 tests the function ResolucaoNaPraticaExercicio4
89+
// by capturing its output and verifying that it contains the expected string
90+
// "All goroutines completed". It also checks if the integer value extracted from
91+
// the output is greater than 42000. If any of these conditions are not met, the
92+
// test will fail with an appropriate error message.
93+
func TestResolucaoNaPraticaExercicio4(t *testing.T) {
94+
output := output.New()
95+
result, err := output.Capture(ResolucaoNaPraticaExercicio4)
96+
if err != nil {
97+
logger.Log("Failed to capture output: %v", err)
98+
t.Fatalf("unexpected error: %v", err)
99+
}
100+
101+
trim := trim.New()
102+
103+
expectString := `All goroutines completed`
104+
105+
if !strings.Contains(trim.String(result), trim.String(expectString)) {
106+
t.Errorf(expectTemplate, expectString, result)
107+
}
108+
109+
expectInt := strings.Split(result, ":")[1]
110+
expectInt = strings.TrimSpace(expectInt)
111+
112+
expect, err := strconv.Atoi(expectInt)
113+
if err != nil {
114+
t.Fatalf("failed to convert expect to int: %v", err)
115+
}
116+
117+
if expect <= 42000 {
118+
t.Errorf(expectTemplate, strconv.Itoa(40000), expectInt)
119+
}
120+
}
121+
122+
// TestResolucaoNapraticaExercicio5 tests the function ResolucaoNaPraticaExercicio5
123+
// by capturing its output and comparing it to the expected string "Contador: 100".
124+
// If the captured output does not contain the expected string, the test fails.
125+
func TestResolucaoNapraticaExercicio5(t *testing.T) {
126+
output := output.New()
127+
result, err := output.Capture(ResolucaoNaPraticaExercicio5)
128+
if err != nil {
129+
logger.Log("Failed to capture output: %v", err)
130+
t.Fatalf("unexpected error: %v", err)
131+
}
132+
133+
trim := trim.New()
134+
135+
expectString := `Contador: 100`
136+
137+
if !strings.Contains(trim.String(result), trim.String(expectString)) {
138+
t.Errorf(expectTemplate, expectString, result)
139+
}
140+
}
141+
142+
// TestResolucaoNaPraticaExercicio6 tests the function ResolucaoNaPraticaExercicio6
143+
// by capturing its output and comparing it to the expected result.
144+
// It ensures that the function correctly prints the operating system and architecture.
145+
func TestResolucaoNaPraticaExercicio6(t *testing.T) {
146+
output := output.New()
147+
result, err := output.Capture(ResolucaoNaPraticaExercicio6)
148+
if err != nil {
149+
logger.Log("Failed to capture output: %v", err)
150+
t.Fatalf("unexpected error: %v", err)
151+
}
152+
153+
trim := trim.New()
154+
155+
expect := `
156+
Sistema Operacional: darwin
157+
Arquitetura: amd64
158+
`
159+
160+
if !strings.Contains(trim.String(result), trim.String(expect)) {
161+
t.Errorf(expectTemplate, expect, result)
162+
}
163+
}
164+
165+
// TestResolucaoNaPraticaExercicio7 tests the function ResolucaoNaPraticaExercicio7
166+
// by capturing its output and comparing it to the expected result.
167+
// It uses the output.Capture method to capture the function's output and
168+
// the trim.String method to normalize the output and expected strings before comparison.
169+
// If the captured output does not contain the expected string, the test fails.
170+
func TestResolucaoNaPraticaExercicio7(t *testing.T) {
171+
output := output.New()
172+
result, err := output.Capture(ResolucaoNaPraticaExercicio7)
173+
if err != nil {
174+
logger.Log("Failed to capture output: %v", err)
175+
t.Fatalf("unexpected error: %v", err)
176+
}
177+
178+
trim := trim.New()
179+
180+
expect := `
181+
https://dev.to/fabianoflorentino/a-interface-write-11c5
182+
`
183+
184+
if !strings.Contains(trim.String(result), trim.String(expect)) {
185+
t.Errorf(expectTemplate, expect, result)
186+
}
187+
}

0 commit comments

Comments
 (0)