From d418edb51bf7f048ab490fb31cdd059f5dd4a3ab Mon Sep 17 00:00:00 2001 From: nhatthm Date: Wed, 8 Sep 2021 11:20:58 +0200 Subject: [PATCH] Deprecate in favor of github.com/godogx/clocksteps --- .github/workflows/golangci-lint.yaml | 38 ------ .github/workflows/test.yaml | 60 ---------- README.md | 4 + clock.go | 96 ++-------------- clock_test.go | 74 ------------ doc.go | 2 + features/background.feature | 14 --- features/bootstrap/doc.go | 2 - features/bootstrap/godog_test.go | 165 --------------------------- features/clock.feature | 65 ----------- go.mod | 6 +- go.sum | 8 +- godog.go | 112 ------------------ godog_go114_test.go | 20 ---- godog_go115_test.go | 20 ---- godog_test.go | 51 --------- 16 files changed, 24 insertions(+), 713 deletions(-) delete mode 100644 .github/workflows/golangci-lint.yaml delete mode 100644 .github/workflows/test.yaml delete mode 100644 clock_test.go delete mode 100644 features/background.feature delete mode 100644 features/bootstrap/doc.go delete mode 100644 features/bootstrap/godog_test.go delete mode 100644 features/clock.feature delete mode 100644 godog.go delete mode 100644 godog_go114_test.go delete mode 100644 godog_go115_test.go delete mode 100644 godog_test.go diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml deleted file mode 100644 index 254eeb9..0000000 --- a/.github/workflows/golangci-lint.yaml +++ /dev/null @@ -1,38 +0,0 @@ -name: lint -on: - push: - tags: - - v* - branches: - - master - - main - pull_request: -jobs: - golangci: - name: golangci-lint - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: golangci-lint - uses: golangci/golangci-lint-action@v2.5.2 - with: - # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.42.0 - - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - # args: --issues-exit-code=0 - - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true - - # Optional: if set to true then the action will use pre-installed Go. - # skip-go-installation: true - - # Optional: if set to true then the action don't cache or restore ~/go/pkg. - # skip-pkg-cache: true - - # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. - # skip-build-cache: true diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml deleted file mode 100644 index 777e986..0000000 --- a/.github/workflows/test.yaml +++ /dev/null @@ -1,60 +0,0 @@ -name: test - -on: - push: - branches: - - master - pull_request: - -env: - GO111MODULE: "on" - GO_LATEST_VERSION: "1.17.x" - -jobs: - test: - strategy: - fail-fast: false - matrix: - os: [ ubuntu-latest, macos-latest ] - go-version: [ 1.16.x, 1.17.x ] - runs-on: ${{ matrix.os }} - steps: - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go-version }} - - - name: Checkout code - uses: actions/checkout@v2 - - - name: Go cache - uses: actions/cache@v2 - with: - # In order: - # * Module download cache - # * Build cache (Linux) - path: | - ~/go/pkg/mod - ~/.cache/go-build - key: ${{ runner.os }}-go-${{ matrix.go-version }}-cache-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go-${{ matrix.go-version }}-cache - - - name: Test - id: test - run: | - make test - - - name: Upload code coverage (unit) - if: matrix.go-version == env.GO_LATEST_VERSION - uses: codecov/codecov-action@v1 - with: - file: ./unit.coverprofile - flags: unittests-${{ runner.os }} - - - name: Upload code coverage (features) - if: matrix.go-version == env.GO_LATEST_VERSION - uses: codecov/codecov-action@v1 - with: - file: ./features.coverprofile - flags: featuretests-${{ runner.os }} diff --git a/README.md b/README.md index 9c19a46..7f0cdcb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +> ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ +> +> This module is deprecated. Use `github.com/godogx/clocksteps` instead. + # Cucumber Clock steps for Golang [![GitHub Releases](https://img.shields.io/github/v/release/nhatthm/clockdog)](https://github.com/nhatthm/clockdog/releases/latest) diff --git a/clock.go b/clock.go index e4cf89c..68ee8e6 100644 --- a/clock.go +++ b/clock.go @@ -1,99 +1,23 @@ package clockdog import ( - "errors" - "sync" - "time" - - clock "github.com/nhatthm/go-clock" + "github.com/godogx/clocksteps" ) // ErrClockIsNotSet indicates that the clock must be set by either Clock.Set() or Clock.Freeze() before adding some // time.Duration into it. -var ErrClockIsNotSet = errors.New("clock is not set") - -var _ clock.Clock = (*Clock)(nil) +// +// Deprecated: Use clocksteps.ErrClockIsNotSet instead. +var ErrClockIsNotSet = clocksteps.ErrClockIsNotSet // Clock is a clock.Clock. -type Clock struct { - timestamp *time.Time - mu sync.Mutex -} - -// Now returns a fixed timestamp or time.Now(). -func (c *Clock) Now() time.Time { - c.mu.Lock() - defer c.mu.Unlock() - - if c.timestamp == nil { - return time.Now() - } - - return *c.timestamp -} - -// Set fixes the clock at a time. -func (c *Clock) Set(t time.Time) { - c.mu.Lock() - defer c.mu.Unlock() - - c.timestamp = timestamp(t) -} - -// Add adds time to the clock. -func (c *Clock) Add(d time.Duration) error { - c.mu.Lock() - defer c.mu.Unlock() - - if c.timestamp == nil { - return ErrClockIsNotSet - } - - c.timestamp = timestamp(c.timestamp.Add(d)) - - return nil -} - -// AddDate adds date to the clock. -func (c *Clock) AddDate(years, months, days int) error { - c.mu.Lock() - defer c.mu.Unlock() - - if c.timestamp == nil { - return ErrClockIsNotSet - } - - c.timestamp = timestamp(c.timestamp.AddDate(years, months, days)) - - return nil -} - -// Freeze freezes the clock. -func (c *Clock) Freeze() { - c.mu.Lock() - defer c.mu.Unlock() - - c.timestamp = timestamp(time.Now()) -} - -// Unfreeze unfreezes the clock. -func (c *Clock) Unfreeze() { - c.mu.Lock() - defer c.mu.Unlock() - - c.timestamp = nil -} - -// Clock provides clock.Clock. -func (c *Clock) Clock() clock.Clock { - return c -} +// +// Deprecated: Use clocksteps.Clock instead. +type Clock = clocksteps.Clock // New initiates a new Clock. +// +// Deprecated: Use clocksteps.New instead. func New() *Clock { - return &Clock{} -} - -func timestamp(t time.Time) *time.Time { - return &t + return clocksteps.New() } diff --git a/clock_test.go b/clock_test.go deleted file mode 100644 index fe41fad..0000000 --- a/clock_test.go +++ /dev/null @@ -1,74 +0,0 @@ -package clockdog_test - -import ( - "testing" - "time" - - "github.com/nhatthm/clockdog" - "github.com/stretchr/testify/assert" -) - -func TestClock(t *testing.T) { - t.Parallel() - - c := clockdog.New() - - now := time.Now() - - assert.True(t, now.Before(c.Now())) - - // Errors while adding time to a live clock. - assert.Equal(t, clockdog.ErrClockIsNotSet, c.Add(time.Hour)) - assert.Equal(t, clockdog.ErrClockIsNotSet, c.AddDate(0, 0, 1)) - - // Freeze the clock. - c.Freeze() - - ts := c.Now() - - <-time.After(50 * time.Millisecond) - - assert.Equal(t, ts, c.Now()) - - // Set to another time. - ts = time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) - - c.Set(ts) - - <-time.After(50 * time.Millisecond) - - assert.Equal(t, ts, c.Now()) - - // Change the time. - ts = ts.Add(2 * time.Hour) - err := c.Add(2 * time.Hour) - assert.NoError(t, err) - - <-time.After(50 * time.Millisecond) - - assert.Equal(t, ts, c.Now()) - - // Change the date. - ts = ts.AddDate(2, 1, 3) - err = c.AddDate(2, 1, 3) - assert.NoError(t, err) - - <-time.After(50 * time.Millisecond) - - assert.Equal(t, ts, c.Now()) - - // Unfreeze the clock. - c.Unfreeze() - - now = time.Now() - - assert.True(t, now.Before(c.Now())) -} - -func TestClock_Clock(t *testing.T) { - t.Parallel() - - c := clockdog.New() - - assert.Equal(t, c, c.Clock()) -} diff --git a/doc.go b/doc.go index 05e065d..3723544 100644 --- a/doc.go +++ b/doc.go @@ -1,2 +1,4 @@ // Package clockdog provides a clock for tests with cucumber/godog. +// +// Deprecated: Use github.com/godogx/clocksteps instead. package clockdog diff --git a/features/background.feature b/features/background.feature deleted file mode 100644 index a016c0c..0000000 --- a/features/background.feature +++ /dev/null @@ -1,14 +0,0 @@ -Feature: Test with background - - Background: - Given now is "2020-04-05T06:07:08Z" - - Scenario: Add days - Given someone adds 2 days to the clock - - Then the time is "2020-04-07T06:07:08Z" - - Scenario: Add months - Given someone adds 1 month to the clock - - Then the time is "2020-05-05T06:07:08Z" diff --git a/features/bootstrap/doc.go b/features/bootstrap/doc.go deleted file mode 100644 index 014ca98..0000000 --- a/features/bootstrap/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package bootstrap provides integration tests. -package bootstrap diff --git a/features/bootstrap/godog_test.go b/features/bootstrap/godog_test.go deleted file mode 100644 index 8634f1d..0000000 --- a/features/bootstrap/godog_test.go +++ /dev/null @@ -1,165 +0,0 @@ -package bootstrap - -import ( - "bytes" - "flag" - "fmt" - "io/ioutil" - "math/rand" - "path/filepath" - "strings" - "testing" - "time" - - "github.com/cucumber/godog" - "github.com/nhatthm/clockdog" - "github.com/nhatthm/timeparser" - "github.com/stretchr/testify/assert" -) - -// Used by init(). -// -//nolint:gochecknoglobals -var ( - runGoDogTests bool - - out = new(bytes.Buffer) - opt = godog.Options{ - Strict: true, - Output: out, - } -) - -// This has to run on init to define -godog flag, otherwise "undefined flag" error happens. -// -//nolint:gochecknoinits -func init() { - flag.BoolVar(&runGoDogTests, "godog", false, "Set this flag is you want to run godog BDD tests") - godog.BindCommandLineFlags("", &opt) -} - -func TestIntegration(t *testing.T) { - if !runGoDogTests { - t.Skip(`Missing "-godog" flag, skipping integration test.`) - } - - c := clockdog.New() - - RunSuite(t, "..", func(_ *testing.T, ctx *godog.ScenarioContext) { - c.RegisterContext(ctx) - registerClock(c, ctx) - }) -} - -func RunSuite(t *testing.T, path string, featureContext func(t *testing.T, ctx *godog.ScenarioContext)) { - t.Helper() - - flag.Parse() - - if opt.Randomize == 0 { - opt.Randomize = rand.Int63() // nolint: gosec - } - - var paths []string - - files, err := ioutil.ReadDir(filepath.Clean(path)) - assert.NoError(t, err) - - paths = make([]string, 0, len(files)) - - for _, f := range files { - if strings.HasSuffix(f.Name(), ".feature") { - paths = append(paths, filepath.Join(path, f.Name())) - } - } - - for _, path := range paths { - path := path - - t.Run(path, func(t *testing.T) { - opt.Paths = []string{path} - suite := godog.TestSuite{ - Name: "Integration", - TestSuiteInitializer: nil, - ScenarioInitializer: func(s *godog.ScenarioContext) { - featureContext(t, s) - }, - Options: &opt, - } - status := suite.Run() - - if status != 0 { - fmt.Println(out.String()) - assert.Fail(t, "one or more scenarios failed in feature: "+path) - } - }) - } -} - -func registerClock(c *clockdog.Clock, ctx *godog.ScenarioContext) { - ctx.Step(`the time is now`, func() error { - return isNow(c) - }) - - ctx.Step(`the time is not now`, func() error { - return isNotNow(c) - }) - - ctx.Step(`the time is "([^"]*)"`, func(s string) error { - return expectTime(c, s) - }) - - ctx.Step(`wait for ([^\s]*)`, waitFor) -} - -func isNow(c *clockdog.Clock) error { - now := time.Now() - ts := c.Now() - min := now.Add(-10 * time.Millisecond) - max := now.Add(10 * time.Millisecond) - - if min.After(ts) || max.Before(ts) { - return fmt.Errorf("expected: %q < %q < %q", min.String(), ts.String(), max.String()) - } - - return nil -} - -func isNotNow(c *clockdog.Clock) error { - now := time.Now() - ts := c.Now() - min := now.Add(-10 * time.Millisecond) - max := now.Add(10 * time.Millisecond) - - if ts.After(min) && ts.Before(max) { - return fmt.Errorf("the time is now") - } - - return nil -} - -func expectTime(c *clockdog.Clock, s string) error { - expected, err := timeparser.Parse(s) - if err != nil { - return err - } - - now := c.Now() - - if now != expected { - return fmt.Errorf("expected: %q, got %q", expected.String(), now.String()) - } - - return nil -} - -func waitFor(s string) error { - d, err := time.ParseDuration(s) - if err != nil { - return err - } - - <-time.After(d) - - return nil -} diff --git a/features/clock.feature b/features/clock.feature deleted file mode 100644 index 66a0567..0000000 --- a/features/clock.feature +++ /dev/null @@ -1,65 +0,0 @@ -Feature: Without Background - - Scenario: Set time - Given the clock is at "2020-01-02T03:04:05Z" - - Then the time is "2020-01-02T03:04:05Z" - - Given the clock is set to "2020-02-03T04:05:06Z" - - Then the time is "2020-02-03T04:05:06Z" - - Given Someone sets the clock to "2020-03-04T05:06:07Z" - - Then the time is "2020-03-04T05:06:07Z" - - Given now is "2020-04-05T06:07:08Z" - - Then the time is "2020-04-05T06:07:08Z" - - Scenario: Add time - Given the clock is at "2020-01-02T03:04:05Z" - And someone adds 1h5s to the clock - - Then the time is "2020-01-02T04:04:10Z" - - Given someone adds 2 days to the clock - - Then the time is "2020-01-04T04:04:10Z" - - Given someone adds 1 month to the clock - - Then the time is "2020-02-04T04:04:10Z" - - Given someone adds 3 years to the clock - - Then the time is "2023-02-04T04:04:10Z" - - Given someone adds 1 month 2 days to the clock - - Then the time is "2023-03-06T04:04:10Z" - - Given someone adds 2 year 3 days to the clock - - Then the time is "2025-03-09T04:04:10Z" - - Given someone adds 3 year 4 months to the clock - - Then the time is "2028-07-09T04:04:10Z" - - Given someone adds 4 year 5 months 6 days to the clock - - Then the time is "2032-12-15T04:04:10Z" - - Scenario: Freeze and Unfreeze - Given the time is now - - When I freeze the clock - And I wait for 50ms - - Then the time is not now - - When I wait for 50ms - And I release the clock - - Then the time is now diff --git a/go.mod b/go.mod index 99c6ba0..455f95c 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ +// Deprecated: Use github.com/godogx/clocksteps instead. module github.com/nhatthm/clockdog go 1.17 require ( - github.com/cucumber/godog v0.12.0 - github.com/nhatthm/go-clock v0.6.0 + github.com/cucumber/godog v0.12.1 + github.com/godogx/clocksteps v0.1.0 github.com/nhatthm/timeparser v0.2.0 github.com/stretchr/testify v1.7.0 ) @@ -17,6 +18,7 @@ require ( 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/nhatthm/go-clock v0.6.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect diff --git a/go.sum b/go.sum index 0d785f6..4ea3021 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,8 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 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.0 h1:xVOc9ML+1joT0CqcdQTpfXiT7G1hOLbCmlUnYOyJ80w= -github.com/cucumber/godog v0.12.0/go.mod h1:u6SD7IXC49dLpPN35kal0oYEjsXZWee4pW6Tm9t5pIc= +github.com/cucumber/godog v0.12.1 h1:IhWVYFKDReM5WsuA9AuRLRPWOyvFNO9UBUKrNfLPais= +github.com/cucumber/godog v0.12.1/go.mod h1:u6SD7IXC49dLpPN35kal0oYEjsXZWee4pW6Tm9t5pIc= 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= @@ -51,6 +51,8 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2 github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/godogx/clocksteps v0.1.0 h1:qraQYUcwUOzwGD/ttRQAXc2uIMhRR4AOxO5R0QxyxTM= +github.com/godogx/clocksteps v0.1.0/go.mod h1:hLtx0CNFy3K+8xR78ggdnaOHRUVzPd1gaEkNWQ4xjvI= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -84,11 +86,9 @@ github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyN github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= -github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrhxx+FVELeXpVPE= 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.0 h1:xdXq34gBOMEloa9rlGStLxmfX/dyIK8htOv36dQUwHU= github.com/hashicorp/go-memdb v1.3.0/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g= github.com/hashicorp/go-memdb v1.3.2 h1:RBKHOsnSszpU6vxq80LzC2BaQjuuvoyaQbkLTf7V7g8= github.com/hashicorp/go-memdb v1.3.2/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g= diff --git a/godog.go b/godog.go deleted file mode 100644 index 8af2fd1..0000000 --- a/godog.go +++ /dev/null @@ -1,112 +0,0 @@ -package clockdog - -import ( - "context" - "strconv" - "time" - - "github.com/cucumber/godog" - "github.com/nhatthm/timeparser" -) - -// RegisterContext registers clock to godog tests. -func (c *Clock) RegisterContext(ctx *godog.ScenarioContext) { - ctx.After(func(context.Context, *godog.Scenario, error) (context.Context, error) { - // Unfreeze the clock. - c.Unfreeze() - - return nil, nil - }) - - ctx.Step(`(?:the )?clock is at "([^"]*)"`, c.set) - ctx.Step(`(?:the )?clock is set to "([^"]*)"`, c.set) - ctx.Step(`sets? (?:the )?clock to "([^"]*)"`, c.set) - ctx.Step(`now is "([^"]*)"`, c.set) - - ctx.Step(`adds? ([^\s]*) to (?:the )?clock`, c.add) - ctx.Step(`adds? ([0-9]+) days? to (?:the )?clock`, c.addDay) - ctx.Step(`adds? ([0-9]+) months? to (?:the )?clock`, c.addMonth) - ctx.Step(`adds? ([0-9]+) years? to (?:the )?clock`, c.addYear) - ctx.Step(`adds? ([0-9]+) months?,? ([0-9]+) days? to (?:the )?clock`, c.addMonthDay) - ctx.Step(`adds? ([0-9]+) years?,? ([0-9]+) days? to (?:the )?clock`, c.addYearDay) - ctx.Step(`adds? ([0-9]+) years?,? ([0-9]+) months? to (?:the )?clock`, c.addYearMonth) - ctx.Step(`adds? ([0-9]+) years?,? ([0-9]+) months?,? ([0-9]+) days? to (?:the )?clock`, c.addDate) - - ctx.Step(`\s*freeze (?:the )?clock`, c.freeze) - ctx.Step(`(?:(?:release)|(?:unset)|(?:reset)) (?:the )?clock$`, c.unfreeze) -} - -func (c *Clock) set(t string) error { - ts, err := timeparser.Parse(t) - if err != nil { - return err - } - - c.Set(ts) - - return nil -} - -func (c *Clock) add(s string) error { - d, err := time.ParseDuration(s) - if err != nil { - return err - } - - return c.Add(d) -} - -func (c *Clock) addDay(days string) error { - return c.addDate("0", "0", days) -} - -func (c *Clock) addMonth(months string) error { - return c.addDate("0", months, "0") -} - -func (c *Clock) addYear(years string) error { - return c.addDate(years, "0", "0") -} - -func (c *Clock) addMonthDay(months, days string) error { - return c.addDate("0", months, days) -} - -func (c *Clock) addYearDay(years, days string) error { - return c.addDate(years, "0", days) -} - -func (c *Clock) addYearMonth(years, months string) error { - return c.addDate(years, months, "0") -} - -func (c *Clock) addDate(years, months, days string) error { - y, err := strconv.Atoi(years) - if err != nil { - return err - } - - m, err := strconv.Atoi(months) - if err != nil { - return err - } - - d, err := strconv.Atoi(days) - if err != nil { - return err - } - - return c.AddDate(y, m, d) -} - -func (c *Clock) freeze() error { - c.Freeze() - - return nil -} - -func (c *Clock) unfreeze() error { - c.Freeze() - - return nil -} diff --git a/godog_go114_test.go b/godog_go114_test.go deleted file mode 100644 index 9b1b2ff..0000000 --- a/godog_go114_test.go +++ /dev/null @@ -1,20 +0,0 @@ -// +build !go1.15 - -package clockdog - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestClock_addError(t *testing.T) { - t.Parallel() - - c := New() - - err := c.add("foobar") - expectedError := `time: invalid duration foobar` - - assert.EqualError(t, err, expectedError) -} diff --git a/godog_go115_test.go b/godog_go115_test.go deleted file mode 100644 index 3af5c6a..0000000 --- a/godog_go115_test.go +++ /dev/null @@ -1,20 +0,0 @@ -// +build go1.15 - -package clockdog - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestClock_addError(t *testing.T) { - t.Parallel() - - c := New() - - err := c.add("foobar") - expectedError := `time: invalid duration "foobar"` - - assert.EqualError(t, err, expectedError) -} diff --git a/godog_test.go b/godog_test.go deleted file mode 100644 index 6039b8a..0000000 --- a/godog_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package clockdog - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestClock_setError(t *testing.T) { - t.Parallel() - - c := New() - - err := c.set("foobar") - expectedError := `parsing time "foobar" as "2006-01-02": cannot parse "foobar" as "2006"` - - assert.EqualError(t, err, expectedError) -} - -func TestClock_addDateError(t *testing.T) { - t.Parallel() - - c := New() - - t.Run("invalid year", func(t *testing.T) { - t.Parallel() - - err := c.addDate("foobar", "0", "0") - expectedError := `strconv.Atoi: parsing "foobar": invalid syntax` - - assert.EqualError(t, err, expectedError) - }) - - t.Run("invalid month", func(t *testing.T) { - t.Parallel() - - err := c.addDate("0", "foobar", "0") - expectedError := `strconv.Atoi: parsing "foobar": invalid syntax` - - assert.EqualError(t, err, expectedError) - }) - - t.Run("invalid year", func(t *testing.T) { - t.Parallel() - - err := c.addDate("0", "0", "foobar") - expectedError := `strconv.Atoi: parsing "foobar": invalid syntax` - - assert.EqualError(t, err, expectedError) - }) -}