Skip to content

Commit

Permalink
Move examples to dynamic_test package
Browse files Browse the repository at this point in the history
  • Loading branch information
nqv committed Oct 27, 2016
1 parent 2ad635f commit bd814ab
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 149 deletions.
File renamed without changes.
149 changes: 0 additions & 149 deletions dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package dynamic

import (
"encoding/json"
"fmt"
"testing"
)

var _ (json.Marshaler) = (*Type)(nil)
var _ (json.Unmarshaler) = (*Type)(nil)

var _ (json.Unmarshaler) = (*Data)(nil)

type a struct {
Expand Down Expand Up @@ -43,101 +41,6 @@ func TestNameDuplicated(t *testing.T) {
Register("a", createB)
}

func ExampleType_unmarshal() {
var test Type
data := `
{
"type": "a",
"A": "This is A"
}`

if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%#v\n", test.Value())
// Output:
// &dynamic.a{A:"This is A"}
}

func ExampleType_unmarshalStruct() {
var test struct {
X Type
}
data := `
{
"X": {
"type": "a",
"A": "This is A"
}
}`
if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%#v\n", test.X.Value())
// Output:
// &dynamic.a{A:"This is A"}
}

func ExampleType_unmarshalList() {
var test []Type
data := `
[
{
"type": "a",
"A": "This is A",
"C": "This is C"
},
{
"Type": "b",
"A": "This is A",
"B": "This is B"
}
]`

if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
for _, t := range test {
fmt.Printf("%#v\n", t.Value())
}
// Output:
// &dynamic.a{A:"This is A"}
// &dynamic.b{B:"This is B"}
}

func ExampleType_unmarshalListInStruct() {
var test struct {
X []Type
}
data := `
{
"X": [
{
"type": "a",
"A": "This is A"
},
{
"Type": "b",
"A": "This is A",
"B": "This is B"
}
]
}`
if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
for _, t := range test.X {
fmt.Printf("%#v\n", t.Value())
}
// Output:
// &dynamic.a{A:"This is A"}
// &dynamic.b{B:"This is B"}
}

func TestUnmarshalUnsupportedType(t *testing.T) {
var test Type
data := `
Expand Down Expand Up @@ -207,55 +110,3 @@ func TestUnmarshalEmbeddedStruct(t *testing.T) {
}
}
}

func ExampleType_marshal() {
var test Type

test.SetValue(&a{"This is A"})
data, err := json.Marshal(&test)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%s\n", data)
// Output:
// {"A":"This is A"}
}

func ExampleType_marshalStruct() {
var test struct {
X Type
Y Type
}

test.X.SetValue(&a{"This is A"})
test.Y.SetValue(&b{"This is B"})
data, err := json.Marshal(&test)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%s\n", data)
// Output:
// {"X":{"A":"This is A"},"Y":{"B":"This is B"}}
}

func ExampleType_marshalListInStruct() {
var test struct {
X []Type
}

test.X = []Type{
Type{&a{"This is A"}},
Type{&b{"This is B"}},
}

data, err := json.Marshal(&test)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%s\n", data)
// Output:
// {"X":[{"A":"This is A"},{"B":"This is B"}]}
}
165 changes: 165 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package dynamic_test

import (
"encoding/json"
"fmt"

"github.com/goburrow/dynamic"
)

type Gopher struct {
G string
}

type Breaver struct {
B string
}

func init() {
dynamic.Register("go", func() interface{} { return &Gopher{} })
dynamic.Register("br", func() interface{} { return &Breaver{} })
}

func ExampleType_unmarshal() {
var test dynamic.Type
data := `
{
"type": "go",
"G": "gopher"
}`

if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%#v\n", test.Value())
// Output:
// &dynamic_test.Gopher{G:"gopher"}
}

func ExampleType_unmarshalStruct() {
var test struct {
X dynamic.Type
}
data := `
{
"X": {
"type": "go",
"G": "gopher"
}
}`
if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%#v\n", test.X.Value())
// Output:
// &dynamic_test.Gopher{G:"gopher"}
}

func ExampleType_unmarshalList() {
var test []dynamic.Type
data := `
[
{
"type": "go",
"G": "gopher"
},
{
"Type": "br",
"B": "breaver"
}
]`

if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
for _, t := range test {
fmt.Printf("%#v\n", t.Value())
}
// Output:
// &dynamic_test.Gopher{G:"gopher"}
// &dynamic_test.Breaver{B:"breaver"}
}

func ExampleType_unmarshalListInStruct() {
var test struct {
X []dynamic.Type
}
data := `
{
"X": [
{
"Type": "go",
"G": "gopher"
},
{
"type": "br",
"B": "breaver"
}
]
}`
if err := json.Unmarshal([]byte(data), &test); err != nil {
fmt.Printf("%v\n", err)
return
}
for _, t := range test.X {
fmt.Printf("%#v\n", t.Value())
}
// Output:
// &dynamic_test.Gopher{G:"gopher"}
// &dynamic_test.Breaver{B:"breaver"}
}

func ExampleType_marshal() {
var test dynamic.Type

test.SetValue(&Gopher{"gopher"})
data, err := json.Marshal(&test)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%s\n", data)
// Output:
// {"G":"gopher"}
}

func ExampleType_marshalStruct() {
var test struct {
X dynamic.Type
Y dynamic.Type
}

test.X.SetValue(&Gopher{"gopher"})
test.Y.SetValue(&Breaver{"breaver"})
data, err := json.Marshal(&test)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%s\n", data)
// Output:
// {"X":{"G":"gopher"},"Y":{"B":"breaver"}}
}

func ExampleType_marshalListInStruct() {
var test struct {
X []dynamic.Type
}

test.X = []dynamic.Type{
dynamic.Type{&Gopher{"gopher"}},
dynamic.Type{&Breaver{"breaver"}},
}

data, err := json.Marshal(&test)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("%s\n", data)
// Output:
// {"X":[{"G":"gopher"},{"B":"breaver"}]}
}

0 comments on commit bd814ab

Please sign in to comment.