github.com/CyCoreSystems/ari@v4.8.4+incompatible/datetime_test.go (about)

     1  package ari
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  // test data
    13  
    14  var dtMarshalTests = []struct {
    15  	Input    dtTest
    16  	Output   string
    17  	HasError bool
    18  }{
    19  	{dtTest{DateTime(time.Date(2005, 02, 04, 13, 12, 6, 0, time.UTC))}, `{"dt":"2005-02-04T13:12:06.000+0000"}`, false},
    20  }
    21  
    22  var dtUnmarshalTests = []struct {
    23  	Input    string
    24  	Output   dtTest
    25  	HasError bool
    26  }{
    27  	{`{"dt":"2005-02-04T13:12:06.000+0000"}`, dtTest{DateTime(time.Date(2005, 02, 04, 13, 12, 6, 0, time.UTC))}, false},
    28  	{`{"dt":"2x05-02-04T13:12:06.000+0000"}`, dtTest{}, true},
    29  	{`{"dt": 0 }`, dtTest{}, true},
    30  }
    31  
    32  var dsUnmarshalTests = []struct {
    33  	Input    string
    34  	Output   dsTest
    35  	HasError bool
    36  }{
    37  	{`{"ds":4}`, dsTest{DurationSec(4 * time.Second)}, false},
    38  	{`{"ds":40}`, dsTest{DurationSec(40 * time.Second)}, false},
    39  	{`{"ds":"4"}`, dsTest{}, true},
    40  	{`{"ds":""}`, dsTest{}, true},
    41  	{`{"ds":"xzsad"}`, dsTest{}, true},
    42  }
    43  
    44  var dsMarshalTests = []struct {
    45  	Input    dsTest
    46  	Output   string
    47  	HasError bool
    48  }{
    49  	{dsTest{DurationSec(4 * time.Second)}, `{"ds":4}`, false},
    50  	{dsTest{DurationSec(40 * time.Second)}, `{"ds":40}`, false},
    51  }
    52  
    53  // test runners
    54  func TestDateTimeMarshal(t *testing.T) {
    55  	for _, tx := range dtMarshalTests {
    56  		ret := runTestMarshal(tx.Input, tx.Output, tx.HasError)
    57  		if ret != "" {
    58  			t.Errorf(ret)
    59  		}
    60  	}
    61  }
    62  func TestDateTimeUnmarshal(t *testing.T) {
    63  	for _, tx := range dtUnmarshalTests {
    64  		var out dtTest
    65  		ret := runTestUnmarshal(&out, tx.Input, &tx.Output, tx.HasError)
    66  		if ret != "" {
    67  			t.Errorf(ret)
    68  		}
    69  	}
    70  }
    71  
    72  func TestDurationSecsMarshal(t *testing.T) {
    73  	for _, tx := range dsMarshalTests {
    74  		ret := runTestMarshal(tx.Input, tx.Output, tx.HasError)
    75  		if ret != "" {
    76  			t.Errorf(ret)
    77  		}
    78  	}
    79  }
    80  func TestDurationSecsUnmarshal(t *testing.T) {
    81  	for _, tx := range dsUnmarshalTests {
    82  		var out dsTest
    83  		ret := runTestUnmarshal(&out, tx.Input, &tx.Output, tx.HasError)
    84  		if ret != "" {
    85  			t.Errorf(ret)
    86  		}
    87  	}
    88  }
    89  
    90  // generalized test functions
    91  
    92  func runTestMarshal(input interface{}, output string, hasError bool) (ret string) {
    93  	var buf bytes.Buffer
    94  	err := json.NewEncoder(&buf).Encode(input)
    95  	out := strings.TrimSpace(buf.String())
    96  
    97  	failed := false
    98  	failed = failed || (err == nil && hasError)
    99  	failed = failed || (out != output)
   100  
   101  	if failed {
   102  		ret = fmt.Sprintf("Marshal(%s) => '%s', 'err != nil => %v'; expected '%s', 'err != nil => %v'.", input, out, err != nil, output, hasError)
   103  	}
   104  
   105  	return
   106  }
   107  
   108  func runTestUnmarshal(out eq, input string, output eq, hasError bool) (ret string) {
   109  	err := json.NewDecoder(strings.NewReader(input)).Decode(&out)
   110  
   111  	failed := false
   112  	failed = failed || (err == nil && hasError)
   113  	failed = failed || (!out.Equal(output))
   114  
   115  	if failed {
   116  		ret = fmt.Sprintf("Unmarshal(%s) => '%s', 'err != nil => %v'; expected '%s', 'err != nil => %v'.", input, out, err != nil, output, hasError)
   117  	}
   118  
   119  	return
   120  }
   121  
   122  // test structures
   123  
   124  type dtTest struct {
   125  	Date DateTime `json:"dt"`
   126  }
   127  
   128  func (dt *dtTest) Equal(i interface{}) bool {
   129  	o, ok := i.(*dtTest)
   130  	return ok && time.Time(dt.Date).Equal(time.Time(o.Date))
   131  }
   132  
   133  type eq interface {
   134  	Equal(i interface{}) bool
   135  }
   136  
   137  type dsTest struct {
   138  	Duration DurationSec `json:"ds"`
   139  }
   140  
   141  func (ds *dsTest) Equal(i interface{}) bool {
   142  	o, ok := i.(*dsTest)
   143  	return ok && time.Duration(ds.Duration) == time.Duration(o.Duration)
   144  }