From 31e0226908ff016d8ba91a190809827975248941 Mon Sep 17 00:00:00 2001 From: Aleksandr Petrukhin Date: Tue, 29 May 2018 16:32:28 +0000 Subject: [PATCH] [Tests] add tests for disallow_unknown_fields --- .gitignore | 1 + Makefile | 1 + README.md | 2 ++ tests/basic_test.go | 8 ++++++++ tests/disallow_unknown.go | 8 ++++++++ 5 files changed, 20 insertions(+) create mode 100644 tests/disallow_unknown.go diff --git a/.gitignore b/.gitignore index db8c66e..26156fb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *_easyjson.go *.iml .idea +*.swp diff --git a/Makefile b/Makefile index 49c80f3..7717c1e 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ generate: root build .root/bin/easyjson .root/src/$(PKG)/tests/named_type.go .root/bin/easyjson .root/src/$(PKG)/tests/custom_map_key_type.go .root/bin/easyjson .root/src/$(PKG)/tests/embedded_type.go + .root/bin/easyjson -disallow_unknown_fields .root/src/$(PKG)/tests/disallow_unknown.go test: generate root go test \ diff --git a/README.md b/README.md index 9366e3f..b59d3ad 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ Usage of easyjson: use lowerCamelCase instead of CamelCase by default -stubs only generate stubs for marshaler/unmarshaler funcs + -disallow_unknown_fields + return error if some unknown field in json occured ``` Using `-all` will generate marshalers/unmarshalers for all Go structs in the diff --git a/tests/basic_test.go b/tests/basic_test.go index 28f0fdf..ab166f3 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -232,3 +232,11 @@ func TestUnmarshalStructWithEmbeddedPtrStruct(t *testing.T) { t.Errorf("easyjson.Unmarshal() = %#v; want %#v", s, structWithInterfaceValueFilled) } } + +func TestDisallowUnknown(t *testing.T) { + var d DisallowUnknown + err := easyjson.Unmarshal([]byte(disallowUnknownString), &d) + if err == nil { + t.Error("want error, got nil") + } +} diff --git a/tests/disallow_unknown.go b/tests/disallow_unknown.go new file mode 100644 index 0000000..5b884c6 --- /dev/null +++ b/tests/disallow_unknown.go @@ -0,0 +1,8 @@ +package tests + +//easyjson:json +type DisallowUnknown struct { + FieldOne string `json:"field_one"` +} + +var disallowUnknownString = `{"field_one": "one", "field_two": "two"}`