github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_append_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 TestAppend(t *testing.T) {
    18  	cases := []struct {
    19  		name, pl, in string
    20  		expected     interface{}
    21  		fail         bool
    22  		outkey       string
    23  	}{
    24  		{
    25  			name: "append a float number",
    26  			pl: `abc = ["1", "2"]
    27  			abc = append(abc, 5.1)
    28  			add_key(arr, abc)`,
    29  			in:       `test`,
    30  			expected: "[\"1\",\"2\",5.1]",
    31  			outkey:   "arr",
    32  		},
    33  		{
    34  			name: "append a string",
    35  			pl: `abc = ["hello"]
    36  			abc = append(abc, "world")
    37  			add_key(arr, abc)`,
    38  			in:       `test`,
    39  			expected: "[\"hello\",\"world\"]",
    40  			outkey:   "arr",
    41  		},
    42  		{
    43  			name: "append a string",
    44  			pl: `abc = [1, 2]
    45  			abc = append(abc, "3")
    46  			add_key(arr, abc)`,
    47  			in:       `test`,
    48  			expected: "[1,2,\"3\"]",
    49  			outkey:   "arr",
    50  		},
    51  		{
    52  			name: "append by Identifier",
    53  			pl: `a = [1, 2]
    54  			b = append(a, 3)
    55  			add_key(arr, b)`,
    56  			in:       `test`,
    57  			expected: "[1,2,3]",
    58  			outkey:   "arr",
    59  		},
    60  		{
    61  			name: "append an array",
    62  			pl: `a = [1, 2]
    63  			b = [3, 4]
    64  			c = append(a, b)
    65  			add_key(arr, c)`,
    66  			in:       `test`,
    67  			expected: "[1,2,[3,4]]",
    68  			outkey:   "arr",
    69  		},
    70  		{
    71  			name: "append but not assign",
    72  			pl: `a = [1, 2]
    73  			b = 3
    74  			append(a, b)
    75  			add_key(arr, a)`,
    76  			in:       `test`,
    77  			expected: "[1,2]",
    78  			outkey:   "arr",
    79  		},
    80  	}
    81  
    82  	for idx, tc := range cases {
    83  		t.Run(tc.name, func(t *testing.T) {
    84  			runner, err := NewTestingRunner(tc.pl)
    85  			if err != nil {
    86  				if tc.fail {
    87  					t.Logf("[%d]expect error: %s", idx, err)
    88  				} else {
    89  					t.Errorf("[%d] failed: %s", idx, err)
    90  				}
    91  				return
    92  			}
    93  			pt := ptinput.NewPlPoint(
    94  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    95  			errR := runScript(runner, pt)
    96  			if errR != nil {
    97  				t.Fatal(*errR)
    98  			}
    99  			v, _, err := pt.Get(tc.outkey)
   100  			assert.NoError(t, err)
   101  			assert.Equal(t, tc.expected, v)
   102  			t.Logf("[%d] PASS", idx)
   103  		})
   104  	}
   105  }