serializer_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opml // import "miniflux.app/v2/internal/reader/opml"
  4. import (
  5. "bytes"
  6. "testing"
  7. )
  8. func TestSerialize(t *testing.T) {
  9. var subscriptions []subcription
  10. subscriptions = append(subscriptions, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  11. subscriptions = append(subscriptions, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: "Category 1"})
  12. subscriptions = append(subscriptions, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"})
  13. output := serialize(subscriptions)
  14. feeds, err := parse(bytes.NewBufferString(output))
  15. if err != nil {
  16. t.Error(err)
  17. }
  18. if len(feeds) != 3 {
  19. t.Errorf("Wrong number of subscriptions: %d instead of %d", len(feeds), 3)
  20. }
  21. found := false
  22. for _, feed := range feeds {
  23. if feed.Title == "Feed 1" && feed.CategoryName == "Category 1" &&
  24. feed.FeedURL == "http://example.org/feed/1" && feed.SiteURL == "http://example.org/1" {
  25. found = true
  26. break
  27. }
  28. }
  29. if !found {
  30. t.Error("Serialized feed is incorrect")
  31. }
  32. }
  33. func TestNormalizedCategoriesOrder(t *testing.T) {
  34. var orderTests = []struct {
  35. naturalOrderName string
  36. correctOrderName string
  37. }{
  38. {"Category 2", "Category 1"},
  39. {"Category 3", "Category 2"},
  40. {"Category 1", "Category 3"},
  41. }
  42. var subscriptions []subcription
  43. subscriptions = append(subscriptions, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: orderTests[0].naturalOrderName})
  44. subscriptions = append(subscriptions, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: orderTests[1].naturalOrderName})
  45. subscriptions = append(subscriptions, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: orderTests[2].naturalOrderName})
  46. feeds := convertSubscriptionsToOPML(subscriptions)
  47. for i, o := range orderTests {
  48. if feeds.Outlines[i].Text != o.correctOrderName {
  49. t.Fatalf("need %v, got %v", o.correctOrderName, feeds.Outlines[i].Text)
  50. }
  51. }
  52. }