|
| 1 | +// Copyright (c) VirtualTam |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package opml |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// AssertDocumentsEqual asserts two OPML documents are equal. |
| 12 | +func AssertDocumentsEqual(t *testing.T, got, want Document) { |
| 13 | + t.Helper() |
| 14 | + |
| 15 | + // OPML metadata |
| 16 | + if got.XMLName.Local != want.XMLName.Local { |
| 17 | + t.Errorf("want XMLName.Local %q, got %q", want.XMLName.Local, got.XMLName.Local) |
| 18 | + } |
| 19 | + if got.Version != want.Version { |
| 20 | + t.Errorf("want Version %q, got %q", want.Version, got.Version) |
| 21 | + } |
| 22 | + |
| 23 | + // Head |
| 24 | + if !got.Head.DateCreated.Equal(want.Head.DateCreated) { |
| 25 | + t.Errorf("want Head > DateCreated %q, got %q", want.Head.DateCreated, got.Head.DateCreated) |
| 26 | + } |
| 27 | + if !got.Head.DateModified.Equal(want.Head.DateModified) { |
| 28 | + t.Errorf("want Head > DateModified %q, got %q", want.Head.DateModified, got.Head.DateModified) |
| 29 | + } |
| 30 | + if got.Head.OwnerName != want.Head.OwnerName { |
| 31 | + t.Errorf("want Head > OwnerName %q, got %q", want.Head.OwnerName, got.Head.OwnerName) |
| 32 | + } |
| 33 | + if got.Head.OwnerEmail != want.Head.OwnerEmail { |
| 34 | + t.Errorf("want Head > OwnerEmail %q, got %q", want.Head.OwnerEmail, got.Head.OwnerEmail) |
| 35 | + } |
| 36 | + if got.Head.VertScrollState != want.Head.VertScrollState { |
| 37 | + t.Errorf("want Head > VertScrollState %d, got %d", want.Head.VertScrollState, got.Head.VertScrollState) |
| 38 | + } |
| 39 | + if got.Head.WindowTop != want.Head.WindowTop { |
| 40 | + t.Errorf("want Head > WindowTop %d, got %d", want.Head.WindowTop, got.Head.WindowTop) |
| 41 | + } |
| 42 | + if got.Head.WindowLeft != want.Head.WindowLeft { |
| 43 | + t.Errorf("want Head > WindowLeft %d, got %d", want.Head.WindowLeft, got.Head.WindowLeft) |
| 44 | + } |
| 45 | + if got.Head.WindowBottom != want.Head.WindowBottom { |
| 46 | + t.Errorf("want Head > WindowBottom %d, got %d", want.Head.WindowBottom, got.Head.WindowBottom) |
| 47 | + } |
| 48 | + if got.Head.WindowRight != want.Head.WindowRight { |
| 49 | + t.Errorf("want Head > WindowRight %d, got %d", want.Head.WindowRight, got.Head.WindowRight) |
| 50 | + } |
| 51 | + |
| 52 | + // Body |
| 53 | + AssertOutlinesEqual(t, got.Body.Outlines, want.Body.Outlines) |
| 54 | +} |
| 55 | + |
| 56 | +// AssertOutlinesEqual asserts two lists of OPML outlines are equal. |
| 57 | +func AssertOutlinesEqual(t *testing.T, gotOutlines, wantOutlines []Outline) { |
| 58 | + t.Helper() |
| 59 | + |
| 60 | + assertOutlinesEqual(t, "", gotOutlines, wantOutlines) |
| 61 | +} |
| 62 | + |
| 63 | +// AssertOutlinesEqual asserts two lists of OPML outlines are equal. |
| 64 | +func assertOutlinesEqual(t *testing.T, prefix string, gotOutlines, wantOutlines []Outline) { |
| 65 | + t.Helper() |
| 66 | + |
| 67 | + if len(gotOutlines) != len(wantOutlines) { |
| 68 | + t.Fatalf("want %d Outlines, got %d", len(wantOutlines), len(gotOutlines)) |
| 69 | + } |
| 70 | + |
| 71 | + for index, wantOutline := range wantOutlines { |
| 72 | + gotOutline := gotOutlines[index] |
| 73 | + |
| 74 | + if gotOutline.Text != wantOutline.Text { |
| 75 | + t.Errorf("want Outline %s%d Text %q, got %q", prefix, index, wantOutline.Text, gotOutline.Text) |
| 76 | + } |
| 77 | + |
| 78 | + if len(gotOutline.Categories) != len(wantOutline.Categories) { |
| 79 | + t.Errorf("want Outline %s%d %d Categories, got %d", prefix, index, len(wantOutline.Categories), len(gotOutline.Categories)) |
| 80 | + } |
| 81 | + |
| 82 | + for cIndex, wantCategory := range wantOutline.Categories { |
| 83 | + gotCategory := gotOutline.Categories[cIndex] |
| 84 | + |
| 85 | + if gotCategory != wantCategory { |
| 86 | + t.Errorf("want Outline %s%d Category %d %q, got %q", prefix, index, cIndex, wantCategory, gotCategory) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if !gotOutline.Created.Equal(wantOutline.Created) { |
| 91 | + t.Errorf("want Outline %s%d Created %q, got %q", prefix, index, wantOutline.Created, gotOutline.Created) |
| 92 | + } |
| 93 | + if gotOutline.IsBreakpoint != wantOutline.IsBreakpoint { |
| 94 | + t.Errorf("want Outline %s%d IsBreakpoint %t, got %t", prefix, index, wantOutline.IsBreakpoint, gotOutline.IsBreakpoint) |
| 95 | + } |
| 96 | + if gotOutline.IsComment != wantOutline.IsComment { |
| 97 | + t.Errorf("want Outline %s%d IsComment %t, got %t", prefix, index, wantOutline.IsComment, gotOutline.IsComment) |
| 98 | + } |
| 99 | + if gotOutline.Type != wantOutline.Type { |
| 100 | + t.Errorf("want Outline %s%d Type %q, got %q", prefix, index, wantOutline.Type, gotOutline.Type) |
| 101 | + } |
| 102 | + |
| 103 | + if gotOutline.Url != wantOutline.Url { |
| 104 | + t.Errorf("want Outline %s%d URL %q, got %q", prefix, index, wantOutline.Url, gotOutline.Url) |
| 105 | + } |
| 106 | + |
| 107 | + if gotOutline.Version != wantOutline.Version { |
| 108 | + t.Errorf("want Outline %s%d Version %q, got %q", prefix, index, wantOutline.Version, gotOutline.Version) |
| 109 | + } |
| 110 | + if gotOutline.Title != wantOutline.Title { |
| 111 | + t.Errorf("want Outline %s%d Title %q, got %q", prefix, index, wantOutline.Title, gotOutline.Title) |
| 112 | + } |
| 113 | + if gotOutline.Description != wantOutline.Description { |
| 114 | + t.Errorf("want Outline %s%d Description %q, got %q", prefix, index, wantOutline.Description, gotOutline.Description) |
| 115 | + } |
| 116 | + if gotOutline.Language != wantOutline.Language { |
| 117 | + t.Errorf("want Outline %s%d Language %q, got %q", prefix, index, wantOutline.Language, gotOutline.Language) |
| 118 | + } |
| 119 | + if gotOutline.HtmlUrl != wantOutline.HtmlUrl { |
| 120 | + t.Errorf("want Outline %s%d HtmlUrl %q, got %q", prefix, index, wantOutline.HtmlUrl, gotOutline.HtmlUrl) |
| 121 | + } |
| 122 | + if gotOutline.XmlUrl != wantOutline.XmlUrl { |
| 123 | + t.Errorf("want Outline %s%d XmlUrl %q, got %q", prefix, index, wantOutline.XmlUrl, gotOutline.XmlUrl) |
| 124 | + } |
| 125 | + |
| 126 | + childPrefix := fmt.Sprintf("%s%d.", prefix, index) |
| 127 | + assertOutlinesEqual(t, childPrefix, gotOutline.Outlines, wantOutline.Outlines) |
| 128 | + } |
| 129 | +} |
0 commit comments