[READ-ONLY] Mirror of https://github.com/darccio/zas. Most simple static website generator in Golang.
go static-site-generation static-site-generator
Something went wrong. Try again.
zas layout_embed_test.go
3.8 kB · 83 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384/* * Copyright (c) 2013 Dario Castañé. * This file is part of Zas. * * Zas is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Zas is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Zas. If not, see <http://www.gnu.org/licenses/>. */
package zas
import ( "strings" "testing")
// render only ever parses a page's own source. An <embed> tag written// directly into layout.html - outside {{.Body}} - is never seen by that// parse; the only place it can resolve is Generate's second parseAndReplace// pass over the fully assembled page (layout + body). This pins that the// second pass is load-bearing, not redundant work: dropping it, or// replacing it with something that only walks the page's own body, would// silently stop layout-level embeds from working.//// Html's handler (generate.go) splices in only the embedded file's body// contents, not its whole parsed document, so a layout-level embed like// this one - which has no further re-parse downstream to paper over a// nesting mistake - must still deploy with exactly the outer page's own// single <html>/<head>/<body>, not a second, stray one nested from the// embedded document.func TestGenerateResolvesEmbedInLayoutItself(t *testing.T) { newTestSite(t, "layout-embed-site") if err := generate(t); err != nil { t.Fatalf("generate() error = %v, want nil", err) } got := readDeploy(t, "index.html") if !strings.Contains(got, "footer from the layout itself") { t.Fatalf("deployed index.html = %q, want it to contain the layout's own embedded footer", got) } if strings.Contains(got, "<embed") { t.Fatalf("deployed index.html = %q, want the layout's <embed> tag replaced, not left in place", got) } for _, tag := range []string{"<html", "<head", "<body"} { if n := strings.Count(got, tag); n != 1 { t.Fatalf("deployed index.html = %q, want exactly one %q (the outer page's own), got %d", got, tag, n) } }}
// An <embed> written inside a page's own body now resolves relative to// that page's own directory (see TestGenerateEmbedResolvesRelativeToPage),// but an <embed> written directly into layout.html is a different case:// layout.html is a single, fixed file shared by every page, not content// that lives "in" any one page's directory, so its own embeds still// resolve against the site root regardless of which page is currently// being rendered. This renders "section/index.html" - a page one// directory below the site root - through the same layout as// TestGenerateResolvesEmbedInLayoutItself and confirms the layout's// site-root "footer.html" embed still resolves, rather than the layout// handler mistakenly looking for "section/footer.html" (which doesn't// exist) and failing the build.func TestGenerateLayoutEmbedStaysRootRelativeForSubdirectoryPage(t *testing.T) { newTestSite(t, "layout-embed-site") if err := generate(t); err != nil { t.Fatalf("generate() error = %v, want nil", err) } got := readDeploy(t, "section/index.html") if !strings.Contains(got, "footer from the layout itself") { t.Fatalf("deployed section/index.html = %q, want it to contain the layout's own embedded footer", got) } if !strings.Contains(got, "section content") { t.Fatalf("deployed section/index.html = %q, want it to contain the page's own content", got) }}