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

     1  // Copyright 2017 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  package main
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestParseDuration(t *testing.T) {
    13  	var tests = []struct {
    14  		in  string
    15  		out time.Duration
    16  		err error
    17  	}{
    18  		{"", time.Duration(0), errDuration},
    19  		{"xyz", time.Duration(0), errDuration},
    20  		{"-2.5", time.Duration(0), errDuration},
    21  		{"-2.5s", time.Duration(0), errDuration},
    22  		{"2.5", time.Duration(2500 * time.Millisecond), nil},
    23  		{"2.5s", time.Duration(2500 * time.Millisecond), nil},
    24  		{"300m", time.Duration(300 * time.Minute), nil},
    25  		{"2h45m", time.Duration(2*time.Hour + 45*time.Minute), nil},
    26  	}
    27  
    28  	// Table-driven testing
    29  	for _, tt := range tests {
    30  		out, err := parseDuration(tt.in)
    31  		if out != tt.out || err != tt.err {
    32  			t.Errorf("parseDuration(%#v) = %v, %v; want %v, %v",
    33  				tt.in, out, err, tt.out, tt.err)
    34  		}
    35  	}
    36  }