github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/cmd/octsdb/main_test.go (about) 1 // Copyright (c) 2016 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package main 6 7 import ( 8 "math" 9 "testing" 10 11 "github.com/aristanetworks/goarista/test" 12 pb "github.com/openconfig/gnmi/proto/gnmi" 13 ) 14 15 func TestParseValue(t *testing.T) { // Because parsing JSON sucks. 16 var staticValueMap map[string]int64 17 staticValueMap = make(map[string]int64) 18 newJSONVal := func(v string) *pb.TypedValue { 19 return &pb.TypedValue{ 20 Value: &pb.TypedValue_JsonVal{JsonVal: []byte(v)}, 21 } 22 } 23 newDoubleVal := func(v float64) *pb.TypedValue { 24 return &pb.TypedValue{ 25 Value: &pb.TypedValue_DoubleVal{DoubleVal: v}, 26 } 27 } 28 29 testcases := []struct { 30 input *pb.TypedValue 31 staticValueMap map[string]int64 32 expected interface{} 33 }{ 34 {newJSONVal("42"), staticValueMap, []interface{}{int64(42)}}, 35 {newJSONVal("-42"), staticValueMap, []interface{}{int64(-42)}}, 36 {newJSONVal("42.42"), staticValueMap, []interface{}{float64(42.42)}}, 37 {newJSONVal("-42.42"), staticValueMap, []interface{}{float64(-42.42)}}, 38 {newDoubleVal(42.42), staticValueMap, []interface{}{float64(42.42)}}, 39 {newDoubleVal(-42.42), staticValueMap, []interface{}{float64(-42.42)}}, 40 {newJSONVal(`"foo"`), staticValueMap, []interface{}(nil)}, 41 {newJSONVal("9223372036854775807"), staticValueMap, []interface{}{int64(math.MaxInt64)}}, 42 {newJSONVal("-9223372036854775808"), staticValueMap, []interface{}{int64(math.MinInt64)}}, 43 {newJSONVal("9223372036854775808"), staticValueMap, 44 []interface{}{uint64(math.MaxInt64) + 1}}, 45 {newJSONVal("[1,3,5,7,9]"), staticValueMap, 46 []interface{}{int64(1), int64(3), int64(5), int64(7), int64(9)}}, 47 {newJSONVal("[1,9223372036854775808,0,-9223372036854775808]"), staticValueMap, 48 []interface{}{int64(1), uint64(math.MaxInt64) + 1, int64(0), int64(math.MinInt64)}}, 49 {newJSONVal(`[1,{"value":9},5,7,9]`), staticValueMap, 50 []interface{}{int64(1), int64(9), int64(5), int64(7), int64(9)}}, 51 {newJSONVal(`"intfOperUp"`), map[string]int64{"intfOperUp": 1}, []interface{}{int64(1)}}, 52 {newJSONVal(`"default"`), map[string]int64{"default": 0}, []interface{}{int64(0)}}, 53 } 54 for i, tcase := range testcases { 55 actual := parseValue(&pb.Update{Val: tcase.input}, tcase.staticValueMap) 56 if d := test.Diff(tcase.expected, actual); d != "" { 57 t.Errorf("#%d: %s: %#v vs %#v", i, d, tcase.expected, actual) 58 } 59 } 60 }