|
| 1 | +// Package exercicios_ninja_nivel_4 contains solutions and tests for exercises |
| 2 | +// aimed at practicing and improving Go programming skills. The exercises cover |
| 3 | +// various topics and concepts, providing hands-on experience and reinforcing |
| 4 | +// understanding of the language. |
| 5 | +package exercicios_ninja_nivel_4 |
| 6 | + |
| 7 | +import ( |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/fabianoflorentino/aprendago/pkg/output" |
| 12 | + "github.com/fabianoflorentino/aprendago/pkg/trim" |
| 13 | +) |
| 14 | + |
| 15 | +// testTemplate is a constant string used for formatting test output. |
| 16 | +// It provides a template for displaying the expected and actual values |
| 17 | +// in test results, making it easier to compare them. |
| 18 | +const ( |
| 19 | + expectTemplate = "\nwant:\n%s\n, \ngot:\n%s\n" |
| 20 | +) |
| 21 | + |
| 22 | +// TestResolucaoNaPraticaExercicio1 tests the functionality of iterating over an array of integers, |
| 23 | +// concatenating each integer to a string with a comma and space separator, and comparing the result |
| 24 | +// to an expected string. If the resulting string does not match the expected string, the test fails. |
| 25 | +func TestResolucaoNaPraticaExercicio1(t *testing.T) { |
| 26 | + output := output.New() |
| 27 | + result := output.Capture(ResolucaoNaPraticaExercicio1) |
| 28 | + |
| 29 | + expect := ` |
| 30 | +Resolução: |
| 31 | +1, 2, 3, 4, 5 |
| 32 | +` |
| 33 | + |
| 34 | + trim := trim.New() |
| 35 | + |
| 36 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 37 | + t.Errorf(expectTemplate, expect, result) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestResolucaoNaPraticaExercicio2 tests the function ResolucaoNaPraticaExercicio2 |
| 42 | +// by capturing its output and comparing it to the expected output. It uses a |
| 43 | +// deferred function to recover from any panic that might occur during the execution |
| 44 | +// of ResolucaoNaPraticaExercicio2 and captures the panic message if it happens. |
| 45 | +// The test checks if the captured output contains the expected string, ignoring |
| 46 | +// leading and trailing whitespace, and reports an error if it does not match. |
| 47 | +func TestResolucaoNaPraticaExercicio2(t *testing.T) { |
| 48 | + output := output.New() |
| 49 | + result := output.Capture(ResolucaoNaPraticaExercicio2) |
| 50 | + |
| 51 | + expect := ` |
| 52 | +Resolução: |
| 53 | +
|
| 54 | +Slice: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
| 55 | +Tipo: []string |
| 56 | +` |
| 57 | + |
| 58 | + trim := trim.New() |
| 59 | + |
| 60 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 61 | + t.Errorf(expectTemplate, expect, result) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TestResolucaoNaPraticaExercicio3 tests the function ResolucaoNaPraticaExercicio3 |
| 66 | +// by capturing its output and comparing it to the expected result. It uses the |
| 67 | +// output.Capture utility to capture the function's output and the trim.New utility |
| 68 | +// to normalize the strings before comparison. If the captured output does not |
| 69 | +// contain the expected result, the test fails with an error message. |
| 70 | +func TestResolucaoNaPraticaExercicio3(t *testing.T) { |
| 71 | + output := output.New() |
| 72 | + result := output.Capture(ResolucaoNaPraticaExercicio3) |
| 73 | + |
| 74 | + expect := ` |
| 75 | +Resolução: |
| 76 | +
|
| 77 | +Slice: 1, 2, 3, 4 |
| 78 | +Tipo: []string |
| 79 | +` |
| 80 | + |
| 81 | + trim := trim.New() |
| 82 | + |
| 83 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 84 | + t.Errorf(expectTemplate, expect, result) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestResolucaoNaPraticaExercicio4 tests the function ResolucaoNaPraticaExercicio4 |
| 89 | +// by capturing its output and comparing it to the expected result. The test |
| 90 | +// verifies that the function correctly appends elements to slices and produces |
| 91 | +// the expected output. If the actual output does not match the expected output, |
| 92 | +// the test will fail and report the discrepancy. |
| 93 | +func TestResolucaoNaPraticaExercicio4(t *testing.T) { |
| 94 | + output := output.New() |
| 95 | + result := output.Capture(ResolucaoNaPraticaExercicio4) |
| 96 | + |
| 97 | + expect := ` |
| 98 | +Resolução: |
| 99 | +append52: [42 43 44 45 46 47 48 49 50 51 52] |
| 100 | +append53to55: [42 43 44 45 46 47 48 49 50 51 52 53 54 55] |
| 101 | +appendSliceY: [42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60] |
| 102 | +` |
| 103 | + trim := trim.New() |
| 104 | + |
| 105 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 106 | + t.Errorf(expectTemplate, expect, result) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestResolucaoNaPraticaExercicio5 tests the function ResolucaoNaPraticaExercicio5 |
| 111 | +// by capturing its output and comparing it to the expected result. |
| 112 | +// It uses the output.Capture utility to capture the function's output |
| 113 | +// and the trim.New utility to normalize the strings before comparison. |
| 114 | +// If the captured output does not contain the expected result, the test fails |
| 115 | +// and an error message is displayed. |
| 116 | +func TestResolucaoNaPraticaExercicio5(t *testing.T) { |
| 117 | + output := output.New() |
| 118 | + result := output.Capture(ResolucaoNaPraticaExercicio5) |
| 119 | + |
| 120 | + expect := ` |
| 121 | +Resolução: |
| 122 | +[42 43 44 48 49 50 51] |
| 123 | +` |
| 124 | + trim := trim.New() |
| 125 | + |
| 126 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 127 | + t.Errorf(expectTemplate, expect, result) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// TestResolucaoNaPraticaExercicio6 tests the function ResolucaoNaPraticaExercicio6. |
| 132 | +// It captures the output of the function and compares it with the expected result. |
| 133 | +// If the captured output does not match the expected result, the test fails with an error message. |
| 134 | +func TestResolucaoNaPraticaExercicio6(t *testing.T) { |
| 135 | + output := output.New() |
| 136 | + result := output.Capture(ResolucaoNaPraticaExercicio6) |
| 137 | + |
| 138 | + expect := ` |
| 139 | +Resolução: |
| 140 | +len: 26 cap: 26 |
| 141 | +Estados: Acre, Alagoas, Amapá, Amazonas, Bahia, Ceará, Espírito Santo, Goiás, Maranhão, Mato Grosso, Mato Grosso do Sul, Minas Gerais, Pará, Paraíba, Paraná, Pernambuco, Piauí, Rio de Janeiro, Rio Grande do Norte, Rio Grande do Sul, Rondônia, Roraima, Santa Catarina, São Paulo, Sergipe, Tocantins |
| 142 | +` |
| 143 | + |
| 144 | + trim := trim.New() |
| 145 | + |
| 146 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 147 | + t.Errorf(expectTemplate, expect, result) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// TestResolucaoNaPraticaExercicio7 tests the function ResolucaoNaPraticaExercicio7 |
| 152 | +// by capturing its output and comparing it to the expected result. The test |
| 153 | +// verifies that the output contains the expected formatted strings for names, |
| 154 | +// surnames, and favorite hobbies. |
| 155 | +func TestResolucaoNaPraticaExercicio7(t *testing.T) { |
| 156 | + output := output.New() |
| 157 | + result := output.Capture(ResolucaoNaPraticaExercicio7) |
| 158 | + |
| 159 | + expect := ` |
| 160 | +Resolução: |
| 161 | +Nome: Fabiano, Sobrenome: Florentino, Hobby favorito: Programar |
| 162 | +Nome: Fulano, Sobrenome: de Tal, Hobby favorito: Jogar bola |
| 163 | +Nome: Ciclano, Sobrenome: da Silva, Hobby favorito: Assistir filmes |
| 164 | +` |
| 165 | + |
| 166 | + trim := trim.New() |
| 167 | + |
| 168 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 169 | + t.Errorf(expectTemplate, expect, result) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// TestResolucaoNaPraticaExercicio8 tests the function ResolucaoNaPraticaExercicio8 |
| 174 | +// by capturing its output and comparing it with the expected result. The test |
| 175 | +// checks if the output contains the expected formatted string with names and hobbies. |
| 176 | +// If the output does not match the expected result, the test will fail and report an error. |
| 177 | +func TestResolucaoNaPraticaExercicio8(t *testing.T) { |
| 178 | + output := output.New() |
| 179 | + result := output.Capture(ResolucaoNaPraticaExercicio8) |
| 180 | + |
| 181 | + expect := ` |
| 182 | +Resolução: |
| 183 | +Sobrenome_Nome: florentino_fabiano |
| 184 | + Hobby 1: Programar |
| 185 | + Hobby 2: Jogar bola |
| 186 | + Hobby 3: Assistir filmes |
| 187 | +Sobrenome_Nome: de_tal_fulano |
| 188 | + Hobby 1: Programar |
| 189 | + Hobby 2: Jogar bola |
| 190 | + Hobby 3: Assistir filmes |
| 191 | +Sobrenome_Nome: da_silva_ciclano |
| 192 | + Hobby 1: Programar 2 |
| 193 | + Hobby 2: Jogar bola 2 |
| 194 | + Hobby 3: Assistir filmes 2 |
| 195 | +` |
| 196 | + |
| 197 | + trim := trim.New() |
| 198 | + |
| 199 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 200 | + t.Errorf(expectTemplate, expect, result) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// TestResolucaoNaPraticaExercicio9 tests the function ResolucaoNaPraticaExercicio9 |
| 205 | +// by capturing its output and comparing it to the expected result. The test |
| 206 | +// verifies that the output contains the expected formatted strings for different |
| 207 | +// individuals and their hobbies. If the output does not match the expected result, |
| 208 | +// the test will fail and report the discrepancy. |
| 209 | +func TestResolucaoNaPraticaExercicio9(t *testing.T) { |
| 210 | + output := output.New() |
| 211 | + result := output.Capture(ResolucaoNaPraticaExercicio9) |
| 212 | + |
| 213 | + expect := ` |
| 214 | +Resolução: |
| 215 | +Sobrenome_Nome: florentino_fabiano |
| 216 | + Hobby 1: Programar |
| 217 | + Hobby 2: Jogar bola |
| 218 | + Hobby 3: Assistir filmes |
| 219 | +Sobrenome_Nome: de_tal_fulano |
| 220 | + Hobby 1: Programar |
| 221 | + Hobby 2: Jogar bola |
| 222 | + Hobby 3: Assistir filmes |
| 223 | +Sobrenome_Nome: da_silva_ciclano |
| 224 | + Hobby 1: Programar 2 |
| 225 | + Hobby 2: Jogar bola 2 |
| 226 | + Hobby 3: Assistir filmes 2 |
| 227 | +Sobrenome_Nome: de_tal_ciclano |
| 228 | + Hobby 1: Programar 3 |
| 229 | + Hobby 2: Jogar bola 3 |
| 230 | + Hobby 3: Assistir filmes 3 |
| 231 | +` |
| 232 | + |
| 233 | + trim := trim.New() |
| 234 | + |
| 235 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 236 | + t.Errorf(expectTemplate, expect, result) |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +// TestResolucaoNaPraticaExercicio10 tests the function ResolucaoNaPraticaExercicio10. |
| 241 | +// It captures the output of the function and compares it with the expected output. |
| 242 | +// If the captured output does not match the expected output, the test fails and an error is reported. |
| 243 | +func TestResolucaoNaPraticaExercicio10(t *testing.T) { |
| 244 | + output := output.New() |
| 245 | + result := output.Capture(ResolucaoNaPraticaExercicio10) |
| 246 | + |
| 247 | + expect := ` |
| 248 | +Resolução: |
| 249 | +Sobrenome_Nome: florentino_fabio |
| 250 | + Hobby 1: Programar |
| 251 | + Hobby 2: Jogar bola |
| 252 | + Hobby 3: Assistir filmes |
| 253 | +Sobrenome_Nome: de_tal_fulano |
| 254 | + Hobby 1: Programar |
| 255 | + Hobby 2: Jogar bola |
| 256 | + Hobby 3: Assistir filmes |
| 257 | +Sobrenome_Nome: de_tal_ciclano |
| 258 | + Hobby 1: Programar 3 |
| 259 | + Hobby 2: Jogar bola 3 |
| 260 | + Hobby 3: Assistir filmes 3 |
| 261 | +` |
| 262 | + |
| 263 | + trim := trim.New() |
| 264 | + |
| 265 | + if !strings.Contains(trim.String(result), trim.String(expect)) { |
| 266 | + t.Errorf(expectTemplate, expect, result) |
| 267 | + } |
| 268 | +} |
0 commit comments