-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from devalexandre/feature/add-milt-args
Feature/add milt args
- Loading branch information
Showing
4 changed files
with
191 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/devalexandre/pipe | ||
|
||
go 1.20 | ||
|
||
require github.com/devalexandre/gofn v1.0.1 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/devalexandre/gofn v1.0.1 h1:+pM0fEU5E4ebYt7ReR3FgQMjyESsHNvROJWU1kLa6lg= | ||
github.com/devalexandre/gofn v1.0.1/go.mod h1:fQLcy6EQZaySBvbJAoxcyjKCHHby01Cm24+IHbJU+qY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,84 @@ package v1 | |
|
||
import ( | ||
"fmt" | ||
"github.com/devalexandre/gofn/pipe" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// Exemplos de funções adaptadas para usar com generics | ||
func ToUpper(s string) (string, error) { | ||
return strings.ToUpper(s), nil | ||
} | ||
|
||
// Wrapper para strings.TrimSpace que se encaixa na assinatura esperada por Pipe. | ||
func Trim(s string) (string, error) { | ||
return strings.TrimSpace(s), nil | ||
} | ||
|
||
func TestPipe(t *testing.T) { | ||
func ValidateCPF(cpf string) (string, error) { | ||
if len(cpf) != 11 { | ||
return "", fmt.Errorf("CPF must have 11 digits") | ||
} | ||
return cpf, nil | ||
} | ||
|
||
func FormatCPF(cpf string) (string, error) { | ||
return fmt.Sprintf("%s.%s.%s-%s", cpf[0:3], cpf[3:6], cpf[6:9], cpf[9:11]), nil | ||
} | ||
|
||
func Sum(a, b int) (int, error) { | ||
return a + b, nil | ||
} | ||
|
||
func Multiply(a int) (int, error) { | ||
return a * 2, nil | ||
} | ||
|
||
func ShowResult(result int) (int, error) { | ||
fmt.Println(result) | ||
return result, nil | ||
} | ||
|
||
// Estrutura Person e funções de validação ajustadas | ||
type Person struct { | ||
Name string | ||
Email string | ||
Phone string | ||
} | ||
|
||
func ValidatePersonName(p Person) (Person, error) { | ||
if p.Name == "" { | ||
return p, fmt.Errorf("Name is required") | ||
} | ||
return p, nil | ||
} | ||
|
||
func ValidatePersonEmail(p Person) (Person, error) { | ||
if p.Email == "" || !strings.Contains(p.Email, "@") { | ||
return p, fmt.Errorf("Valid email is required") | ||
} | ||
return p, nil | ||
} | ||
|
||
func ValidatePersonPhone(p Person) (Person, error) { | ||
if p.Phone == "" { | ||
return p, fmt.Errorf("Phone is required") | ||
} | ||
return p, nil | ||
} | ||
|
||
func SumMore(a, b, c int) (int, error) { | ||
return a + b + c, nil | ||
} | ||
|
||
func GenerateTreeNumbers(n int) (int, int, int) { | ||
return n, n * 2, n * 3 | ||
} | ||
|
||
// Testes ajustados para usar a função Pipe com generics | ||
|
||
func TestStringPipeline(t *testing.T) { | ||
pipeline := Pipe( | ||
ToUpper, | ||
Trim, | ||
|
@@ -34,63 +97,110 @@ func TestPipe(t *testing.T) { | |
} | ||
} | ||
|
||
func TestParseTo(t *testing.T) { | ||
|
||
result := "test result" | ||
var target string | ||
func TestCPFPipeline(t *testing.T) { | ||
pipeline := Pipe( | ||
ValidateCPF, | ||
FormatCPF, | ||
) | ||
|
||
err := ParseTo(result, &target) | ||
input := "12345678901" | ||
expected := "123.456.789-01" | ||
result, err := pipeline(input) | ||
if err != nil { | ||
t.Errorf("ParseTo returned an error: %v", err) | ||
t.Errorf("Pipeline returned an error: %v", err) | ||
} | ||
|
||
if result != expected { | ||
t.Errorf("Expected %s, got %s", expected, result) | ||
} | ||
} | ||
|
||
func TestMathPipeline(t *testing.T) { | ||
pipeline := Pipe( | ||
Sum, | ||
Multiply, | ||
ShowResult, | ||
) | ||
|
||
if target != result { | ||
t.Errorf("Expected target to be %s, got %s", result, target) | ||
expected := 18 | ||
result, err := pipeline(5, 4) | ||
if err != nil { | ||
t.Errorf("Pipeline returned an error: %v", err) | ||
} | ||
|
||
var intTarget int | ||
err = ParseTo(result, &intTarget) | ||
if err == nil { | ||
t.Errorf("Expected ParseTo to fail when types are incompatible") | ||
if result != expected { | ||
t.Errorf("Expected %d, got %d", expected, result) | ||
} | ||
} | ||
|
||
// Sum recebe dois números e retorna a sua soma. | ||
func Sum(a, b int) (int, error) { | ||
return a + b, nil | ||
} | ||
func TestPersonPipeline(t *testing.T) { | ||
pipeline := Pipe( | ||
ValidatePersonName, | ||
ValidatePersonEmail, | ||
ValidatePersonPhone, | ||
) | ||
|
||
input := Person{ | ||
Name: "John Doe", | ||
Email: "[email protected]", | ||
Phone: "1234567890", | ||
} | ||
expected := input // assumindo que a entrada é válida e esperada como saída | ||
|
||
// IntToString recebe um número e retorna sua representação em string. | ||
func IntToString(n int) (string, error) { | ||
return fmt.Sprintf("%d", n), nil | ||
result, err := pipeline(input) | ||
if err != nil { | ||
t.Errorf("Pipeline returned an error: %v", err) | ||
} | ||
|
||
if result != expected { | ||
t.Errorf("Expected %+v, got %+v", expected, result) | ||
} | ||
} | ||
|
||
func TestSumPipeline(t *testing.T) { | ||
// Cria o pipeline. | ||
func TestMoreArgs(t *testing.T) { | ||
pipeline := Pipe( | ||
Sum, | ||
IntToString, | ||
GenerateTreeNumbers, | ||
SumMore, | ||
) | ||
|
||
// Define a entrada e a saída esperada. | ||
input1 := 5 | ||
input2 := 7 | ||
expected := "12" | ||
expected := 6 | ||
result, err := pipeline(1) | ||
if err != nil { | ||
t.Errorf("Pipeline returned an error: %v", err) | ||
} | ||
|
||
if result != expected { | ||
t.Errorf("Expected %d, got %d", expected, result) | ||
} | ||
} | ||
|
||
func TestPipeGoFn(t *testing.T) { | ||
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} | ||
expected := []int{4, 8, 12, 16, 20} | ||
// Definindo o pipeline | ||
process := Pipe( | ||
pipe.Filter(func(i int) bool { return i%2 == 0 }), | ||
pipe.Map(func(i int) int { return i * 2 }), | ||
) | ||
|
||
// Executa o pipeline com os números de entrada. | ||
resultInterface, err := pipeline(input1, input2) | ||
// Aplicando o pipeline aos dados | ||
resultInterface, err := process(data) | ||
if err != nil { | ||
t.Fatalf("Pipeline returned an error: %v", err) | ||
fmt.Println("Erro ao processar:", err) | ||
return | ||
} | ||
|
||
// Converte o resultado para string. | ||
result, ok := resultInterface.(string) | ||
result, ok := resultInterface.([]int) | ||
if !ok { | ||
t.Fatalf("Failed to convert result to string") | ||
t.Errorf("Expected result type []int, got %T", resultInterface) | ||
} | ||
|
||
// Verifica se o resultado é o esperado. | ||
if result != expected { | ||
t.Errorf("Expected %s, got %s", expected, result) | ||
if len(result) != len(expected) { | ||
t.Errorf("Expected %v, got %v", expected, result) | ||
} | ||
|
||
if !reflect.DeepEqual(result, expected) { | ||
t.Errorf("Expected %v, got %v", expected, result) | ||
} | ||
|
||
} |