templatedescriptor_test.go - hugo - [fork] hugo port for 9front
 (HTM) git clone https://git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
       templatedescriptor_test.go (4193B)
       ---
            1 package tplimpl
            2 
            3 import (
            4         "testing"
            5 
            6         qt "github.com/frankban/quicktest"
            7         "github.com/gohugoio/hugo/output"
            8         "github.com/gohugoio/hugo/resources/kinds"
            9 )
           10 
           11 func TestTemplateDescriptorCompare(t *testing.T) {
           12         c := qt.New(t)
           13 
           14         dh := descriptorHandler{
           15                 opts: StoreOptions{
           16                         OutputFormats:       output.DefaultFormats,
           17                         DefaultOutputFormat: "html",
           18                 },
           19         }
           20 
           21         less := func(category Category, this, other1, other2 TemplateDescriptor) {
           22                 c.Helper()
           23                 result1 := dh.compareDescriptors(category, false, this, other1)
           24                 result2 := dh.compareDescriptors(category, false, this, other2)
           25                 c.Assert(result1.w1 < result2.w1, qt.IsTrue, qt.Commentf("%d < %d", result1, result2))
           26         }
           27 
           28         check := func(category Category, this, other TemplateDescriptor, less bool) {
           29                 c.Helper()
           30                 result := dh.compareDescriptors(category, false, this, other)
           31                 if less {
           32                         c.Assert(result.w1 < 0, qt.IsTrue, qt.Commentf("%d", result))
           33                 } else {
           34                         c.Assert(result.w1 >= 0, qt.IsTrue, qt.Commentf("%d", result))
           35                 }
           36         }
           37 
           38         check(
           39 
           40                 CategoryBaseof,
           41                 TemplateDescriptor{Kind: "", LayoutFromTemplate: "", Lang: "", OutputFormat: "404", MediaType: "text/html"},
           42                 TemplateDescriptor{Kind: "", LayoutFromTemplate: "", Lang: "", OutputFormat: "html", MediaType: "text/html"},
           43                 false,
           44         )
           45 
           46         check(
           47                 CategoryLayout,
           48                 TemplateDescriptor{Kind: "", Lang: "en", OutputFormat: "404", MediaType: "text/html"},
           49                 TemplateDescriptor{Kind: "", LayoutFromTemplate: "", Lang: "", OutputFormat: "alias", MediaType: "text/html"},
           50                 true,
           51         )
           52 
           53         less(
           54                 CategoryLayout,
           55                 TemplateDescriptor{Kind: kinds.KindHome, LayoutFromTemplate: "list", OutputFormat: "html"},
           56                 TemplateDescriptor{LayoutFromTemplate: "list", OutputFormat: "html"},
           57                 TemplateDescriptor{Kind: kinds.KindHome, OutputFormat: "html"},
           58         )
           59 
           60         check(
           61                 CategoryLayout,
           62                 TemplateDescriptor{Kind: kinds.KindHome, LayoutFromTemplate: "list", OutputFormat: "html", MediaType: "text/html"},
           63                 TemplateDescriptor{Kind: kinds.KindHome, LayoutFromTemplate: "list", OutputFormat: "myformat", MediaType: "text/html"},
           64                 false,
           65         )
           66 }
           67 
           68 // INFO  timer:  name resolveTemplate count 779 duration 5.482274ms average 7.037µs median 4µs
           69 func BenchmarkCompareDescriptors(b *testing.B) {
           70         dh := descriptorHandler{
           71                 opts: StoreOptions{
           72                         OutputFormats:       output.DefaultFormats,
           73                         DefaultOutputFormat: "html",
           74                 },
           75         }
           76 
           77         pairs := []struct {
           78                 d1, d2 TemplateDescriptor
           79         }{
           80                 {
           81                         TemplateDescriptor{Kind: "", LayoutFromTemplate: "", OutputFormat: "404", MediaType: "text/html", Lang: "en", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           82                         TemplateDescriptor{Kind: "", LayoutFromTemplate: "", OutputFormat: "rss", MediaType: "application/rss+xml", Lang: "", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           83                 },
           84                 {
           85                         TemplateDescriptor{Kind: "page", LayoutFromTemplate: "single", OutputFormat: "html", MediaType: "text/html", Lang: "en", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           86                         TemplateDescriptor{Kind: "", LayoutFromTemplate: "list", OutputFormat: "", MediaType: "application/rss+xml", Lang: "", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           87                 },
           88                 {
           89                         TemplateDescriptor{Kind: "page", LayoutFromTemplate: "single", OutputFormat: "html", MediaType: "text/html", Lang: "en", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           90                         TemplateDescriptor{Kind: "", LayoutFromTemplate: "", OutputFormat: "alias", MediaType: "text/html", Lang: "", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           91                 },
           92                 {
           93                         TemplateDescriptor{Kind: "page", LayoutFromTemplate: "single", OutputFormat: "rss", MediaType: "application/rss+xml", Lang: "en", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           94                         TemplateDescriptor{Kind: "", LayoutFromTemplate: "single", OutputFormat: "rss", MediaType: "application/rss+xml", Lang: "nn", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false},
           95                 },
           96         }
           97 
           98         b.ResetTimer()
           99         for i := 0; i < b.N; i++ {
          100                 for _, pair := range pairs {
          101                         _ = dh.compareDescriptors(CategoryLayout, false, pair.d1, pair.d2)
          102                 }
          103         }
          104 }