From 5a796b97993469c19a0856e7865e5c69c68028a9 Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 22 Jan 2024 02:02:47 +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=20godog=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 11 ++++ go.sum | 37 ++++++++++++++ pkg/service/features/app.feature | 16 ++++++ pkg/service/init_test.go | 58 +++++++++++++++++++++ pkg/service/service.go | 2 +- pkg/service/service_ginkgo_test.go | 31 +----------- pkg/service/service_test.go | 81 ++++++++++++++++++++++++++++++ pkg/testgodoglib/testgodoglib.go | 55 ++++++++++++++++++++ 8 files changed, 260 insertions(+), 31 deletions(-) create mode 100644 pkg/service/features/app.feature create mode 100644 pkg/service/init_test.go create mode 100644 pkg/service/service_test.go create mode 100644 pkg/testgodoglib/testgodoglib.go diff --git a/go.mod b/go.mod index 5e57dea..70dc998 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,24 @@ module my/go-translator go 1.17 +require github.com/cucumber/godog v0.13.0 + require ( + github.com/cucumber/gherkin-go/v19 v19.0.3 // indirect + github.com/cucumber/messages-go/v16 v16.0.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect + github.com/gofrs/uuid v4.2.0+incompatible // indirect github.com/golang/protobuf v1.5.2 // 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/nxadm/tail v1.4.8 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.24.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect golang.org/x/net v0.1.0 // indirect golang.org/x/sys v0.1.0 // indirect golang.org/x/text v0.4.0 // indirect @@ -20,3 +29,5 @@ require ( gopkg.in/yaml.v2 v2.4.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 index 9ab9927..f457997 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,9 @@ +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/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= @@ -6,6 +12,9 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= @@ -22,7 +31,23 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 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/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +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/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= @@ -41,14 +66,23 @@ github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9 github.com/onsi/gomega v1.24.0 h1:+0glovB9Jd6z3VR+ScSwQqXVTIfJcGA9UBM8yzQxhqg= github.com/onsi/gomega v1.24.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg= 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.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +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= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= @@ -88,6 +122,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e h1:4nW4NLDYnU28ojHaHO8OVxFHk/aQ33U01a9cjED+pzE= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= @@ -109,6 +144,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= 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/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= @@ -117,5 +153,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 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/service/features/app.feature b/pkg/service/features/app.feature new file mode 100644 index 0000000..eda97f7 --- /dev/null +++ b/pkg/service/features/app.feature @@ -0,0 +1,16 @@ +# Во имя Бога Милостивого, Милосердного!!! +# language: ru +Функциональность: Преобразование в C++ + + Сценарий: Пустой пакет + * Исходник: +``` +package test +``` + * Результат: +``` +void loop() {} +void setup() {} +``` + +``` diff --git a/pkg/service/init_test.go b/pkg/service/init_test.go new file mode 100644 index 0000000..f57fc8b --- /dev/null +++ b/pkg/service/init_test.go @@ -0,0 +1,58 @@ +package service + +import ( + "context" + "os" + "testing" + + . "my/go-translator/pkg/testgodoglib" + + "github.com/cucumber/godog" + "github.com/cucumber/godog/colors" + + . "github.com/onsi/gomega" +) + +func InitializeScenario(ctx *godog.ScenarioContext) { + ctx.Step(`^Исходник:$`, исходник) + ctx.Step(`^Результат:$`, результат) + + // ----------------------- + 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) { + if err == nil { + afterScenario() + } + + return ctx, nil + }) + InitializeGomegaForGodog(ctx) + _ = Ω +} + +func InitializeSuite(tsc *godog.TestSuiteContext) { + tsc.BeforeSuite(beforeSuite) + tsc.AfterSuite(afterSuite) +} + +func TestMain(t *testing.T) { + testUtils(t) + + var opts = godog.Options{ + Output: colors.Colored(os.Stdout), + Strict: true, + StopOnFailure: true, + TestingT: t, + } + + godog.BindCommandLineFlags("godog.", &opts) + godog.TestSuite{ + Name: "app", + TestSuiteInitializer: InitializeSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, + }.Run() +} diff --git a/pkg/service/service.go b/pkg/service/service.go index c361810..3b1049d 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -79,7 +79,7 @@ func (s *defaultService) Start() error { } // If source has no declarations then main it to an empty for loop. if file.Decls == nil { - fmt.Fprint(s.out, "void loop() {} void setup() {}") + fmt.Fprint(s.out, "void loop() {}void setup() {}") return nil } // Use Goroutines to work concurrently. diff --git a/pkg/service/service_ginkgo_test.go b/pkg/service/service_ginkgo_test.go index fe27a7b..aff9549 100644 --- a/pkg/service/service_ginkgo_test.go +++ b/pkg/service/service_ginkgo_test.go @@ -1,8 +1,6 @@ package service import ( - "bytes" - "strings" "testing" . "github.com/onsi/ginkgo" @@ -10,7 +8,7 @@ import ( gomega_format "github.com/onsi/gomega/format" ) -func TestUtils(t *testing.T) { +func testUtils(t *testing.T) { RegisterFailHandler(Fail) gomega_format.CharactersAroundMismatchToInclude = 50 RunSpecs(t, "Go Translator") @@ -18,13 +16,6 @@ func TestUtils(t *testing.T) { var _ = Describe("Go Translator", func() { Describe("All", func() { - It("Empty_Package", func() { - source := `package test` - expected := `void loop(){} - void setup(){}` - Compare(source, expected) - }) - It("Function_Declaration", func() { source := `package test func foo() {} @@ -978,25 +969,5 @@ var _ = Describe("Go Translator", func() { }) }) -func Compare(source, expected string) { - var in, out bytes.Buffer - in.WriteString(source) - service := NewService(&in, &out) - err := service.Start() - got := out.String() - tgot, texpected := Trim(got), Trim(expected) - // spew.Dump(tgot) - ExpectWithOffset(1, err).NotTo(HaveOccurred()) - ExpectWithOffset(1, tgot).To(Be(texpected)) -} - -func Trim(s string) string { - s = strings.Replace(s, " ", "", -1) - s = strings.Replace(s, "\n", "", -1) - s = strings.Replace(s, "\r", "", -1) - s = strings.Replace(s, "\t", "", -1) - return s -} - func NDescribe(s string, i func()) int { return 0 } func NIt(s string, i func()) int { return 0 } diff --git a/pkg/service/service_test.go b/pkg/service/service_test.go new file mode 100644 index 0000000..8af0cb8 --- /dev/null +++ b/pkg/service/service_test.go @@ -0,0 +1,81 @@ +package service + +import ( + "bytes" + "strings" + + . "my/go-translator/pkg/testgodoglib" + + "github.com/cucumber/godog" + . "github.com/onsi/gomega" +) + +type testData struct { + src string +} + +var ( + t *testData +) + +func resetTestData() { + t = &testData{} + _ = Ω +} + +func beforeSuite() { +} +func afterSuite() { +} + +func beforeScenario() { + resetTestData() +} +func afterScenario() { +} + +// ----------------------- + +func исходник(src *godog.DocString) { + t.src = src.Content +} +func результат(out *godog.DocString) { + Compare_new(t.src, out.Content) +} + +func Compare_new(source, expected string) { + var in, out bytes.Buffer + in.WriteString(source) + service := NewService(&in, &out) + err := service.Start() + got := out.String() + tgot, texpected := trim_new(got), trim_new(expected) + // spew.Dump(got) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, tgot).To(Be(texpected)) +} +func trim_new(s string) string { + s = strings.Replace(s, "\n", "", -1) + s = strings.Replace(s, "\r", "", -1) + s = strings.Replace(s, "\t", "", -1) + return s +} + +func Compare(source, expected string) { + var in, out bytes.Buffer + in.WriteString(source) + service := NewService(&in, &out) + err := service.Start() + got := out.String() + tgot, texpected := Trim(got), Trim(expected) + // spew.Dump(tgot) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, tgot).To(Be(texpected)) +} +func Trim(s string) string { + s = strings.Replace(s, " ", "", -1) + s = strings.Replace(s, "\n", "", -1) + s = strings.Replace(s, "\r", "", -1) + s = strings.Replace(s, "\t", "", -1) + return s +} diff --git a/pkg/testgodoglib/testgodoglib.go b/pkg/testgodoglib/testgodoglib.go new file mode 100644 index 0000000..86c4620 --- /dev/null +++ b/pkg/testgodoglib/testgodoglib.go @@ -0,0 +1,55 @@ +package testlib + +import ( + "context" + "fmt" + "strconv" + + "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, 64) + 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) + } + }) +}