github.com/elves/elvish@v0.15.0/pkg/testutil/scaled_ms_test.go (about)

     1  package testutil
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/elves/elvish/pkg/env"
     9  )
    10  
    11  var scaledMsTests = []struct {
    12  	name string
    13  	env  string
    14  	ms   int
    15  
    16  	want time.Duration
    17  }{
    18  	{"default 10ms", "", 10, 10 * time.Millisecond},
    19  
    20  	{"2x 10ms", "2", 10, 20 * time.Millisecond},
    21  	{"2x 3000ms", "2", 3000, 6 * time.Second},
    22  	{"0.5x 10ms", "0.5", 10, 5 * time.Millisecond},
    23  
    24  	{"invalid treated as 1", "a", 10, 10 * time.Millisecond},
    25  	{"0 treated as 1", "0", 10, 10 * time.Millisecond},
    26  	{"negative treated as 1", "-1", 10, 10 * time.Millisecond},
    27  }
    28  
    29  func TestScaledMs(t *testing.T) {
    30  	envSave := os.Getenv(env.ELVISH_TEST_TIME_SCALE)
    31  	defer os.Setenv(env.ELVISH_TEST_TIME_SCALE, envSave)
    32  
    33  	for _, test := range scaledMsTests {
    34  		t.Run(test.name, func(t *testing.T) {
    35  			os.Setenv(env.ELVISH_TEST_TIME_SCALE, test.env)
    36  			got := ScaledMs(test.ms)
    37  			if got != test.want {
    38  				t.Errorf("got %v, want %v", got, test.want)
    39  			}
    40  		})
    41  	}
    42  }