2019-01-02 06:33:26 -05:00
// Copyright 2019 The Hugo Authors. All rights reserved.
2018-03-15 04:37:30 -04:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugolib
import (
"fmt"
"testing"
2019-08-10 15:05:17 -04:00
qt "github.com/frankban/quicktest"
2019-01-02 05:58:32 -05:00
"github.com/gohugoio/hugo/resources/resource"
2018-03-15 04:37:30 -04:00
)
2019-01-02 06:33:26 -05:00
// TODO(bep) move and rewrite in resource/page.
2018-03-15 04:37:30 -04:00
func TestMergeLanguages ( t * testing . T ) {
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2018-03-15 04:37:30 -04:00
b := newTestSiteForLanguageMerge ( t , 30 )
b . CreateSites ( )
b . Build ( BuildCfg { SkipRender : true } )
h := b . H
enSite := h . Sites [ 0 ]
frSite := h . Sites [ 1 ]
nnSite := h . Sites [ 2 ]
2019-08-10 15:05:17 -04:00
c . Assert ( len ( enSite . RegularPages ( ) ) , qt . Equals , 31 )
c . Assert ( len ( frSite . RegularPages ( ) ) , qt . Equals , 6 )
c . Assert ( len ( nnSite . RegularPages ( ) ) , qt . Equals , 12 )
2018-03-15 04:37:30 -04:00
for i := 0 ; i < 2 ; i ++ {
2019-01-02 06:33:26 -05:00
mergedNN := nnSite . RegularPages ( ) . MergeByLanguage ( enSite . RegularPages ( ) )
2019-08-10 15:05:17 -04:00
c . Assert ( len ( mergedNN ) , qt . Equals , 31 )
2018-04-19 03:02:18 -04:00
for i := 1 ; i <= 31 ; i ++ {
2018-03-15 04:37:30 -04:00
expectedLang := "en"
2018-04-19 03:02:18 -04:00
if i == 2 || i % 3 == 0 || i == 31 {
2018-03-15 04:37:30 -04:00
expectedLang = "nn"
}
p := mergedNN [ i - 1 ]
2019-08-10 15:05:17 -04:00
c . Assert ( p . Language ( ) . Lang , qt . Equals , expectedLang )
2018-03-15 04:37:30 -04:00
}
}
2019-01-02 06:33:26 -05:00
mergedFR := frSite . RegularPages ( ) . MergeByLanguage ( enSite . RegularPages ( ) )
2019-08-10 15:05:17 -04:00
c . Assert ( len ( mergedFR ) , qt . Equals , 31 )
2018-04-19 03:02:18 -04:00
for i := 1 ; i <= 31 ; i ++ {
2018-03-15 04:37:30 -04:00
expectedLang := "en"
if i % 5 == 0 {
expectedLang = "fr"
}
p := mergedFR [ i - 1 ]
2019-08-10 15:05:17 -04:00
c . Assert ( p . Language ( ) . Lang , qt . Equals , expectedLang )
2018-03-15 04:37:30 -04:00
}
2019-01-02 06:33:26 -05:00
firstNN := nnSite . RegularPages ( ) [ 0 ]
2019-08-10 15:05:17 -04:00
c . Assert ( len ( firstNN . Sites ( ) ) , qt . Equals , 4 )
c . Assert ( firstNN . Sites ( ) . First ( ) . Language ( ) . Lang , qt . Equals , "en" )
2018-03-15 04:37:30 -04:00
2018-07-17 05:18:29 -04:00
nnBundle := nnSite . getPage ( "page" , "bundle" )
enBundle := enSite . getPage ( "page" , "bundle" )
2018-04-19 03:02:18 -04:00
2019-08-10 15:05:17 -04:00
c . Assert ( len ( enBundle . Resources ( ) ) , qt . Equals , 6 )
c . Assert ( len ( nnBundle . Resources ( ) ) , qt . Equals , 2 )
2018-04-19 03:02:18 -04:00
2019-01-02 06:33:26 -05:00
var ri interface { } = nnBundle . Resources ( )
2018-04-19 03:02:18 -04:00
// This looks less ugly in the templates ...
2019-01-02 06:33:26 -05:00
mergedNNResources := ri . ( resource . ResourcesLanguageMerger ) . MergeByLanguage ( enBundle . Resources ( ) )
2019-08-10 15:05:17 -04:00
c . Assert ( len ( mergedNNResources ) , qt . Equals , 6 )
2018-07-21 05:41:23 -04:00
2019-01-02 06:33:26 -05:00
unchanged , err := nnSite . RegularPages ( ) . MergeByLanguageInterface ( nil )
2019-08-10 15:05:17 -04:00
c . Assert ( err , qt . IsNil )
c . Assert ( unchanged , deepEqualsPages , nnSite . RegularPages ( ) )
2018-07-21 05:41:23 -04:00
2018-03-15 04:37:30 -04:00
}
func TestMergeLanguagesTemplate ( t * testing . T ) {
t . Parallel ( )
b := newTestSiteForLanguageMerge ( t , 15 )
b . WithTemplates ( "home.html" , `
{ { $ pages := . Site . RegularPages } }
{ { . Scratch . Set "pages" $ pages } }
2019-01-02 06:33:26 -05:00
{ { if eq . Language . Lang "nn" } } :
2018-03-15 04:37:30 -04:00
{ { $ enSite := index . Sites 0 } }
{ { $ frSite := index . Sites 1 } }
2018-04-19 03:02:18 -04:00
{ { $ nnBundle := . Site . GetPage "page" "bundle" } }
{ { $ enBundle := $ enSite . GetPage "page" "bundle" } }
2018-03-15 04:37:30 -04:00
{ { . Scratch . Set "pages" ( $ pages | lang . Merge $ frSite . RegularPages | lang . Merge $ enSite . RegularPages ) } }
2018-04-19 03:02:18 -04:00
{ { . Scratch . Set "pages2" ( sort ( $ nnBundle . Resources | lang . Merge $ enBundle . Resources ) "Title" ) } }
2018-03-15 04:37:30 -04:00
{ { end } }
{ { $ pages := . Scratch . Get "pages" } }
2018-04-19 03:02:18 -04:00
{ { $ pages2 := . Scratch . Get "pages2" } }
2019-01-02 06:33:26 -05:00
Pages1 : { { range $ i , $ p := $ pages } } { { add $ i 1 } } : { { . File . Path } } { { . Language . Lang } } | { { end } }
Pages2 : { { range $ i , $ p := $ pages2 } } { { add $ i 1 } } : { { . Title } } { { . Language . Lang } } | { { end } }
2018-04-19 03:02:18 -04:00
2018-03-15 04:37:30 -04:00
` ,
"shortcodes/shortcode.html" , "MyShort" ,
"shortcodes/lingo.html" , "MyLingo" ,
)
b . CreateSites ( )
b . Build ( BuildCfg { } )
2018-04-19 03:02:18 -04:00
b . AssertFileContent ( "public/nn/index.html" , "Pages1: 1: p1.md en | 2: p2.nn.md nn | 3: p3.nn.md nn | 4: p4.md en | 5: p5.fr.md fr | 6: p6.nn.md nn | 7: p7.md en | 8: p8.md en | 9: p9.nn.md nn | 10: p10.fr.md fr | 11: p11.md en | 12: p12.nn.md nn | 13: p13.md en | 14: p14.md en | 15: p15.nn.md nn" )
b . AssertFileContent ( "public/nn/index.html" , "Pages2: 1: doc100 en | 2: doc101 nn | 3: doc102 nn | 4: doc103 en | 5: doc104 en | 6: doc105 en" )
2018-03-15 04:37:30 -04:00
}
func newTestSiteForLanguageMerge ( t testing . TB , count int ) * sitesBuilder {
contentTemplate := ` -- -
title : doc % d
weight : % d
date : "2018-02-28"
-- -
# doc
* some "content" *
{ { < shortcode > } }
{ { < lingo > } }
`
builder := newTestSitesBuilder ( t ) . WithDefaultMultiSiteConfig ( )
// We need some content with some missing translations.
// "en" is the main language, so add some English content + some Norwegian (nn, nynorsk) content.
var contentPairs [ ] string
for i := 1 ; i <= count ; i ++ {
content := fmt . Sprintf ( contentTemplate , i , i )
contentPairs = append ( contentPairs , [ ] string { fmt . Sprintf ( "p%d.md" , i ) , content } ... )
if i == 2 || i % 3 == 0 {
// Add page 2,3, 6, 9 ... to both languages
contentPairs = append ( contentPairs , [ ] string { fmt . Sprintf ( "p%d.nn.md" , i ) , content } ... )
}
if i % 5 == 0 {
// Add some French content, too.
contentPairs = append ( contentPairs , [ ] string { fmt . Sprintf ( "p%d.fr.md" , i ) , content } ... )
}
}
2018-04-19 03:02:18 -04:00
// See https://github.com/gohugoio/hugo/issues/4644
// Add a bundles
j := 100
contentPairs = append ( contentPairs , [ ] string { "bundle/index.md" , fmt . Sprintf ( contentTemplate , j , j ) } ... )
for i := 0 ; i < 6 ; i ++ {
contentPairs = append ( contentPairs , [ ] string { fmt . Sprintf ( "bundle/pb%d.md" , i ) , fmt . Sprintf ( contentTemplate , i + j , i + j ) } ... )
}
contentPairs = append ( contentPairs , [ ] string { "bundle/index.nn.md" , fmt . Sprintf ( contentTemplate , j , j ) } ... )
for i := 1 ; i < 3 ; i ++ {
contentPairs = append ( contentPairs , [ ] string { fmt . Sprintf ( "bundle/pb%d.nn.md" , i ) , fmt . Sprintf ( contentTemplate , i + j , i + j ) } ... )
}
2018-03-15 04:37:30 -04:00
builder . WithContent ( contentPairs ... )
return builder
}
func BenchmarkMergeByLanguage ( b * testing . B ) {
const count = 100
builder := newTestSiteForLanguageMerge ( b , count )
builder . CreateSites ( )
builder . Build ( BuildCfg { SkipRender : true } )
h := builder . H
enSite := h . Sites [ 0 ]
nnSite := h . Sites [ 2 ]
for i := 0 ; i < b . N ; i ++ {
2019-01-02 06:33:26 -05:00
merged := nnSite . RegularPages ( ) . MergeByLanguage ( enSite . RegularPages ( ) )
2018-03-15 04:37:30 -04:00
if len ( merged ) != count {
b . Fatal ( "Count mismatch" )
}
}
}