forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Truncated; .Site.Params; First function
* Add `.Truncated` bool to each page; will be set true if the `.Summary` is truncated and it's worth showing a "more" link of some kind. * Add `Params` to the site config, defining `.Site.Params` accessible to each page; this lets the site maintainer associate arbitrary data with names, on a site-wide basis. * Provide a `First` function to templates: * Use-case: `{{range First 5 .Site.Recent}}` or anything else which is a simple iterable provided by hugolib * Tests by me for `.Truncated` and `First` Also @noahcampbell contributed towards this: * Add UnitTest for `.Site.Params`: > Digging into this test case a bit more, I'm realizing that we need > to create a param test case to ensure that for each type we render > (page, index, homepage, rss, etc.) that the proper fields are > represented. This will help us refactor without fear in the > future. Sample config.yaml: ```yaml title: "Test site" params: Subtitle: "More tests always good" AuthorName: "John Doe" SidebarRecentLimit: 5 ``` Signed-off-by: Noah Campbell <noahcampbell@gmail.com>
- Loading branch information
1 parent
6017599
commit 40d05f1
Showing
7 changed files
with
157 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package hugolib | ||
|
||
import ( | ||
"testing" | ||
"bytes" | ||
) | ||
|
||
const SITE_INFO_PARAM_TEMPLATE = `{{ .Site.Params.MyGlobalParam }}` | ||
|
||
|
||
func TestSiteInfoParams(t *testing.T) { | ||
s := &Site{ | ||
Config: Config{Params: map[string]interface{}{"MyGlobalParam": "FOOBAR_PARAM"}}, | ||
} | ||
|
||
s.initialize() | ||
if s.Info.Params["MyGlobalParam"] != "FOOBAR_PARAM" { | ||
t.Errorf("Unable to set site.Info.Param") | ||
} | ||
s.prepTemplates() | ||
s.addTemplate("template", SITE_INFO_PARAM_TEMPLATE) | ||
buf := new(bytes.Buffer) | ||
|
||
err := s.renderThing(s.NewNode(), "template", buf) | ||
if err != nil { | ||
t.Errorf("Unable to render template: %s", err) | ||
} | ||
|
||
if buf.String() != "FOOBAR_PARAM" { | ||
t.Errorf("Expected FOOBAR_PARAM: got %s", buf.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package bundle | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGt(t *testing.T) { | ||
for i, this := range []struct{ | ||
left interface{} | ||
right interface{} | ||
leftShouldWin bool | ||
}{ | ||
{ 5, 8, false }, | ||
{ 8, 5, true }, | ||
{ 5, 5, false }, | ||
{ -2, 1, false }, | ||
{ 2, -5, true }, | ||
{ "8", "5", true }, | ||
{ "5", "0001", true }, | ||
{ []int{100,99}, []int{1,2,3,4}, false }, | ||
} { | ||
leftIsBigger := Gt(this.left, this.right) | ||
if leftIsBigger != this.leftShouldWin { | ||
var which string | ||
if leftIsBigger { | ||
which = "expected right to be bigger, but left was" | ||
} else { | ||
which = "expected left to be bigger, but right was" | ||
} | ||
t.Errorf("[%d] %v compared to %v: %s", i, this.left, this.right, which) | ||
} | ||
} | ||
} | ||
|
||
func TestFirst(t *testing.T) { | ||
for i, this := range []struct{ | ||
count int | ||
sequence interface{} | ||
expect interface{} | ||
} { | ||
{ 2, []string{"a", "b", "c"}, []string{"a", "b"} }, | ||
{ 3, []string{"a", "b"}, []string{"a", "b"} }, | ||
{ 2, []int{100, 200, 300}, []int{100, 200} }, | ||
} { | ||
results, err := First(this.count, this.sequence) | ||
if err != nil { | ||
t.Errorf("[%d] failed: %s", i, err) | ||
continue | ||
} | ||
if !reflect.DeepEqual(results, this.expect) { | ||
t.Errorf("[%d] First %d items, got %v but expected %v", i, this.count, results, this.expect) | ||
} | ||
} | ||
} |