|
| 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