-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape using json package. Add tests.
- Loading branch information
1 parent
80808e6
commit 6ca8c4f
Showing
2 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package git | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestEscapeName1(t *testing.T) { | ||
type args struct { | ||
str string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Escape Apostrophe", | ||
args: args{ | ||
str: `John O'Keefe`, | ||
}, | ||
want: `John O'Keefe`, | ||
}, | ||
{ | ||
name: "Escape backslash", | ||
args: args{ | ||
str: `MYDOMAIN\USER`, | ||
}, | ||
want: `MYDOMAIN\\USER`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := EscapeName(tt.args.str) | ||
var js json.RawMessage | ||
jsonVal := fmt.Sprintf(`{"name": "%s"}`, got) | ||
err = json.Unmarshal([]byte(jsonVal), &js) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("EscapeName() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("EscapeName() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |