-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6498e29
commit c0f6cc3
Showing
6 changed files
with
141 additions
and
139 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,60 @@ | ||
package pseudo | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIntBoolJSON_MarshalJSON(t *testing.T) { | ||
type TestCase struct { | ||
name string | ||
i IntBoolJSON | ||
want string | ||
wantErr bool | ||
} | ||
|
||
cases := []TestCase{ | ||
{name: "true", i: IntBoolJSON(true), want: "1", wantErr: false}, | ||
{name: "false", i: IntBoolJSON(false), want: "0", wantErr: false}, | ||
{name: "native true", i: true, want: "1", wantErr: false}, | ||
{name: "native false", i: false, want: "0", wantErr: false}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
got, err := c.i.MarshalJSON() | ||
assert.NoError(t, err, "it should not error") | ||
assert.Equal(t, got, []byte(c.want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestIntBoolJSON_UnmarshalJSON(t *testing.T) { | ||
type TestCase struct { | ||
name string | ||
json string | ||
want IntBoolJSON | ||
wantErr bool | ||
} | ||
|
||
cases := []TestCase{ | ||
{name: "number 1", json: "1", want: true, wantErr: false}, | ||
{name: "number 0", json: "0", want: false, wantErr: false}, | ||
{name: "non-number", json: `"foo"`, want: false, wantErr: true}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
var i IntBoolJSON | ||
err := i.UnmarshalJSON([]byte(c.json)) | ||
|
||
if c.wantErr { | ||
assert.Error(t, err, "it should error", c.json) | ||
} else { | ||
assert.NoError(t, err, "it should not error", c.json) | ||
assert.Equal(t, c.want, i) | ||
} | ||
|
||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,62 @@ | ||
package pseudo | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestMessageOverrideJSON_MarshalJSON(t *testing.T) { | ||
type TestCase struct { | ||
name string | ||
m MessageOverrideJSON[int] | ||
want string | ||
} | ||
|
||
value := 5 | ||
|
||
cases := []TestCase{ | ||
{name: "has Value has Message", m: MessageOverrideJSON[int]{Value: &value, Message: "Test"}, want: `"Test"`}, | ||
{name: "has Value no Message", m: MessageOverrideJSON[int]{Value: &value, Message: ""}, want: `5`}, | ||
{name: "no Value and Message", m: MessageOverrideJSON[int]{Value: nil, Message: ""}, want: `null`}, | ||
{name: "no Value has Message", m: MessageOverrideJSON[int]{Value: nil, Message: "Test"}, want: `"Test"`}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
got, err := c.m.MarshalJSON() | ||
assert.NoError(t, err, "it should not error") | ||
assert.Equal(t, []byte(c.want), got) | ||
}) | ||
} | ||
} | ||
|
||
func TestMessageOverrideJSON_UnmarshalJSON(t *testing.T) { | ||
type TestCase struct { | ||
name string | ||
json string | ||
want MessageOverrideJSON[int] | ||
wantErr bool | ||
} | ||
|
||
value := 5 | ||
|
||
cases := []TestCase{ | ||
{name: "string message", json: `"here is a message"`, want: MessageOverrideJSON[int]{Message: "here is a message"}, wantErr: false}, | ||
{name: "expected value", json: `5`, want: MessageOverrideJSON[int]{Value: &value, Message: ""}, wantErr: false}, | ||
{name: "unexpected value", json: `{"not an int": true}`, wantErr: true}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
var got MessageOverrideJSON[int] | ||
err := got.UnmarshalJSON([]byte(c.json)) | ||
|
||
if c.wantErr { | ||
assert.Error(t, err, "it should return an error") | ||
} else { | ||
assert.NoError(t, err, "it should not return an error") | ||
assert.Equal(t, got, c.want) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.