From 8bbcc3419b777acf50e6ae19b3b2175f6463c985 Mon Sep 17 00:00:00 2001 From: Gabriel Crispino Date: Mon, 8 Jan 2024 17:50:10 -0300 Subject: [PATCH] fix: example tests (#27) This fixes two problems in the example tests: 1. They apparently weren't being picked up because of the underscore prefix of the `_examples` folder ; 2. On `TestApi`, the test's `ResponseRecorder` instance kept in the `suite` struct wasn't being reset which was leading to it mixing up responses from different test cases, hence making tests fail (the failures weren't being caught by CI because of the underscore issue outlined above). This PR fixes above issues by renaming `_examples` to `examples` (1) and by adding a `Before` hook in the tests to reset the response recorder (2). --- custom_test.go | 5 +++-- {_examples => examples}/api/api.go | 0 {_examples => examples}/api/api_test.go | 10 ++++++++-- {_examples => examples}/api/features/version.feature | 0 {_examples => examples}/api/helpers.go | 0 {_examples => examples}/datatable/datatable.feature | 0 {_examples => examples}/datatable/datatable_test.go | 0 {_examples => examples}/simple/simple.feature | 0 simple_test.go | 4 ++-- 9 files changed, 13 insertions(+), 6 deletions(-) rename {_examples => examples}/api/api.go (100%) rename {_examples => examples}/api/api_test.go (92%) rename {_examples => examples}/api/features/version.feature (100%) rename {_examples => examples}/api/helpers.go (100%) rename {_examples => examples}/datatable/datatable.feature (100%) rename {_examples => examples}/datatable/datatable_test.go (100%) rename {_examples => examples}/simple/simple.feature (100%) diff --git a/custom_test.go b/custom_test.go index c8bd453..7594b6a 100644 --- a/custom_test.go +++ b/custom_test.go @@ -4,13 +4,14 @@ import ( "regexp" "testing" - "github.com/regen-network/gocuke" "github.com/stretchr/testify/require" + + "github.com/regen-network/gocuke" ) func TestCustomSteps(t *testing.T) { gocuke.NewRunner(t, &customStepsSuite{}). - Path("_examples/simple/simple.feature"). + Path("examples/simple/simple.feature"). Step(`I have (\d+) cukes`, (*customStepsSuite).A). Step(regexp.MustCompile(`I eat (\d+)`), (*customStepsSuite).B). Step(`I have (\d+) left`, (*customStepsSuite).C). diff --git a/_examples/api/api.go b/examples/api/api.go similarity index 100% rename from _examples/api/api.go rename to examples/api/api.go diff --git a/_examples/api/api_test.go b/examples/api/api_test.go similarity index 92% rename from _examples/api/api_test.go rename to examples/api/api_test.go index 2c65fe1..d255081 100644 --- a/_examples/api/api_test.go +++ b/examples/api/api_test.go @@ -6,9 +6,10 @@ import ( "net/http/httptest" "testing" - "github.com/regen-network/gocuke" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/regen-network/gocuke" ) type suite struct { @@ -19,7 +20,12 @@ type suite struct { func TestApi(t *testing.T) { scope := &suite{TestingT: t, resp: httptest.NewRecorder()} - gocuke.NewRunner(t, scope). + run := gocuke.NewRunner(t, scope) + run.Before(func() { + scope.resp = httptest.NewRecorder() + }) + + run. Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, scope.ISendRequestTo). Step(`^the response code should be (\d+)$`, scope.TheResponseCodeShouldBe). Step(`^the response should match json:$`, scope.TheResponseShouldMatchJson). diff --git a/_examples/api/features/version.feature b/examples/api/features/version.feature similarity index 100% rename from _examples/api/features/version.feature rename to examples/api/features/version.feature diff --git a/_examples/api/helpers.go b/examples/api/helpers.go similarity index 100% rename from _examples/api/helpers.go rename to examples/api/helpers.go diff --git a/_examples/datatable/datatable.feature b/examples/datatable/datatable.feature similarity index 100% rename from _examples/datatable/datatable.feature rename to examples/datatable/datatable.feature diff --git a/_examples/datatable/datatable_test.go b/examples/datatable/datatable_test.go similarity index 100% rename from _examples/datatable/datatable_test.go rename to examples/datatable/datatable_test.go diff --git a/_examples/simple/simple.feature b/examples/simple/simple.feature similarity index 100% rename from _examples/simple/simple.feature rename to examples/simple/simple.feature diff --git a/simple_test.go b/simple_test.go index 04ce17e..e504376 100644 --- a/simple_test.go +++ b/simple_test.go @@ -7,7 +7,7 @@ import ( ) func TestSimple(t *testing.T) { - gocuke.NewRunner(t, &simpleSuite{}).Path("_examples/simple/simple.feature").Run() + gocuke.NewRunner(t, &simpleSuite{}).Path("examples/simple/simple.feature").Run() } type simpleSuite struct { @@ -31,7 +31,7 @@ func (s *simpleSuite) IHaveLeft(a int64) { // test if a struct that doesn't use a pointer and a global var func TestSimpleNonPointer(t *testing.T) { - gocuke.NewRunner(t, simpleSuiteNP{}).Path("_examples/simple/simple.feature").Run() + gocuke.NewRunner(t, simpleSuiteNP{}).Path("examples/simple/simple.feature").Run() } var globalCukes int64