瀏覽代碼

add test framework

Tao Wen 7 年之前
父節點
當前提交
a9b3f36b2f
共有 2 個文件被更改,包括 101 次插入0 次删除
  1. 7 0
      type_tests/array_test.go
  2. 94 0
      type_tests/type_test.go

+ 7 - 0
type_tests/array_test.go

@@ -0,0 +1,7 @@
+package test
+
+func init() {
+	testCases = append(testCases,
+		(*[4]bool)(nil),
+	)
+}

+ 94 - 0
type_tests/type_test.go

@@ -0,0 +1,94 @@
+package test
+
+import (
+	"testing"
+	"reflect"
+	"fmt"
+	"github.com/google/gofuzz"
+	"strings"
+	"github.com/json-iterator/go"
+	"encoding/json"
+	"bytes"
+	"github.com/davecgh/go-spew/spew"
+)
+
+var testCases []interface{}
+
+func Test(t *testing.T) {
+	for _, testCase := range testCases {
+		valType := reflect.TypeOf(testCase).Elem()
+		fz := fuzz.New().MaxDepth(10).NilChance(0.3)
+		for i := 0; i < 100; i++ {
+			beforePtrVal := reflect.New(valType)
+			beforePtr := beforePtrVal.Interface()
+			fz.Fuzz(beforePtr)
+			before := beforePtrVal.Elem().Interface()
+
+			jbStd, err := json.Marshal(before)
+			if err != nil {
+				t.Fatalf("failed to marshal with stdlib: %v", err)
+			}
+			if len(strings.TrimSpace(string(jbStd))) == 0 {
+				t.Fatal("stdlib marshal produced empty result and no error")
+			}
+			jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+			if err != nil {
+				t.Fatalf("failed to marshal with jsoniter: %v", err)
+			}
+			if len(strings.TrimSpace(string(jbIter))) == 0 {
+				t.Fatal("jsoniter marshal produced empty result and no error")
+			}
+			if string(jbStd) != string(jbIter) {
+				t.Fatalf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+					indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+			}
+
+			afterStdPtrVal := reflect.New(valType)
+			afterStdPtr := afterStdPtrVal.Interface()
+			err = json.Unmarshal(jbIter, afterStdPtr)
+			if err != nil {
+				t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n    %s",
+					err, indent(jbIter, "    "))
+			}
+			afterIterPtrVal := reflect.New(valType)
+			afterIterPtr := afterIterPtrVal.Interface()
+			err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, afterIterPtr)
+			if err != nil {
+				t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n    %s",
+					err, indent(jbIter, "    "))
+			}
+			afterStd := afterStdPtrVal.Elem().Interface()
+			afterIter := afterIterPtrVal.Elem().Interface()
+			if fingerprint(afterStd) != fingerprint(afterIter) {
+				t.Fatalf("unmarshal expected:\n    %s\ngot:\n    %s\nvia:\n    %s",
+					dump(afterStd), dump(afterIter), indent(jbIter, "    "))
+			}
+		}
+	}
+}
+
+const indentStr = ">  "
+
+func fingerprint(obj interface{}) string {
+	c := spew.ConfigState{
+		SortKeys: true,
+		SpewKeys: true,
+	}
+	return c.Sprintf("%v", obj)
+}
+
+func dump(obj interface{}) string {
+	cfg := spew.ConfigState{
+		Indent: indentStr,
+	}
+	return cfg.Sdump(obj)
+}
+
+func indent(src []byte, prefix string) string {
+	var buf bytes.Buffer
+	err := json.Indent(&buf, src, prefix, indentStr)
+	if err != nil {
+		return fmt.Sprintf("!!! %v", err)
+	}
+	return buf.String()
+}