@@ -2,21 +2,84 @@ package v1
22
33import (
44 "fmt"
5+ "github.com/devalexandre/gofn/pipe"
6+ "reflect"
57 "strings"
68 "testing"
79)
810
11+ // Exemplos de funções adaptadas para usar com generics
912func ToUpper (s string ) (string , error ) {
1013 return strings .ToUpper (s ), nil
1114}
1215
13- // Wrapper para strings.TrimSpace que se encaixa na assinatura esperada por Pipe.
1416func Trim (s string ) (string , error ) {
1517 return strings .TrimSpace (s ), nil
1618}
1719
18- func TestPipe (t * testing.T ) {
20+ func ValidateCPF (cpf string ) (string , error ) {
21+ if len (cpf ) != 11 {
22+ return "" , fmt .Errorf ("CPF must have 11 digits" )
23+ }
24+ return cpf , nil
25+ }
26+
27+ func FormatCPF (cpf string ) (string , error ) {
28+ return fmt .Sprintf ("%s.%s.%s-%s" , cpf [0 :3 ], cpf [3 :6 ], cpf [6 :9 ], cpf [9 :11 ]), nil
29+ }
30+
31+ func Sum (a , b int ) (int , error ) {
32+ return a + b , nil
33+ }
34+
35+ func Multiply (a int ) (int , error ) {
36+ return a * 2 , nil
37+ }
1938
39+ func ShowResult (result int ) (int , error ) {
40+ fmt .Println (result )
41+ return result , nil
42+ }
43+
44+ // Estrutura Person e funções de validação ajustadas
45+ type Person struct {
46+ Name string
47+ Email string
48+ Phone string
49+ }
50+
51+ func ValidatePersonName (p Person ) (Person , error ) {
52+ if p .Name == "" {
53+ return p , fmt .Errorf ("Name is required" )
54+ }
55+ return p , nil
56+ }
57+
58+ func ValidatePersonEmail (p Person ) (Person , error ) {
59+ if p .Email == "" || ! strings .Contains (p .Email , "@" ) {
60+ return p , fmt .Errorf ("Valid email is required" )
61+ }
62+ return p , nil
63+ }
64+
65+ func ValidatePersonPhone (p Person ) (Person , error ) {
66+ if p .Phone == "" {
67+ return p , fmt .Errorf ("Phone is required" )
68+ }
69+ return p , nil
70+ }
71+
72+ func SumMore (a , b , c int ) (int , error ) {
73+ return a + b + c , nil
74+ }
75+
76+ func GenerateTreeNumbers (n int ) (int , int , int ) {
77+ return n , n * 2 , n * 3
78+ }
79+
80+ // Testes ajustados para usar a função Pipe com generics
81+
82+ func TestStringPipeline (t * testing.T ) {
2083 pipeline := Pipe (
2184 ToUpper ,
2285 Trim ,
@@ -34,63 +97,110 @@ func TestPipe(t *testing.T) {
3497 }
3598}
3699
37- func TestParseTo (t * testing.T ) {
38-
39- result := "test result"
40- var target string
100+ func TestCPFPipeline (t * testing.T ) {
101+ pipeline := Pipe (
102+ ValidateCPF ,
103+ FormatCPF ,
104+ )
41105
42- err := ParseTo (result , & target )
106+ input := "12345678901"
107+ expected := "123.456.789-01"
108+ result , err := pipeline (input )
43109 if err != nil {
44- t .Errorf ("ParseTo returned an error: %v" , err )
110+ t .Errorf ("Pipeline returned an error: %v" , err )
111+ }
112+
113+ if result != expected {
114+ t .Errorf ("Expected %s, got %s" , expected , result )
45115 }
116+ }
117+
118+ func TestMathPipeline (t * testing.T ) {
119+ pipeline := Pipe (
120+ Sum ,
121+ Multiply ,
122+ ShowResult ,
123+ )
46124
47- if target != result {
48- t .Errorf ("Expected target to be %s, got %s" , result , target )
125+ expected := 18
126+ result , err := pipeline (5 , 4 )
127+ if err != nil {
128+ t .Errorf ("Pipeline returned an error: %v" , err )
49129 }
50130
51- var intTarget int
52- err = ParseTo (result , & intTarget )
53- if err == nil {
54- t .Errorf ("Expected ParseTo to fail when types are incompatible" )
131+ if result != expected {
132+ t .Errorf ("Expected %d, got %d" , expected , result )
55133 }
56134}
57135
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- }
136+ func TestPersonPipeline (t * testing.T ) {
137+ pipeline := Pipe (
138+ ValidatePersonName ,
139+ ValidatePersonEmail ,
140+ ValidatePersonPhone ,
141+ )
142+
143+ input := Person {
144+ Name : "John Doe" ,
145+ Email : "john.doe@example.com" ,
146+ Phone : "1234567890" ,
147+ }
148+ expected := input // assumindo que a entrada é válida e esperada como saída
62149
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
150+ result , err := pipeline (input )
151+ if err != nil {
152+ t .Errorf ("Pipeline returned an error: %v" , err )
153+ }
154+
155+ if result != expected {
156+ t .Errorf ("Expected %+v, got %+v" , expected , result )
157+ }
66158}
67159
68- func TestSumPipeline (t * testing.T ) {
69- // Cria o pipeline.
160+ func TestMoreArgs (t * testing.T ) {
70161 pipeline := Pipe (
71- Sum ,
72- IntToString ,
162+ GenerateTreeNumbers ,
163+ SumMore ,
73164 )
74165
75- // Define a entrada e a saída esperada.
76- input1 := 5
77- input2 := 7
78- expected := "12"
166+ expected := 6
167+ result , err := pipeline (1 )
168+ if err != nil {
169+ t .Errorf ("Pipeline returned an error: %v" , err )
170+ }
171+
172+ if result != expected {
173+ t .Errorf ("Expected %d, got %d" , expected , result )
174+ }
175+ }
176+
177+ func TestPipeGoFn (t * testing.T ) {
178+ data := []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
179+ expected := []int {4 , 8 , 12 , 16 , 20 }
180+ // Definindo o pipeline
181+ process := Pipe (
182+ pipe .Filter (func (i int ) bool { return i % 2 == 0 }),
183+ pipe .Map (func (i int ) int { return i * 2 }),
184+ )
79185
80- // Executa o pipeline com os números de entrada.
81- resultInterface , err := pipeline ( input1 , input2 )
186+ // Aplicando o pipeline aos dados
187+ resultInterface , err := process ( data )
82188 if err != nil {
83- t .Fatalf ("Pipeline returned an error: %v" , err )
189+ fmt .Println ("Erro ao processar:" , err )
190+ return
84191 }
85192
86- // Converte o resultado para string.
87- result , ok := resultInterface .(string )
193+ result , ok := resultInterface .([]int )
88194 if ! ok {
89- t .Fatalf ( "Failed to convert result to string" )
195+ t .Errorf ( "Expected result type []int, got %T" , resultInterface )
90196 }
91197
92- // Verifica se o resultado é o esperado.
93- if result != expected {
94- t .Errorf ("Expected %s, got %s" , expected , result )
198+ if len (result ) != len (expected ) {
199+ t .Errorf ("Expected %v, got %v" , expected , result )
200+ }
201+
202+ if ! reflect .DeepEqual (result , expected ) {
203+ t .Errorf ("Expected %v, got %v" , expected , result )
95204 }
205+
96206}
0 commit comments