1
0
Fork 0

Added first package test

This commit is contained in:
Maxim Lebedev 2023-11-26 01:48:08 +06:00
parent aaaf52cbb8
commit d64a46a1d4
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
3 changed files with 68 additions and 0 deletions

2
go.mod
View File

@ -6,3 +6,5 @@ require (
golang.org/x/net v0.18.0
golang.org/x/text v0.14.0
)
require github.com/google/go-cmp v0.6.0 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=

64
jf2_test.go Normal file
View File

@ -0,0 +1,64 @@
package jf2_test
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"source.toby3d.me/toby3d/jf2"
)
func TestJF2_01(t *testing.T) {
t.Parallel()
type (
Author struct {
Type jf2.Type `json:"type"`
Name string `json:"name"`
URL string `json:"url"`
Photo string `json:"photo"`
}
Object struct {
Type jf2.Type `json:"type"`
Published string `json:"published"`
URL string `json:"url"`
Author Author `json:"author"`
Name string `json:"name"`
Content string `json:"content"`
Category string `json:"category"`
}
)
expect := &Object{
Type: jf2.TypeEntry,
Published: "2015-10-20T15:49:00-0700",
URL: "http://example.com/post/fsjeuu8372",
Name: "Hello World",
Content: "This is a blog post",
Category: "Posts",
Author: Author{
Type: jf2.TypeCard,
Name: "Alice",
URL: "http://alice.example.com",
Photo: "http://alice.example.com/photo.jpg",
},
}
data, err := os.ReadFile(filepath.Join(".", "testdata", "spec-ex-01.json"))
if err != nil {
t.Fatal(err)
}
actual := new(Object)
if err := json.Unmarshal(data, actual); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(actual, expect, cmp.AllowUnexported(jf2.Type{})); diff != "" {
t.Error(diff)
}
}