|
1 | 1 | package v1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 | ) |
7 | 8 |
|
| 9 | +func ToUpper(s string) (string, error) { |
| 10 | + return strings.ToUpper(s), nil |
| 11 | +} |
| 12 | + |
| 13 | +// Wrapper para strings.TrimSpace que se encaixa na assinatura esperada por Pipe. |
| 14 | +func Trim(s string) (string, error) { |
| 15 | + return strings.TrimSpace(s), nil |
| 16 | +} |
| 17 | + |
8 | 18 | func TestPipe(t *testing.T) { |
9 | 19 |
|
10 | 20 | pipeline := Pipe( |
11 | | - strings.ToUpper, |
12 | | - strings.TrimSpace, |
| 21 | + ToUpper, |
| 22 | + Trim, |
13 | 23 | ) |
14 | 24 |
|
15 | 25 | input := " go is awesome " |
@@ -44,3 +54,43 @@ func TestParseTo(t *testing.T) { |
44 | 54 | t.Errorf("Expected ParseTo to fail when types are incompatible") |
45 | 55 | } |
46 | 56 | } |
| 57 | + |
| 58 | +// Sum recebe dois números e retorna a sua soma. |
| 59 | +func Sum(a, b int) (int, error) { |
| 60 | + return a + b, nil |
| 61 | +} |
| 62 | + |
| 63 | +// IntToString recebe um número e retorna sua representação em string. |
| 64 | +func IntToString(n int) (string, error) { |
| 65 | + return fmt.Sprintf("%d", n), nil |
| 66 | +} |
| 67 | + |
| 68 | +func TestSumPipeline(t *testing.T) { |
| 69 | + // Cria o pipeline. |
| 70 | + pipeline := Pipe( |
| 71 | + Sum, |
| 72 | + IntToString, |
| 73 | + ) |
| 74 | + |
| 75 | + // Define a entrada e a saída esperada. |
| 76 | + input1 := 5 |
| 77 | + input2 := 7 |
| 78 | + expected := "12" |
| 79 | + |
| 80 | + // Executa o pipeline com os números de entrada. |
| 81 | + resultInterface, err := pipeline(input1, input2) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("Pipeline returned an error: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Converte o resultado para string. |
| 87 | + result, ok := resultInterface.(string) |
| 88 | + if !ok { |
| 89 | + t.Fatalf("Failed to convert result to string") |
| 90 | + } |
| 91 | + |
| 92 | + // Verifica se o resultado é o esperado. |
| 93 | + if result != expected { |
| 94 | + t.Errorf("Expected %s, got %s", expected, result) |
| 95 | + } |
| 96 | +} |
0 commit comments