github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_strfmt_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 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestStrfmt(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.second, a_second) 30 json(_, a.third, a_third) 31 cast(a_second, "int") 32 json(_, a.forth, a_forth) 33 strfmt(bb, "%d %s %v", a_second, a_third, a_forth) 34 `, 35 in: `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`, 36 outKey: "bb", 37 expected: "2 abc true", 38 fail: false, 39 }, 40 41 { 42 name: "normal", 43 pl: ` 44 json(_, a.first, a_first) 45 cast(a_first, "float") 46 strfmt(bb, "%.4f", a_first) 47 `, 48 in: `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`, 49 outKey: "bb", 50 expected: "2.3000", 51 fail: false, 52 }, 53 54 { 55 name: "normal", 56 pl: ` 57 json(_, a.first, a_first) 58 cast(a_first, "float") 59 strfmt(bb, "%.4f%d", a_first, 3) 60 `, 61 in: `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`, 62 outKey: "bb", 63 expected: "2.30003", 64 fail: false, 65 }, 66 67 { 68 name: "normal", 69 pl: ` 70 json(_, a.first, a_first) 71 cast(a_first, "float") 72 strfmt(bb, "%.4f%.1f", a_first, 3.5) 73 `, 74 in: `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`, 75 outKey: "bb", 76 expected: "2.30003.5", 77 fail: false, 78 }, 79 80 { 81 name: "normal", 82 pl: ` 83 json(_, a.forth, a_forth) 84 strfmt(bb, "%v%s", a_forth, "tone") 85 `, 86 in: `{"a":{"first":2.3,"second":2,"third":"abcd","forth":true},"age":47}`, 87 outKey: "bb", 88 expected: "truetone", 89 fail: false, 90 }, 91 92 { 93 name: "not enough arg", 94 pl: ` 95 json(_, a.first, a_first) 96 cast(a_first, "float") 97 strfmt(bb) 98 `, 99 in: `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`, 100 fail: true, 101 }, 102 103 { 104 name: "incorrect arg type", 105 pl: ` 106 json(_, a.first, a_first) 107 cast(a_first, "float") 108 strfmt(bb, 1) 109 `, 110 in: `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`, 111 fail: true, 112 }, 113 } 114 115 for idx, tc := range cases { 116 t.Run(tc.name, func(t *testing.T) { 117 runner, err := NewTestingRunner(tc.pl) 118 if err != nil { 119 if tc.fail { 120 t.Logf("[%d]expect error: %s", idx, err) 121 } else { 122 t.Errorf("[%d] failed: %s", idx, err) 123 } 124 return 125 } 126 127 pt := ptinput.NewPlPoint( 128 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 129 errR := runScript(runner, pt) 130 131 if errR != nil { 132 t.Fatal(errR.Error()) 133 } 134 135 assert.Equal(t, "test", pt.GetPtName()) 136 137 if v, _, err := pt.Get(tc.outKey); err != nil { 138 if !tc.fail { 139 t.Errorf("[%d]expect error: %s", idx, err) 140 } 141 } else { 142 tu.Equals(t, tc.expected, v) 143 t.Logf("[%d] PASS", idx) 144 } 145 }) 146 } 147 }