Refactor codec test

This commit is contained in:
2023-02-07 05:31:13 +00:00
parent 77bf3a4a92
commit 3507805c29

View File

@@ -4,32 +4,25 @@ import "testing"
// TestEncrypt tests the Encrypt and Decrypt function.
func TestCryptDecrypt(t *testing.T) {
// Case is a struct to store test cases.
type Case struct {
name string
key int
A string
B string
}
// Run the test cases.
for _, tt := range []Case{
{"Test encrypt-decrypt: simple text",
5, "clark", "hqfwp"},
{"Test encrypt-decrypt: text with shifted outbounds chars",
3, "cherry blossom", "fkhuub eorvvrp"},
{"Test encrypt-decrypt: text with shifted outbounds chars and non-letter chars",
3, "cherry123 blossom!", "fkhuub123 eorvvrp!"},
for _, tc := range []struct {
name, A, B string
key int
}{
{"Test encrypt-decrypt: Simple text",
"clark", "hqfwp", 5},
{"Test encrypt-decrypt: Text with shifted outbounds chars",
"cherry blossom", "fkhuub eorvvrp", 3},
{"Test encrypt-decrypt: Text with shifted outbounds chars and non-letter chars",
"cherry123 blossom!", "fkhuub123 eorvvrp!", 3},
} {
t.Run(tt.name, func(t *testing.T) {
enc := Encrypt(tt.A, tt.key)
if enc != tt.B {
t.Errorf("Encrypt() = %v, want %v", enc, tt.B)
t.Run(tc.name, func(t *testing.T) {
if enc := Encrypt(tc.A, tc.key); enc != tc.B {
t.Errorf("Encrypt() = %v, want %v", enc, tc.B)
}
dec := Decrypt(enc, tt.key)
if dec != tt.A {
t.Errorf("Decrypt() = %v, want %v", dec, tt.A)
if dec := Decrypt(tc.B, tc.key); dec != tc.A {
t.Errorf("Decrypt() = %v, want %v", dec, tc.A)
}
})
}