git.greeks.studio/lethews/hugo@v0.47.1/hugolib/page_taxonomy_test.go (about) 1 // Copyright 2015 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package hugolib 15 16 import ( 17 "reflect" 18 "strings" 19 "testing" 20 ) 21 22 var pageYamlWithTaxonomiesA = `--- 23 tags: ['a', 'B', 'c'] 24 categories: 'd' 25 --- 26 YAML frontmatter with tags and categories taxonomy.` 27 28 var pageYamlWithTaxonomiesB = `--- 29 tags: 30 - "a" 31 - "B" 32 - "c" 33 categories: 'd' 34 --- 35 YAML frontmatter with tags and categories taxonomy.` 36 37 var pageYamlWithTaxonomiesC = `--- 38 tags: 'E' 39 categories: 'd' 40 --- 41 YAML frontmatter with tags and categories taxonomy.` 42 43 var pageJSONWithTaxonomies = `{ 44 "categories": "D", 45 "tags": [ 46 "a", 47 "b", 48 "c" 49 ] 50 } 51 JSON Front Matter with tags and categories` 52 53 var pageTomlWithTaxonomies = `+++ 54 tags = [ "a", "B", "c" ] 55 categories = "d" 56 +++ 57 TOML Front Matter with tags and categories` 58 59 func TestParseTaxonomies(t *testing.T) { 60 t.Parallel() 61 for _, test := range []string{pageTomlWithTaxonomies, 62 pageJSONWithTaxonomies, 63 pageYamlWithTaxonomiesA, 64 pageYamlWithTaxonomiesB, 65 pageYamlWithTaxonomiesC, 66 } { 67 68 s := newTestSite(t) 69 p, _ := s.NewPage("page/with/taxonomy") 70 _, err := p.ReadFrom(strings.NewReader(test)) 71 if err != nil { 72 t.Fatalf("Failed parsing %q: %s", test, err) 73 } 74 75 param := p.getParamToLower("tags") 76 77 if params, ok := param.([]string); ok { 78 expected := []string{"a", "b", "c"} 79 if !reflect.DeepEqual(params, expected) { 80 t.Errorf("Expected %s: got: %s", expected, params) 81 } 82 } else if params, ok := param.(string); ok { 83 expected := "e" 84 if params != expected { 85 t.Errorf("Expected %s: got: %s", expected, params) 86 } 87 } 88 89 param = p.getParamToLower("categories") 90 singleparam := param.(string) 91 92 if singleparam != "d" { 93 t.Fatalf("Expected: d, got: %s", singleparam) 94 } 95 } 96 }