github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/cmds/core/elvish/util/source_range_test.go (about)

     1  package util
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  var sourceRangeTests = []struct {
     9  	*SourceRange
    10  	indent            string
    11  	wantPprint        string
    12  	wantPprintCompact string
    13  }{
    14  	// Single-line culprit
    15  	{parseSourceRange("echo (bad)", "(", ")", true), "_",
    16  		`
    17  [test], line 1:
    18  _echo <(bad)>`[1:],
    19  
    20  		`[test], line 1: echo <(bad)>`,
    21  	},
    22  	// Multi-line culprit
    23  	{parseSourceRange("echo (bad\nbad)", "(", ")", true), "_",
    24  		`
    25  [test], line 1-2:
    26  _echo <(bad>
    27  _<bad)>`[1:],
    28  		`
    29  [test], line 1-2: echo <(bad>
    30  _                  <bad)>`[1:],
    31  	},
    32  	// Empty culprit
    33  	{parseSourceRange("echo x", "x", "x", false), "",
    34  		`
    35  [test], line 1:
    36  echo <^>x`[1:],
    37  		"[test], line 1: echo <^>x",
    38  	},
    39  }
    40  
    41  func TestSourceRange(t *testing.T) {
    42  	CulpritLineBegin = "<"
    43  	CulpritLineEnd = ">"
    44  	for i, test := range sourceRangeTests {
    45  		gotPprint := test.SourceRange.Pprint(test.indent)
    46  		if gotPprint != test.wantPprint {
    47  			t.Errorf("test%d.Pprint(%q) = %q, want %q",
    48  				i, test.indent, gotPprint, test.wantPprint)
    49  		}
    50  		gotPprintCompact := test.SourceRange.PprintCompact(test.indent)
    51  		if gotPprintCompact != test.wantPprintCompact {
    52  			t.Errorf("test%d.PprintCompact(%q) = %q, want %q",
    53  				i, test.indent, gotPprintCompact, test.wantPprintCompact)
    54  		}
    55  	}
    56  }
    57  
    58  // Parse a string into a source range, using the first appearance of certain
    59  // texts as start and end positions.
    60  func parseSourceRange(s, starter, ender string, endAfter bool) *SourceRange {
    61  	end := strings.Index(s, ender)
    62  	if endAfter {
    63  		end += len(ender)
    64  	}
    65  	return NewSourceRange("[test]", s, strings.Index(s, starter), end)
    66  }