| 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, this, other1, nil, nil) |
| 24 |
result2 := dh.compareDescriptors(category, this, other2, nil, nil) |
| 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, this, other, nil, nil) |
| 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: "", OutputFormat: "404", MediaType: "text/html"}, |
| 42 |
TemplateDescriptor{Kind: "", LayoutFromTemplate: "", OutputFormat: "html", MediaType: "text/html"}, |
| 43 |
false, |
| 44 |
) |
| 45 |
|
| 46 |
check( |
| 47 |
CategoryLayout, |
| 48 |
TemplateDescriptor{Kind: "", OutputFormat: "404", MediaType: "text/html"}, |
| 49 |
TemplateDescriptor{Kind: "", LayoutFromTemplate: "", 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", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 82 |
TemplateDescriptor{Kind: "", LayoutFromTemplate: "", OutputFormat: "rss", MediaType: "application/rss+xml", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 83 |
}, |
| 84 |
{ |
| 85 |
TemplateDescriptor{Kind: "page", LayoutFromTemplate: "single", OutputFormat: "html", MediaType: "text/html", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 86 |
TemplateDescriptor{Kind: "", LayoutFromTemplate: "list", OutputFormat: "", MediaType: "application/rss+xml", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
TemplateDescriptor{Kind: "page", LayoutFromTemplate: "single", OutputFormat: "html", MediaType: "text/html", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 90 |
TemplateDescriptor{Kind: "", LayoutFromTemplate: "", OutputFormat: "alias", MediaType: "text/html", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 91 |
}, |
| 92 |
{ |
| 93 |
TemplateDescriptor{Kind: "page", LayoutFromTemplate: "single", OutputFormat: "rss", MediaType: "application/rss+xml", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 94 |
TemplateDescriptor{Kind: "", LayoutFromTemplate: "single", OutputFormat: "rss", MediaType: "application/rss+xml", Variant1: "", Variant2: "", LayoutFromUserMustMatch: false, IsPlainText: false}, |
| 95 |
}, |
| 96 |
} |
| 97 |
|
| 98 |
for b.Loop() { |
| 99 |
for _, pair := range pairs { |
| 100 |
_ = dh.compareDescriptors(CategoryLayout, pair.d1, pair.d2, nil, nil) |
| 101 |
} |
| 102 |
} |
| 103 |
} |