github.com/enetx/g@v1.0.80/tests/string_encdec_test.go (about) 1 package g_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/enetx/g" 8 ) 9 10 func TestStringJSON(t *testing.T) { 11 // Test case 1: Encoding a struct 12 type Person struct { 13 Name string 14 Age int 15 } 16 17 person := Person{Name: "John", Age: 30} 18 expectedResult1 := g.String(`{"Name":"John","Age":30}`) 19 result1 := g.String("").Enc().JSON(person).Ok() 20 if !result1.Eq(expectedResult1) { 21 t.Errorf("Test case 1 failed: Expected result is %v, got %v", expectedResult1, result1) 22 } 23 24 // Test case 2: Encoding a map 25 data2 := map[string]any{"Name": "Alice", "Age": 25} 26 expectedResult2 := g.String(`{"Age":25,"Name":"Alice"}`) 27 result2 := g.String("").Enc().JSON(data2).Ok() 28 if !result2.Eq(expectedResult2) { 29 t.Errorf("Test case 2 failed: Expected result is %v, got %v", expectedResult2, result2) 30 } 31 32 // Test case 3: Encoding an array 33 data3 := []int{1, 2, 3} 34 expectedResult3 := g.String("[1,2,3]") 35 result3 := g.String("").Enc().JSON(data3).Ok() 36 if !result3.Eq(expectedResult3) { 37 t.Errorf("Test case 3 failed: Expected result is %v, got %v", expectedResult3, result3) 38 } 39 40 // Test case 4: Encoding a nil value 41 expectedResult4 := g.String("null") 42 result4 := g.String("").Enc().JSON(nil).Ok() 43 if !result4.Eq(expectedResult4) { 44 t.Errorf("Test case 4 failed: Expected result is %v, got %v", expectedResult4, result4) 45 } 46 } 47 48 func TestStringJSONDecode(t *testing.T) { 49 // Test case 1: Decoding a valid JSON string into a struct 50 type Person struct { 51 Name string 52 Age int 53 } 54 55 inputJSON1 := `{"Name":"John","Age":30}` 56 var person1 Person 57 expectedResult1 := g.String(inputJSON1) 58 result1 := g.String(inputJSON1).Dec().JSON(&person1).Ok() 59 if !result1.Eq(expectedResult1) { 60 t.Errorf("Test case 1 failed: Expected result is %v, got %v", expectedResult1, result1) 61 } 62 expectedPerson1 := Person{Name: "John", Age: 30} 63 if !reflect.DeepEqual(person1, expectedPerson1) { 64 t.Errorf("Test case 1 failed: Decoded struct is not equal to expected struct") 65 } 66 67 // Test case 2: Decoding a valid JSON string into a map 68 inputJSON2 := `{"Name":"Alice","Age":25}` 69 var data2 map[string]any 70 expectedResult2 := g.String(inputJSON2) 71 result2 := g.String(inputJSON2).Dec().JSON(&data2).Ok() 72 if !result2.Eq(expectedResult2) { 73 t.Errorf("Test case 2 failed: Expected result is %v, got %v", expectedResult2, result2) 74 } 75 expectedData2 := map[string]any{"Name": "Alice", "Age": float64(25)} 76 if !reflect.DeepEqual(data2, expectedData2) { 77 t.Errorf("Test case 2 failed: Decoded map is not equal to expected map") 78 } 79 } 80 81 func TestStringBase64Encode(t *testing.T) { 82 tests := []struct { 83 name string 84 e g.String 85 want g.String 86 }{ 87 {"empty", "", ""}, 88 {"hello", "hello", "aGVsbG8="}, 89 {"hello world", "hello world", "aGVsbG8gd29ybGQ="}, 90 } 91 92 for _, tt := range tests { 93 t.Run(tt.name, func(t *testing.T) { 94 if got := tt.e.Enc().Base64(); got != tt.want { 95 t.Errorf("enc.Base64Encode() = %v, want %v", got, tt.want) 96 } 97 }) 98 } 99 } 100 101 func TestStringBase64Decode(t *testing.T) { 102 tests := []struct { 103 name string 104 d g.String 105 want g.String 106 }{ 107 {"base64 decode", "aGVsbG8gd29ybGQ=", "hello world"}, 108 } 109 110 for _, tt := range tests { 111 t.Run(tt.name, func(t *testing.T) { 112 if got := tt.d.Dec().Base64().Unwrap(); got != tt.want { 113 t.Errorf("dec.Base64Decode() = %v, want %v", got, tt.want) 114 } 115 }) 116 } 117 } 118 119 func TestStringRot13Encoding(t *testing.T) { 120 // Test case 1: Encoding uppercase letters 121 inputData1 := g.NewString("HELLO") 122 expectedEncoded1 := g.NewString("URYYB") 123 result1 := inputData1.Enc().Rot13() 124 if !result1.Eq(expectedEncoded1) { 125 t.Errorf("Test case 1 failed: Expected encoded string is %s, got %s", expectedEncoded1, result1) 126 } 127 128 // Test case 2: Encoding lowercase letters 129 inputData2 := g.NewString("hello") 130 expectedEncoded2 := g.NewString("uryyb") 131 result2 := inputData2.Enc().Rot13() 132 if !result2.Eq(expectedEncoded2) { 133 t.Errorf("Test case 2 failed: Expected encoded string is %s, got %s", expectedEncoded2, result2) 134 } 135 136 // Test case 3: Encoding mixed case letters 137 inputData3 := g.NewString("Hello, World!") 138 expectedEncoded3 := g.NewString("Uryyb, Jbeyq!") 139 result3 := inputData3.Enc().Rot13() 140 if !result3.Eq(expectedEncoded3) { 141 t.Errorf("Test case 3 failed: Expected encoded string is %s, got %s", expectedEncoded3, result3) 142 } 143 144 // Test case 4: Encoding non-alphabetic characters 145 inputData4 := g.NewString("12345 !@#$") 146 expectedEncoded4 := g.NewString("12345 !@#$") 147 result4 := inputData4.Enc().Rot13() 148 if !result4.Eq(expectedEncoded4) { 149 t.Errorf("Test case 4 failed: Expected encoded string is %s, got %s", expectedEncoded4, result4) 150 } 151 } 152 153 func TestStringRot13Decoding(t *testing.T) { 154 // Test cases for Rot13 155 testCases := []struct { 156 input g.String 157 expected string 158 }{ 159 {"Uryyb", "Hello"}, 160 {"jbeyq", "world"}, 161 {"Grfg123", "Test123"}, 162 {"nopqrstuvwxyzabcdefghijklm", "abcdefghijklmnopqrstuvwxyz"}, 163 {"NOPQRSTUVWXYZABCDEFGHIJKLM", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, 164 } 165 166 for _, tc := range testCases { 167 // Apply the Rot13 transformation 168 result := tc.input.Dec().Rot13() 169 // Check if the result matches the expected output 170 if result.Std() != tc.expected { 171 t.Errorf("Rot13(%s) returned %s, expected %s", tc.input, result, tc.expected) 172 } 173 } 174 } 175 176 func TestStringXOR(t *testing.T) { 177 for range 100 { 178 input := g.NewString("").Random(g.NewInt(30).RandomRange(100)) 179 key := g.NewString("").Random(10) 180 obfuscated := input.Enc().XOR(key) 181 deobfuscated := obfuscated.Dec().XOR(key) 182 183 if input != deobfuscated { 184 t.Errorf("expected %s, but got %s", input, deobfuscated) 185 } 186 } 187 } 188 189 func TestXOR(t *testing.T) { 190 tests := []struct { 191 input string 192 key string 193 want string 194 }{ 195 {"01", "qsCDE", "AB"}, 196 {"123", "ABCDE", "ppp"}, 197 {"12345", "98765", "\x08\x0a\x04\x02\x00"}, 198 {"Hello", "wORLD", "?*> +"}, 199 // {"Hello,", "World!", "\x0f\x0a\x1e\x00\x0b\x0d"}, 200 // {"`c345", "QQ", "12345"}, 201 {"abcde", "01234", "QSQWQ"}, 202 {"lowercase", "9?' ", "UPPERCASE"}, 203 {"test", "", "test"}, 204 {"test", "test", "\x00\x00\x00\x00"}, 205 } 206 207 for _, tt := range tests { 208 got := g.NewString(tt.input).Enc().XOR(g.String(tt.key)) 209 if got != g.String(tt.want) { 210 t.Errorf("XOR(%q, %q) = %q; want %q", tt.input, tt.key, got, tt.want) 211 } 212 } 213 } 214 215 func TestGzFlateDecode(t *testing.T) { 216 testCases := []struct { 217 name string 218 input g.String 219 expected g.String 220 }{ 221 {"Valid compressed data", "8kjNycnXUXCvcstJLElVBAAAAP//AQAA//8=", "Hello, GzFlate!"}, 222 } 223 224 for _, tc := range testCases { 225 t.Run(tc.name, func(t *testing.T) { 226 result := tc.input.Dec().Base64().Unwrap().Decomp().Flate().Unwrap() 227 if result.Ne(tc.expected) { 228 t.Errorf("GzFlateDecode, expected: %s, got: %s", tc.expected, result) 229 } 230 }) 231 } 232 } 233 234 func TestGzFlateEncode(t *testing.T) { 235 testCases := []struct { 236 name string 237 input g.String 238 expected g.String 239 }{ 240 {"Empty input", "", "AAAA//8BAAD//w=="}, 241 {"Valid input", "Hello, GzFlate!", "8kjNycnXUXCvcstJLElVBAAAAP//AQAA//8="}, 242 } 243 244 for _, tc := range testCases { 245 t.Run(tc.name, func(t *testing.T) { 246 result := tc.input.Comp().Flate().Enc().Base64() 247 if result.Ne(tc.expected) { 248 t.Errorf("GzFlateEncode, expected: %s, got: %s", tc.expected, result) 249 } 250 }) 251 } 252 } 253 254 func TestStringBinaryEncodingAndDecoding(t *testing.T) { 255 // Test case 1: Encoding a string to binary 256 inputStr1 := g.NewString("Hello") 257 expectedBinary1 := g.NewString("0100100001100101011011000110110001101111") 258 result1 := inputStr1.Enc().Binary() 259 if !result1.Eq(expectedBinary1) { 260 t.Errorf("Test case 1 failed: Expected binary string is %s, got %s", expectedBinary1, result1) 261 } 262 263 // Test case 2: Decoding a binary string back to the original string 264 inputBinary2 := g.NewString("0100100001100101011011000110110001101111") 265 expectedStr2 := g.NewString("Hello") 266 result2 := inputBinary2.Dec().Binary() 267 if result2.IsErr() { 268 t.Errorf("Test case 2 failed: Error occurred during decoding: %v", result2.Err()) 269 } else if result2.Ok().Ne(expectedStr2) { 270 t.Errorf("Test case 2 failed: Expected decoded string is %s, got %s", expectedStr2, result2.Ok()) 271 } 272 } 273 274 func TestXMLEncodingAndDecoding(t *testing.T) { 275 // Define a struct to represent data for testing XML encoding and decoding 276 type Person struct { 277 Name string `xml:"name"` 278 Age int `xml:"age"` 279 City string `xml:"city"` 280 Email string `xml:"email"` 281 } 282 283 // Test case 1: Encoding data to XML 284 inputData1 := Person{Name: "John", Age: 30, City: "New York", Email: "john@example.com"} 285 expectedXML1 := g.NewString( 286 "<Person><name>John</name><age>30</age><city>New York</city><email>john@example.com</email></Person>", 287 ) 288 result1 := g.NewString("").Enc().XML(inputData1) 289 if !result1.Ok().Eq(expectedXML1) { 290 t.Errorf("Test case 1 failed: Expected XML is %s, got %s", expectedXML1, result1.Ok()) 291 } 292 293 // Test case 2: Decoding XML back to data 294 xmlData2 := g.NewString( 295 "<Person><name>Alice</name><age>25</age><city>London</city><email>alice@example.com</email></Person>", 296 ) 297 var decodedData2 Person 298 expectedData2 := Person{Name: "Alice", Age: 25, City: "London", Email: "alice@example.com"} 299 result2 := xmlData2.Dec().XML(&decodedData2) 300 if result2.IsErr() { 301 t.Errorf("Test case 2 failed: Error occurred during XML decoding: %v", result2.Err()) 302 } else if decodedData2 != expectedData2 { 303 t.Errorf("Test case 2 failed: Expected decoded data is %+v, got %+v", expectedData2, decodedData2) 304 } 305 } 306 307 func TestStringHTMLEncodingAndDecoding(t *testing.T) { 308 // Test case 1: Encoding HTML 309 inputData1 := g.NewString("<p>Hello, <b>World</b>!</p>") 310 expectedEncoded1 := g.NewString("<p>Hello, <b>World</b>!</p>") 311 result1 := inputData1.Enc().HTML() 312 if !result1.Eq(expectedEncoded1) { 313 t.Errorf("Test case 1 failed: Expected encoded HTML is %s, got %s", expectedEncoded1, result1) 314 } 315 316 // Test case 2: Decoding HTML 317 htmlData2 := g.NewString("<a href="https://example.com">Link</a>") 318 expectedDecoded2 := g.NewString("<a href=\"https://example.com\">Link</a>") 319 result2 := htmlData2.Dec().HTML() 320 if !result2.Eq(expectedDecoded2) { 321 t.Errorf("Test case 2 failed: Expected decoded HTML is %s, got %s", expectedDecoded2, result2) 322 } 323 } 324 325 func TestStringDecBase64_Success(t *testing.T) { 326 // Input string encoded in Base64 327 encodedStr := "SGVsbG8gV29ybGQh" 328 329 // Create a dec instance wrapping the encoded string 330 dec := g.NewString(encodedStr).Dec() 331 332 // Decode the string using Base64 333 decodedResult := dec.Base64() 334 335 // Check if the result is successful 336 if decodedResult.IsErr() { 337 t.Errorf( 338 "TestDec_Base64_Success: Expected decoding to be successful, but got an error: %v", 339 decodedResult.Err(), 340 ) 341 } 342 343 // Check if the decoded string is correct 344 expectedDecodedStr := "Hello World!" 345 if decodedResult.Ok().Std() != expectedDecodedStr { 346 t.Errorf( 347 "TestDec_Base64_Success: Expected decoded string %s, got %s", 348 expectedDecodedStr, 349 decodedResult.Ok().Std(), 350 ) 351 } 352 } 353 354 func TestDecStringBase64Failure(t *testing.T) { 355 // Invalid Base64 encoded string (contains invalid character) 356 invalidEncodedStr := "SGVsbG8gV29ybGQh===" 357 358 // Create a dec instance wrapping the invalid encoded string 359 dec := g.NewString(invalidEncodedStr).Dec() 360 361 // Decode the string using Base64 362 decodedResult := dec.Base64() 363 364 // Check if the result is an error 365 if !decodedResult.IsErr() { 366 t.Errorf( 367 "TestDec_Base64_Failure: Expected decoding to fail, but got a successful result: %s", 368 decodedResult.Ok().Std(), 369 ) 370 } 371 } 372 373 func TestStringHexEncode(t *testing.T) { 374 // Test cases for Hex 375 testCases := []struct { 376 input g.String 377 expected g.String 378 }{ 379 {"Hello", "48656c6c6f"}, 380 {"world", "776f726c64"}, 381 {"Test123", "54657374313233"}, 382 {"", ""}, 383 } 384 385 for _, tc := range testCases { 386 // Encode the string to hex using your package method 387 result := tc.input.Enc().Hex() 388 // Check if the result matches the expected output 389 if result != tc.expected { 390 t.Errorf("Hex(%s) returned %s, expected %s", tc.input, result, tc.expected) 391 } 392 } 393 } 394 395 func TestStringHexDec(t *testing.T) { 396 // Test cases for Hex decoding 397 testCases := []struct { 398 input g.String 399 expected g.String 400 err error 401 }{ 402 {"48656c6c6f20576f726c64", "Hello World", nil}, 403 {"74657374", "test", nil}, 404 {"", "", nil}, // Empty input should return empty output 405 } 406 407 for _, tc := range testCases { 408 result := tc.input.Dec().Hex().Unwrap() 409 if result != tc.expected { 410 t.Errorf("Hex(%s) returned %s, expected %s", tc.input, result, tc.expected) 411 } 412 } 413 } 414 415 func TestStringOctalEnc(t *testing.T) { 416 // Test cases 417 testCases := []struct { 418 input g.String 419 expected g.String 420 }{ 421 {"hello", "150 145 154 154 157"}, 422 {"world", "167 157 162 154 144"}, 423 {"", ""}, 424 {"123", "61 62 63"}, 425 } 426 427 // Test each case 428 for _, tc := range testCases { 429 result := tc.input.Enc().Octal() 430 if result != tc.expected { 431 t.Errorf("Octal encoding is incorrect %s, exceted %s", result, tc.expected) 432 } 433 } 434 } 435 436 func TestStringOctalDec(t *testing.T) { 437 // Test cases 438 testCases := []struct { 439 input g.String 440 expected g.String 441 }{ 442 {"150 145 154 154 157", "hello"}, 443 {"167 157 162 154 144", "world"}, 444 {"61 62 63", "123"}, 445 } 446 447 // Test each case 448 for _, tc := range testCases { 449 result := tc.input.Dec().Octal() 450 451 // Assert the result 452 if result.IsErr() { 453 t.Errorf("Error occurred during Octal decoding: %s", result.Err()) 454 } 455 456 if result.Ok() != tc.expected { 457 t.Errorf("Octal decoding is incorrect %s, exceted %s", result.Ok(), tc.expected) 458 } 459 } 460 } 461 462 func TestURLEncode(t *testing.T) { 463 testCases := []struct { 464 input g.String 465 safe g.String 466 expected g.String 467 }{ 468 { 469 input: "https://www.test.com/?query=test¶m=value", 470 safe: "", 471 expected: "https%3A%2F%2Fwww.test.com%2F%3Fquery%3Dtest%26param%3Dvalue", 472 }, 473 { 474 input: "https://www.test.com/?query=test¶m=value", 475 safe: ":", 476 expected: "https:%2F%2Fwww.test.com%2F%3Fquery%3Dtest%26param%3Dvalue", 477 }, 478 { 479 input: "https://www.test.com/?query=test¶m=value", 480 safe: ":/&", 481 expected: "https://www.test.com/%3Fquery%3Dtest¶m%3Dvalue", 482 }, 483 { 484 input: "https://www.test.com/?query=test¶m=value", 485 safe: ":/&?", 486 expected: "https://www.test.com/?query%3Dtest¶m%3Dvalue", 487 }, 488 { 489 input: "https://www.test.com/?query=test¶m=value", 490 safe: ":/&?=", 491 expected: "https://www.test.com/?query=test¶m=value", 492 }, 493 } 494 495 for _, tc := range testCases { 496 encoded := tc.input.Enc().URL(tc.safe) 497 if encoded != tc.expected { 498 t.Errorf( 499 "For input: %s and safe: %s, expected: %s, but got: %s", 500 tc.input, 501 tc.safe, 502 tc.expected, 503 encoded.Std(), 504 ) 505 } 506 } 507 } 508 509 func TestURLDecode(t *testing.T) { 510 tests := []struct { 511 input g.String 512 expected g.String 513 }{ 514 { 515 input: "hello+world", 516 expected: "hello world", 517 }, 518 { 519 input: "hello%20world", 520 expected: "hello world", 521 }, 522 { 523 input: "a%2Bb%3Dc%2Fd", 524 expected: "a+b=c/d", 525 }, 526 { 527 input: "foo%3Fbar%3Dbaz%26abc%3D123", 528 expected: "foo?bar=baz&abc=123", 529 }, 530 { 531 input: "", 532 expected: "", 533 }, 534 } 535 536 for _, test := range tests { 537 actual := test.input.Dec().URL().Unwrap() 538 if actual != test.expected { 539 t.Errorf("UnEscape(%s): expected %s, but got %s", test.input, test.expected, actual) 540 } 541 } 542 }