Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions cmd/tracker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package main

import (
"fmt"
"log"

"github.com/Yandex-Practicum/tracker/internal/daysteps"
"github.com/Yandex-Practicum/tracker/internal/spentcalories"
"github.com/St-Ivanov/step-by-step/internal/actioninfo"
"github.com/St-Ivanov/step-by-step/internal/daysteps"
"github.com/St-Ivanov/step-by-step/internal/personaldata"
"github.com/St-Ivanov/step-by-step/internal/trainings"
)

func main() {
weight := 84.6
height := 1.87
person := personaldata.Personal{
Name: "Витя",
Weight: 84.6,
Height: 1.87,
}

// дневная активность
input := []string{
Expand All @@ -25,22 +29,16 @@ func main() {

fmt.Println("Активность в течение дня")

var (
dayActionsInfo string
dayActionsLog []string
)

for _, v := range input {
dayActionsInfo = daysteps.DayActionInfo(v, weight, height)
dayActionsLog = append(dayActionsLog, dayActionsInfo)
daySteps := daysteps.DaySteps{
Personal: person,
}

for _, v := range dayActionsLog {
fmt.Println(v)
}
daySteps.Print()

actioninfo.Info(input, &daySteps)

// тренировки
trainings := []string{
// // тренировки
actions := []string{
"3456,Ходьба,3h00m",
"something is wrong",
"678,Бег,0h5m",
Expand All @@ -50,20 +48,13 @@ func main() {
"15392,Бег,0h45m",
}

var trainingLog []string

for _, v := range trainings {
trainingInfo, err := spentcalories.TrainingInfo(v, weight, height)
if err != nil {
log.Printf("не получилось получить информацию о тренировке: %v", err)
continue
}
trainingLog = append(trainingLog, trainingInfo)
trains := trainings.Training{
Personal: person,
}

fmt.Println("Журнал тренировок")

for _, v := range trainingLog {
fmt.Println(v)
}
trains.Print()

actioninfo.Info(actions, &trains)
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Yandex-Practicum/tracker
module github.com/St-Ivanov/step-by-step

go 1.24.1

Expand All @@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.10.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
27 changes: 27 additions & 0 deletions internal/actioninfo/actioninfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package actioninfo

import (
"fmt"
"log"
)

type DataParser interface {
Parse(string) error
ActionInfo() (string, error)
}

func Info(dataset []string, dp DataParser) {
for _, v := range dataset {
err := dp.Parse(v)
if err != nil {
log.Println(err)
continue
}
s, err := dp.ActionInfo()
if err != nil {
log.Println(err)
continue
}
fmt.Print(s)
}
}
85 changes: 85 additions & 0 deletions internal/actioninfo/actioninfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package actioninfo

import (
"bytes"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

// MockDataParser is a mock implementation of the DataParser interface
type MockDataParser struct {
mock.Mock
}

func (m *MockDataParser) Parse(data string) error {
args := m.Called(data)
return args.Error(0)
}

func (m *MockDataParser) ActionInfo() (string, error) {
args := m.Called()
return args.String(0), args.Error(1)
}

func TestInfo(t *testing.T) {
// Set up log output capture
var logBuf bytes.Buffer
log.SetOutput(&logBuf)
defer log.SetOutput(os.Stderr)

// Set up stdout capture
var stdoutBuf bytes.Buffer
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stdout = w
defer func() { os.Stdout = old }()

tests := []struct {
name string
dataset []string
setup func(*MockDataParser)
expectedOutput string
}{
{
name: "happy path - single item",
dataset: []string{"test data"},
setup: func(m *MockDataParser) {
m.On("Parse", "test data").Return(nil)
m.On("ActionInfo").Return("processed test data", nil)
},
expectedOutput: "processed test data\n",
},
{
name: "empty dataset",
dataset: []string{},
setup: func(m *MockDataParser) {},
expectedOutput: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logBuf.Reset()
stdoutBuf.Reset()

mockParser := new(MockDataParser)
tt.setup(mockParser)

Info(tt.dataset, mockParser)

// Close the write end of the pipe and read the output
w.Close()
stdoutBuf.ReadFrom(r)

mockParser.AssertExpectations(t)
assert.Contains(t, tt.expectedOutput, stdoutBuf.String(), "вывод в stdout должен соответствовать ожидаемому результату")
})
}
}
54 changes: 44 additions & 10 deletions internal/daysteps/daysteps.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
package daysteps

import (
"fmt"
"strconv"
"strings"
"time"
)

const (
// Длина одного шага в метрах
stepLength = 0.65
// Количество метров в одном километре
mInKm = 1000
"github.com/St-Ivanov/step-by-step/internal/errors"
"github.com/St-Ivanov/step-by-step/internal/personaldata"
"github.com/St-Ivanov/step-by-step/internal/spentenergy"
)

func parsePackage(data string) (int, time.Duration, error) {
// TODO: реализовать функцию
type DaySteps struct {
Steps int
Duration time.Duration
personaldata.Personal
}

func (ds *DaySteps) Parse(datastring string) (err error) {
data := strings.Split(datastring, ",")
if len(data) != 2 {
return errors.ErrIncDataEnt
}
steps, err := strconv.Atoi(data[0])
if err != nil {
return errors.ErrConvToInt
}
if steps <= 0 {
return fmt.Errorf("%w Exactly the steps.", errors.ErrNegativeValue)
}
duration, err := time.ParseDuration(data[1])
if err != nil {
return errors.ErrConvToTime
}
if duration <= 0 {
return fmt.Errorf("%w Exactly the time.", errors.ErrNegativeValue)
}
ds.Steps = steps
ds.Duration = duration
return nil
}

func DayActionInfo(data string, weight, height float64) string {
// TODO: реализовать функцию
func (ds DaySteps) ActionInfo() (string, error) {
distance := spentenergy.Distance(ds.Steps, ds.Height)
calories, err := spentenergy.WalkingSpentCalories(ds.Steps, ds.Weight, ds.Height, ds.Duration)
if err != nil {
return "", err
}
return fmt.Sprintf(`Количество шагов: %d.
Дистанция составила %0.2f км.
Вы сожгли %0.2f ккал.
`, ds.Steps, distance, calories), nil
}
Loading