Skip to content

Commit

Permalink
Merge pull request #1 from bsm/feature/new-test-set
Browse files Browse the repository at this point in the history
New Test set & other minor improvements
  • Loading branch information
ajn authored Feb 15, 2017
2 parents aa9eb84 + 037d7ee commit 59c433b
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 388 deletions.
91 changes: 40 additions & 51 deletions duration_test.go
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"))
})

})
2 changes: 2 additions & 0 deletions offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (o *Offset) UnmarshalText(data []byte) error {
}
o.Percent = float32(p) / 100
return nil
} else {
o.Percent = 0
}
var d Duration
o.Duration = &d
Expand Down
72 changes: 33 additions & 39 deletions offset_test.go
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%")
}
})
8 changes: 8 additions & 0 deletions testdata/vast_inline_linear.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
</CompanionAds>
</Creative>
</Creatives>
<Extensions>
<Extension>
<Geo>
<Country>US</Country>
<State>CA</State>
</Geo>
</Extension>
</Extensions>
</InLine>
</Ad>
</VAST>
17 changes: 17 additions & 0 deletions uri.go
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
}
33 changes: 33 additions & 0 deletions uri_test.go
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")),
)

})
18 changes: 9 additions & 9 deletions vast.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ type InLine struct {
}

type Error struct {
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}

// Impression is a URI that directs the video player to a tracking resource file that
// the video player should request when the first frame of the ad is displayed
type Impression struct {
ID string `xml:"id,attr,omitempty"`
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}

// Pricing provides a value that represents a price that can be used by real-time
Expand Down Expand Up @@ -146,7 +146,7 @@ type Wrapper struct {
// The name of the ad server that returned the ad
AdSystem *AdSystem
// URL of ad tag of downstream Secondary Ad Server
VASTAdTagURI string
VASTAdTagURI URI
// One or more URIs that directs the video player to a tracking resource file that the
// video player should request when the first frame of the ad is displayed
Impressions []Impression `xml:"Impression"`
Expand Down Expand Up @@ -255,7 +255,7 @@ type Linear struct {
// begins playing.
SkipOffset *Offset `xml:"skipoffset,attr,omitempty"`
// Duration in standard time format, hh:mm:ss
Duration string
Duration *Duration
AdParameters *AdParameters `xml:",omitempty"`
Icons []Icon
TrackingEvents []Tracking `xml:"TrackingEvents>Tracking,omitempty"`
Expand Down Expand Up @@ -461,15 +461,15 @@ type Tracking struct {
// The time during the video at which this url should be pinged. Must be present for
// progress event. Must match (\d{2}:[0-5]\d:[0-5]\d(\.\d\d\d)?|1?\d?\d(\.?\d)*%)
Offset *Offset `xml:"offset,attr,omitempty"`
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}

// StaticResource is the URL to a static file, such as an image or SWF file
type StaticResource struct {
// Mime type of static resource
CreativeType string `xml:"creativeType,attr,omitempty"`
// URL to a static file, such as an image or SWF file
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}

// HTMLResource is a container for HTML data
Expand All @@ -496,7 +496,7 @@ type VideoClicks struct {
// VideoClick defines a click URL for a linear creative
type VideoClick struct {
ID string `xml:"id,attr,omitempty"`
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}

// MediaFile defines a reference to a linear creative asset
Expand Down Expand Up @@ -534,7 +534,7 @@ type MediaFile struct {
// (for Flash/Flex), “initParams” (for Silverlight) and “GetVariables” (variables
// placed in key/value pairs on the asset request).
APIFramework string `xml:"apiFramework,attr,omitempty"`
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}

// Extensions defines extensions
Expand All @@ -554,5 +554,5 @@ type Extension struct {

type CompanionClickThrough struct {
// URL to a static file, such as an image or SWF file
URI string `xml:",cdata"`
URI URI `xml:",cdata"`
}
Loading

0 comments on commit 59c433b

Please sign in to comment.