Skip to content

Commit aa87abd

Browse files
Merge pull request #68 from fabianoflorentino/development
Development to Main
2 parents bc4a381 + 35af845 commit aa87abd

File tree

7 files changed

+519
-6
lines changed

7 files changed

+519
-6
lines changed

internal/exercicios_ninja_nivel_5/resolution_exercises_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,24 @@ Cor do sedan: Branco
9696
t.Errorf(expectTemplate, expect, result)
9797
}
9898
}
99+
100+
// TestResolucaoNaPraticaExercicio4 tests the function ResolucaoNaPraticaExercicio4
101+
// by capturing its output and comparing it to the expected result. If the captured
102+
// output does not match the expected output, the test will fail and log an error.
103+
func TestResolucaoNaPraticaExercicio4(t *testing.T) {
104+
output := output.New()
105+
result, err := output.Capture(ResolucaoNaPraticaExercicio4)
106+
if err != nil {
107+
logger.Log("Failed to capture output: %v", err)
108+
}
109+
110+
expect := `
111+
{Fabiano 39 map[Ale:46 Lucas:38] [Pizza Lasanha Hamburguer]}
112+
`
113+
114+
trim := trim.New()
115+
116+
if !strings.Contains(trim.String(result), trim.String(expect)) {
117+
t.Errorf(expectTemplate, expect, result)
118+
}
119+
}

internal/exercicios_ninja_nivel_6/resolution_exercises.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ func ResolucaoNaPraticaExercicio5() {
127127
// In this example, the anonymous function prints a message to the console when executed.
128128
func ResolucaoNaPraticaExercicio6() {
129129

130-
// Uma função anônima é uma função sem nome que pode ser definida e executada inline.
131-
// Elas são úteis para tarefas rápidas e podem ser passadas como argumentos para outras funções.
130+
// An anonymous function is a function without a name that can be defined and executed inline.
131+
// They are useful for quick tasks and can be passed as arguments to other functions.
132132
func() {
133-
fmt.Println("Função anônima executada.")
133+
fmt.Println("Anonymous function executed.")
134134
}()
135135
}
136136

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
// Package exercicios_ninja_nivel_6 contains test functions for exercises in level 6 of the Ninja Go course.
2+
// These tests are designed to validate the solutions to the practical exercises provided in the course.
3+
package exercicios_ninja_nivel_6
4+
5+
import (
6+
"strings"
7+
"testing"
8+
9+
"github.com/fabianoflorentino/aprendago/pkg/logger"
10+
"github.com/fabianoflorentino/aprendago/pkg/output"
11+
"github.com/fabianoflorentino/aprendago/pkg/trim"
12+
)
13+
14+
// expectTemplate is a format string used to display the expected and actual values
15+
// in a test output. It includes placeholders for the expected value ("want") and
16+
// the actual value ("got"), each followed by a newline for better readability.
17+
const (
18+
expectTemplate = "\nwant:\n%s\n\ngot:\n%s\n"
19+
)
20+
21+
// TestResolucaoNaPraticaExercicio1 tests the function ResolucaoNaPraticaExercicio1
22+
// by capturing its output and comparing it to the expected result.
23+
// It uses the output.Capture method to capture the function's output and
24+
// the trim.String method to normalize the strings before comparison.
25+
// If the captured output does not contain the expected result, the test fails
26+
// and an error message is logged.
27+
func TestResolucaoNaPraticaExercicio1(t *testing.T) {
28+
output := output.New()
29+
result, err := output.Capture(ResolucaoNaPraticaExercicio1)
30+
if err != nil {
31+
logger.Log("Failed to capture output: %v", err)
32+
}
33+
34+
expect := `
35+
Inteiro: 42
36+
Inteiro&Texto: 42 e Olá, mundo!
37+
`
38+
39+
trim := trim.New()
40+
41+
if !strings.Contains(trim.String(result), trim.String(expect)) {
42+
t.Errorf(expectTemplate, expect, result)
43+
}
44+
}
45+
46+
// TestResolucaoNaPraticaExercicio2 tests the function ResolucaoNaPraticaExercicio2
47+
// by capturing its output and comparing it to the expected result.
48+
// It verifies that the function correctly calculates and prints the sum of a variadic
49+
// argument and a slice. If the captured output does not match the expected output,
50+
// the test will fail and log an error.
51+
func TestResolucaoNaPraticaExercicio2(t *testing.T) {
52+
output := output.New()
53+
result, err := output.Capture(ResolucaoNaPraticaExercicio2)
54+
if err != nil {
55+
logger.Log("Failed to capture output: %v", err)
56+
}
57+
58+
expect := `
59+
Soma do variádico: 55
60+
Soma do slice: 66
61+
`
62+
63+
trim := trim.New()
64+
65+
if !strings.Contains(trim.String(result), trim.String(expect)) {
66+
t.Errorf(expectTemplate, expect, result)
67+
}
68+
}
69+
70+
// TestResolucaoNaPraticaExercicio3 tests the function ResolucaoNaPraticaExercicio3
71+
// by capturing its output and comparing it to the expected result.
72+
// It uses the output.Capture method to capture the function's output and
73+
// the strings.Contains method to check if the captured output matches the expected output.
74+
// If the output does not match, it logs an error with the expected and actual results.
75+
func TestResolucaoNaPraticaExercicio3(t *testing.T) {
76+
output := output.New()
77+
result, err := output.Capture(ResolucaoNaPraticaExercicio3)
78+
if err != nil {
79+
logger.Log("Failed to capture output: %v", err)
80+
}
81+
82+
expect := `
83+
Execução do defer ocorre ao final do contexto ao qual ela pertence.
84+
Execução do defer ocorreu ao final do contexto.
85+
`
86+
87+
trim := trim.New()
88+
89+
if !strings.Contains(trim.String(result), trim.String(expect)) {
90+
t.Errorf(expectTemplate, expect, result)
91+
}
92+
}
93+
94+
// TestResolucaoNaPraticaExercicio4 tests the function ResolucaoNaPraticaExercicio4
95+
// by capturing its output and comparing it to the expected result.
96+
// It logs an error if the captured output does not match the expected output.
97+
func TestResolucaoNaPraticaExercicio4(t *testing.T) {
98+
output := output.New()
99+
result, err := output.Capture(ResolucaoNaPraticaExercicio4)
100+
if err != nil {
101+
logger.Log("Failed to capture output: %v", err)
102+
}
103+
104+
expect := `
105+
Nome: Fabiano Florentino
106+
Idade: 39
107+
`
108+
trim := trim.New()
109+
110+
if !strings.Contains(trim.String(result), trim.String(expect)) {
111+
t.Errorf(expectTemplate, expect, result)
112+
}
113+
}
114+
115+
// TestResolucaoNaPraticaExercicio5 tests the function ResolucaoNaPraticaExercicio5
116+
// by capturing its output and comparing it to the expected output.
117+
// It verifies that the function correctly calculates and prints the area of a square and a circle.
118+
func TestResolucaoNaPraticaExercicio5(t *testing.T) {
119+
output := output.New()
120+
result, err := output.Capture(ResolucaoNaPraticaExercicio5)
121+
if err != nil {
122+
logger.Log("Failed to capture output: %v", err)
123+
}
124+
125+
expect := `
126+
Área do quadrado: 100.000000
127+
Área do círculo: 31.400000
128+
`
129+
130+
trim := trim.New()
131+
132+
if !strings.Contains(trim.String(result), trim.String(expect)) {
133+
t.Errorf(expectTemplate, expect, result)
134+
}
135+
}
136+
137+
// TestResolucaoNaPraticaExercicio6 tests the function ResolucaoNaPraticaExercicio6
138+
// by capturing its output and comparing it to the expected output.
139+
// It uses the output.Capture method to capture the function's output and
140+
// the trim.String method to normalize the strings before comparison.
141+
// If the captured output does not contain the expected string, the test fails.
142+
func TestResolucaoNaPraticaExercicio6(t *testing.T) {
143+
output := output.New()
144+
result, err := output.Capture(ResolucaoNaPraticaExercicio6)
145+
if err != nil {
146+
logger.Log("Failed to capture output: %v", err)
147+
}
148+
149+
expect := `
150+
Anonymous function executed.
151+
`
152+
153+
trim := trim.New()
154+
155+
if !strings.Contains(trim.String(result), trim.String(expect)) {
156+
t.Errorf(expectTemplate, expect, result)
157+
}
158+
}
159+
160+
// TestResolucaoNaPraticaExercicio7 tests the function ResolucaoNaPraticaExercicio7
161+
// by capturing its output and comparing it to the expected result.
162+
// It logs an error if the captured output does not match the expected output.
163+
func TestResolucaoNaPraticaExercicio7(t *testing.T) {
164+
output := output.New()
165+
result, err := output.Capture(ResolucaoNaPraticaExercicio7)
166+
if err != nil {
167+
logger.Log("Failed to capture output: %v", err)
168+
}
169+
170+
expect := `
171+
Função atribuída a uma variável.
172+
`
173+
174+
trim := trim.New()
175+
176+
if !strings.Contains(trim.String(result), trim.String(expect)) {
177+
t.Errorf(expectTemplate, expect, result)
178+
}
179+
}
180+
181+
// TestResolucaoNaPraticaExercicio8 tests the function ResolucaoNaPraticaExercicio8
182+
// by capturing its output and comparing it to the expected result.
183+
// It uses the output.Capture method to capture the function's output
184+
// and the strings.Contains method to check if the captured output
185+
// contains the expected string. If the output does not match the expected
186+
// result, it logs an error.
187+
func TestResolucaoNaPraticaExercicio8(t *testing.T) {
188+
output := output.New()
189+
result, err := output.Capture(ResolucaoNaPraticaExercicio8)
190+
if err != nil {
191+
logger.Log("Failed to capture output: %v", err)
192+
}
193+
194+
expect := `
195+
Função retornada.
196+
`
197+
198+
trim := trim.New()
199+
200+
if !strings.Contains(trim.String(result), trim.String(expect)) {
201+
t.Errorf(expectTemplate, expect, result)
202+
}
203+
}
204+
205+
// TestResolucaoNaPraticaExercicio9 tests the function ResolucaoNaPraticaExercicio9
206+
// by capturing its output and comparing it to the expected result.
207+
// It uses the output.Capture method to capture the function's output
208+
// and the trim.String method to normalize the strings before comparison.
209+
// If the captured output does not contain the expected string, the test fails.
210+
func TestResolucaoNaPraticaExercicio9(t *testing.T) {
211+
output := output.New()
212+
result, err := output.Capture(ResolucaoNaPraticaExercicio9)
213+
if err != nil {
214+
logger.Log("Failed to capture output: %v", err)
215+
}
216+
217+
expect := `
218+
Função passada como argumento
219+
`
220+
221+
trim := trim.New()
222+
223+
if !strings.Contains(trim.String(result), trim.String(expect)) {
224+
t.Errorf(expectTemplate, expect, result)
225+
}
226+
}
227+
228+
// TestResolucaoNaPraticaExercicio10 tests the function ResolucaoNaPraticaExercicio10
229+
// by capturing its output and comparing it to the expected result.
230+
// It uses the output.Capture method to capture the function's output and
231+
// the strings.Contains method to check if the captured output contains the expected string.
232+
// If the captured output does not match the expected result, it logs an error.
233+
func TestResolucaoNaPraticaExercicio10(t *testing.T) {
234+
output := output.New()
235+
result, err := output.Capture(ResolucaoNaPraticaExercicio10)
236+
if err != nil {
237+
logger.Log("Failed to capture output: %v", err)
238+
}
239+
240+
expect := `
241+
10
242+
`
243+
244+
trim := trim.New()
245+
246+
if !strings.Contains(trim.String(result), trim.String(expect)) {
247+
t.Errorf(expectTemplate, expect, result)
248+
}
249+
}
250+
251+
// TestResolucaoNaPraticaExercicio11 tests the function ResolucaoNaPraticaExercicio11
252+
// by capturing its output and comparing it to the expected output. If the captured
253+
// output does not match the expected output, the test fails and logs an error.
254+
func TestResolucaoNaPraticaExercicio11(t *testing.T) {
255+
output := output.New()
256+
result, err := output.Capture(ResolucaoNaPraticaExercicio11)
257+
if err != nil {
258+
logger.Log("Failed to capture output: %v", err)
259+
}
260+
261+
expect := `
262+
- Uma das melhores maneiras de aprender é ensinando.
263+
- Para este exercício escolha o seu código favorito dentre os que vimos estudando funções. Pode ser das aulas ou da seção de exercícios. Então:
264+
- Faça download e instale isso aqui: https://obsproject.com/
265+
- Grave um vídeo onde *você* ensina o tópico em questão
266+
- Faça upload do vídeo no YouTube
267+
- Compartilhe o vídeo no Twitter e me marque no tweet (@ellenkorbes)
268+
`
269+
270+
trim := trim.New()
271+
272+
if !strings.Contains(trim.String(result), trim.String(expect)) {
273+
t.Errorf(expectTemplate, expect, result)
274+
}
275+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package exercicios_ninja_nivel_7
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/fabianoflorentino/aprendago/pkg/logger"
8+
"github.com/fabianoflorentino/aprendago/pkg/output"
9+
"github.com/fabianoflorentino/aprendago/pkg/trim"
10+
)
11+
12+
// expectTemplate is a format string used to display the expected and actual values
13+
// in test output. It helps in comparing the expected result with the actual result
14+
// by formatting them in a readable way.
15+
const (
16+
expectTemplate = "\nwant:\n%s\n\ngot:\n%s\n"
17+
)
18+
19+
// TestResolucaoNaPraticaExercicio1 tests the function ResolucaoNaPraticaExercicio1
20+
// by capturing its output and logging any errors that occur during the capture process.
21+
// It also prints a message indicating that it will show the memory address of a string variable.
22+
func TestResolucaoNaPraticaExercicio1(t *testing.T) {
23+
output := output.New()
24+
result, err := output.Capture(ResolucaoNaPraticaExercicio1)
25+
if err != nil {
26+
logger.Log("Failed to capture output: %v", err)
27+
}
28+
29+
println("Show the memory address of a string variable")
30+
println("N/A", result)
31+
}
32+
33+
// TestResolucaoNaPraticaExercicio2 tests the function ResolucaoNaPraticaExercicio2
34+
// by capturing its output and comparing it to the expected result.
35+
// It logs an error if the captured output does not match the expected output.
36+
func TestResolucaoNaPraticaExercicio2(t *testing.T) {
37+
output := output.New()
38+
result, err := output.Capture(ResolucaoNaPraticaExercicio2)
39+
if err != nil {
40+
logger.Log("Failed to capture output: %v", err)
41+
}
42+
43+
expect := `
44+
Original -> Nome: Fulano de Tal, Idade: 25
45+
Alterado -> Nome: João Silva, Idade: 30
46+
`
47+
48+
trim := trim.New()
49+
50+
if !strings.Contains(trim.String(result), trim.String(expect)) {
51+
t.Errorf(expectTemplate, expect, result)
52+
}
53+
}

internal/exercicios_ninja_nivel_8/resolution_exercises.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ type byAge []user
4949
//
5050
// Package internal/exercicios_ninja_nivel_8 contains exercises and solutions for advanced Go programming concepts.
5151
func ResolucaoNaPraticaExercicio1() {
52-
fmt.Printf("Exemplo 1 \n\n")
52+
fmt.Printf("\nExemplo 1 \n\n")
5353

5454
for index, user := range users() {
5555
userToJson, _ := json.Marshal(user)
5656
fmt.Printf("User %d: %s\n", index+1, userToJson)
5757
}
5858

59-
fmt.Printf("\nExemplo 2 \n\n%v", resolucaoNaPraticaExercicio1())
59+
fmt.Printf("\nExemplo 2 \n\n%v\n", resolucaoNaPraticaExercicio1())
6060
}
6161

6262
// ResolucaoNaPraticaExercicio2 unmarshals a JSON byte slice into a slice of user2 structs,

0 commit comments

Comments
 (0)