1// Copyright (C) 2013-2015 Sören Tempel2//3// This program is free software: you can redistribute it and/or modify4// it under the terms of the GNU General Public License as published by5// the Free Software Foundation, either version 3 of the License, or6// (at your option) any later version.7//8// This program is distributed in the hope that it will be useful,9// but WITHOUT ANY WARRANTY; without even the implied warranty of10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11// GNU General Public License for more details.12//13// You should have received a copy of the GNU General Public License14// along with this program. If not, see <http://www.gnu.org/licenses/>.1516package store1718import (19 "io/ioutil"20 "os"21 "path/filepath"22 "testing"23)2425func TestLoad(t *testing.T) {26 store, err := Load("testdata/testLoad.txt")27 if err != nil {28 t.Fatal(err)29 }3031 urls := []string{32 "http://feed.thisamericanlife.org/talpodcast",33 "http://www.npr.org/rss/podcast.php?id=510294",34 }3536 for _, url := range urls {37 if !store.Contains(url) {38 t.Fail()39 }40 }41}4243func TestAdd(t *testing.T) {44 url := "http://example.com"45 store := new(Store)4647 store.Add(url)48 if !store.Contains(url) {49 t.Fail()50 }51}5253func TestContains(t *testing.T) {54 url := "http://foo.com"55 store := &Store{"", []string{url}}5657 if !store.Contains(url) {58 t.Fail()59 }6061 if store.Contains("http://foo.bar") {62 t.Fail()63 }64}6566func TestFetch(t *testing.T) {67 url := "http://feed.thisamericanlife.org/talpodcast"68 store := &Store{"", []string{url}}6970 channel := store.Fetch()71 podcast := <-channel7273 if podcast.Error != nil {74 t.Fatal(podcast.Error)75 }7677 feed := podcast.Feed78 if url != podcast.URL {79 t.Fatalf("Expected %q - got %q", podcast.URL, url)80 }8182 expected := "This American Life"83 if feed.Title != expected {84 t.Fatalf("Expected %q - got %q", expected, feed.Title)85 }86}8788func TestSave(t *testing.T) {89 url := "http://example.io"90 fp := filepath.Join(os.TempDir(), "testSave")9192 store := &Store{fp, []string{url}}93 if err := store.Save(); err != nil {94 t.Fatal(err)95 }9697 defer os.Remove(fp)98 data, err := ioutil.ReadFile(fp)99 if err != nil {100 t.Fatal(err)101 }102103 expected := url + "\n"104 if string(data) != expected {105 t.Fatalf("Expected %q - got %q", string(data), expected)106 }107}