github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_trim_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 TestTrim(t *testing.T) {
    18  	cases := []struct {
    19  		name, pl, in string
    20  		outkey       string
    21  		expect       interface{}
    22  		fail         bool
    23  	}{
    24  		{
    25  			name: "trim space",
    26  			in:   `trim space`,
    27  			pl: `
    28  			item = " not_space "
    29  			trim(item)
    30  	`,
    31  			outkey: "item",
    32  			expect: "not_space",
    33  			fail:   false,
    34  		},
    35  		{
    36  			name: "trim ABC_-",
    37  			in:   `trim ABC_-`,
    38  			pl: `
    39  			item = "BC_-AAACAnot_spaceABACC"
    40  			trim(item, "ABC_-")
    41  	`,
    42  			outkey: "item",
    43  			expect: "not_space",
    44  			fail:   false,
    45  		},
    46  		{
    47  			name: "trim ABC_",
    48  			in:   `trim ABC_`,
    49  			pl: `
    50  			add_key(test_data, "ACCAA_test_DataA_ACBA")
    51  			trim(test_data, "ABC_")
    52  	`,
    53  			outkey: "test_data",
    54  			expect: "test_Data",
    55  			fail:   false,
    56  		},
    57  	}
    58  
    59  	for idx, tc := range cases {
    60  		t.Run(tc.name, func(t *testing.T) {
    61  			runner, err := NewTestingRunner(tc.pl)
    62  			if err != nil {
    63  				if tc.fail {
    64  					t.Logf("[%d]expect error: %s", idx, err)
    65  				} else {
    66  					t.Errorf("[%d] failed: %s", idx, err)
    67  				}
    68  				return
    69  			}
    70  			pt := ptinput.NewPlPoint(
    71  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    72  			errR := runScript(runner, pt)
    73  
    74  			if errR != nil {
    75  				t.Fatal(errR.Error())
    76  			}
    77  
    78  			v, _, _ := pt.Get(tc.outkey)
    79  			tu.Equals(t, tc.expect, v)
    80  			t.Logf("[%d] PASS", idx)
    81  		})
    82  	}
    83  }