fileInfo_test.go (2025B)
1 // Copyright 2017-present 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 source 15 16 import ( 17 "path/filepath" 18 "strings" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 ) 23 24 func TestFileInfo(t *testing.T) { 25 c := qt.New(t) 26 27 s := newTestSourceSpec() 28 29 for _, this := range []struct { 30 base string 31 filename string 32 assert func(f *FileInfo) 33 }{ 34 {filepath.FromSlash("/a/"), filepath.FromSlash("/a/b/page.md"), func(f *FileInfo) { 35 c.Assert(f.Filename(), qt.Equals, filepath.FromSlash("/a/b/page.md")) 36 c.Assert(f.Dir(), qt.Equals, filepath.FromSlash("b/")) 37 c.Assert(f.Path(), qt.Equals, filepath.FromSlash("b/page.md")) 38 c.Assert(f.Section(), qt.Equals, "b") 39 c.Assert(f.TranslationBaseName(), qt.Equals, filepath.FromSlash("page")) 40 c.Assert(f.BaseFileName(), qt.Equals, filepath.FromSlash("page")) 41 }}, 42 {filepath.FromSlash("/a/"), filepath.FromSlash("/a/b/c/d/page.md"), func(f *FileInfo) { 43 c.Assert(f.Section(), qt.Equals, "b") 44 }}, 45 {filepath.FromSlash("/a/"), filepath.FromSlash("/a/b/page.en.MD"), func(f *FileInfo) { 46 c.Assert(f.Section(), qt.Equals, "b") 47 c.Assert(f.Path(), qt.Equals, filepath.FromSlash("b/page.en.MD")) 48 c.Assert(f.TranslationBaseName(), qt.Equals, filepath.FromSlash("page")) 49 c.Assert(f.BaseFileName(), qt.Equals, filepath.FromSlash("page.en")) 50 }}, 51 } { 52 path := strings.TrimPrefix(this.filename, this.base) 53 f, err := s.NewFileInfoFrom(path, this.filename) 54 c.Assert(err, qt.IsNil) 55 this.assert(f) 56 } 57 }