github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/util/string_test.go (about) 1 package util 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 type UtilTestParameter struct { 10 Input interface{} 11 Expected interface{} 12 } 13 14 var padLeftParameters = []UtilTestParameter{ 15 {Input: "123", Expected: "0000000123"}, 16 {Input: "1234567890", Expected: "1234567890"}, 17 } 18 19 var digitParameters = []UtilTestParameter{ 20 {Input: "0123456789", Expected: true}, 21 {Input: " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,/()*&=-+!:?<>.;_\"", Expected: false}, 22 } 23 24 var basicCharacter = []UtilTestParameter{ 25 {Input: " 0123456789,/()*&=-+!:?<>.;_\"", Expected: false}, 26 {Input: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", Expected: true}, 27 } 28 29 var caixaSpecialCharacter = []UtilTestParameter{ 30 {Input: " ,/()*=-+!:?.;_'", Expected: true}, 31 {Input: "01223456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%¨{}[]^~Çç\"&<>\\", Expected: false}, 32 } 33 34 func TestPadLeft(t *testing.T) { 35 length := 10 36 paddingCaracter := "0" 37 38 for _, fact := range padLeftParameters { 39 result := PadLeft(fact.Input.(string), paddingCaracter, uint(length)) 40 assert.Equal(t, fact.Expected, result, "O numero deve ser ajustado corretamente") 41 } 42 } 43 44 func TestIsDigit(t *testing.T) { 45 for _, fact := range digitParameters { 46 s := fact.Input.(string) 47 for _, c := range s { 48 result := IsDigit(c) 49 assert.Equal(t, fact.Expected, result, "A verificação de dígito deve ocorrer corretamente") 50 } 51 } 52 } 53 54 func TestIsBasicCharacter(t *testing.T) { 55 for _, fact := range basicCharacter { 56 s := fact.Input.(string) 57 for _, c := range s { 58 result := IsBasicCharacter(c) 59 assert.Equal(t, fact.Expected, result, "A verificação de caracter deve ocorrer corretamente") 60 } 61 } 62 } 63 64 func TestIsSpecialCharacterCaixa(t *testing.T) { 65 for _, fact := range caixaSpecialCharacter { 66 s := fact.Input.(string) 67 for _, c := range s { 68 result := IsCaixaSpecialCharacter(c) 69 assert.Equal(t, fact.Expected, result, "A verificação de caracter deve ocorrer corretamente") 70 } 71 } 72 } 73 74 func TestStringfy(t *testing.T) { 75 expected := `{"Input":"Texto","Expected":1234}` 76 77 input := UtilTestParameter{ 78 Input: "Texto", 79 Expected: 1234, 80 } 81 82 result := Stringify(input) 83 84 assert.Equal(t, expected, result) 85 } 86 87 func TestParseJson(t *testing.T) { 88 input := `{"Input":"Texto","Expected":1234.0}` 89 90 result := ParseJSON(input, new(UtilTestParameter)).(*UtilTestParameter) 91 92 assert.Equal(t, "Texto", result.Input) 93 assert.Equal(t, 1234.0, result.Expected) 94 } 95 96 func TestMinifyString(t *testing.T) { 97 input := `<html> 98 <body> 99 <p><b>Get My PDF</b></p> 100 </body> 101 </html>` 102 103 expected := `<html><body><p><b>Get My PDF</b></p></body></html>` 104 105 result := MinifyString(input, "text/html") 106 107 assert.Equal(t, expected, result) 108 109 input = `{ 110 "Input":"Texto", 111 "Expected":1234.0 112 }` 113 expected = `{"Input":"Texto","Expected":1234.0}` 114 115 result = MinifyString(input, "application/json") 116 117 assert.Equal(t, expected, result) 118 119 } 120 121 func TestSanitizeBody(t *testing.T) { 122 input := `{ 123 "bankNumber": 174, 124 "authentication": { 125 "Username": "altsa", 126 "Password": "altsa" 127 }, 128 "agreement": { 129 "agreementNumber": 267, 130 "wallet": 36, 131 "agency": "00000" 132 }, 133 "title": { 134 "expireDate": "2050-12-30", 135 "amountInCents": 200, 136 "ourNumber": 1, 137 "instructions": "Não receber após a data de vencimento.", 138 "documentNumber": "1234567890" 139 }, 140 "recipient": { 141 "name": "Empresa - Boletos", 142 "document": { 143 "type": "CNPJ", 144 "number": "29799428000128" 145 }, 146 "address": { 147 "street": "Avenida Miguel Estefno, 2394", 148 "complement": "Água Funda", 149 "zipCode": "04301-002", 150 "city": "São Paulo", 151 "stateCode": "SP" 152 } 153 }, 154 "buyer": { 155 "name": "Usuario \t Teste", 156 "email": "p@p.com", 157 "document": { 158 "type": "CNPJ", 159 "number": "29.799.428/0001-28" 160 }, 161 "address": { 162 "street": "Rua \t Teste", 163 "number": "2", 164 "complement": "SALA 1", 165 "zipCode": "20931-001", 166 "district": "Centro", 167 "city": "Rio de Janeiro", 168 "stateCode": "RJ" 169 } 170 } 171 }` 172 173 result := SanitizeBody(input) 174 assert.NotContains(t, result, "\t") 175 }