瀏覽代碼

Add tests for JSON unmarshaling

Josh Wright 11 年之前
父節點
當前提交
c69f520a6c
共有 1 個文件被更改,包括 21 次插入0 次删除
  1. 21 0
      uuid_test.go

+ 21 - 0
uuid_test.go

@@ -137,3 +137,24 @@ func TestTimeUUID(t *testing.T) {
 		timestamp = ts
 	}
 }
+
+func TestUnmarshalJSON(t *testing.T) {
+	var withHyphens, withoutHypens, tooLong UUID
+
+	// With hyphens
+	withHyphens.UnmarshalJSON([]byte(`"486f3a88-775b-11e3-ae07-d231feb1dc81"`))
+	if withHyphens.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
+		t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", withHyphens.Time())
+	}
+
+	withoutHypens.UnmarshalJSON([]byte(`"486f3a88775b11e3ae07d231feb1dc81"`))
+	if withoutHypens.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
+		t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", withoutHypens.Time())
+	}
+
+	err := tooLong.UnmarshalJSON([]byte(`"486f3a88-775b-11e3-ae07-d231feb1dc81486f3a88"`))
+	if err == nil {
+		t.Errorf("no error for invalid JSON UUID")
+	}
+
+}