serializer_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package opml // import "miniflux.app/reader/opml"
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. func TestSerialize(t *testing.T) {
  10. var subscriptions SubcriptionList
  11. subscriptions = append(subscriptions, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  12. subscriptions = append(subscriptions, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: "Category 1"})
  13. subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"})
  14. output := Serialize(subscriptions)
  15. feeds, err := Parse(bytes.NewBufferString(output))
  16. if err != nil {
  17. t.Error(err)
  18. }
  19. if len(feeds) != 3 {
  20. t.Errorf("Wrong number of subscriptions: %d instead of %d", len(feeds), 3)
  21. }
  22. found := false
  23. for _, feed := range feeds {
  24. if feed.Title == "Feed 1" && feed.CategoryName == "Category 1" &&
  25. feed.FeedURL == "http://example.org/feed/1" && feed.SiteURL == "http://example.org/1" {
  26. found = true
  27. break
  28. }
  29. }
  30. if !found {
  31. t.Error("Serialized feed is incorrect")
  32. }
  33. }
  34. func TestNormalizedCategoriesOrder(t *testing.T) {
  35. var orderTests = []struct {
  36. naturalOrderName string
  37. correctOrderName string
  38. }{
  39. {"Category 2", "Category 1"},
  40. {"Category 3", "Category 2"},
  41. {"Category 1", "Category 3"},
  42. }
  43. var subscriptions SubcriptionList
  44. subscriptions = append(subscriptions, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: orderTests[0].naturalOrderName})
  45. subscriptions = append(subscriptions, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: orderTests[1].naturalOrderName})
  46. subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: orderTests[2].naturalOrderName})
  47. feeds := convertSubscriptionsToOPML(subscriptions)
  48. for i, o := range orderTests {
  49. if feeds.Outlines[i].Text != o.correctOrderName {
  50. t.Fatalf("need %v, got %v", o.correctOrderName, feeds.Outlines[i].Text)
  51. }
  52. }
  53. }