add calories
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-03-18 14:35:05 +07:00
parent c52a1f7f40
commit 871b6c6dcd
3 changed files with 43 additions and 32 deletions
+37
View File
@@ -0,0 +1,37 @@
package calories
import (
"strconv"
"strings"
)
var (
caloriesMap = map[string]int{
"чай": 79,
"яблоко": 100,
}
)
func CalcCalories(text string) (int, error) {
count, ok := caloriesMap[strings.ToLower(text)]
if ok {
return count, nil
}
arr := strings.Split(text, " ")
if len(arr) == 2 {
coun1, err := strconv.Atoi(arr[0])
if err != nil {
return 0, nil
}
count2, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
return coun1 * count2 / 100, nil
}
count, err := strconv.Atoi(text)
if err != nil {
return 0, nil
}
return count, nil
}
+54
View File
@@ -0,0 +1,54 @@
package calories
import "testing"
func TestCalcCalories(t *testing.T) {
t.Parallel()
tests := []struct {
name string
text string
want int
wantErr bool
}{
{
"empty text",
"",
0,
false,
},
{
"1 text",
"1",
1,
false,
},
{
"2 5 text",
"20 50",
10,
false,
},
{
"чай text",
"чай",
39,
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
count, err := calcCalories(tt.text)
if (err != nil) != tt.wantErr {
t.Errorf("error calc calories: %v", err)
return
}
if count != tt.want {
t.Errorf("error count: %v", err)
return
}
})
}
}