gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/date/date_test.go (about)

     1  // Copyright 2012 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //  created by Manoel Vilela (manoel_vilela@engineer.com)
     6  
     7  package main
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  // without any flag
    15  func TestDateNoFlags(t *testing.T) {
    16  	t.Log("::  Printing date with default location (no flags)...")
    17  	n := time.Now()
    18  	d := date(n, time.Local)
    19  	t.Logf("Date: %v\n", d)
    20  	dParsed, err := time.Parse(time.UnixDate, d)
    21  	if err != nil {
    22  		t.Error(err)
    23  	}
    24  	dTest := dParsed.Format(time.UnixDate)
    25  	if d != dTest {
    26  		t.Errorf("Mismatched dates; want %v, got %v\n", d, dTest)
    27  	}
    28  
    29  }
    30  
    31  // using u flag
    32  func TestDateUniversal(t *testing.T) {
    33  	t.Log("::  Printing date with UTC (using -u flag)...")
    34  	n := time.Now()
    35  	d := date(n, time.UTC)
    36  	t.Logf("Date: %v\n", d)
    37  	dParsed, err := time.Parse(time.UnixDate, d)
    38  	if err != nil {
    39  		t.Error(err)
    40  	}
    41  	dTest := dParsed.Format(time.UnixDate)
    42  	if d != dTest {
    43  		t.Errorf("Mismatched dates; want %v, got %v\n", d, dTest)
    44  	}
    45  }
    46  
    47  func TestFormatParser(t *testing.T) {
    48  	test := "%d %w %x %D %% asdf qwer qwe s sd fqwer % qwer"
    49  	expected := []string{"%d", "%w", "%x", "%D"}
    50  	t.Log(":: Test of FormatParser greping the n flags")
    51  	for index, match := range formatParser(test) {
    52  		t.Logf(":: Parsed on iteration %d: %v", index, match)
    53  		if match != expected[index] {
    54  			t.Errorf("Parsing Error; Want %v, got %v\n", expected[index], match)
    55  		}
    56  	}
    57  }
    58  
    59  func TestDateMap(t *testing.T) {
    60  	t.Log(":: Test of DateMap formatting")
    61  	posixFormat := "%a %b %e %H:%M:%S %Z %Y"
    62  	n := time.Now()
    63  	test := dateMap(n, time.Local, posixFormat)
    64  	expected := n.Format(time.UnixDate)
    65  
    66  	if test != expected {
    67  		t.Errorf("Mismatch outputs; \nwant %v, \n got %v", expected, test)
    68  	}
    69  }
    70  
    71  func TestDateMapExamples(t *testing.T) {
    72  	type dateTest struct {
    73  		format  string // format flags
    74  		example string // correct example
    75  	}
    76  
    77  	var tests = []dateTest{
    78  		{
    79  			"%a %b %e %H:%M:%S %Z %Y",
    80  			"Tue Jun 26 09:58:10 PDT 1990",
    81  		},
    82  		{
    83  			"DATE: %m/%d/%y%nTIME: %H:%M:%S",
    84  			"DATE: 11/02/91\nTIME: 13:36:16",
    85  		},
    86  		{
    87  			"TIME: %r",
    88  			"TIME: 01:36:32 PM",
    89  		},
    90  	}
    91  
    92  	t.Log(":: Sequence of examples for dateMap")
    93  	n := time.Now()
    94  	for _, test := range tests {
    95  		t.Logf(" Format: \n%v\n", test.format)
    96  		t.Logf("Example: \n%v\n", test.example)
    97  		t.Logf(" Output: \n%v\n", dateMap(n, time.Local, test.format))
    98  	}
    99  }