gitlab.com/flarenetwork/coreth@v0.1.1/plugin/evm/config_test.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package evm 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "testing" 10 "time" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestUnmarshalConfig(t *testing.T) { 16 tests := []struct { 17 name string 18 givenJSON []byte 19 expected Config 20 expectedErr bool 21 }{ 22 { 23 "string durations parsed", 24 []byte(`{"api-max-duration": "1m", "continuous-profiler-frequency": "2m"}`), 25 Config{APIMaxDuration: Duration{1 * time.Minute}, ContinuousProfilerFrequency: Duration{2 * time.Minute}}, 26 false, 27 }, 28 { 29 "integer durations parsed", 30 []byte(fmt.Sprintf(`{"api-max-duration": "%v", "continuous-profiler-frequency": "%v"}`, 1*time.Minute, 2*time.Minute)), 31 Config{APIMaxDuration: Duration{1 * time.Minute}, ContinuousProfilerFrequency: Duration{2 * time.Minute}}, 32 false, 33 }, 34 { 35 "nanosecond durations parsed", 36 []byte(`{"api-max-duration": 5000000000, "continuous-profiler-frequency": 5000000000}`), 37 Config{APIMaxDuration: Duration{5 * time.Second}, ContinuousProfilerFrequency: Duration{5 * time.Second}}, 38 false, 39 }, 40 { 41 "bad durations", 42 []byte(`{"api-max-duration": "bad-duration"}`), 43 Config{}, 44 true, 45 }, 46 } 47 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 var tmp Config 51 err := json.Unmarshal(tt.givenJSON, &tmp) 52 if tt.expectedErr { 53 assert.Error(t, err) 54 } else { 55 assert.NoError(t, err) 56 assert.Equal(t, tt.expected, tmp) 57 } 58 }) 59 } 60 }