github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_lowercase_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 TestLowercase(t *testing.T) { 18 cases := []struct { 19 name, pl, in string 20 expected interface{} 21 fail bool 22 outkey string 23 }{ 24 { 25 name: "normal", 26 pl: `json(_, a.third) 27 lowercase(a.third) 28 `, 29 in: `{"a":{"first":2.3,"second":2,"third":"aBC","forth":true},"age":47}`, 30 expected: "abc", 31 outkey: "a.third", 32 }, 33 { 34 name: "normal", 35 pl: `json(_, a.third) 36 lowercase(a.third) 37 `, 38 in: `{"a":{"first":2.3,"second":2,"third":"aBC","forth":true,"age":"WWW"},"age":"wWW"}`, 39 expected: nil, 40 outkey: "a.age", 41 }, 42 { 43 name: "normal", 44 pl: `json(_, a.third) 45 lowercase(a.third) 46 `, 47 in: `{"a":{"first":2.3,"second":2,"third":"aBC","forth":true,"age":"WWW"},"age":"wWW"}`, 48 expected: nil, 49 outkey: "a.forth", 50 }, 51 { 52 name: "normal", 53 pl: `json(_, a.third) 54 lowercase(a.third) 55 `, 56 in: `{"a":{"first":"222SSd","second":2,"third":"aBC","forth":true,"age":"WWW"},"age":"wWW"}`, 57 expected: nil, 58 outkey: "a.first", 59 }, 60 { 61 name: "normal", 62 pl: `json(_, a.first) 63 lowercase(a.first) 64 `, 65 in: `{"a":{"first":"SSd","second":2,"third":"aBC","forth":true,"age":"WWW"},"age":"wWW"}`, 66 expected: "ssd", 67 outkey: "a.first", 68 }, 69 { 70 name: "normal", 71 pl: `json(_, age) 72 lowercase(age) 73 `, 74 in: `{"a":{"first":"SSd","second":2,"third":"aBC","forth":true,"age":"WWW"},"age":"wWW"}`, 75 expected: "www", 76 outkey: "age", 77 }, 78 } 79 for idx, tc := range cases { 80 t.Run(tc.name, func(t *testing.T) { 81 runner, err := NewTestingRunner(tc.pl) 82 if err != nil { 83 if tc.fail { 84 t.Logf("[%d]expect error: %s", idx, err) 85 } else { 86 t.Errorf("[%d] failed: %s", idx, err) 87 } 88 return 89 } 90 91 pt := ptinput.NewPlPoint( 92 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 93 errR := runScript(runner, pt) 94 95 if errR != nil { 96 t.Fatal(errR.Error()) 97 } 98 99 v, _, _ := pt.Get(tc.outkey) 100 // tu.Equals(t, nil, err) 101 tu.Equals(t, tc.expected, v) 102 103 t.Logf("[%d] PASS", idx) 104 }) 105 } 106 }