From 6809e376d7b7eceb337c576165799b3b44a2084e Mon Sep 17 00:00:00 2001 From: Softonik Date: Tue, 25 Jun 2024 18:05:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=B8=20go.mod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/tasks.json | 30 +++++++++++++ go.mod | 24 +++++++++++ go.sum | 63 +++++++++++++++++++++++++++ pkg/accuracy/accuracy.go | 34 +++++++++++++++ pkg/accuracy/accuracy_test.go | 60 ++++++++++++++++++++++++++ pkg/accuracy/features/app.feature | 9 ++++ pkg/accuracy/init_test.go | 55 +++++++++++++++++++++++ pkg/lib/lib.go | 72 +++++++++++++++++++++++++++++++ pkg/schet/helpers.go | 2 +- pkg/schet/init_test.go | 2 +- pkg/schet/schet_test.go | 2 +- pkg/testlib/patching.go | 15 +++++++ pkg/testlib/testlib.go | 60 ++++++++++++++++++++++++++ 13 files changed, 425 insertions(+), 3 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/accuracy/accuracy.go create mode 100644 pkg/accuracy/accuracy_test.go create mode 100644 pkg/accuracy/features/app.feature create mode 100644 pkg/accuracy/init_test.go create mode 100644 pkg/lib/lib.go create mode 100644 pkg/testlib/patching.go create mode 100644 pkg/testlib/testlib.go diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0fbe4b0 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "process", + "label": "go: test package", + "command": "/s/t", + "args": [ + "${fileDirname}", + "${fileDirnameBasename}" + ], + "problemMatcher": [ + "$go" + ], + "group": { + "kind": "test", + "isDefault": true + }, + "detail": "cd /app; go test ${fileDirname}", + "presentation": { + "echo": false, + "reveal": "always", + "focus": false, + "panel": "shared", + "showReuseMessage": false, + "clear": true + } + } + ] +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c3b36a3 --- /dev/null +++ b/go.mod @@ -0,0 +1,24 @@ +module my/schet + +go 1.20 + +require ( + github.com/agiledragon/gomonkey/v2 v2.9.0 // indirect + github.com/cucumber/gherkin-go/v19 v19.0.3 // indirect + github.com/cucumber/godog v0.12.6 // indirect + github.com/cucumber/messages-go/v16 v16.0.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-telegram/bot v1.2.2 // indirect + github.com/gofrs/uuid v4.2.0+incompatible // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/hashicorp/go-immutable-radix v1.3.1 // indirect + github.com/hashicorp/go-memdb v1.3.2 // indirect + github.com/hashicorp/golang-lru v0.5.4 // indirect + github.com/onsi/gomega v1.27.4 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/text v0.8.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace github.com/cucumber/godog => /gopath/src/my/godog diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..63f857b --- /dev/null +++ b/go.sum @@ -0,0 +1,63 @@ +github.com/agiledragon/gomonkey/v2 v2.9.0 h1:PDiKKybR596O6FHW+RVSG0Z7uGCBNbmbUXh3uCNQ7Hc= +github.com/agiledragon/gomonkey/v2 v2.9.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cucumber/gherkin-go/v19 v19.0.3 h1:mMSKu1077ffLbTJULUfM5HPokgeBcIGboyeNUof1MdE= +github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw= +github.com/cucumber/godog v0.12.6 h1:3IToXviU45G7FgijwTk/LdB4iojn8zUFDfQLj4MMiHc= +github.com/cucumber/godog v0.12.6/go.mod h1:Y02TTpimPXDb70PnG6M3zpODXm1+bjCsuZzcW76xAww= +github.com/cucumber/messages-go/v16 v16.0.0/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/cucumber/messages-go/v16 v16.0.1 h1:fvkpwsLgnIm0qugftrw2YwNlio+ABe2Iu94Ap8GMYIY= +github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/go-telegram/bot v1.2.2 h1:LwGbSzjcSi0w4Ke8JUpgbBhJwwYTl2ITmhubeM2WvN8= +github.com/go-telegram/bot v1.2.2/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= +github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= +github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-memdb v1.3.2 h1:RBKHOsnSszpU6vxq80LzC2BaQjuuvoyaQbkLTf7V7g8= +github.com/hashicorp/go-memdb v1.3.2/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E= +github.com/onsi/gomega v1.27.4/go.mod h1:riYq/GJKh8hhoM01HN6Vmuy93AarCXCBGpvFDK3q3fQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/accuracy/accuracy.go b/pkg/accuracy/accuracy.go new file mode 100644 index 0000000..c3c28c0 --- /dev/null +++ b/pkg/accuracy/accuracy.go @@ -0,0 +1,34 @@ +package accuracy + +import ( + "math" +) + +const ( + roundingTimeHoursDigitsCount = 1 + roundingTimeHoursТочнееDigitsCount = 5 +) + +func RoundTimeHours(number float64) float64 { + return RoundFloat(number, roundingTimeHoursDigitsCount) +} +func RoundTimeHoursТочнее(number float64) float64 { + return RoundFloat(number, roundingTimeHoursТочнееDigitsCount) +} +func RoundFloat(number float64, roundingTempDigitsCount int) float64 { + temp := math.Pow(10, float64(roundingTempDigitsCount)) + return math.Round(number*temp) / temp +} + +func СекундыВМинуты(seconds int) int { + s_ostatok := seconds % 60 + res := seconds / 60 + if s_ostatok < 30 { + return res + } + return res + 1 +} + +func ОкруглитьРубли(сумма float64) int { + return int(math.Round(сумма)) +} diff --git a/pkg/accuracy/accuracy_test.go b/pkg/accuracy/accuracy_test.go new file mode 100644 index 0000000..2578484 --- /dev/null +++ b/pkg/accuracy/accuracy_test.go @@ -0,0 +1,60 @@ +package accuracy + +import ( + . "github.com/onsi/gomega" +) + +type testData struct { +} + +var ( + t *testData +) + +func resetTestData() { + t = &testData{} + _ = Ω +} + +func beforeSuite() { +} +func afterSuite() { +} + +func beforeScenario() { + resetTestData() +} +func afterScenario() { +} + +// ----------------------- +func roundTempВозвращаетОкруглённое() { + // res := RoundTemp(0.0000003) + // Ω(res).To(testlib.Be(0.0)) + + // res = RoundTemp(0.000003) + // Ω(res).To(testlib.Be(0.0)) + + // res = RoundTemp(0.003) + // Ω(res).To(testlib.Be(0.0)) + + // res = RoundTemp(0.199) + // Ω(res).To(testlib.Be(0.2)) + + // res = RoundTemp(0.3333) + // Ω(res).To(testlib.Be(0.3)) +} + +func roundDistВозвращаетОкруглённое() { + // res := RoundDist(0.0000003) + // Ω(res).To(testlib.Be(0.0)) + + // res = RoundDist(0.003) + // Ω(res).To(testlib.Be(0.0)) + + // res = RoundDist(0.03) + // Ω(res).To(testlib.Be(0.03)) + + // res = RoundDist(0.333) + // Ω(res).To(testlib.Be(0.33)) +} diff --git a/pkg/accuracy/features/app.feature b/pkg/accuracy/features/app.feature new file mode 100644 index 0000000..283aff6 --- /dev/null +++ b/pkg/accuracy/features/app.feature @@ -0,0 +1,9 @@ +# language: ru + +Функциональность: Точность + + Сценарий: Округление температуры до десятых + То RoundTemp возвращает округлённое + + Сценарий: Округление расстояний до сотых + То RoundDist возвращает округлённое diff --git a/pkg/accuracy/init_test.go b/pkg/accuracy/init_test.go new file mode 100644 index 0000000..e7e0f14 --- /dev/null +++ b/pkg/accuracy/init_test.go @@ -0,0 +1,55 @@ +package accuracy + +import ( + "context" + "os" + "testing" + + "my/schet/pkg/testlib" + + "github.com/cucumber/godog" + "github.com/cucumber/godog/colors" + + . "github.com/onsi/gomega" + gomega_format "github.com/onsi/gomega/format" +) + +func InitializeScenario(ctx *godog.ScenarioContext) { + ctx.Step(`^RoundTemp возвращает округлённое$`, roundTempВозвращаетОкруглённое) + ctx.Step(`^RoundDist возвращает округлённое$`, roundDistВозвращаетОкруглённое) + + // ----------------------- + ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { + beforeScenario() + return ctx, nil + }) + ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) { + afterScenario() + return ctx, nil + }) + testlib.InitializeGomegaForGodog(ctx) + gomega_format.CharactersAroundMismatchToInclude = 50 + _ = Ω +} + +func InitializeSuite(tsc *godog.TestSuiteContext) { + tsc.BeforeSuite(beforeSuite) + tsc.AfterSuite(afterSuite) +} + +func TestMain(m *testing.M) { + var opts = godog.Options{ + Output: colors.Colored(os.Stdout), + Strict: true, + StopOnFailure: true, + } + + godog.BindCommandLineFlags("godog.", &opts) + r := godog.TestSuite{ + Name: "app", + TestSuiteInitializer: InitializeSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, + }.Run() + os.Exit(r) +} diff --git a/pkg/lib/lib.go b/pkg/lib/lib.go new file mode 100644 index 0000000..789fa40 --- /dev/null +++ b/pkg/lib/lib.go @@ -0,0 +1,72 @@ +package lib + +import ( + "io" + "log" + "os" + "os/exec" +) + +func Bash(cmd string) error { + c := exec.Command("/bin/bash", "-c", cmd) + c.Stderr = os.Stderr + c.Stdout = os.Stdout + c.Stdin = os.Stdin + return c.Run() +} +func BashV(cmd string) error { + log.Println("cmd:", cmd) + return Bash(cmd) +} + +func BashOutput(cmd string) (string, error) { + out, err := exec.Command("/bin/bash", "-c", cmd).CombinedOutput() + return string(out), err +} + +func ReadFile(path string) (string, error) { + ret, err := os.ReadFile(path) + if err != nil { + return "", err + } + return string(ret), nil +} +func WriteFile(path, data string) error { + return os.WriteFile(path, []byte(data), 0644) +} + +func CopyFile(src, dst string) (err error) { + in, err := os.Open(src) + if err != nil { + return + } + + defer in.Close() + + out, err := os.Create(dst) + if err != nil { + return + } + + defer func() { + cerr := out.Close() + if err == nil { + err = cerr + } + }() + + if _, err = io.Copy(out, in); err != nil { + return + } + + err = out.Sync() + + return +} + +func Plus(in bool) string { + if in { + return "+" + } + return "-" +} diff --git a/pkg/schet/helpers.go b/pkg/schet/helpers.go index 24ec99c..b4fab48 100644 --- a/pkg/schet/helpers.go +++ b/pkg/schet/helpers.go @@ -1,7 +1,7 @@ package schet import ( - "my/gnocount/pkg/accuracy" + "my/schet/pkg/accuracy" ) func процентСуммы(сумма float64) float64 { diff --git a/pkg/schet/init_test.go b/pkg/schet/init_test.go index d52555d..6903fcd 100644 --- a/pkg/schet/init_test.go +++ b/pkg/schet/init_test.go @@ -5,7 +5,7 @@ import ( "os" "testing" - . "my/gnocount/pkg/testlib" + . "my/schet/pkg/testlib" "github.com/cucumber/godog" "github.com/cucumber/godog/colors" diff --git a/pkg/schet/schet_test.go b/pkg/schet/schet_test.go index 8469222..9af4910 100644 --- a/pkg/schet/schet_test.go +++ b/pkg/schet/schet_test.go @@ -2,7 +2,7 @@ package schet import ( "fmt" - . "my/gnocount/pkg/testlib" + . "my/schet/pkg/testlib" . "github.com/onsi/gomega" ) diff --git a/pkg/testlib/patching.go b/pkg/testlib/patching.go new file mode 100644 index 0000000..4f566ea --- /dev/null +++ b/pkg/testlib/patching.go @@ -0,0 +1,15 @@ +package testlib + +import ( + "unicode" + + . "github.com/agiledragon/gomonkey/v2" +) + +func Patch(target interface{}, methodName string, double interface{}) *Patches { + c := []rune(methodName)[0] + if unicode.IsUpper(c) { + return ApplyMethod(target, methodName, double) + } + return ApplyPrivateMethod(target, methodName, double) +} diff --git a/pkg/testlib/testlib.go b/pkg/testlib/testlib.go new file mode 100644 index 0000000..6d2c5dc --- /dev/null +++ b/pkg/testlib/testlib.go @@ -0,0 +1,60 @@ +package testlib + +import ( + "context" + "fmt" + "strconv" + "time" + + "github.com/cucumber/godog" + . "github.com/onsi/gomega" +) + +var Be = Equal + +func NoErr(err error) { + ExpectWithOffset(1, err).NotTo(HaveOccurred()) +} + +var Ok = NoErr + +func Yes(b bool) { + ExpectWithOffset(1, b).To(BeTrue()) +} +func YesText(b bool, text string) { + ExpectWithOffset(1, b).To(BeTrue(), text) +} + +func Atoi(in string) int { + res, err := strconv.Atoi(in) + Ok(err) + return res +} +func Atof(in string) float64 { + res, err := strconv.ParseFloat(in, 32) + Ok(err) + return res +} + +var TestResult error + +func InitializeGomegaForGodog(ctx *godog.ScenarioContext) { + ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) { + TestResult = nil + return ctx, nil + }) + ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) { + return ctx, TestResult + }) + RegisterFailHandler(func(message string, callerSkip ...int) { + // remember only the expectation failed first + // anything thereafter is not to be believed + if TestResult == nil { + TestResult = fmt.Errorf(message) + } + }) +} + +func Sleep(m time.Duration) { + time.Sleep(m * time.Millisecond) +}