github.com/puellanivis/breton@v0.2.16/lib/net/dash/mpd/duration_test.go (about)

     1  package mpd
     2  
     3  import (
     4  	"encoding/xml"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestDuration(t *testing.T) {
    10  	durs := map[time.Duration]string{
    11  		time.Second:                        "PT1S",
    12  		time.Minute:                        "PT1M",
    13  		time.Hour:                          "PT1H",
    14  		time.Second + time.Nanosecond:      "PT1.000000001S",
    15  		time.Second + 10*time.Nanosecond:   "PT1.00000001S",
    16  		time.Second + 100*time.Nanosecond:  "PT1.0000001S",
    17  		time.Second + 1*time.Microsecond:   "PT1.000001S",
    18  		time.Second + 10*time.Microsecond:  "PT1.00001S",
    19  		time.Second + 100*time.Microsecond: "PT1.0001S",
    20  
    21  		time.Second + 500*time.Microsecond:    "PT1.0005S",
    22  		time.Second + time.Millisecond:        "PT1.001S",
    23  		time.Second + 10*time.Millisecond:     "PT1.01S",
    24  		time.Second + 100*time.Millisecond:    "PT1.1S",
    25  		time.Hour + time.Second:               "PT1H1S",
    26  		time.Minute + time.Second:             "PT1M1S",
    27  		time.Hour + time.Minute:               "PT1H1M",
    28  		time.Hour + time.Minute + time.Second: "PT1H1M1S",
    29  
    30  		Day: "P1D",
    31  
    32  		Day + time.Hour + time.Minute + time.Second: "P1DT1H1M1S",
    33  	}
    34  
    35  	var attr xml.Attr
    36  	for dur, expect := range durs {
    37  		d := Duration{
    38  			ns: dur,
    39  		}
    40  
    41  		if d.XMLString() != expect {
    42  			t.Errorf("Duration %s marshaled wrong: expected %q, got %q", dur, expect, d.XMLString())
    43  		}
    44  
    45  		var d2 Duration
    46  		attr.Value = expect
    47  
    48  		if err := d2.UnmarshalXMLAttr(attr); err != nil {
    49  			t.Errorf("Error while Unmarshalling %q: %v", expect, err)
    50  			continue
    51  		}
    52  
    53  		if d2.ns != dur {
    54  			t.Errorf("XML value %q unmarshaled wrong: expected %s, got %s", expect, dur, d2.ns)
    55  		}
    56  	}
    57  }