pagr

A 'static site generator', built using dati.
Log | Files | Refs | Atom

pagr_test.go (6274B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"io/ioutil"
      6 	"os"
      7 	"path/filepath"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 /* shared *_test.go functions, since pagr.go doesn't have anything that requires testing */
     13 
     14 var templates = map[string]string{ // [ext]template
     15 	"tmpl": "{{.Contents}}",
     16 	"hmpl": "{{.Contents}}",
     17 	"mst":  "{{Contents}}",
     18 }
     19 
     20 func createTestTemplates(dir string) (err error) {
     21 	writef := func(path, data string) {
     22 		if err == nil {
     23 			err = ioutil.WriteFile(path, []byte(data), 0644)
     24 		}
     25 	}
     26 
     27 	for ext, data := range templates {
     28 		writef(filepath.Join(dir, fmt.Sprintf("root.%s", ext)), data)
     29 		writef(filepath.Join(dir, fmt.Sprintf("root.ignore.%s", ext)), data)
     30 		writef(filepath.Join(dir, fmt.Sprintf("root.%s.ignore", ext)), data)
     31 
     32 		writef(filepath.Join(dir, fmt.Sprintf("partial.%s", ext)), data)
     33 		writef(filepath.Join(dir, fmt.Sprintf("partial.ignore.%s", ext)), data)
     34 		writef(filepath.Join(dir, fmt.Sprintf("partial.%s.ignore", ext)), data)
     35 
     36 		if err != nil {
     37 			break
     38 		}
     39 	}
     40 
     41 	return err
     42 }
     43 
     44 // file contents used in "contents" files
     45 const contentsTxt = `p1
     46 p2
     47 
     48 	pre1
     49 	pre2
     50 p3
     51 
     52 p4
     53 `
     54 const contentsHtml = `<p>p1<br>
     55 p2</p>
     56 <pre>pre1
     57 pre2
     58 </pre>
     59 <p>p3</p>
     60 <p>p4</p>`
     61 const contentsMd = `p1
     62 p2
     63 
     64 	pre1
     65 	pre2
     66 
     67 p3
     68 `
     69 
     70 var contents = map[string]string{
     71 	"":      contentsTxt,
     72 	".txt":  contentsTxt,
     73 	".html": contentsHtml,
     74 	".md":   contentsMd,
     75 }
     76 
     77 var asset = []byte{ // 5x5 black png image - unecessary but I think this is cool
     78 	0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00,
     79 	0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
     80 	0x00, 0x05, 0x01, 0x03, 0x00, 0x00, 0x00, 0xb7, 0xa1, 0xb4, 0xa6,
     81 	0x00, 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00,
     82 	0x00, 0x00, 0x00, 0xa5, 0x67, 0xb9, 0xcf, 0x00, 0x00, 0x00, 0x02,
     83 	0x74, 0x52, 0x4e, 0x53, 0xff, 0x00, 0xe5, 0xb7, 0x30, 0x4a, 0x00,
     84 	0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0e, 0xc4,
     85 	0x00, 0x00, 0x0e, 0xc4, 0x01, 0x95, 0x2b, 0x0e, 0x1b, 0x00, 0x00,
     86 	0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x08, 0x99, 0x63, 0x60, 0x66,
     87 	0x60, 0x66, 0x60, 0x00, 0x62, 0x76, 0x00, 0x00, 0x4a, 0x00, 0x11,
     88 	0x3a, 0x34, 0x8c, 0xad, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
     89 	0x44, 0xae, 0x42, 0x60, 0x82,
     90 }
     91 
     92 func createTestContents(dir string) (err error) {
     93 	writef := func(path, data string) {
     94 		if err == nil {
     95 			err = ioutil.WriteFile(path, []byte(data), 0644)
     96 		}
     97 	}
     98 
     99 	for l, lang := range contentExts {
    100 		if l == 0 {
    101 			writef(filepath.Join(dir, "defaults.json"), "{ \"default\": \"data\" }")
    102 		} else if l == 1 {
    103 			dir = filepath.Join(dir, lang[1:])
    104 		} else if l >= 2 {
    105 			dir = filepath.Join(filepath.Dir(dir), lang[1:])
    106 		}
    107 		if l >= 1 {
    108 			if err := os.MkdirAll(dir, 0775); err != nil {
    109 				fmt.Errorf("failed to create temporary test dir '%s': %s", dir, err)
    110 			}
    111 		}
    112 		writef(filepath.Join(dir, "meta.toml"), "page = \"data\"")
    113 		writef(filepath.Join(dir, fmt.Sprintf("body%d%s", l, lang)), contents[lang])
    114 		writef(filepath.Join(dir, "image.png"), string(asset))
    115 		writef(filepath.Join(dir, "video.mkv"), "foo")
    116 		writef(filepath.Join(dir, "audio.mp3"), "foo")
    117 		writef(filepath.Join(dir, "misc.zip"), "foo")
    118 
    119 		if err != nil {
    120 			break
    121 		}
    122 	}
    123 
    124 	return
    125 }
    126 
    127 func validateTestPagesNav(t *testing.T, pages []Page) {
    128 	for _, p := range pages {
    129 		var allUnique []string
    130 		for _, navp := range p.Nav.All {
    131 			for _, a := range allUnique {
    132 				if a == navp.Path {
    133 					t.Errorf("'%s' has .Nav.All items with duplicate .Path values (%s)",
    134 						p.Path, a)
    135 				}
    136 			}
    137 			allUnique = append(allUnique, navp.Path)
    138 		}
    139 		if len(p.Nav.All) != len(pages) {
    140 			t.Errorf("'%s' has %d in .Nav.All (should be %d)",
    141 				p.Path, len(p.Nav.All), len(pages))
    142 		}
    143 
    144 		foundAll := 0
    145 		for _, navp := range p.Nav.All {
    146 			for _, pp := range pages {
    147 				if navp.Path == pp.Path {
    148 					foundAll++
    149 					break
    150 				}
    151 			}
    152 		}
    153 		if foundAll != len(p.Nav.All) {
    154 			t.Errorf("found %d/%d pages in .Nav.All for '%s'",
    155 				foundAll, len(p.Nav.All), p.Path)
    156 		}
    157 
    158 		foundRoot := false
    159 		foundParent := false
    160 		for _, pp := range pages {
    161 			if !foundRoot {
    162 				if p.Nav.Root == nil {
    163 					foundRoot = true
    164 				} else if p.Nav.Root.Path == pp.Path {
    165 					foundRoot = true
    166 				}
    167 			}
    168 			if !foundParent {
    169 				if p.Nav.Parent == nil {
    170 					foundParent = true
    171 				} else if p.Nav.Parent.Path == pp.Path {
    172 					foundParent = true
    173 				}
    174 			}
    175 			if foundRoot && foundParent {
    176 				break
    177 			}
    178 		}
    179 		if !foundRoot {
    180 			t.Errorf("could not find .Root '%s' for '%s'",
    181 				p.Nav.Root.Path, p.Path)
    182 		}
    183 		if !foundParent {
    184 			t.Errorf("could not find .Parent '%s' for '%s'",
    185 				p.Nav.Parent.Path, p.Path)
    186 		}
    187 
    188 		// TODO test .Nav.Children, figure out how many should exist
    189 		// TODO test .Nav.Crumbs, figure out how many should exist
    190 	}
    191 }
    192 
    193 func validateTestPages(t *testing.T, pages []Page, e error) {
    194 	if len(pages) != len(contents) {
    195 		t.Fatalf("invalid number of pages returned (%d should be %d)",
    196 			len(pages), len(contents))
    197 	}
    198 
    199 	var last, pt time.Time
    200 	for i, p := range pages {
    201 		if len(p.Slug) == 0 && p.Slug != filepath.Base(p.Path) {
    202 			t.Errorf("bad Slug for page: '%s' (%s) - should be '%s'",
    203 				p.Slug, p.Path, filepath.Base(p.Path))
    204 		}
    205 		if len(p.Path) == 0 {
    206 			t.Error("empty Path for page:", p)
    207 		}
    208 		validateTestPagesNav(t, pages)
    209 		if _, ok := p.Meta["page"]; !ok || len(p.Meta) == 0 {
    210 			t.Errorf("missing page Meta key for page: '%s'", p.Path)
    211 		}
    212 		if _, ok := p.Meta["default"]; !ok || len(p.Meta) == 0 {
    213 			t.Error("empty 'default' Meta key for page:", p.Path)
    214 		}
    215 		if len(p.Contents) == 0 {
    216 			t.Error("empty Contents for page:", p.Path)
    217 		}
    218 		if len(p.Assets.All) != 4 {
    219 			t.Error("invalid number of Assets.All for page:", p.Path)
    220 		}
    221 		if len(p.Assets.Image) != 1 {
    222 			t.Error("invalid number of Assets.Image for page:", p.Path)
    223 		}
    224 		if len(p.Assets.Video) != 1 {
    225 			t.Error("invalid number of Assets.Video for page:", p.Path)
    226 		}
    227 		if len(p.Assets.Audio) != 1 {
    228 			t.Error("invalid number of Assets.Audio for page:", p.Path)
    229 		}
    230 		if len(p.Assets.Misc) != 1 {
    231 			t.Error("invalid number of Assets.Misc for page:", p.Path)
    232 		}
    233 		if pt, e = time.Parse(timefmt, p.Updated); e != nil {
    234 			t.Fatal(e)
    235 		}
    236 
    237 		if i == 0 {
    238 			last = pt
    239 		} else if pt.Before(last) {
    240 			for _, pp := range pages {
    241 				t.Logf("%s - %s", pp.Path, pp.Updated)
    242 			}
    243 			t.Error("Contents Pages returned in wrong order")
    244 		}
    245 	}
    246 }