github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_b64dec_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 TestB64dec(t *testing.T) {
    18  	cases := []struct {
    19  		name     string
    20  		outKey   string
    21  		pl, in   string
    22  		expected interface{}
    23  		fail     bool
    24  	}{
    25  		{
    26  			name:     "normal",
    27  			pl:       "json(_, `str`); b64dec(`str`)",
    28  			in:       `{"str": "MTM4MzgxMzA1MTc="}`,
    29  			outKey:   "str",
    30  			expected: "13838130517",
    31  			fail:     false,
    32  		},
    33  		{
    34  			name:     "normal",
    35  			pl:       "json(_, `str`); b64dec(`str`)",
    36  			in:       `{"str": "aGVsbG8sIHdvcmxk"}`,
    37  			outKey:   "str",
    38  			expected: "hello, world",
    39  			fail:     false,
    40  		},
    41  		{
    42  			name:     "normal",
    43  			pl:       "json(_, `str`); b64dec(`str`)",
    44  			in:       `{"str": "5L2g5aW9"}`,
    45  			outKey:   "str",
    46  			expected: "你好",
    47  			fail:     false,
    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  
    63  			pt := ptinput.NewPlPoint(
    64  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    65  			errR := runScript(runner, pt)
    66  
    67  			if errR != nil {
    68  				t.Fatal(errR)
    69  			}
    70  
    71  			if tc.fail {
    72  				t.Logf("[%d]expect error: %s", idx, err)
    73  			}
    74  			v, _, _ := pt.Get(tc.outKey)
    75  			tu.Equals(t, tc.expected, v)
    76  			t.Logf("[%d] PASS", idx)
    77  		})
    78  	}
    79  }