-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbug.st_test.go
55 lines (50 loc) · 1.47 KB
/
bug.st_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Written by Cristian Maglie.
package json_test
import (
"testing"
"github.com/stretchr/testify/require"
"go.bug.st/json"
)
// Test the extra features provided by the go.bug.st/json package
func TestRequiredField(t *testing.T) {
type s struct {
Required string `json:"req,required"`
Optional string `json:"opt"`
OmitEmpty string `json:"omit,omitempty"`
}
{
var x s
err := json.Unmarshal([]byte(`{}`), &x)
require.EqualError(t, err, "json: undefined required field req into json_test.s")
}
{
var x s
err := json.Unmarshal([]byte(`{"req":"hello"}`), &x)
require.NoError(t, err)
require.Equal(t, s{Required: "hello"}, x)
}
{
var x s
err := json.Unmarshal([]byte(`{"opt":"hello"}`), &x)
require.EqualError(t, err, "json: undefined required field req into json_test.s")
}
{
var x s
err := json.Unmarshal([]byte(`{"omit":"hello"}`), &x)
require.EqualError(t, err, "json: undefined required field req into json_test.s")
}
{
var x s
err := json.Unmarshal([]byte(`{"opt":"hello","omit":"hello"}`), &x)
require.EqualError(t, err, "json: undefined required field req into json_test.s")
}
{
var x s
err := json.Unmarshal([]byte(`{"req":"HI!","opt":"hello","omit":"hello"}`), &x)
require.NoError(t, err)
require.Equal(t, s{Required: "HI!", Optional: "hello", OmitEmpty: "hello"}, x)
}
}