Skip to content

Commit

Permalink
hugolib: Add Page.Equals
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 16, 2017
1 parent 71ae9b4 commit f0f49ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hugolib/site_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func (p *Page) IsAncestor(other interface{}) (bool, error) {
return helpers.HasStringsPrefix(pp.sections, p.sections), nil
}

// Equals returns whether the current page equals the given page.
// Note that this is more accurate than doing `{{ if eq $page $otherPage }}`
// since a Page can be embedded in another type.
func (p *Page) Equals(other interface{}) (bool, error) {
pp, err := unwrapPage(other)
if err != nil {
return false, err
}

return p == pp, nil
}

func unwrapPage(in interface{}) (*Page, error) {
if po, ok := in.(*PageOutput); ok {
in = po.Page
Expand Down
10 changes: 10 additions & 0 deletions hugolib/site_sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
d := p.s.getPage(KindSection, "empty2", "b", "c", "d")
assert.NotNil(d)
assert.Equal("T41_-1", d.Title)

equals, err := c.Equals(d)
assert.NoError(err)
assert.False(equals)
equals, err = c.Equals(c)
assert.NoError(err)
assert.True(equals)
_, err = c.Equals("asdf")
assert.Error(err)

}},
{"empty3", func(p *Page) {
// b,c,d with regular page in b
Expand Down

0 comments on commit f0f49ed

Please sign in to comment.