|
| 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 | +} |
0 commit comments