github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_decoder_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  	"golang.org/x/text/encoding/simplifiedchinese"
    16  )
    17  
    18  type funcCase struct {
    19  	name     string
    20  	in       string
    21  	script   string
    22  	expected interface{}
    23  	key      string
    24  	fail     bool
    25  }
    26  
    27  func TestDecode(t *testing.T) {
    28  	data := []string{"测试一下", "不知道", "测试一下123456", "哈哈哈哈哈", "-汪98阿萨德离开家"}
    29  	decodeDataSlice := make([]string, 10)
    30  
    31  	for idx, cont := range data {
    32  		decodeData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(cont))
    33  		decodeDataSlice[idx] = string(decodeData)
    34  	}
    35  
    36  	testCase := []*funcCase{
    37  		{
    38  			in:     decodeDataSlice[0],
    39  			script: `decode(_,"gbk")`,
    40  			key:    "message",
    41  		},
    42  		{
    43  			in:     decodeDataSlice[1],
    44  			script: `decode(_,"gbk")`,
    45  			key:    "message",
    46  		},
    47  		{
    48  			in:     decodeDataSlice[2],
    49  			script: `decode(_,"gbk")`,
    50  			key:    "message",
    51  		},
    52  		{
    53  			in:     decodeDataSlice[3],
    54  			script: `decode(_,"gbk")`,
    55  			key:    "message",
    56  		},
    57  		{
    58  			in:     decodeDataSlice[4],
    59  			script: `decode(_,"gbk")`,
    60  			key:    "message",
    61  		},
    62  	}
    63  	for idx, tc := range testCase {
    64  		t.Run(tc.name, func(t *testing.T) {
    65  			runner, err := NewTestingRunner(tc.script)
    66  			tu.Equals(t, nil, err)
    67  
    68  			pt := ptinput.NewPlPoint(
    69  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    70  			errR := runScript(runner, pt)
    71  
    72  			if errR != nil {
    73  				t.Fatal(errR)
    74  			}
    75  
    76  			v, _, _ := pt.Get(tc.key)
    77  			tu.Equals(t, data[idx], v)
    78  
    79  			t.Logf("[%d] PASS", idx)
    80  		})
    81  	}
    82  }