~chrisppy/go-barefeed

91a25b6475769cec59251a06e6907645a9fd0a5d — Chris Palmer 9 months ago 64bb083
Add tests
9 files changed, 159 insertions(+), 68 deletions(-)

M barefeed.go
A barefeed_test.go
D feed.go
M go.mod
M go.sum
D item.go
D message.go
A msg.go
M time.go
M barefeed.go => barefeed.go +3 -17
@@ 12,20 12,6 @@ import (
	"git.sr.ht/~sircmpwn/go-bare"
)

// Message is the core type for the barefeed message format. It will handle any
// new version that may be released
type Message interface {
	bare.Union
}

// IsUnion is needed for unioning to the Message type
func (t MessageV1) IsUnion() {}

func init() {
	bare.RegisterUnion((*Message)(nil)).
		Member(*new(MessageV1), 0)
}

// FromFile will read barefeed from a file
func FromFile(path string) (*Message, error) {
	b, err := ioutil.ReadFile(filepath.Clean(path))


@@ 54,12 40,12 @@ func FromBytes(b []byte) (*Message, error) {
}

// Bytes will return the barefeed as a slice of bytes
func Bytes(t *Message) ([]byte, error) {
	return bare.Marshal(t)
func Bytes(t Message) ([]byte, error) {
	return bare.Marshal(&t)
}

// WriteFile will write barefeed to a file at the designated path
func WriteFile(t *Message, path string) error {
func WriteFile(t Message, path string) error {
	b, err := Bytes(t)
	if err != nil {
		return err

A barefeed_test.go => barefeed_test.go +91 -0
@@ 0,0 1,91 @@
package barefeed

import (
	"bytes"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestBytes(t *testing.T) {
	m := getTestMessage()

	b, err := Bytes(m)
	if err != nil {
		assert.Fail(t, err.Error())
	}
	assert.NotNil(t, b)

	var m2 *Message
	m2, err = FromBytes(b)
	if err != nil {
		assert.Fail(t, err.Error())
	}
	assert.NotNil(t, m2)

	msg1 := m.(MessageV1)
	msg2 := (*m2).(*MessageV1)

	assert.Equal(t, msg1.Feeds, msg2.Feeds)
	assert.Equal(t, msg1.Created, msg2.Created)
	assert.Equal(t, msg1.Generator, msg2.Generator)
}

func TestReader(t *testing.T) {
	m := getTestMessage()

	b, err := Bytes(m)
	if err != nil {
		assert.Fail(t, err.Error())
	}
	assert.NotNil(t, b)

	var m2 *Message
	m2, err = FromReader(bytes.NewReader(b))
	if err != nil {
		assert.Fail(t, err.Error())
	}
	assert.NotNil(t, m2)

	msg1 := m.(MessageV1)
	msg2 := (*m2).(*MessageV1)

	assert.Equal(t, msg1.Feeds, msg2.Feeds)
	assert.Equal(t, msg1.Created, msg2.Created)
	assert.Equal(t, msg1.Generator, msg2.Generator)
}

func TestTimestamp(t *testing.T) {
	ts := Timestamp(time.Now().UTC().Unix())
	tm := ts.Time()
	assert.Equal(t, ts, ToTimestamp(tm))
}

func getTestMessage() Message {
	i := Item{
		Link:     "a",
		Title:    "b",
		Content:  "c",
		Location: "d",
		Mimetype: "e",
		Length:   "f",
		Read:     true,
		Favorite: true,
		Date:     ToTimestamp(time.Now()),
	}

	f := Feed{
		Feed:        "https://example.com/feed.xml",
		Title:       "Test file",
		Description: "This is a test",
		Link:        "example.com",
		Items:       []Item{i},
	}

	return Message(MessageV1{
		Created:   ToTimestamp(time.Now()),
		Generator: "generated",
		Feeds:     []Feed{f},
	})
}

D feed.go => feed.go +0 -10
@@ 1,10 0,0 @@
package barefeed

// Feed contains all info needed for a RSS Feed
type Feed struct {
	Feed        string `bare:"feed"`
	Title       string `bare:"title"`
	Description string `bare:"description"`
	Link        string `bare:"link"`
	Items       []Item `bare:"items"`
}

M go.mod => go.mod +4 -1
@@ 2,4 2,7 @@ module git.sr.ht/~chrisppy/go-barefeed

go 1.15

require git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287
require (
	git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287
	github.com/stretchr/testify v1.6.1
)

M go.sum => go.sum +1 -0
@@ 1,3 1,4 @@
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk=
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287 h1:mfySyh3Gq6RTlvHFGhrWKtmvTKBFKQQDpRc2Em0cDow=
git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA=

D item.go => item.go +0 -14
@@ 1,14 0,0 @@
package barefeed

// Item contains all info needed for a RSS Item
type Item struct {
	Link     string `bare:"link"`
	Title    string `bare:"title"`
	Content  string `bare:"content"`
	Location string `bare:"location"`
	Mimetype string `bare:"mimetype"`
	Length   string `bare:"length"`
	Read     bool   `bare:"read"`
	Favorite bool   `bare:"favorite"`
	Date     Time   `bare:"date"`
}

D message.go => message.go +0 -8
@@ 1,8 0,0 @@
package barefeed

// MessageV1 is the first implementation of the barefeed message format
type MessageV1 struct {
	Created   Time   `bare:"created"`
	Generator string `bare:"generator"`
	Feeds     []Feed `bare:"feeds"`
}

A msg.go => msg.go +51 -0
@@ 0,0 1,51 @@
package barefeed

// Code generated by go-bare/cmd/gen, DO NOT EDIT.

import (
	"git.sr.ht/~sircmpwn/go-bare"
)

// MessageV1 is the first implementation of the barefeed message format
type MessageV1 struct {
	Created   Timestamp `bare:"created"`
	Generator string    `bare:"generator"`
	Feeds     []Feed    `bare:"feeds"`
}

// Feed contains all info needed for a RSS Feed
type Feed struct {
	Feed        string `bare:"feed"`
	Title       string `bare:"title"`
	Description string `bare:"description"`
	Link        string `bare:"link"`
	Items       []Item `bare:"items"`
}

// Item contains all info needed for a RSS Item
type Item struct {
	Link     string    `bare:"link"`
	Title    string    `bare:"title"`
	Content  string    `bare:"content"`
	Location string    `bare:"location"`
	Mimetype string    `bare:"mimetype"`
	Length   string    `bare:"length"`
	Read     bool      `bare:"read"`
	Favorite bool      `bare:"favorite"`
	Date     Timestamp `bare:"date"`
}

// Message is the core type for the barefeed message format. It will handle any
// new version that may be released
type Message interface {
	bare.Union
}

// IsUnion is necessary to make the type compatible with the Union interface
func (_ MessageV1) IsUnion() {}

func init() {
	bare.RegisterUnion((*Message)(nil)).
		Member(*new(MessageV1), 0)

}

M time.go => time.go +9 -18
@@ 1,27 1,18 @@
package barefeed

import (
	"fmt"
	"time"

	"git.sr.ht/~sircmpwn/go-bare"
)

// Time type conforming to RFC3339
type Time time.Time

// Unmarshal the time type
func (t *Time) Unmarshal(r *bare.Reader) error {
	st, err := r.ReadString()
	if err != nil {
		return fmt.Errorf("Time.Unmarshal: read string: %e", err)
	}
// Timestamp type
type Timestamp int64

	tm, err := time.Parse(time.RFC3339, st)
	if err != nil {
		return fmt.Errorf("Time.Unmarshal: parse time: %e", err)
	}
// Time will convert the timestamp to time
func (t Timestamp) Time() time.Time {
	return time.Unix(int64(t), 0).UTC()
}

	*t = Time(tm)
	return nil
// ToTimestamp will convert the time to timestamp
func ToTimestamp(t time.Time) Timestamp {
	return Timestamp(t.UTC().Unix())
}