github.com/leanovate/gopter@v0.2.9/gen/time_test.go (about)

     1  package gen_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/leanovate/gopter/gen"
     8  )
     9  
    10  func TestTime(t *testing.T) {
    11  	timeGen := gen.Time()
    12  	for i := 0; i < 100; i++ {
    13  		value, ok := timeGen.Sample()
    14  
    15  		if !ok || value == nil {
    16  			t.Errorf("Invalid time: %#v", value)
    17  		}
    18  		v, ok := value.(time.Time)
    19  		if !ok || v.String() == "" {
    20  			t.Errorf("Invalid time: %#v", value)
    21  		}
    22  		if v.Year() < 0 || v.Year() > 9999 {
    23  			t.Errorf("Year out of range: %#v", v)
    24  		}
    25  	}
    26  }
    27  
    28  func TestAnyTime(t *testing.T) {
    29  	timeGen := gen.AnyTime()
    30  	for i := 0; i < 100; i++ {
    31  		value, ok := timeGen.Sample()
    32  
    33  		if !ok || value == nil {
    34  			t.Errorf("Invalid time: %#v", value)
    35  		}
    36  		v, ok := value.(time.Time)
    37  		if !ok || v.String() == "" {
    38  			t.Errorf("Invalid time: %#v", value)
    39  		}
    40  	}
    41  }
    42  
    43  func TestTimeRegion(t *testing.T) {
    44  	duration := time.Duration(10*24*365) * time.Hour
    45  	from := time.Unix(1000, 0)
    46  	until := from.Add(duration)
    47  	timeRange := gen.TimeRange(from, duration)
    48  
    49  	for i := 0; i < 100; i++ {
    50  		value, ok := timeRange.Sample()
    51  
    52  		if !ok || value == nil {
    53  			t.Errorf("Invalid time: %#v", value)
    54  		}
    55  		v, ok := value.(time.Time)
    56  		if !ok || v.Before(from) || v.After(until) {
    57  			t.Errorf("Invalid time: %#v", value)
    58  		}
    59  	}
    60  }