Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TestMustGetParsedDuration backwards compatible #63

Merged
merged 2 commits into from Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions properties_go1.15_test.go
@@ -0,0 +1,25 @@
//go:build go1.15
// +build go1.15

package properties

import (
"testing"
"time"

"github.com/magiconair/properties/assert"
)

// TestMustGetParsedDuration works with go1.15 and beyond where the panic
// message was changed slightly. We keep this test (!) here to demonstrate the
// backwards compatibility and to keep the author happy as long as it does not
// affect any real users. Thank you! Frank :)
//
// See https://github.com/magiconair/properties/pull/63
func TestMustGetParsedDuration(t *testing.T) {
input := "key = 123ms\nkey2 = ghi"
p := mustParse(t, input)
assert.Equal(t, p.MustGetParsedDuration("key"), 123*time.Millisecond)
assert.Panic(t, func() { p.MustGetParsedDuration("key2") }, `time: invalid duration "ghi"`)
assert.Panic(t, func() { p.MustGetParsedDuration("invalid") }, "unknown property: invalid")
}
25 changes: 25 additions & 0 deletions properties_pre_go1.15_test.go
@@ -0,0 +1,25 @@
//go:build !go1.15
// +build !go1.15

package properties

import (
"testing"
"time"

"github.com/magiconair/properties/assert"
)

// TestMustGetParsedDuration works with go before go1.15 where the panic
// message was changed slightly. We keep this test (!) here to demonstrate the
// backwards compatibility and to keep the author happy as long as it does not
// affect any real users. Thank you! Frank :)
//
// See https://github.com/magiconair/properties/pull/63
func TestMustGetParsedDuration(t *testing.T) {
input := "key = 123ms\nkey2 = ghi"
p := mustParse(t, input)
assert.Equal(t, p.MustGetParsedDuration("key"), 123*time.Millisecond)
assert.Panic(t, func() { p.MustGetParsedDuration("key2") }, `time: invalid duration ghi`)
assert.Panic(t, func() { p.MustGetParsedDuration("invalid") }, "unknown property: invalid")
}
27 changes: 0 additions & 27 deletions properties_test.go
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -556,31 +554,6 @@ func TestGetParsedDuration(t *testing.T) {
}
}

func TestMustGetParsedDuration(t *testing.T) {
input := "key = 123ms\nkey2 = ghi"
p := mustParse(t, input)
assert.Equal(t, p.MustGetParsedDuration("key"), 123*time.Millisecond)

// parse runtime.Version into major and minor version
var major, minor int
ver := strings.Split(runtime.Version(), ".")
devel := !strings.HasPrefix(ver[0], "go")
major, _ = strconv.Atoi(strings.TrimPrefix(ver[0], "go"))
if len(ver) > 1 {
minor, _ = strconv.Atoi(ver[1])
}

switch {
case devel || major == 1 && minor >= 15:
// go1.15 ... gotip
assert.Panic(t, func() { p.MustGetParsedDuration("key2") }, `time: invalid duration "ghi"`)
default:
// go1.x..go1.14
assert.Panic(t, func() { p.MustGetParsedDuration("key2") }, `time: invalid duration ghi`)
}
assert.Panic(t, func() { p.MustGetParsedDuration("invalid") }, "unknown property: invalid")
}

func TestGetFloat64(t *testing.T) {
for _, test := range floatTests {
p := mustParse(t, test.input)
Expand Down