Skip to content

Commit e13ff0b

Browse files
committed
#13 - go
1 parent 2ffa31b commit e13ff0b

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
)
8+
9+
func sum(a, b int) int {
10+
return a + b
11+
}
12+
13+
func TestSum(t *testing.T) {
14+
testCases := []struct {
15+
a, b int
16+
expected int
17+
}{
18+
{1, 2, 3},
19+
{5, 7, 12},
20+
{-3, 3, 0},
21+
{0, 0, 0},
22+
}
23+
24+
for _, tc := range testCases {
25+
result := sum(tc.a, tc.b)
26+
if result != tc.expected {
27+
t.Errorf("sum(%d, %d) = %d; expected %d", tc.a, tc.b, result, tc.expected)
28+
}
29+
}
30+
}
31+
32+
/************************************** RETO ******************************************************/
33+
func TestPersonDictionary(t *testing.T) {
34+
person := map[string]interface{}{
35+
"name": "John Doe",
36+
"age": 30,
37+
"birth_date": "1990-01-01",
38+
"programming_languages": []string{"Go", "Python", "JavaScript"},
39+
}
40+
41+
// Primer test: verificar la existencia de todos los campos
42+
expectedFields := []string{"name", "age", "birth_date", "programming_languages"}
43+
for _, field := range expectedFields {
44+
if _, exists := person[field]; !exists {
45+
t.Errorf("El campo '%s' no existe en el diccionario", field)
46+
}
47+
}
48+
49+
// Segundo test: verificar que los datos introducidos son correctos
50+
if name, ok := person["name"].(string); !ok || name == "" {
51+
t.Error("El campo 'name' no es una cadena válida")
52+
}
53+
54+
if age, ok := person["age"].(int); !ok || age <= 0 {
55+
t.Error("El campo 'age' no es un entero válido")
56+
}
57+
58+
if birthDate, ok := person["birth_date"].(string); !ok {
59+
t.Error("El campo 'birth_date' no es una cadena")
60+
} else {
61+
_, err := time.Parse("2006-01-02", birthDate)
62+
if err != nil {
63+
t.Error("El campo 'birth_date' no tiene un formato válido (YYYY-MM-DD)")
64+
}
65+
}
66+
67+
if languages, ok := person["programming_languages"].([]string); !ok {
68+
t.Error("El campo 'programming_languages' no es un slice de cadenas")
69+
} else if len(languages) == 0 {
70+
t.Error("El campo 'programming_languages' está vacío")
71+
}
72+
}
73+
74+
func TestPersonDictionaryValues(t *testing.T) {
75+
person := map[string]interface{}{
76+
"name": "John Doe",
77+
"age": 30,
78+
"birth_date": "1990-01-01",
79+
"programming_languages": []string{"Go", "Python", "JavaScript"},
80+
}
81+
82+
expectedPerson := map[string]interface{}{
83+
"name": "John Doe",
84+
"age": 30,
85+
"birth_date": "1990-01-01",
86+
"programming_languages": []string{"Go", "Python", "JavaScript"},
87+
}
88+
89+
if !reflect.DeepEqual(person, expectedPerson) {
90+
t.Errorf("Los valores del diccionario no coinciden con los esperados")
91+
}
92+
}

0 commit comments

Comments
 (0)