From d2aeaa156bfc8eb43855bafbb314902cc1b5d1f0 Mon Sep 17 00:00:00 2001 From: ramya-bangera Date: Wed, 9 Oct 2024 12:53:06 +0530 Subject: [PATCH] Add unit test --- internal/cli/main.go | 2 +- internal/cli/main_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 internal/cli/main_test.go diff --git a/internal/cli/main.go b/internal/cli/main.go index 48873d4b3..29330057a 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -77,7 +77,7 @@ func EscapeIfNeeded(str string) string { // If the str is already unescaped or an error occurred, escape it return url.QueryEscape(str) } - // If the str was successfully unescaped, return it as is + // If the str was successfully unescaped and is different from the original, return the original return str } diff --git a/internal/cli/main_test.go b/internal/cli/main_test.go new file mode 100644 index 000000000..ab3c192db --- /dev/null +++ b/internal/cli/main_test.go @@ -0,0 +1,48 @@ +package cli + +import ( + "testing" +) + +func TestEscapeIfNeeded(t *testing.T) { + tests := []struct { + name string + input string + expected string + }{ + { + name: "AlreadyEscaped", + input: "hello%20world", + expected: "hello%20world", + }, + { + name: "Unescaped", + input: "hello world", + expected: "hello+world", + }, + { + name: "PartiallyEscaped", + input: "hello%20world!", + expected: "hello%20world!", + }, + { + name: "EmptyString", + input: "", + expected: "", + }, + { + name: "SpecialCharacters", + input: "hello@world.com", + expected: "hello%40world.com", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := EscapeIfNeeded(tt.input) + if result != tt.expected { + t.Errorf("EscapeIfNeeded(%q) actual = %q; want = %q", tt.input, result, tt.expected) + } + }) + } +}