diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml deleted file mode 100644 index 7d0b2c9..0000000 --- a/.github/workflows/ci.yaml +++ /dev/null @@ -1,46 +0,0 @@ -name: Go - -on: [push, pull_request] - -jobs: - build: - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] - fail-fast: false - - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.17.1 - - - name: Setup gotestsum - uses: autero1/action-gotestsum@v1.0.0 - with: - gotestsum_version: 1.7.0 - - - name: Test - run: gotestsum --format short-verbose -- -race -timeout=20m -coverprofile=coverage_txt -covermode=atomic ./... - - - uses: codecov/codecov-action@v2 - with: - files: coverage_txt - - lint: - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] - fail-fast: false - - steps: - - uses: actions/checkout@v2 - - uses: golangci/golangci-lint-action@v2 - with: - args: --timeout=5m diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 0000000..3808f40 --- /dev/null +++ b/.github/workflows/go-test.yml @@ -0,0 +1,50 @@ +name: go test + +on: [push, pull_request] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: stable + check-latest: true + cache: true + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + only-new-issues: true + + test: + name: Unit tests + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ ubuntu-latest, macos-latest, windows-latest ] + go_version: ['oldstable', 'stable' ] + + steps: + - name: Run unit tests + uses: actions/setup-go@v4 + with: + go-version: '${{ matrix.go_version }}' + check-latest: true + cache: true + - uses: actions/checkout@v3 + + - run: go test -v -race -coverprofile="coverage-${{ matrix.os }}.${{ matrix.go_version }}.out" -covermode=atomic ./... + + - name: Upload coverage to codecov + uses: codecov/codecov-action@v3 + with: + files: './coverage-${{ matrix.os }}.${{ matrix.go_version }}.out,./coverage-integration-${{ matrix.os }}.${{ matrix.go_version }}.out' + flags: '${{ matrix.go_version }}' + os: '${{ matrix.os }}' + fail_ci_if_error: false + verbose: true + diff --git a/.golangci.yml b/.golangci.yml index be4899c..22f8d21 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,56 +4,58 @@ linters-settings: golint: min-confidence: 0 gocyclo: - min-complexity: 31 + min-complexity: 45 maligned: suggest-new: true dupl: - threshold: 100 + threshold: 200 goconst: min-len: 2 - min-occurrences: 4 + min-occurrences: 3 linters: - enable: - - revive - - goimports - - gosec + enable-all: true + disable: + - maligned - unparam - - unconvert - - predeclared - - prealloc - - misspell - - # disable: - # - maligned - # - lll - # - gochecknoinits - # - gochecknoglobals - # - godox - # - gocognit - # - whitespace - # - wsl - # - funlen - # - wrapcheck - # - testpackage - # - nlreturn - # - gofumpt - # - goerr113 - # - gci - # - gomnd - # - godot - # - exhaustivestruct - # - paralleltest - # - varnamelen - # - ireturn - # - exhaustruct - # #- thelper - -issues: - exclude-rules: - - path: bson.go - text: "should be .*ObjectID" - linters: - - golint - - stylecheck - + - lll + - gochecknoinits + - gochecknoglobals + - funlen + - godox + - gocognit + - whitespace + - wsl + - wrapcheck + - testpackage + - nlreturn + - gomnd + - exhaustivestruct + - goerr113 + - errorlint + - nestif + - godot + - gofumpt + - paralleltest + - tparallel + - thelper + - ifshort + - exhaustruct + - varnamelen + - gci + - depguard + - errchkjson + - inamedparam + - nonamedreturns + - musttag + - ireturn + - forcetypeassert + - cyclop + # deprecated linters + - deadcode + - interfacer + - scopelint + - varcheck + - structcheck + - golint + - nosnakecase diff --git a/README.md b/README.md index 0cf89d7..ae82dad 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ -# Strfmt [![Build Status](https://travis-ci.org/go-openapi/strfmt.svg?branch=master)](https://travis-ci.org/go-openapi/strfmt) [![codecov](https://codecov.io/gh/go-openapi/strfmt/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/strfmt) [![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io) - +# Strfmt [![Build Status](https://github.com/go-openapi/strfmt/actions/workflows/go-test.yaml/badge.svg)](https://github.com/go-openapi/strfmt/actions?query=workflow%3A"go+test") [![codecov](https://codecov.io/gh/go-openapi/strfmt/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/strfmt) +[![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io) [![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/strfmt/master/LICENSE) [![GoDoc](https://godoc.org/github.com/go-openapi/strfmt?status.svg)](http://godoc.org/github.com/go-openapi/strfmt) -[![GolangCI](https://golangci.com/badges/github.com/go-openapi/strfmt.svg)](https://golangci.com) [![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/strfmt)](https://goreportcard.com/report/github.com/go-openapi/strfmt) This package exposes a registry of data types to support string formats in the go-openapi toolkit. diff --git a/bson.go b/bson.go index a8a3604..ddae3fd 100644 --- a/bson.go +++ b/bson.go @@ -39,10 +39,10 @@ func IsBSONObjectID(str string) bool { // ObjectId represents a BSON object ID (alias to go.mongodb.org/mongo-driver/bson/primitive.ObjectID) // // swagger:strfmt bsonobjectid -type ObjectId bsonprim.ObjectID //nolint:revive +type ObjectId bsonprim.ObjectID //nolint:revive,stylecheck // NewObjectId creates a ObjectId from a Hex String -func NewObjectId(hex string) ObjectId { //nolint:revive +func NewObjectId(hex string) ObjectId { //nolint:revive,stylecheck oid, err := bsonprim.ObjectIDFromHex(hex) if err != nil { panic(err) diff --git a/bson_test.go b/bson_test.go index 0dd971e..7a367ad 100644 --- a/bson_test.go +++ b/bson_test.go @@ -18,36 +18,37 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" ) func TestBSONObjectId_fullCycle(t *testing.T) { id := NewObjectId("507f1f77bcf86cd799439011") bytes, err := id.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) var idCopy ObjectId err = idCopy.Scan(bytes) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, id, idCopy) err = idCopy.UnmarshalText(bytes) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, id, idCopy) jsonBytes, err := id.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) err = idCopy.UnmarshalJSON(jsonBytes) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, id, idCopy) bsonBytes, err := bson.Marshal(&id) - assert.NoError(t, err) + require.NoError(t, err) err = bson.Unmarshal(bsonBytes, &idCopy) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, id, idCopy) } diff --git a/conv/ulid_test.go b/conv/ulid_test.go index 62525f9..0bfb797 100644 --- a/conv/ulid_test.go +++ b/conv/ulid_test.go @@ -5,6 +5,7 @@ import ( "github.com/go-openapi/strfmt" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const testUlid = string("01EYXZVGBHG26MFTG4JWR4K558") @@ -14,7 +15,7 @@ func TestULIDValue(t *testing.T) { value := strfmt.ULID{} err := value.UnmarshalText([]byte(testUlid)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, value, ULIDValue(&value)) ulidRef := ULID(value) diff --git a/date_test.go b/date_test.go index 7d86989..6a16b73 100644 --- a/date_test.go +++ b/date_test.go @@ -23,6 +23,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" ) @@ -32,43 +33,43 @@ var _ driver.Valuer = Date{} func TestDate(t *testing.T) { pp := Date{} err := pp.UnmarshalText([]byte{}) - assert.NoError(t, err) + require.NoError(t, err) err = pp.UnmarshalText([]byte("yada")) - assert.Error(t, err) + require.Error(t, err) orig := "2014-12-15" bj := []byte("\"" + orig + "\"") err = pp.UnmarshalText([]byte(orig)) - assert.NoError(t, err) + require.NoError(t, err) txt, err := pp.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, orig, string(txt)) err = pp.UnmarshalJSON(bj) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, orig, pp.String()) err = pp.UnmarshalJSON([]byte(`"1972/01/01"`)) - assert.Error(t, err) + require.Error(t, err) b, err := pp.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, bj, b) dateOriginal := Date(time.Date(2014, 10, 10, 0, 0, 0, 0, time.UTC)) bsonData, err := bson.Marshal(&dateOriginal) - assert.NoError(t, err) + require.NoError(t, err) var dateCopy Date err = bson.Unmarshal(bsonData, &dateCopy) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, dateOriginal, dateCopy) var dateZero Date err = dateZero.UnmarshalJSON([]byte(jsonNull)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, Date{}, dateZero) } @@ -85,18 +86,18 @@ func TestDate_Scan(t *testing.T) { dd := Date{} err := dd.Scan(nil) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, Date{}, dd) err = dd.Scan(19700101) - assert.Error(t, err) + require.Error(t, err) } func TestDate_Value(t *testing.T) { ref := time.Now().Truncate(24 * time.Hour).UTC() date := Date(ref) dbv, err := date.Value() - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, dbv, ref.Format("2006-01-02")) } @@ -148,14 +149,14 @@ func TestGobEncodingDate(t *testing.T) { b := bytes.Buffer{} enc := gob.NewEncoder(&b) err := enc.Encode(Date(now)) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEmpty(t, b.Bytes()) var result Date dec := gob.NewDecoder(&b) err = dec.Decode(&result) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, now.Year(), time.Time(result).Year()) assert.Equal(t, now.Month(), time.Time(result).Month()) assert.Equal(t, now.Day(), time.Time(result).Day()) diff --git a/default_test.go b/default_test.go index b1875c0..7a20b0f 100644 --- a/default_test.go +++ b/default_test.go @@ -27,6 +27,7 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" ) @@ -181,7 +182,7 @@ func TestFormatUUID3(t *testing.T) { // special case for zero UUID var uuidZero UUID3 err := uuidZero.UnmarshalJSON([]byte(jsonNull)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, UUID3(""), uuidZero) } @@ -195,7 +196,7 @@ func TestFormatUUID4(t *testing.T) { // special case for zero UUID var uuidZero UUID4 err := uuidZero.UnmarshalJSON([]byte(jsonNull)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, UUID4(""), uuidZero) } @@ -209,7 +210,7 @@ func TestFormatUUID5(t *testing.T) { // special case for zero UUID var uuidZero UUID5 err := uuidZero.UnmarshalJSON([]byte(jsonNull)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, UUID5(""), uuidZero) } @@ -223,7 +224,7 @@ func TestFormatUUID(t *testing.T) { // special case for zero UUID var uuidZero UUID err := uuidZero.UnmarshalJSON([]byte(jsonNull)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, UUID(""), uuidZero) } @@ -283,28 +284,28 @@ func TestFormatBase64(t *testing.T) { var subj Base64 err := subj.UnmarshalText([]byte(str)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, expected, subj) b, err = subj.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, []byte(str), b) var subj2 Base64 err = subj2.UnmarshalJSON(bj) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, expected, subj2) b, err = subj2.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, bj, b) bsonData, err := bson.Marshal(subj2) - assert.NoError(t, err) + require.NoError(t, err) var b64Copy Base64 err = bson.Unmarshal(bsonData, &b64Copy) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, subj2, b64Copy) testValid(t, "byte", str) @@ -312,7 +313,7 @@ func TestFormatBase64(t *testing.T) { // Valuer interface sqlvalue, err := subj2.Value() - assert.NoError(t, err) + require.NoError(t, err) sqlvalueAsString, ok := sqlvalue.(string) if assert.Truef(t, ok, "[%s]Value: expected driver value to be a string", "byte") { assert.EqualValuesf(t, str, sqlvalueAsString, "[%s]Value: expected %v and %v to be equal", "byte", sqlvalue, str) @@ -320,16 +321,16 @@ func TestFormatBase64(t *testing.T) { // Scanner interface var subj3 Base64 err = subj3.Scan([]byte(str)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, str, subj3.String()) var subj4 Base64 err = subj4.Scan(str) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, str, subj4.String()) err = subj4.Scan(123) - assert.Error(t, err) + require.Error(t, err) } type testableFormat interface { @@ -350,14 +351,14 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va // text encoding interface b := []byte(with) err := what.UnmarshalText(b) - assert.NoError(t, err) + require.NoError(t, err) val := reflect.Indirect(reflect.ValueOf(what)) strVal := val.String() assert.Equalf(t, with, strVal, "[%s]UnmarshalText: expected %v and %v to be value equal", format, strVal, with) b, err = what.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equalf(t, []byte(with), b, "[%s]MarshalText: expected %v and %v to be value equal as []byte", format, string(b), with) // Stringer @@ -367,23 +368,23 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va // JSON encoding interface bj := []byte("\"" + with + "\"") err = what.UnmarshalJSON(bj) - assert.NoError(t, err) + require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() assert.EqualValuesf(t, with, strVal, "[%s]UnmarshalJSON: expected %v and %v to be value equal", format, strVal, with) b, err = what.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equalf(t, bj, b, "[%s]MarshalJSON: expected %v and %v to be value equal as []byte", format, string(b), with) // bson encoding interface bsonData, err := bson.Marshal(what) - assert.NoError(t, err) + require.NoError(t, err) resetValue(t, format, what) err = bson.Unmarshal(bsonData, what) - assert.NoError(t, err) + require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() assert.EqualValuesf(t, with, strVal, "[%s]bson.Unmarshal: expected %v and %v to be equal (reset value) ", format, what, with) @@ -391,23 +392,23 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va // Scanner interface resetValue(t, format, what) err = what.Scan(with) - assert.NoError(t, err) + require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() assert.EqualValuesf(t, with, strVal, "[%s]Scan: expected %v and %v to be value equal", format, strVal, with) err = what.Scan([]byte(with)) - assert.NoError(t, err) + require.NoError(t, err) val = reflect.Indirect(reflect.ValueOf(what)) strVal = val.String() assert.EqualValuesf(t, with, strVal, "[%s]Scan: expected %v and %v to be value equal", format, strVal, with) err = what.Scan(123) - assert.Error(t, err) + require.Error(t, err) // Valuer interface sqlvalue, err := what.Value() - assert.NoError(t, err) + require.NoError(t, err) sqlvalueAsString, ok := sqlvalue.(string) if assert.Truef(t, ok, "[%s]Value: expected driver value to be a string", format) { assert.EqualValuesf(t, with, sqlvalueAsString, "[%s]Value: expected %v and %v to be equal", format, sqlvalue, with) @@ -427,7 +428,7 @@ func resetValue(t *testing.T, format string, what encoding.TextUnmarshaler) { t.Helper() err := what.UnmarshalText([]byte("reset value")) - assert.NoError(t, err) + require.NoError(t, err) val := reflect.Indirect(reflect.ValueOf(what)) strVal := val.String() assert.Equalf(t, "reset value", strVal, "[%s]UnmarshalText: expected %v and %v to be equal (reset value) ", format, strVal, "reset value") diff --git a/duration_test.go b/duration_test.go index 7180cbd..75562d2 100644 --- a/duration_test.go +++ b/duration_test.go @@ -19,6 +19,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" ) @@ -26,51 +27,51 @@ func TestDuration(t *testing.T) { pp := Duration(0) err := pp.UnmarshalText([]byte("0ms")) - assert.NoError(t, err) + require.NoError(t, err) err = pp.UnmarshalText([]byte("yada")) - assert.Error(t, err) + require.Error(t, err) orig := "2ms" b := []byte(orig) bj := []byte("\"" + orig + "\"") err = pp.UnmarshalText(b) - assert.NoError(t, err) + require.NoError(t, err) err = pp.UnmarshalText([]byte("three week")) - assert.Error(t, err) + require.Error(t, err) err = pp.UnmarshalText([]byte("9999999999999999999999999999999999999999999999999999999 weeks")) - assert.Error(t, err) + require.Error(t, err) txt, err := pp.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, orig, string(txt)) err = pp.UnmarshalJSON(bj) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, orig, pp.String()) err = pp.UnmarshalJSON([]byte("yada")) - assert.Error(t, err) + require.Error(t, err) err = pp.UnmarshalJSON([]byte(`"12 parsecs"`)) - assert.Error(t, err) + require.Error(t, err) err = pp.UnmarshalJSON([]byte(`"12 y"`)) - assert.Error(t, err) + require.Error(t, err) b, err = pp.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, bj, b) dur := Duration(42) bsonData, err := bson.Marshal(&dur) - assert.NoError(t, err) + require.NoError(t, err) var durCopy Duration err = bson.Unmarshal(bsonData, &durCopy) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, dur, durCopy) } @@ -78,13 +79,13 @@ func testDurationParser(t *testing.T, toParse string, expected time.Duration) { t.Helper() r, e := ParseDuration(toParse) - assert.NoError(t, e) + require.NoError(t, e) assert.Equal(t, expected, r) } func TestDurationParser_Failed(t *testing.T) { _, e := ParseDuration("45 wekk") - assert.Error(t, e) + require.Error(t, e) } func TestIsDuration_Failed(t *testing.T) { @@ -99,12 +100,12 @@ func testDurationSQLScanner(t *testing.T, dur time.Duration) { for _, value := range values { var result Duration err := result.Scan(value) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, dur, time.Duration(result)) // And the other way around resv, erv := result.Value() - assert.NoError(t, erv) + require.NoError(t, erv) assert.EqualValues(t, value, resv) } @@ -113,11 +114,11 @@ func testDurationSQLScanner(t *testing.T, dur time.Duration) { func TestDurationScanner_Nil(t *testing.T) { var result Duration err := result.Scan(nil) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, 0, time.Duration(result)) err = result.Scan("1 ms") - assert.Error(t, err) + require.Error(t, err) } func TestDurationParser(t *testing.T) { diff --git a/format.go b/format.go index ad3b3c3..ce99ce5 100644 --- a/format.go +++ b/format.go @@ -94,7 +94,7 @@ func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry { } // MapStructureHookFunc is a decode hook function for mapstructure -func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc { //nolint:gocyclo,cyclop +func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc { return func(from reflect.Type, to reflect.Type, obj interface{}) (interface{}, error) { if from.Kind() != reflect.String { return obj, nil diff --git a/format_test.go b/format_test.go index 525f19e..65882b5 100644 --- a/format_test.go +++ b/format_test.go @@ -21,6 +21,7 @@ import ( "github.com/mitchellh/mapstructure" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type testFormat string @@ -223,9 +224,9 @@ func TestDecodeHook(t *testing.T) { Result: test, } d, err := mapstructure.NewDecoder(cfg) - assert.Nil(t, err) + require.NoError(t, err) err = d.Decode(m) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, exp, test) } @@ -257,11 +258,11 @@ func TestDecodeDateTimeHook(t *testing.T) { Result: test, } d, err := mapstructure.NewDecoder(cfg) - assert.Nil(t, err) + require.NoError(t, err) input := make(map[string]interface{}) input["datetime"] = tc.Input err = d.Decode(input) - assert.Error(t, err, "error expected got none") + require.Error(t, err, "error expected got none") }) } } @@ -296,11 +297,11 @@ func TestDecode_ULID_Hook_Negative(t *testing.T) { Result: test, } d, err := mapstructure.NewDecoder(cfg) - assert.NoError(t, err) + require.NoError(t, err) input := make(map[string]interface{}) input["ulid"] = tc.Input err = d.Decode(input) - assert.Error(t, err, "error expected got none") + require.Error(t, err, "error expected got none") }) } } diff --git a/time_test.go b/time_test.go index c2babb1..2dc4f2f 100644 --- a/time_test.go +++ b/time_test.go @@ -21,6 +21,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" ) @@ -83,7 +84,7 @@ func TestIsUnixZero(t *testing.T) { func TestParseDateTime_errorCases(t *testing.T) { _, err := ParseDateTime("yada") - assert.Error(t, err) + require.Error(t, err) } // TestParseDateTime tests the full cycle: @@ -93,11 +94,11 @@ func TestParseDateTime_fullCycle(t *testing.T) { t.Logf("Case #%d", caseNum) parsed, err := ParseDateTime(example.str) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, example.time, parsed) mt, err := parsed.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, []byte(example.str), mt) if example.str != "" { @@ -109,12 +110,12 @@ func TestParseDateTime_fullCycle(t *testing.T) { pp := NewDateTime() err = pp.UnmarshalText(mt) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, example.time, pp) pp = NewDateTime() err = pp.Scan(mt) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, DateTime(example.time), pp) } } @@ -145,9 +146,9 @@ func TestDateTime_IsDateTime_errorCases(t *testing.T) { func TestDateTime_UnmarshalText_errorCases(t *testing.T) { pp := NewDateTime() err := pp.UnmarshalText([]byte("yada")) - assert.Error(t, err) + require.Error(t, err) err = pp.UnmarshalJSON([]byte("yada")) - assert.Error(t, err) + require.Error(t, err) } func TestDateTime_UnmarshalText(t *testing.T) { @@ -155,12 +156,12 @@ func TestDateTime_UnmarshalText(t *testing.T) { t.Logf("Case #%d", caseNum) pp := NewDateTime() err := pp.UnmarshalText(example.in) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, example.time, pp) // Other way around val, erv := pp.Value() - assert.NoError(t, erv) + require.NoError(t, erv) assert.EqualValues(t, example.str, val) } @@ -170,22 +171,22 @@ func TestDateTime_UnmarshalJSON(t *testing.T) { t.Logf("Case #%d", caseNum) pp := NewDateTime() err := pp.UnmarshalJSON(esc(example.in)) - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, example.time, pp) } // Check UnmarshalJSON failure with no lexed items pp := NewDateTime() err := pp.UnmarshalJSON([]byte("zorg emperor")) - assert.Error(t, err) + require.Error(t, err) // Check lexer failure err = pp.UnmarshalJSON([]byte(`"zorg emperor"`)) - assert.Error(t, err) + require.Error(t, err) // Check null case err = pp.UnmarshalJSON([]byte("null")) - assert.Nil(t, err) + require.NoError(t, err) } func esc(v []byte) []byte { @@ -201,7 +202,7 @@ func TestDateTime_MarshalText(t *testing.T) { t.Logf("Case #%d", caseNum) dt := DateTime(example.time) mt, err := dt.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, []byte(example.str), mt) } } @@ -210,7 +211,7 @@ func TestDateTime_MarshalJSON(t *testing.T) { t.Logf("Case #%d", caseNum) dt := DateTime(example.time) bb, err := dt.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, esc([]byte(example.str)), bb) } } @@ -227,7 +228,7 @@ func TestDateTime_MarshalJSON_Override(t *testing.T) { t.Logf("Case #%d", caseNum) dt := DateTime(example.time.UTC()) bb, err := dt.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, esc([]byte(example.utcStr)), bb) } } @@ -238,17 +239,17 @@ func TestDateTime_Scan(t *testing.T) { pp := NewDateTime() err := pp.Scan(example.in) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, DateTime(example.time), pp) pp = NewDateTime() err = pp.Scan(string(example.in)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, DateTime(example.time), pp) pp = NewDateTime() err = pp.Scan(example.time) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, DateTime(example.time), pp) } } @@ -258,20 +259,20 @@ func TestDateTime_Scan_Failed(t *testing.T) { zero := NewDateTime() err := pp.Scan(nil) - assert.NoError(t, err) + require.NoError(t, err) // Zero values differ... // assert.Equal(t, zero, pp) assert.Equal(t, DateTime{}, pp) err = pp.Scan("") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, zero, pp) err = pp.Scan(int64(0)) - assert.Error(t, err) + require.Error(t, err) err = pp.Scan(float64(0)) - assert.Error(t, err) + require.Error(t, err) } func TestDateTime_BSON(t *testing.T) { @@ -280,32 +281,32 @@ func TestDateTime_BSON(t *testing.T) { dt := DateTime(example.time) bsonData, err := bson.Marshal(&dt) - assert.NoError(t, err) + require.NoError(t, err) var dtCopy DateTime err = bson.Unmarshal(bsonData, &dtCopy) - assert.NoError(t, err) + require.NoError(t, err) // BSON DateTime type loses timezone information, so compare UTC() assert.Equal(t, time.Time(dt).UTC(), time.Time(dtCopy).UTC()) // Check value marshaling explicitly m := bson.M{"data": dt} bsonData, err = bson.Marshal(&m) - assert.NoError(t, err) + require.NoError(t, err) var mCopy bson.M err = bson.Unmarshal(bsonData, &mCopy) - assert.NoError(t, err) + require.NoError(t, err) data, ok := m["data"].(DateTime) - assert.Equal(t, true, ok) + assert.True(t, ok) assert.Equal(t, time.Time(dt).UTC(), time.Time(data).UTC()) } } func TestDeepCopyDateTime(t *testing.T) { p, err := ParseDateTime("2011-08-18T19:03:37.000000000+01:00") - assert.NoError(t, err) + require.NoError(t, err) in := &p out := new(DateTime) @@ -326,14 +327,14 @@ func TestGobEncodingDateTime(t *testing.T) { b := bytes.Buffer{} enc := gob.NewEncoder(&b) err := enc.Encode(DateTime(now)) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEmpty(t, b.Bytes()) var result DateTime dec := gob.NewDecoder(&b) err = dec.Decode(&result) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, now.Year(), time.Time(result).Year()) assert.Equal(t, now.Month(), time.Time(result).Month()) assert.Equal(t, now.Day(), time.Time(result).Day()) diff --git a/ulid_test.go b/ulid_test.go index 6f3f9b6..02f4879 100644 --- a/ulid_test.go +++ b/ulid_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" ) @@ -24,29 +25,29 @@ func TestFormatULID_Text(t *testing.T) { t.Run("positive", func(t *testing.T) { t.Parallel() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) res, err := ulid.MarshalText() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, testUlid, string(res)) ulid2, _ := ParseULID(testUlidAlt) - assert.NoError(t, err) + require.NoError(t, err) what := []byte(testUlid) err = ulid2.UnmarshalText(what) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, testUlid, ulid2.String()) }) t.Run("negative", func(t *testing.T) { t.Parallel() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) what := []byte("00000000-0000-0000-0000-000000000000") err = ulid.UnmarshalText(what) - assert.Error(t, err) + require.Error(t, err) }) } @@ -57,35 +58,35 @@ func TestFormatULID_BSON(t *testing.T) { ulid, _ := ParseULID(testUlid) bsonData, err := bson.Marshal(&ulid) - assert.NoError(t, err) + require.NoError(t, err) var ulidUnmarshaled ULID err = bson.Unmarshal(bsonData, &ulidUnmarshaled) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, ulid, ulidUnmarshaled) // Check value marshaling explicitly m := bson.M{"data": ulid} bsonData, err = bson.Marshal(&m) - assert.NoError(t, err) + require.NoError(t, err) var mUnmarshaled bson.M err = bson.Unmarshal(bsonData, &mUnmarshaled) - assert.NoError(t, err) + require.NoError(t, err) data, ok := m["data"].(ULID) - assert.Equal(t, true, ok) + assert.True(t, ok) assert.Equal(t, ulid, data) }) t.Run("negative", func(t *testing.T) { t.Parallel() uuid := UUID("00000000-0000-0000-0000-000000000000") bsonData, err := bson.Marshal(&uuid) - assert.NoError(t, err) + require.NoError(t, err) var ulidUnmarshaled ULID err = bson.Unmarshal(bsonData, &ulidUnmarshaled) - assert.Error(t, err) + require.Error(t, err) }) } @@ -94,36 +95,36 @@ func TestFormatULID_JSON(t *testing.T) { t.Run("positive", func(t *testing.T) { t.Parallel() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) whatStr := fmt.Sprintf(`"%s"`, testUlidAlt) what := []byte(whatStr) err = ulid.UnmarshalJSON(what) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, testUlidAlt, ulid.String()) data, err := ulid.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, whatStr, string(data)) }) t.Run("null", func(t *testing.T) { t.Parallel() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) err = ulid.UnmarshalJSON([]byte("null")) - assert.Nil(t, err) + require.NoError(t, err) }) t.Run("negative", func(t *testing.T) { t.Parallel() // Check UnmarshalJSON failure with no lexed items ulid := NewULIDZero() err := ulid.UnmarshalJSON([]byte("zorg emperor")) - assert.Error(t, err) + require.Error(t, err) // Check lexer failure err = ulid.UnmarshalJSON([]byte(`"zorg emperor"`)) - assert.Error(t, err) + require.Error(t, err) }) } @@ -137,15 +138,15 @@ func TestFormatULID_Scan(t *testing.T) { srcUlid := testUlidAlt ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) err = ulid.Scan(srcUlid) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, srcUlid, ulid.String()) ulid, _ = ParseULID(testUlid) err = ulid.Scan([]byte(srcUlid)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, srcUlid, ulid.String()) }) t.Run("db.Scan_Failed", func(t *testing.T) { @@ -155,21 +156,21 @@ func TestFormatULID_Scan(t *testing.T) { ulid, err := ParseULID(testUlid) zero := NewULIDZero() - assert.NoError(t, err) + require.NoError(t, err) err = ulid.Scan(nil) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, zero, ulid) err = ulid.Scan("") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, zero, ulid) err = ulid.Scan(int64(0)) - assert.Error(t, err) + require.Error(t, err) err = ulid.Scan(float64(0)) - assert.Error(t, err) + require.Error(t, err) }) t.Run("db.Value", func(t *testing.T) { t.Parallel() @@ -177,10 +178,10 @@ func TestFormatULID_Scan(t *testing.T) { defer testUlidOverrideValMtx.Unlock() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) val, err := ulid.Value() - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, testUlid, val) }) @@ -190,9 +191,9 @@ func TestFormatULID_Scan(t *testing.T) { defer testUlidOverrideMtx.Unlock() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) ulid2, err := ParseULID(testUlidAlt) - assert.NoError(t, err) + require.NoError(t, err) ULIDScanOverrideFunc = func(raw interface{}) (ULID, error) { u := NewULIDZero() @@ -209,19 +210,19 @@ func TestFormatULID_Scan(t *testing.T) { bytes := [16]byte(ulid.ULID) err = ulid2.Scan(bytes) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, ulid2, ulid) assert.Equal(t, ulid2.String(), ulid.String()) // check other default cases became unreachable err = ulid2.Scan(testUlid) - assert.Error(t, err) + require.Error(t, err) // return default Scan method ULIDScanOverrideFunc = ULIDScanDefaultFunc err = ulid2.Scan(testUlid) - assert.NoError(t, err) - assert.Equal(t, ulid2.String(), testUlid) + require.NoError(t, err) + assert.Equal(t, testUlid, ulid2.String()) }) t.Run("override.Value", func(t *testing.T) { t.Parallel() @@ -229,9 +230,9 @@ func TestFormatULID_Scan(t *testing.T) { defer testUlidOverrideValMtx.Unlock() ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) ulid2, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) ULIDValueOverrideFunc = func(u ULID) (driver.Value, error) { bytes := [16]byte(u.ULID) @@ -240,7 +241,7 @@ func TestFormatULID_Scan(t *testing.T) { exp := [16]byte(ulid2.ULID) val, err := ulid.Value() - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, exp, val) @@ -248,7 +249,7 @@ func TestFormatULID_Scan(t *testing.T) { ULIDValueOverrideFunc = ULIDValueDefaultFunc val, err = ulid.Value() - assert.NoError(t, err) + require.NoError(t, err) assert.EqualValues(t, testUlid, val) }) @@ -256,7 +257,7 @@ func TestFormatULID_Scan(t *testing.T) { func TestFormatULID_DeepCopy(t *testing.T) { ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) in := &ulid out := new(ULID) @@ -273,19 +274,19 @@ func TestFormatULID_DeepCopy(t *testing.T) { func TestFormatULID_GobEncoding(t *testing.T) { ulid, err := ParseULID(testUlid) - assert.NoError(t, err) + require.NoError(t, err) b := bytes.Buffer{} enc := gob.NewEncoder(&b) err = enc.Encode(ulid) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEmpty(t, b.Bytes()) var result ULID dec := gob.NewDecoder(&b) err = dec.Decode(&result) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, ulid, result) assert.Equal(t, ulid.String(), result.String()) } @@ -294,10 +295,10 @@ func TestFormatULID_NewULID_and_Equal(t *testing.T) { t.Parallel() ulid1, err := NewULID() - assert.NoError(t, err) + require.NoError(t, err) ulid2, err := NewULID() - assert.NoError(t, err) + require.NoError(t, err) //nolint:gocritic assert.True(t, ulid1.Equal(ulid1), "ULID instances should be equal")