Skip to content

Commit

Permalink
Merge pull request #63 from maage/beta-1
Browse files Browse the repository at this point in the history
TestMustGetParsedDuration: drop version tests
  • Loading branch information
magiconair committed Feb 23, 2022
2 parents f999e06 + 419d650 commit 869a559
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
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

0 comments on commit 869a559

Please sign in to comment.