...
1 package commontest
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 )
8
9 type ExampleStruct struct {
10 StringField string `json:"string"`
11 IntField int `json:"int"`
12 OptBoolAsInterfaceField interface{} `json:"optBool"`
13 }
14
15 const (
16 ExampleStructStringFieldName = "string"
17 ExampleStructIntFieldName = "int"
18 ExampleStructOptBoolAsInterfaceFieldName = "optBool"
19 )
20
21 var (
22 ExampleStructData = []byte(`{"string":"s","int":3,"optBool":true}`)
23 ExampleStructValue = ExampleStruct{StringField: "s", IntField: 3, OptBoolAsInterfaceField: true}
24 ExampleStructRequiredFieldNames = []string{ExampleStructStringFieldName, ExampleStructIntFieldName}
25 )
26
27 func MakeBools() []bool {
28 ret := make([]bool, 0, 100)
29 for i := 0; i < 50; i++ {
30 ret = append(ret, false, true)
31 }
32 return ret
33 }
34
35 func MakeBoolsJSON(bools []bool) []byte {
36 var buf bytes.Buffer
37 buf.WriteRune('[')
38 for i, val := range bools {
39 if i > 0 {
40 buf.WriteRune(',')
41 }
42 buf.WriteString(fmt.Sprintf("%t", val))
43 }
44 buf.WriteRune(']')
45 return buf.Bytes()
46 }
47
48 func MakeStrings() []string {
49 ret := make([]string, 0, 100)
50 for i := 0; i < 50; i++ {
51 ret = append(ret, fmt.Sprintf("value%d", i))
52 ret = append(ret, fmt.Sprintf("value\twith\n\"escaped chars\"%d", i))
53 }
54 return ret
55 }
56
57 func MakeStringsJSON(strings []string) []byte {
58 var buf bytes.Buffer
59 buf.WriteRune('[')
60 for i, val := range strings {
61 if i > 0 {
62 buf.WriteRune(',')
63 }
64 data, _ := json.Marshal(val)
65 _, _ = buf.Write(data)
66 }
67 buf.WriteRune(']')
68 return buf.Bytes()
69 }
70
71 func MakeStructs() []ExampleStruct {
72 ret := make([]ExampleStruct, 0, 100)
73 for i := 0; i < 100; i++ {
74 ret = append(ret, ExampleStruct{
75 StringField: fmt.Sprintf("string%d", i),
76 IntField: i * 10,
77 OptBoolAsInterfaceField: i%2 == 1,
78 })
79 }
80 return ret
81 }
82
83 func MakeStructsJSON(structs []ExampleStruct) []byte {
84 bytes, _ := json.Marshal(structs)
85 return bytes
86 }
87
View as plain text