github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/common/herrors/error_locator_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 contains common Hugo errors and error related utilities.
    15  package herrors
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	qt "github.com/frankban/quicktest"
    22  )
    23  
    24  func TestErrorLocator(t *testing.T) {
    25  	c := qt.New(t)
    26  
    27  	lineMatcher := func(m LineMatcher) bool {
    28  		return strings.Contains(m.Line, "THEONE")
    29  	}
    30  
    31  	lines := `LINE 1
    32  LINE 2
    33  LINE 3
    34  LINE 4
    35  This is THEONE
    36  LINE 6
    37  LINE 7
    38  LINE 8
    39  `
    40  
    41  	location := locateErrorInString(lines, lineMatcher)
    42  	c.Assert(location.Lines, qt.DeepEquals, []string{"LINE 3", "LINE 4", "This is THEONE", "LINE 6", "LINE 7"})
    43  
    44  	pos := location.Position()
    45  	c.Assert(pos.LineNumber, qt.Equals, 5)
    46  	c.Assert(location.LinesPos, qt.Equals, 2)
    47  
    48  	c.Assert(locateErrorInString(`This is THEONE`, lineMatcher).Lines, qt.DeepEquals, []string{"This is THEONE"})
    49  
    50  	location = locateErrorInString(`L1
    51  This is THEONE
    52  L2
    53  `, lineMatcher)
    54  	c.Assert(location.Position().LineNumber, qt.Equals, 2)
    55  	c.Assert(location.LinesPos, qt.Equals, 1)
    56  	c.Assert(location.Lines, qt.DeepEquals, []string{"L1", "This is THEONE", "L2", ""})
    57  
    58  	location = locateErrorInString(`This is THEONE
    59  L2
    60  `, lineMatcher)
    61  	c.Assert(location.LinesPos, qt.Equals, 0)
    62  	c.Assert(location.Lines, qt.DeepEquals, []string{"This is THEONE", "L2", ""})
    63  
    64  	location = locateErrorInString(`L1
    65  This THEONE
    66  `, lineMatcher)
    67  	c.Assert(location.Lines, qt.DeepEquals, []string{"L1", "This THEONE", ""})
    68  	c.Assert(location.LinesPos, qt.Equals, 1)
    69  
    70  	location = locateErrorInString(`L1
    71  L2
    72  This THEONE
    73  `, lineMatcher)
    74  	c.Assert(location.Lines, qt.DeepEquals, []string{"L1", "L2", "This THEONE", ""})
    75  	c.Assert(location.LinesPos, qt.Equals, 2)
    76  
    77  	location = locateErrorInString("NO MATCH", lineMatcher)
    78  	c.Assert(location.Position().LineNumber, qt.Equals, -1)
    79  	c.Assert(location.LinesPos, qt.Equals, -1)
    80  	c.Assert(len(location.Lines), qt.Equals, 0)
    81  
    82  	lineMatcher = func(m LineMatcher) bool {
    83  		return m.LineNumber == 6
    84  	}
    85  
    86  	location = locateErrorInString(`A
    87  B
    88  C
    89  D
    90  E
    91  F
    92  G
    93  H
    94  I
    95  J`, lineMatcher)
    96  
    97  	c.Assert(location.Lines, qt.DeepEquals, []string{"D", "E", "F", "G", "H"})
    98  	c.Assert(location.Position().LineNumber, qt.Equals, 6)
    99  	c.Assert(location.LinesPos, qt.Equals, 2)
   100  
   101  	// Test match EOF
   102  	lineMatcher = func(m LineMatcher) bool {
   103  		return m.LineNumber == 4
   104  	}
   105  
   106  	location = locateErrorInString(`A
   107  B
   108  C
   109  `, lineMatcher)
   110  
   111  	c.Assert(location.Lines, qt.DeepEquals, []string{"B", "C", ""})
   112  	c.Assert(location.Position().LineNumber, qt.Equals, 4)
   113  	c.Assert(location.LinesPos, qt.Equals, 2)
   114  
   115  	offsetMatcher := func(m LineMatcher) bool {
   116  		return m.Offset == 1
   117  	}
   118  
   119  	location = locateErrorInString(`A
   120  B
   121  C
   122  D
   123  E`, offsetMatcher)
   124  
   125  	c.Assert(location.Lines, qt.DeepEquals, []string{"A", "B", "C", "D"})
   126  	c.Assert(location.Position().LineNumber, qt.Equals, 2)
   127  	c.Assert(location.LinesPos, qt.Equals, 1)
   128  }