github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_len_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 tu "github.com/GuanceCloud/cliutils/testutil" 15 ) 16 17 func TestLen(t *testing.T) { 18 cases := []struct { 19 name, pl, in string 20 expected interface{} 21 fail bool 22 outkey string 23 }{ 24 { 25 name: "normal", 26 pl: `abc = ["1", "2"] 27 add_key(abc, len(abc))`, 28 in: `test`, 29 expected: int64(2), 30 outkey: "abc", 31 }, 32 { 33 name: "normal", 34 pl: `abc = [] 35 add_key(abc, len(abc))`, 36 in: `test`, 37 expected: int64(0), 38 outkey: "abc", 39 }, 40 { 41 name: "normal", 42 pl: `abc = {"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47} 43 add_key(abc, len(abc["a"]))`, 44 in: `test`, 45 expected: int64(4), 46 outkey: "abc", 47 }, 48 } 49 50 for idx, tc := range cases { 51 t.Run(tc.name, func(t *testing.T) { 52 runner, err := NewTestingRunner(tc.pl) 53 if err != nil { 54 if tc.fail { 55 t.Logf("[%d]expect error: %s", idx, err) 56 } else { 57 t.Errorf("[%d] failed: %s", idx, err) 58 } 59 return 60 } 61 pt := ptinput.NewPlPoint( 62 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 63 errR := runScript(runner, pt) 64 65 if errR != nil { 66 t.Fatal(errR.Error()) 67 } 68 69 v, _, _ := pt.Get(tc.outkey) 70 // tu.Equals(t, nil, err) 71 tu.Equals(t, tc.expected, v) 72 t.Logf("[%d] PASS", idx) 73 }) 74 } 75 }