From 891b33f8888f1e976cb9bfcbc57c97d2fc4416b5 Mon Sep 17 00:00:00 2001 From: Ghost Date: Fri, 22 Mar 2024 14:11:44 +0800 Subject: [PATCH] chore: modify the code style to specify binding type --- context_test.go | 77 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/context_test.go b/context_test.go index 863074fe..68c1dd2e 100644 --- a/context_test.go +++ b/context_test.go @@ -2016,13 +2016,26 @@ func TestContextShouldBindBodyWithJSON(t *testing.T) { Foo string `json:"foo" binding:"required"` } objJSON := typeJSON{} - if tt.bindingBody != binding.JSON { - assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON)) - assert.Equal(t, typeJSON{}, objJSON) - } else { + + if tt.bindingBody == binding.JSON{ assert.NoError(t, c.ShouldBindBodyWithJSON(&objJSON)) assert.Equal(t, typeJSON{"FOO"}, objJSON) } + + if tt.bindingBody == binding.XML { + assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON)) + assert.Equal(t, typeJSON{}, objJSON) + } + + if tt.bindingBody == binding.YAML { + assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON)) + assert.Equal(t, typeJSON{}, objJSON) + } + + if tt.bindingBody == binding.TOML { + assert.Error(t, c.ShouldBindBodyWithJSON(&objJSON)) + assert.Equal(t, typeJSON{}, objJSON) + } } } @@ -2067,13 +2080,26 @@ func TestContextShouldBindBodyWithXML(t *testing.T) { Foo string `xml:"foo" binding:"required"` } objXML := typeXML{} - if tt.bindingBody != binding.XML { + + if tt.bindingBody == binding.JSON { assert.Error(t, c.ShouldBindBodyWithXML(&objXML)) assert.Equal(t, typeXML{}, objXML) - } else { + } + + if tt.bindingBody == binding.XML { assert.NoError(t, c.ShouldBindBodyWithXML(&objXML)) assert.Equal(t, typeXML{"FOO"}, objXML) } + + if tt.bindingBody == binding.YAML{ + assert.Error(t, c.ShouldBindBodyWithXML(&objXML)) + assert.Equal(t, typeXML{}, objXML) + } + + if tt.bindingBody == binding.TOML { + assert.Error(t, c.ShouldBindBodyWithXML(&objXML)) + assert.Equal(t, typeXML{}, objXML) + } } } @@ -2118,13 +2144,27 @@ func TestContextShouldBindBodyWithYAML(t *testing.T) { Foo string `yaml:"foo" binding:"required"` } objYAML := typeYAML{} - if tt.bindingBody != binding.YAML && tt.bindingBody != binding.JSON { - assert.Error(t, c.ShouldBindBodyWithYAML(&objYAML)) - assert.Equal(t, typeYAML{}, objYAML) - } else { + + // YAML belongs to a super collection of JSON, so JSON can be parsed by YAML + if tt.bindingBody == binding.JSON { assert.NoError(t, c.ShouldBindBodyWithYAML(&objYAML)) assert.Equal(t, typeYAML{"FOO"}, objYAML) } + + if tt.bindingBody == binding.XML { + assert.Error(t, c.ShouldBindBodyWithYAML(&objYAML)) + assert.Equal(t, typeYAML{}, objYAML) + } + + if tt.bindingBody == binding.YAML { + assert.NoError(t, c.ShouldBindBodyWithYAML(&objYAML)) + assert.Equal(t, typeYAML{"FOO"}, objYAML) + } + + if tt.bindingBody == binding.TOML { + assert.Error(t, c.ShouldBindBodyWithYAML(&objYAML)) + assert.Equal(t, typeYAML{}, objYAML) + } } } @@ -2169,10 +2209,23 @@ func TestContextShouldBindBodyWithTOML(t *testing.T) { Foo string `toml:"foo" binding:"required"` } objTOML := typeTOML{} - if tt.bindingBody != binding.TOML { + + if tt.bindingBody == binding.JSON { assert.Error(t, c.ShouldBindBodyWithTOML(&objTOML)) assert.Equal(t, typeTOML{}, objTOML) - } else { + } + + if tt.bindingBody == binding.XML { + assert.Error(t, c.ShouldBindBodyWithTOML(&objTOML)) + assert.Equal(t, typeTOML{}, objTOML) + } + + if tt.bindingBody == binding.YAML { + assert.Error(t, c.ShouldBindBodyWithTOML(&objTOML)) + assert.Equal(t, typeTOML{}, objTOML) + } + + if tt.bindingBody == binding.TOML { assert.NoError(t, c.ShouldBindBodyWithTOML(&objTOML)) assert.Equal(t, typeTOML{"FOO"}, objTOML) }