github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_uppercase_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  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    14  	"github.com/GuanceCloud/cliutils/point"
    15  	tu "github.com/GuanceCloud/cliutils/testutil"
    16  )
    17  
    18  func TestUppercase(t *testing.T) {
    19  	cases := []struct {
    20  		name     string
    21  		pl, in   string
    22  		outKey   string
    23  		expected string
    24  		fail     bool
    25  	}{
    26  		{
    27  			name: "normal",
    28  			pl: `
    29  json(_, a.third)
    30  uppercase(a.third)
    31  `,
    32  			in:       `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`,
    33  			outKey:   "a.third",
    34  			expected: "ABC",
    35  			fail:     false,
    36  		},
    37  
    38  		{
    39  			name: "normal",
    40  			pl: `
    41  json(_, age)
    42  uppercase(age)
    43  `,
    44  			in:       `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`,
    45  			outKey:   "age",
    46  			expected: "47",
    47  			fail:     false,
    48  		},
    49  
    50  		{
    51  			name: "normal",
    52  			pl: `
    53  json(_, a.forth)
    54  uppercase(a.forth)
    55  `,
    56  			in:       `{"a":{"first":2.3,"second":2,"third":"abc","forth":"1a2B3c/d"},"age":47}`,
    57  			outKey:   "a.forth",
    58  			expected: strings.ToUpper("1a2B3C/d"),
    59  			fail:     false,
    60  		},
    61  
    62  		{
    63  			name: "too many args",
    64  			pl: `
    65  		json(_, a.forth)
    66  		uppercase(a.forth, "someArg")
    67  		`,
    68  			in:   `{"a":{"first":2.3,"second":2,"third":"abc","forth":"1a2B3c/d"},"age":47}`,
    69  			fail: true,
    70  		},
    71  
    72  		{
    73  			name: "invalid arg type",
    74  			pl: `
    75  		json(_, a.forth)
    76  		uppercase("hello")
    77  		`,
    78  			in:   `{"a":{"first":2.3,"second":2,"third":"abc","forth":"1a2B3c/d"},"age":47}`,
    79  			fail: true,
    80  		},
    81  	}
    82  
    83  	for idx, tc := range cases {
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			runner, err := NewTestingRunner(tc.pl)
    86  			if err != nil {
    87  				if tc.fail {
    88  					t.Logf("[%d]expect error: %s", idx, err)
    89  				} else {
    90  					t.Errorf("[%d] failed: %s", idx, err)
    91  				}
    92  				return
    93  			}
    94  
    95  			pt := ptinput.NewPlPoint(
    96  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    97  			errR := runScript(runner, pt)
    98  
    99  			if errR != nil {
   100  				t.Fatal(errR.Error())
   101  			}
   102  
   103  			if v, _, err := pt.Get(tc.outKey); err != nil {
   104  				if !tc.fail {
   105  					t.Errorf("[%d]expect error: %s", idx, err)
   106  				}
   107  			} else {
   108  				tu.Equals(t, tc.expected, v)
   109  				t.Logf("[%d] PASS", idx)
   110  			}
   111  		})
   112  	}
   113  }