Skip to content

Commit bc4a381

Browse files
Merge pull request #67 from fabianoflorentino/development
Development to Main
2 parents f99e393 + 00d9574 commit bc4a381

File tree

4 files changed

+151
-17
lines changed

4 files changed

+151
-17
lines changed

internal/exercicios_ninja_nivel_4/resolution_exercises_test.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/fabianoflorentino/aprendago/pkg/logger"
1112
"github.com/fabianoflorentino/aprendago/pkg/output"
1213
"github.com/fabianoflorentino/aprendago/pkg/trim"
1314
)
@@ -24,7 +25,10 @@ const (
2425
// to an expected string. If the resulting string does not match the expected string, the test fails.
2526
func TestResolucaoNaPraticaExercicio1(t *testing.T) {
2627
output := output.New()
27-
result := output.Capture(ResolucaoNaPraticaExercicio1)
28+
result, err := output.Capture(ResolucaoNaPraticaExercicio1)
29+
if err != nil {
30+
logger.Log("Failed to capture output: %v", err)
31+
}
2832

2933
expect := `
3034
Resolução:
@@ -46,7 +50,10 @@ Resolução:
4650
// leading and trailing whitespace, and reports an error if it does not match.
4751
func TestResolucaoNaPraticaExercicio2(t *testing.T) {
4852
output := output.New()
49-
result := output.Capture(ResolucaoNaPraticaExercicio2)
53+
result, err := output.Capture(ResolucaoNaPraticaExercicio2)
54+
if err != nil {
55+
logger.Log("Failed to capture output: %v", err)
56+
}
5057

5158
expect := `
5259
Resolução:
@@ -69,7 +76,10 @@ Tipo: []string
6976
// contain the expected result, the test fails with an error message.
7077
func TestResolucaoNaPraticaExercicio3(t *testing.T) {
7178
output := output.New()
72-
result := output.Capture(ResolucaoNaPraticaExercicio3)
79+
result, err := output.Capture(ResolucaoNaPraticaExercicio3)
80+
if err != nil {
81+
logger.Log("Failed to capture output: %v", err)
82+
}
7383

7484
expect := `
7585
Resolução:
@@ -92,7 +102,10 @@ Tipo: []string
92102
// the test will fail and report the discrepancy.
93103
func TestResolucaoNaPraticaExercicio4(t *testing.T) {
94104
output := output.New()
95-
result := output.Capture(ResolucaoNaPraticaExercicio4)
105+
result, err := output.Capture(ResolucaoNaPraticaExercicio4)
106+
if err != nil {
107+
logger.Log("Failed to capture output: %v", err)
108+
}
96109

97110
expect := `
98111
Resolução:
@@ -115,7 +128,10 @@ appendSliceY: [42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60]
115128
// and an error message is displayed.
116129
func TestResolucaoNaPraticaExercicio5(t *testing.T) {
117130
output := output.New()
118-
result := output.Capture(ResolucaoNaPraticaExercicio5)
131+
result, err := output.Capture(ResolucaoNaPraticaExercicio5)
132+
if err != nil {
133+
logger.Log("Failed to capture output: %v", err)
134+
}
119135

120136
expect := `
121137
Resolução:
@@ -133,7 +149,10 @@ Resolução:
133149
// If the captured output does not match the expected result, the test fails with an error message.
134150
func TestResolucaoNaPraticaExercicio6(t *testing.T) {
135151
output := output.New()
136-
result := output.Capture(ResolucaoNaPraticaExercicio6)
152+
result, err := output.Capture(ResolucaoNaPraticaExercicio6)
153+
if err != nil {
154+
logger.Log("Failed to capture output: %v", err)
155+
}
137156

138157
expect := `
139158
Resolução:
@@ -154,7 +173,10 @@ Estados: Acre, Alagoas, Amapá, Amazonas, Bahia, Ceará, Espírito Santo, Goiás
154173
// surnames, and favorite hobbies.
155174
func TestResolucaoNaPraticaExercicio7(t *testing.T) {
156175
output := output.New()
157-
result := output.Capture(ResolucaoNaPraticaExercicio7)
176+
result, err := output.Capture(ResolucaoNaPraticaExercicio7)
177+
if err != nil {
178+
logger.Log("Failed to capture output: %v", err)
179+
}
158180

159181
expect := `
160182
Resolução:
@@ -176,7 +198,10 @@ Nome: Ciclano, Sobrenome: da Silva, Hobby favorito: Assistir filmes
176198
// If the output does not match the expected result, the test will fail and report an error.
177199
func TestResolucaoNaPraticaExercicio8(t *testing.T) {
178200
output := output.New()
179-
result := output.Capture(ResolucaoNaPraticaExercicio8)
201+
result, err := output.Capture(ResolucaoNaPraticaExercicio8)
202+
if err != nil {
203+
logger.Log("Failed to capture output: %v", err)
204+
}
180205

181206
expect := `
182207
Resolução:
@@ -208,7 +233,10 @@ Sobrenome_Nome: da_silva_ciclano
208233
// the test will fail and report the discrepancy.
209234
func TestResolucaoNaPraticaExercicio9(t *testing.T) {
210235
output := output.New()
211-
result := output.Capture(ResolucaoNaPraticaExercicio9)
236+
result, err := output.Capture(ResolucaoNaPraticaExercicio9)
237+
if err != nil {
238+
logger.Log("Failed to capture output: %v", err)
239+
}
212240

213241
expect := `
214242
Resolução:
@@ -242,7 +270,10 @@ Sobrenome_Nome: de_tal_ciclano
242270
// If the captured output does not match the expected output, the test fails and an error is reported.
243271
func TestResolucaoNaPraticaExercicio10(t *testing.T) {
244272
output := output.New()
245-
result := output.Capture(ResolucaoNaPraticaExercicio10)
273+
result, err := output.Capture(ResolucaoNaPraticaExercicio10)
274+
if err != nil {
275+
logger.Log("Failed to capture output: %v", err)
276+
}
246277

247278
expect := `
248279
Resolução:

internal/exercicios_ninja_nivel_5/resolution_exercises.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func ResolucaoNaPraticaExercicio2() {
6868
}
6969

7070
for k, p := range pessoas {
71-
println(k)
71+
fmt.Println(k)
7272
for _, s := range p.sabores_de_sorvete {
73-
println("\t", s)
73+
fmt.Printf("\t%v\n", s)
7474
}
7575
}
7676
}
@@ -117,8 +117,8 @@ func ResolucaoNaPraticaExercicio3() {
117117
modeloLuxo: true,
118118
}
119119

120-
fmt.Printf("Caminhonete: %+v \n", cnt)
121-
fmt.Printf("Sedan: %+v \n", sdn)
120+
fmt.Printf("Caminhonete: %+v\n", cnt)
121+
fmt.Printf("Sedan: %+v\n", sdn)
122122

123123
fmt.Println("Portas da caminhonete:", cnt.portas)
124124
fmt.Println("Cor do sedan:", sdn.cor)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Package exercicios_ninja_nivel_5 contains exercises and their resolutions
2+
// for the Ninja Level 5 of the Go programming language course. These exercises
3+
// are designed to help learners practice and improve their Go programming skills.
4+
package exercicios_ninja_nivel_5
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+
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 comparing it to the expected result.
21+
// The expected output is a formatted string with names and their favorite ice cream flavors.
22+
// If the captured output does not match the expected output, the test will fail.
23+
func TestResolucaoNaPraticaExercicio1(t *testing.T) {
24+
output := output.New()
25+
result, err := output.Capture(ResolucaoNaPraticaExercicio1)
26+
if err != nil {
27+
logger.Log("Failed to capture output: %v", err)
28+
}
29+
30+
expect := `
31+
Fulano de Tal
32+
Chocolate
33+
Morango
34+
Baunilha
35+
Ciclano da Silva
36+
Pistache
37+
Creme
38+
Coco
39+
`
40+
trim := trim.New()
41+
42+
if !strings.Contains(trim.String(result), trim.String(expect)) {
43+
t.Errorf(expectTemplate, expect, result)
44+
}
45+
}
46+
47+
// TestResolucaoNaPraticaExercicio2 tests the function ResolucaoNaPraticaExercicio2
48+
// by capturing its output and comparing it to the expected result.
49+
// The expected output is a formatted string containing names and their favorite ice cream flavors.
50+
// If the captured output does not match the expected output, the test will fail and report the difference.
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+
de Tal
60+
Chocolate
61+
Morango
62+
Baunilha
63+
da Silva
64+
Pistache
65+
Creme
66+
Coco
67+
`
68+
trim := trim.New()
69+
70+
if !strings.Contains(trim.String(result), trim.String(expect)) {
71+
t.Errorf(expectTemplate, expect, result)
72+
}
73+
}
74+
75+
// TestResolucaoNaPraticaExercicio3 tests the function ResolucaoNaPraticaExercicio3
76+
// by capturing its output and comparing it to the expected result. The test
77+
// checks if the output contains the expected formatted strings for a truck
78+
// and a sedan, including their attributes such as the number of doors and color.
79+
func TestResolucaoNaPraticaExercicio3(t *testing.T) {
80+
output := output.New()
81+
result, err := output.Capture(ResolucaoNaPraticaExercicio3)
82+
if err != nil {
83+
logger.Log("Failed to capture output: %v", err)
84+
}
85+
86+
expect := `
87+
Caminhonete: {veiculo:{portas:4 cor:Preto} tracaoNasQuatro:true}
88+
Sedan: {veiculo:{portas:2 cor:Branco} modeloLuxo:true}
89+
Portas da caminhonete: 4
90+
Cor do sedan: Branco
91+
`
92+
93+
trim := trim.New()
94+
95+
if !strings.Contains(trim.String(result), trim.String(expect)) {
96+
t.Errorf(expectTemplate, expect, result)
97+
}
98+
}

pkg/output/output.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"bytes"
88
"io"
99
"os"
10+
11+
"github.com/fabianoflorentino/aprendago/pkg/logger"
1012
)
1113

1214
// Output represents a structure that holds file descriptors for standard output,
@@ -24,7 +26,10 @@ type Output struct {
2426
// It returns a pointer to the Output struct with stdout set to the standard output,
2527
// and read and write set to the ends of the pipe.
2628
func New() *Output {
27-
read, write, _ := os.Pipe()
29+
read, write, err := os.Pipe()
30+
if err != nil {
31+
logger.Log("Failed to create pipe: %v", err)
32+
}
2833

2934
return &Output{
3035
stdout: os.Stdout,
@@ -37,7 +42,7 @@ func New() *Output {
3742
// captureOutput function, and then restores the standard output. It returns
3843
// the captured output as a string. This is useful for capturing and testing
3944
// output generated by functions that write to standard output.
40-
func (o *Output) Capture(captureOutput func()) string {
45+
func (o *Output) Capture(captureOutput func()) (string, error) {
4146
var buf bytes.Buffer
4247

4348
os.Stdout = o.write
@@ -49,5 +54,5 @@ func (o *Output) Capture(captureOutput func()) string {
4954
os.Stdout = o.stdout
5055
io.Copy(&buf, o.read)
5156

52-
return buf.String()
57+
return buf.String(), nil
5358
}

0 commit comments

Comments
 (0)