package feeds import ( "bytes" "testing" "time" ) var atomOutput = ` jmoiron.net blog http://jmoiron.net/blog 2013-01-16T21:52:35-05:00 This work is copyright © Benjamin Button discussion about tech, footie, photos Jason Moiron jmoiron@jmoiron.net Limiting Concurrency in Go 2013-01-16T21:52:35-05:00 tag:jmoiron.net,2013-01-16:/blog/limiting-concurrency-in-go/ A discussion on controlled parallelism in golang Jason Moiron jmoiron@jmoiron.net Logic-less Template Redux 2013-01-16T21:52:35-05:00 tag:jmoiron.net,2013-01-16:/blog/logicless-template-redux/ More thoughts on logicless templates Idiomatic Code Reuse in Go 2013-01-16T21:52:35-05:00 tag:jmoiron.net,2013-01-16:/blog/idiomatic-code-reuse-in-go/ How to use interfaces <em>effectively</em> Never Gonna Give You Up Mp3 2013-01-16T21:52:35-05:00 tag:example.com,2013-01-16:/RickRoll.mp3 Never gonna give you up - Never gonna let you down. String formatting in Go 2013-01-16T21:52:35-05:00 tag:example.com,2013-01-16:/strings How to use things like %s, %v, %d, etc. ` var rssOutput = ` jmoiron.net blog http://jmoiron.net/blog discussion about tech, footie, photos This work is copyright © Benjamin Button jmoiron@jmoiron.net (Jason Moiron) Wed, 16 Jan 2013 21:52:35 -0500 Limiting Concurrency in Go http://jmoiron.net/blog/limiting-concurrency-in-go/ A discussion on controlled parallelism in golang Jason Moiron Wed, 16 Jan 2013 21:52:35 -0500 Logic-less Template Redux http://jmoiron.net/blog/logicless-template-redux/ More thoughts on logicless templates Wed, 16 Jan 2013 21:52:35 -0500 Idiomatic Code Reuse in Go http://jmoiron.net/blog/idiomatic-code-reuse-in-go/ How to use interfaces <em>effectively</em> Wed, 16 Jan 2013 21:52:35 -0500 Never Gonna Give You Up Mp3 http://example.com/RickRoll.mp3 Never gonna give you up - Never gonna let you down. Wed, 16 Jan 2013 21:52:35 -0500 String formatting in Go http://example.com/strings How to use things like %s, %v, %d, etc. Wed, 16 Jan 2013 21:52:35 -0500 ` var jsonOutput = `{ "version": "https://jsonfeed.org/version/1", "title": "jmoiron.net blog", "home_page_url": "http://jmoiron.net/blog", "description": "discussion about tech, footie, photos", "author": { "name": "Jason Moiron" }, "items": [ { "id": "", "url": "http://jmoiron.net/blog/limiting-concurrency-in-go/", "title": "Limiting Concurrency in Go", "summary": "A discussion on controlled parallelism in golang", "date_published": "2013-01-16T21:52:35-05:00", "author": { "name": "Jason Moiron" } }, { "id": "", "url": "http://jmoiron.net/blog/logicless-template-redux/", "title": "Logic-less Template Redux", "summary": "More thoughts on logicless templates", "date_published": "2013-01-16T21:52:35-05:00" }, { "id": "", "url": "http://jmoiron.net/blog/idiomatic-code-reuse-in-go/", "title": "Idiomatic Code Reuse in Go", "summary": "How to use interfaces \u003cem\u003eeffectively\u003c/em\u003e", "image": "http://example.com/cover.jpg", "date_published": "2013-01-16T21:52:35-05:00" }, { "id": "", "url": "http://example.com/RickRoll.mp3", "title": "Never Gonna Give You Up Mp3", "summary": "Never gonna give you up - Never gonna let you down.", "date_published": "2013-01-16T21:52:35-05:00" }, { "id": "", "url": "http://example.com/strings", "title": "String formatting in Go", "summary": "How to use things like %s, %v, %d, etc.", "date_published": "2013-01-16T21:52:35-05:00" } ] }` func TestFeed(t *testing.T) { now, err := time.Parse(time.RFC3339, "2013-01-16T21:52:35-05:00") if err != nil { t.Error(err) } tz := time.FixedZone("EST", -5*60*60) now = now.In(tz) feed := &Feed{ Title: "jmoiron.net blog", Link: &Link{Href: "http://jmoiron.net/blog"}, Description: "discussion about tech, footie, photos", Author: &Author{Name: "Jason Moiron", Email: "jmoiron@jmoiron.net"}, Created: now, Copyright: "This work is copyright © Benjamin Button", } feed.Items = []*Item{ { Title: "Limiting Concurrency in Go", Link: &Link{Href: "http://jmoiron.net/blog/limiting-concurrency-in-go/"}, Description: "A discussion on controlled parallelism in golang", Author: &Author{Name: "Jason Moiron", Email: "jmoiron@jmoiron.net"}, Created: now, }, { Title: "Logic-less Template Redux", Link: &Link{Href: "http://jmoiron.net/blog/logicless-template-redux/"}, Description: "More thoughts on logicless templates", Created: now, }, { Title: "Idiomatic Code Reuse in Go", Link: &Link{Href: "http://jmoiron.net/blog/idiomatic-code-reuse-in-go/"}, Description: "How to use interfaces effectively", Enclosure: &Enclosure{Url: "http://example.com/cover.jpg", Length: "123456", Type: "image/jpg"}, Created: now, }, { Title: "Never Gonna Give You Up Mp3", Link: &Link{Href: "http://example.com/RickRoll.mp3"}, Enclosure: &Enclosure{Url: "http://example.com/RickRoll.mp3", Length: "123456", Type: "audio/mpeg"}, Description: "Never gonna give you up - Never gonna let you down.", Created: now, }, { Title: "String formatting in Go", Link: &Link{Href: "http://example.com/strings"}, Description: "How to use things like %s, %v, %d, etc.", Created: now, }} atom, err := feed.ToAtom() if err != nil { t.Errorf("unexpected error encoding Atom: %v", err) } if atom != atomOutput { t.Errorf("Atom not what was expected. Got:\n%s\n\nExpected:\n%s\n", atom, atomOutput) } var buf bytes.Buffer if err := feed.WriteAtom(&buf); err != nil { t.Errorf("unexpected error writing Atom: %v", err) } if got := buf.String(); got != atomOutput { t.Errorf("Atom not what was expected. Got:\n%s\n\nExpected:\n%s\n", got, atomOutput) } rss, err := feed.ToRss() if err != nil { t.Errorf("unexpected error encoding RSS: %v", err) } if rss != rssOutput { t.Errorf("Rss not what was expected. Got:\n%s\n\nExpected:\n%s\n", rss, rssOutput) } buf.Reset() if err := feed.WriteRss(&buf); err != nil { t.Errorf("unexpected error writing RSS: %v", err) } if got := buf.String(); got != rssOutput { t.Errorf("Rss not what was expected. Got:\n%s\n\nExpected:\n%s\n", got, rssOutput) } json, err := feed.ToJSON() if err != nil { t.Errorf("unexpected error encoding JSON: %v", err) } if json != jsonOutput { t.Errorf("JSON not what was expected. Got:\n%s\n\nExpected:\n%s\n", json, jsonOutput) } buf.Reset() if err := feed.WriteJSON(&buf); err != nil { t.Errorf("unexpected error writing JSON: %v", err) } if got := buf.String(); got != jsonOutput+"\n" { //json.Encode appends a newline after the JSON output: https://github.com/golang/go/commit/6f25f1d4c901417af1da65e41992d71c30f64f8f#diff-50848cbd686f250623a2ef6ddb07e157 t.Errorf("JSON not what was expected. Got:\n||%s||\n\nExpected:\n||%s||\n", got, jsonOutput) } }