forked from streamrail/vast-1
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bsm/feature/new-test-set
New Test set & other minor improvements
- Loading branch information
Showing
8 changed files
with
412 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,48 @@ | ||
package vast | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDurationMarshaler(t *testing.T) { | ||
b, err := Duration(0).MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "00:00:00", string(b)) | ||
} | ||
b, err = Duration(2 * time.Millisecond).MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "00:00:00.002", string(b)) | ||
} | ||
b, err = Duration(2 * time.Second).MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "00:00:02", string(b)) | ||
} | ||
b, err = Duration(2 * time.Minute).MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "00:02:00", string(b)) | ||
} | ||
b, err = Duration(2 * time.Hour).MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "02:00:00", string(b)) | ||
} | ||
} | ||
var _ = Describe("Duration", func() { | ||
|
||
func TestDurationUnmarshal(t *testing.T) { | ||
var d Duration | ||
if assert.NoError(t, d.UnmarshalText([]byte("00:00:00"))) { | ||
assert.Equal(t, Duration(0), d) | ||
} | ||
d = 0 | ||
if assert.NoError(t, d.UnmarshalText([]byte("00:00:02"))) { | ||
assert.Equal(t, Duration(2*time.Second), d) | ||
} | ||
d = 0 | ||
if assert.NoError(t, d.UnmarshalText([]byte("00:02:00"))) { | ||
assert.Equal(t, Duration(2*time.Minute), d) | ||
} | ||
d = 0 | ||
if assert.NoError(t, d.UnmarshalText([]byte("02:00:00"))) { | ||
assert.Equal(t, Duration(2*time.Hour), d) | ||
} | ||
d = 0 | ||
if assert.NoError(t, d.UnmarshalText([]byte("00:00:00.123"))) { | ||
assert.Equal(t, Duration(123*time.Millisecond), d) | ||
} | ||
assert.EqualError(t, d.UnmarshalText([]byte("00:00:60")), "invalid duration: 00:00:60") | ||
assert.EqualError(t, d.UnmarshalText([]byte("00:60:00")), "invalid duration: 00:60:00") | ||
assert.EqualError(t, d.UnmarshalText([]byte("00:00:00.-1")), "invalid duration: 00:00:00.-1") | ||
assert.EqualError(t, d.UnmarshalText([]byte("00:00:00.1000")), "invalid duration: 00:00:00.1000") | ||
assert.EqualError(t, d.UnmarshalText([]byte("00h01m")), "invalid duration: 00h01m") | ||
} | ||
DescribeTable("marshal", | ||
func(d Duration, exp string) { | ||
b, err := d.MarshalText() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(b)).To(Equal(exp)) | ||
}, | ||
Entry("00:00:00", Duration(0), "00:00:00"), | ||
Entry("00:00:00.002", Duration(2*time.Millisecond), "00:00:00.002"), | ||
Entry("00:00:02", Duration(2*time.Second), "00:00:02"), | ||
Entry("00:02:00", Duration(2*time.Minute), "00:02:00"), | ||
Entry("02:00:00", Duration(2*time.Hour), "02:00:00"), | ||
) | ||
|
||
DescribeTable("unmarshal", | ||
func(s string, exp Duration) { | ||
d := new(Duration) | ||
Expect(d.UnmarshalText([]byte(s))).To(Succeed()) | ||
Expect(*d).To(Equal(exp)) | ||
}, | ||
Entry("00:00:00", "00:00:00", Duration(0)), | ||
Entry("00:00:00.002", "00:00:00.002", Duration(2*time.Millisecond)), | ||
Entry("00:00:02", "00:00:02", Duration(2*time.Second)), | ||
Entry("00:02:00", "00:02:00", Duration(2*time.Minute)), | ||
Entry("02:00:00", "02:00:00", Duration(2*time.Hour)), | ||
) | ||
|
||
It("should fail to unmarshal bad inputs", func() { | ||
d := new(Duration) | ||
Expect(d.UnmarshalText([]byte("00:00:60"))).To(MatchError("invalid duration: 00:00:60")) | ||
Expect(d.UnmarshalText([]byte("00:60:00"))).To(MatchError("invalid duration: 00:60:00")) | ||
Expect(d.UnmarshalText([]byte("00:00:00.-1"))).To(MatchError("invalid duration: 00:00:00.-1")) | ||
Expect(d.UnmarshalText([]byte("00:00:00.1000"))).To(MatchError("invalid duration: 00:00:00.1000")) | ||
Expect(d.UnmarshalText([]byte("00h01m"))).To(MatchError("invalid duration: 00h01m")) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
package vast | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestOffsetMarshaler(t *testing.T) { | ||
b, err := Offset{}.MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "0%", string(b)) | ||
} | ||
b, err = Offset{Percent: .1}.MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "10%", string(b)) | ||
} | ||
d := Duration(0) | ||
b, err = Offset{Duration: &d}.MarshalText() | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "00:00:00", string(b)) | ||
} | ||
} | ||
var _ = Describe("Offset", func() { | ||
|
||
DescribeTable("marshal", | ||
func(o *Offset, exp string) { | ||
b, err := o.MarshalText() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(b)).To(Equal(exp)) | ||
}, | ||
Entry("0%", &Offset{}, "0%"), | ||
Entry("10%", &Offset{Percent: 0.1}, "10%"), | ||
Entry("00:00:00", &Offset{Duration: durationPtr(0)}, "00:00:00"), | ||
) | ||
|
||
DescribeTable("unmarshal", | ||
func(s string, pc float64, dur *Duration) { | ||
o := Offset{} | ||
Expect(o.UnmarshalText([]byte(s))).To(Succeed()) | ||
Expect(o.Percent).To(BeNumerically("~", pc, 0.001)) | ||
Expect(o.Duration).To(Equal(dur)) | ||
}, | ||
Entry("0%", "0%", 0.0, nil), | ||
Entry("10%", "10%", 0.1, nil), | ||
Entry("00:00:00", "00:00:00", 0.0, durationPtr(0)), | ||
) | ||
|
||
It("should fail to unmarshal bad inputs", func() { | ||
o := new(Offset) | ||
Expect(o.UnmarshalText([]byte("abc%"))).To(MatchError("invalid offset: abc%")) | ||
}) | ||
|
||
func TestOffsetUnmarshaler(t *testing.T) { | ||
var o Offset | ||
if assert.NoError(t, o.UnmarshalText([]byte("0%"))) { | ||
assert.Nil(t, o.Duration) | ||
assert.Equal(t, float32(0.0), o.Percent) | ||
} | ||
o = Offset{} | ||
if assert.NoError(t, o.UnmarshalText([]byte("10%"))) { | ||
assert.Nil(t, o.Duration) | ||
assert.Equal(t, float32(0.1), o.Percent) | ||
} | ||
o = Offset{} | ||
if assert.NoError(t, o.UnmarshalText([]byte("00:00:00"))) { | ||
if assert.NotNil(t, o.Duration) { | ||
assert.Equal(t, Duration(0), *o.Duration) | ||
} | ||
assert.Equal(t, float32(0), o.Percent) | ||
} | ||
o = Offset{} | ||
assert.EqualError(t, o.UnmarshalText([]byte("abc%")), "invalid offset: abc%") | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package vast | ||
|
||
import "bytes" | ||
|
||
// URI is a string that allows for stripping of whitespace when Unmarshalled | ||
type URI string | ||
|
||
// MarshalText implements the encoding.TextMarshaler interface. | ||
func (s URI) MarshalText() ([]byte, error) { | ||
return []byte(s), nil | ||
} | ||
|
||
// UnmarshalText implements the encoding.TextUnmarshaler interface. | ||
func (s *URI) UnmarshalText(data []byte) error { | ||
*s = URI(bytes.TrimSpace(data)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package vast | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("URI", func() { | ||
|
||
DescribeTable("marshal", | ||
func(d URI, exp string) { | ||
b, err := d.MarshalText() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(b)).To(Equal(exp)) | ||
}, | ||
Entry("", URI(""), ""), | ||
Entry("http://example.com", URI("http://example.com"), "http://example.com"), | ||
) | ||
|
||
DescribeTable("unmarshal", | ||
func(s string, exp URI) { | ||
d := new(URI) | ||
Expect(d.UnmarshalText([]byte(s))).To(Succeed()) | ||
Expect(*d).To(Equal(exp)) | ||
}, | ||
Entry("Blank", "", URI("")), | ||
Entry("Whitespace only", "\n\t ", URI("")), | ||
Entry("Ideal Example", "http://example.com", URI("http://example.com")), | ||
Entry("Real-world Example", "\n\t\t\t http://example.com \n\t\t\t", URI("http://example.com")), | ||
) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.