...

Source file src/github.com/Azure/go-autorest/autorest/date/timerfc1123_test.go

Documentation: github.com/Azure/go-autorest/autorest/date

     1  package date
     2  
     3  // Copyright 2017 Microsoft Corporation
     4  //
     5  //  Licensed under the Apache License, Version 2.0 (the "License");
     6  //  you may not use this file except in compliance with the License.
     7  //  You may obtain a copy of the License at
     8  //
     9  //      http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  //  Unless required by applicable law or agreed to in writing, software
    12  //  distributed under the License is distributed on an "AS IS" BASIS,
    13  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  //  See the License for the specific language governing permissions and
    15  //  limitations under the License.
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func ExampleTimeRFC1123() {
    26  	d, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
    27  	if err != nil {
    28  		fmt.Println(err)
    29  	}
    30  	fmt.Println(d)
    31  	// Output: 2006-01-02 15:04:05 +0000 MST
    32  }
    33  
    34  func ExampleTimeRFC1123_MarshalBinary() {
    35  	ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
    36  	if err != nil {
    37  		fmt.Println(err)
    38  	}
    39  	d := TimeRFC1123{ti}
    40  	b, err := d.MarshalBinary()
    41  	if err != nil {
    42  		fmt.Println(err)
    43  	}
    44  	fmt.Println(string(b))
    45  	// Output: Mon, 02 Jan 2006 15:04:05 MST
    46  }
    47  
    48  func ExampleTimeRFC1123_UnmarshalBinary() {
    49  	d := TimeRFC1123{}
    50  	t := "Mon, 02 Jan 2006 15:04:05 MST"
    51  	if err := d.UnmarshalBinary([]byte(t)); err != nil {
    52  		fmt.Println(err)
    53  	}
    54  	fmt.Println(d)
    55  	// Output: Mon, 02 Jan 2006 15:04:05 MST
    56  }
    57  
    58  func ExampleTimeRFC1123_MarshalJSON() {
    59  	ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
    60  	if err != nil {
    61  		fmt.Println(err)
    62  	}
    63  	d := TimeRFC1123{ti}
    64  	j, err := json.Marshal(d)
    65  	if err != nil {
    66  		fmt.Println(err)
    67  	}
    68  	fmt.Println(string(j))
    69  	// Output: "Mon, 02 Jan 2006 15:04:05 MST"
    70  }
    71  
    72  func TestTimeRFC1123MarshalJSONInvalid(t *testing.T) {
    73  	ti := time.Date(20000, 01, 01, 00, 00, 00, 00, time.UTC)
    74  	d := TimeRFC1123{ti}
    75  	if _, err := json.Marshal(d); err == nil {
    76  		t.Fatalf("date: TimeRFC1123#Marshal failed for invalid date")
    77  	}
    78  }
    79  
    80  func ExampleTimeRFC1123_UnmarshalJSON() {
    81  	var d struct {
    82  		Time TimeRFC1123 `json:"datetime"`
    83  	}
    84  	j := `{"datetime" : "Mon, 02 Jan 2006 15:04:05 MST"}`
    85  
    86  	if err := json.Unmarshal([]byte(j), &d); err != nil {
    87  		fmt.Println(err)
    88  	}
    89  	fmt.Println(d.Time)
    90  	// Output: Mon, 02 Jan 2006 15:04:05 MST
    91  }
    92  
    93  func ExampleTimeRFC1123_MarshalText() {
    94  	ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
    95  	if err != nil {
    96  		fmt.Println(err)
    97  	}
    98  	d := TimeRFC1123{ti}
    99  	t, err := d.MarshalText()
   100  	if err != nil {
   101  		fmt.Println(err)
   102  	}
   103  	fmt.Println(string(t))
   104  	// Output: Sat, 03 Feb 2001 04:05:06 UTC
   105  }
   106  
   107  func ExampleTimeRFC1123_UnmarshalText() {
   108  	d := TimeRFC1123{}
   109  	t := "Sat, 03 Feb 2001 04:05:06 UTC"
   110  
   111  	if err := d.UnmarshalText([]byte(t)); err != nil {
   112  		fmt.Println(err)
   113  	}
   114  	fmt.Println(d)
   115  	// Output: Sat, 03 Feb 2001 04:05:06 UTC
   116  }
   117  
   118  func TestUnmarshalJSONforInvalidDateRfc1123(t *testing.T) {
   119  	dt := `"Mon, 02 Jan 2000000 15:05 MST"`
   120  	d := TimeRFC1123{}
   121  	if err := d.UnmarshalJSON([]byte(dt)); err == nil {
   122  		t.Fatalf("date: TimeRFC1123#Unmarshal failed for invalid date")
   123  	}
   124  }
   125  
   126  func TestUnmarshalTextforInvalidDateRfc1123(t *testing.T) {
   127  	dt := "Mon, 02 Jan 2000000 15:05 MST"
   128  	d := TimeRFC1123{}
   129  	if err := d.UnmarshalText([]byte(dt)); err == nil {
   130  		t.Fatalf("date: TimeRFC1123#Unmarshal failed for invalid date")
   131  	}
   132  }
   133  
   134  func TestTimeStringRfc1123(t *testing.T) {
   135  	ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
   136  	if err != nil {
   137  		fmt.Println(err)
   138  	}
   139  	d := TimeRFC1123{ti}
   140  	if d.String() != "Mon, 02 Jan 2006 15:04:05 MST" {
   141  		t.Fatalf("date: TimeRFC1123#String failed (%v)", d.String())
   142  	}
   143  }
   144  
   145  func TestTimeStringReturnsEmptyStringForErrorRfc1123(t *testing.T) {
   146  	d := TimeRFC1123{Time: time.Date(20000, 01, 01, 01, 01, 01, 01, time.UTC)}
   147  	if d.String() != "" {
   148  		t.Fatalf("date: TimeRFC1123#String failed empty string for an error")
   149  	}
   150  }
   151  
   152  func TestTimeBinaryRoundTripRfc1123(t *testing.T) {
   153  	ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
   154  	if err != nil {
   155  		t.Fatalf("date: TimeRFC1123#ParseTime failed (%v)", err)
   156  	}
   157  	d1 := TimeRFC1123{ti}
   158  	t1, err := d1.MarshalBinary()
   159  	if err != nil {
   160  		t.Fatalf("date: TimeRFC1123#MarshalBinary failed (%v)", err)
   161  	}
   162  
   163  	d2 := TimeRFC1123{}
   164  	if err = d2.UnmarshalBinary(t1); err != nil {
   165  		t.Fatalf("date: TimeRFC1123#UnmarshalBinary failed (%v)", err)
   166  	}
   167  
   168  	if !reflect.DeepEqual(d1, d2) {
   169  		t.Fatalf("date: Round-trip Binary failed (%v, %v)", d1, d2)
   170  	}
   171  }
   172  
   173  func TestTimeJSONRoundTripRfc1123(t *testing.T) {
   174  	type s struct {
   175  		Time TimeRFC1123 `json:"datetime"`
   176  	}
   177  	var err error
   178  	ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
   179  	if err != nil {
   180  		t.Fatalf("date: TimeRFC1123#ParseTime failed (%v)", err)
   181  	}
   182  	d1 := s{Time: TimeRFC1123{ti}}
   183  	j, err := json.Marshal(d1)
   184  	if err != nil {
   185  		t.Fatalf("date: TimeRFC1123#MarshalJSON failed (%v)", err)
   186  	}
   187  
   188  	d2 := s{}
   189  	if err = json.Unmarshal(j, &d2); err != nil {
   190  		t.Fatalf("date: TimeRFC1123#UnmarshalJSON failed (%v)", err)
   191  	}
   192  
   193  	if !reflect.DeepEqual(d1, d2) {
   194  		t.Fatalf("date: Round-trip JSON failed (%v, %v)", d1, d2)
   195  	}
   196  }
   197  
   198  func TestTimeTextRoundTripRfc1123(t *testing.T) {
   199  	ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
   200  	if err != nil {
   201  		t.Fatalf("date: TimeRFC1123#ParseTime failed (%v)", err)
   202  	}
   203  	d1 := TimeRFC1123{Time: ti}
   204  	t1, err := d1.MarshalText()
   205  	if err != nil {
   206  		t.Fatalf("date: TimeRFC1123#MarshalText failed (%v)", err)
   207  	}
   208  
   209  	d2 := TimeRFC1123{}
   210  	if err = d2.UnmarshalText(t1); err != nil {
   211  		t.Fatalf("date: TimeRFC1123#UnmarshalText failed (%v)", err)
   212  	}
   213  
   214  	if !reflect.DeepEqual(d1, d2) {
   215  		t.Fatalf("date: Round-trip Text failed (%v, %v)", d1, d2)
   216  	}
   217  }
   218  
   219  func TestTimeToTimeRFC1123(t *testing.T) {
   220  	ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
   221  	d := TimeRFC1123{ti}
   222  	if err != nil {
   223  		t.Fatalf("date: TimeRFC1123#ParseTime failed (%v)", err)
   224  	}
   225  	var _ time.Time = d.ToTime()
   226  }
   227  

View as plain text