2022-05-02 10:07:52 -04:00
|
|
|
// Copyright 2022 The Hugo Authors. All rights reserved.
|
2018-10-03 08:58:09 -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.
|
|
|
|
|
2020-04-05 19:07:57 -04:00
|
|
|
// Package herrors contains common Hugo errors and error related utilities.
|
2018-10-03 08:58:09 -04:00
|
|
|
package herrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-10-03 08:58:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrorLocator(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
lineMatcher := func(m LineMatcher) int {
|
|
|
|
if strings.Contains(m.Line, "THEONE") {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lines := `LINE 1
|
|
|
|
LINE 2
|
|
|
|
LINE 3
|
|
|
|
LINE 4
|
|
|
|
This is THEONE
|
|
|
|
LINE 6
|
|
|
|
LINE 7
|
|
|
|
LINE 8
|
|
|
|
`
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
location := locateErrorInString(lines, lineMatcher)
|
|
|
|
pos := location.Position
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"LINE 3", "LINE 4", "This is THEONE", "LINE 6", "LINE 7"})
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(pos.LineNumber, qt.Equals, 5)
|
|
|
|
c.Assert(location.LinesPos, qt.Equals, 2)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
locate := func(s string, m LineMatcherFn) *ErrorContext {
|
2022-05-12 05:43:20 -04:00
|
|
|
ctx := locateErrorInString(s, m)
|
2022-05-02 10:07:52 -04:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(locate(`This is THEONE`, lineMatcher).Lines, qt.DeepEquals, []string{"This is THEONE"})
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
location = locateErrorInString(`L1
|
2018-10-03 08:58:09 -04:00
|
|
|
This is THEONE
|
|
|
|
L2
|
|
|
|
`, lineMatcher)
|
2022-05-12 05:43:20 -04:00
|
|
|
pos = location.Position
|
2022-05-02 10:07:52 -04:00
|
|
|
c.Assert(pos.LineNumber, qt.Equals, 2)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.LinesPos, qt.Equals, 1)
|
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"L1", "This is THEONE", "L2", ""})
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
location = locate(`This is THEONE
|
2018-10-03 08:58:09 -04:00
|
|
|
L2
|
|
|
|
`, lineMatcher)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.LinesPos, qt.Equals, 0)
|
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"This is THEONE", "L2", ""})
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
location = locate(`L1
|
2018-10-03 08:58:09 -04:00
|
|
|
This THEONE
|
|
|
|
`, lineMatcher)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"L1", "This THEONE", ""})
|
|
|
|
c.Assert(location.LinesPos, qt.Equals, 1)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
location = locate(`L1
|
2018-10-03 08:58:09 -04:00
|
|
|
L2
|
|
|
|
This THEONE
|
|
|
|
`, lineMatcher)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"L1", "L2", "This THEONE", ""})
|
|
|
|
c.Assert(location.LinesPos, qt.Equals, 2)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
location = locateErrorInString("NO MATCH", lineMatcher)
|
|
|
|
pos = location.Position
|
|
|
|
c.Assert(pos.LineNumber, qt.Equals, 0)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.LinesPos, qt.Equals, -1)
|
|
|
|
c.Assert(len(location.Lines), qt.Equals, 0)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
lineMatcher = func(m LineMatcher) int {
|
|
|
|
if m.LineNumber == 6 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2018-10-23 02:54:10 -04:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
location = locateErrorInString(`A
|
2018-10-03 08:58:09 -04:00
|
|
|
B
|
|
|
|
C
|
|
|
|
D
|
|
|
|
E
|
|
|
|
F
|
|
|
|
G
|
|
|
|
H
|
|
|
|
I
|
|
|
|
J`, lineMatcher)
|
2022-05-12 05:43:20 -04:00
|
|
|
pos = location.Position
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"D", "E", "F", "G", "H"})
|
2022-05-02 10:07:52 -04:00
|
|
|
c.Assert(pos.LineNumber, qt.Equals, 6)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.LinesPos, qt.Equals, 2)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
// Test match EOF
|
2022-05-12 05:43:20 -04:00
|
|
|
lineMatcher = func(m LineMatcher) int {
|
|
|
|
if m.LineNumber == 4 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
location = locateErrorInString(`A
|
2018-10-03 08:58:09 -04:00
|
|
|
B
|
|
|
|
C
|
|
|
|
`, lineMatcher)
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
pos = location.Position
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"B", "C", ""})
|
2022-05-02 10:07:52 -04:00
|
|
|
c.Assert(pos.LineNumber, qt.Equals, 4)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.LinesPos, qt.Equals, 2)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
offsetMatcher := func(m LineMatcher) int {
|
|
|
|
if m.Offset == 1 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
2018-10-23 02:54:10 -04:00
|
|
|
}
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
location = locateErrorInString(`A
|
2018-10-23 02:54:10 -04:00
|
|
|
B
|
|
|
|
C
|
|
|
|
D
|
|
|
|
E`, offsetMatcher)
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
pos = location.Position
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.Lines, qt.DeepEquals, []string{"A", "B", "C", "D"})
|
2022-05-02 10:07:52 -04:00
|
|
|
c.Assert(pos.LineNumber, qt.Equals, 2)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(location.LinesPos, qt.Equals, 1)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|