Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
demoManito committed May 22, 2024
1 parent fb17998 commit 9a6ae78
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions encoding/form/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,58 @@ func init() {
}
}

func TestFormEncoderAndDecoder(t *testing.T) {
t.Cleanup(func() {
encoder.SetTagName(tagName)
decoder.SetTagName(tagName)
})

encoder.SetTagName(tagNameTest)
decoder.SetTagName(tagNameTest)

type testFormTagName struct {
Name string `form:"name_form" json:"name_json"`
}
v, err := encoder.Encode(&testFormTagName{
Name: "test tag name",
})
if err != nil {
t.Fatal(err)
}
jsonName := v.Get("name_json")
formName := v.Get("name_form")
switch tagNameTest {
case "json":
if jsonName != "test tag name" {
t.Errorf("got: %s", jsonName)
}
if formName != "" {
t.Errorf("want: empty, got: %s", formName)
}
case "form":
if formName != "test tag name" {
t.Errorf("got: %s", formName)
}
if jsonName != "" {
t.Errorf("want: empty, got: %s", jsonName)
}
default:
t.Fatalf("unknown tag name: %s", tagNameTest)
}

var tn *testFormTagName
err = decoder.Decode(&tn, v)
if err != nil {
t.Fatal(err)
}
if tn == nil {
t.Fatal("nil tag name")
}
if tn.Name != "test tag name" {
t.Errorf("got %s", tn.Name)
}
}

type LoginRequest struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Expand Down Expand Up @@ -229,56 +281,3 @@ func TestOptional(t *testing.T) {
t.Fatalf("got %s", query.Encode())
}
}

type testFormTagName struct {
Name string `form:"name_form" json:"name_json"`
}

func TestFormEncoderAndDecoder(t *testing.T) {
t.Cleanup(func() {
encoder.SetTagName(tagName)
decoder.SetTagName(tagName)
})

encoder.SetTagName(tagNameTest)
decoder.SetTagName(tagNameTest)

v, err := encoder.Encode(&testFormTagName{
Name: "test tag name",
})
if err != nil {
t.Fatal(err)
}
jsonName := v.Get("name_json")
formName := v.Get("name_form")
switch tagNameTest {
case "json":
if jsonName != "test tag name" {
t.Errorf("got: %s", jsonName)
}
if formName != "" {
t.Errorf("want: empty, got: %s", formName)
}
case "form":
if formName != "test tag name" {
t.Errorf("got: %s", formName)
}
if jsonName != "" {
t.Errorf("want: empty, got: %s", jsonName)
}
default:
t.Fatalf("unknown tag name: %s", tagNameTest)
}

var tn *testFormTagName
err = decoder.Decode(&tn, v)
if err != nil {
t.Fatal(err)
}
if tn == nil {
t.Fatal("nil tag name")
}
if tn.Name != "test tag name" {
t.Errorf("got %s", tn.Name)
}
}

0 comments on commit 9a6ae78

Please sign in to comment.