exercism

Exercism solutions.
git clone git://code.dwrz.net/exercism
Log | Files | Refs

gigasecond_test.go (1362B)


      1 package gigasecond
      2 
      3 // Write a function AddGigasecond that works with time.Time.
      4 
      5 import (
      6 	"os"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 // date formats used in test data
     12 const (
     13 	fmtD  = "2006-01-02"
     14 	fmtDT = "2006-01-02T15:04:05"
     15 )
     16 
     17 func TestAddGigasecond(t *testing.T) {
     18 	for _, tc := range addCases {
     19 		in := parse(tc.in, t)
     20 		want := parse(tc.want, t)
     21 		got := AddGigasecond(in)
     22 		if !got.Equal(want) {
     23 			t.Fatalf(`FAIL: %s
     24 AddGigasecond(%s)
     25    = %s
     26 want %s`, tc.description, in, got, want)
     27 		}
     28 		t.Log("PASS:", tc.description)
     29 	}
     30 	t.Log("Tested", len(addCases), "cases.")
     31 }
     32 
     33 func parse(s string, t *testing.T) time.Time {
     34 	tt, err := time.Parse(fmtDT, s) // try full date time format first
     35 	if err != nil {
     36 		tt, err = time.Parse(fmtD, s) // also allow just date
     37 	}
     38 	if err != nil {
     39 		// can't run tests if input won't parse.  if this seems to be a
     40 		// development or ci environment, raise an error.  if this condition
     41 		// makes it to the solver though, ask for a bug report.
     42 		_, statErr := os.Stat("example_gen.go")
     43 		if statErr == nil || os.Getenv("TRAVIS_GO_VERSION") > "" {
     44 			t.Fatal(err)
     45 		} else {
     46 			t.Log(err)
     47 			t.Skip("(This is not your fault, and is unexpected.  " +
     48 				"Please file an issue at https://github.com/exercism/go.)")
     49 		}
     50 	}
     51 	return tt
     52 }
     53 
     54 func BenchmarkAddGigasecond(b *testing.B) {
     55 	for i := 0; i < b.N; i++ {
     56 		AddGigasecond(time.Time{})
     57 	}
     58 }