github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_cover_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 TestDz(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`); cover(`str`, [8, 13])", 28 in: `{"str": "13838130517"}`, 29 outKey: "str", 30 expected: "1383813****", 31 fail: false, 32 }, 33 34 { 35 name: "normal", 36 pl: "json(_, `str`) ;cover(`str`, [8, 11])", 37 in: `{"str": "13838130517"}`, 38 outKey: "str", 39 expected: "1383813****", 40 fail: false, 41 }, 42 43 { 44 name: "normal", 45 pl: "json(_, `str`); cover(`str`, [2, 4])", 46 in: `{"str": "13838130517"}`, 47 outKey: "str", 48 expected: "1***8130517", 49 fail: false, 50 }, 51 52 { 53 name: "normal", 54 pl: "json(_, `str`) ;cover(`str`, [1, 1])", 55 in: `{"str": "13838130517"}`, 56 outKey: "str", 57 expected: "*3838130517", 58 fail: false, 59 }, 60 61 { 62 name: "odd range", 63 pl: "json(_, `str`); cover(`str`, [1, 100])", 64 in: `{"str": "刘少波"}`, 65 outKey: "str", 66 expected: "***", 67 fail: false, 68 }, 69 70 { 71 name: "invalid range", 72 pl: "json(_, `str`); cover(`str`, [3, 2])", 73 in: `{"str": "刘少波"}`, 74 fail: true, 75 }, 76 77 { 78 name: "not enough args", 79 pl: "json(_, `str`) ;cover(`str`)", 80 in: `{"str": "刘少波"}`, 81 fail: true, 82 }, 83 84 { 85 name: "invalid range", 86 pl: "json(_, `str`); cover(`str`, [\"刘\", \"波\"])", 87 in: `{"str": "刘少波"}`, 88 fail: true, 89 }, 90 91 { 92 name: "normal", 93 pl: "json(_, `str`) ;cover(`str`, [1, 2])", 94 in: `{"str": 123456}`, 95 fail: true, 96 }, 97 98 { 99 name: "normal", 100 pl: "json(_, `str`) ;cover(`str`, [-1, -2])", 101 in: `{"str": 123456}`, 102 fail: true, 103 }, 104 105 { 106 name: "normal", 107 pl: "json(_, `str`); cover(`str`, [-1, -2])", 108 in: `{"str": 123456}`, 109 fail: true, 110 }, 111 112 { 113 name: "normal", 114 pl: "json(_, `str`) ; cast(`str`, \"int\"); cover(`str`, [-2, 10000])", 115 in: `{"str": 123456}`, 116 outKey: "str", 117 expected: int64(123456), 118 fail: false, 119 }, 120 } 121 122 for idx, tc := range cases { 123 t.Run(tc.name, func(t *testing.T) { 124 runner, err := NewTestingRunner(tc.pl) 125 if err != nil { 126 if tc.fail { 127 t.Logf("[%d]expect error: %s", idx, err) 128 } else { 129 t.Errorf("[%d] failed: %s", idx, err) 130 } 131 return 132 } 133 134 pt := ptinput.NewPlPoint( 135 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 136 errR := runScript(runner, pt) 137 138 if errR != nil { 139 t.Fatal(errR) 140 } 141 142 if tc.fail { 143 t.Logf("[%d]expect error: %s", idx, err) 144 } 145 v, _, _ := pt.Get(tc.outKey) 146 tu.Equals(t, tc.expected, v) 147 t.Logf("[%d] PASS", idx) 148 }) 149 } 150 }