github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/common/herrors/file_error_test.go (about) 1 // Copyright 2018 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 herrors 15 16 import ( 17 "testing" 18 19 "github.com/pkg/errors" 20 21 qt "github.com/frankban/quicktest" 22 ) 23 24 func TestToLineNumberError(t *testing.T) { 25 t.Parallel() 26 27 c := qt.New(t) 28 29 for i, test := range []struct { 30 in error 31 offset int 32 lineNumber int 33 columnNumber int 34 }{ 35 {errors.New("no line number for you"), 0, 1, 1}, 36 {errors.New(`template: _default/single.html:4:15: executing "_default/single.html" at <.Titles>: can't evaluate field Titles in type *hugolib.PageOutput`), 0, 4, 15}, 37 {errors.New("parse failed: template: _default/bundle-resource-meta.html:11: unexpected in operand"), 0, 11, 1}, 38 {errors.New(`failed:: template: _default/bundle-resource-meta.html:2:7: executing "main" at <.Titles>`), 0, 2, 7}, 39 {errors.New(`failed to load translations: (6, 7): was expecting token =, but got "g" instead`), 0, 6, 7}, 40 } { 41 42 got := ToFileError("template", test.in) 43 44 errMsg := qt.Commentf("[%d][%T]", i, got) 45 le, ok := got.(FileError) 46 c.Assert(ok, qt.Equals, true) 47 48 c.Assert(ok, qt.Equals, true, errMsg) 49 pos := le.Position() 50 c.Assert(pos.LineNumber, qt.Equals, test.lineNumber, errMsg) 51 c.Assert(pos.ColumnNumber, qt.Equals, test.columnNumber, errMsg) 52 c.Assert(errors.Cause(got), qt.Not(qt.IsNil)) 53 } 54 }