github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_match_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 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestMatch(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 = "sss" 27 add_key(abc, match("\\w+", abc))`, 28 in: `test`, 29 expected: true, 30 outkey: "abc", 31 }, 32 { 33 name: "normal", 34 pl: `abc = "sss" 35 add_key(abc, match("sss", abc))`, 36 in: `test`, 37 expected: true, 38 outkey: "abc", 39 }, 40 { 41 name: "normal", 42 pl: `abc = "sss" 43 add_key(abc, match(abc, abc))`, 44 in: `test`, 45 expected: true, 46 outkey: "abc", 47 fail: true, 48 }, 49 } 50 51 for idx, tc := range cases { 52 t.Run(tc.name, func(t *testing.T) { 53 runner, err := NewTestingRunner(tc.pl) 54 if err != nil { 55 if tc.fail { 56 t.Logf("[%d]expect error: %s", idx, err) 57 } else { 58 t.Errorf("[%d] failed: %s", idx, err) 59 } 60 return 61 } 62 pt := ptinput.NewPlPoint( 63 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 64 errR := runScript(runner, pt) 65 66 if errR != nil { 67 t.Fatal(errR.Error()) 68 } 69 70 v, _, _ := pt.Get(tc.outkey) 71 // tu.Equals(t, nil, err) 72 assert.Equal(t, tc.expected, v) 73 74 t.Logf("[%d] PASS", idx) 75 }) 76 } 77 }