github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/edit/highlight/emitter_test.go (about)

     1  package highlight
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/u-root/u-root/cmds/elvish/parse"
     9  )
    10  
    11  type styling struct {
    12  	begin int
    13  	end   int
    14  	style string
    15  }
    16  
    17  type emitTests struct {
    18  	source       string
    19  	wantStylings []styling
    20  }
    21  
    22  // In the test cases, commands that start with x are bad, everything else is
    23  // good.
    24  func goodFormHead(head string) bool { return !strings.HasPrefix(head, "x") }
    25  
    26  // This just tests the Highlight method itself, its dependencies are tested
    27  // below.
    28  var emitAllTests = []emitTests{
    29  	//01234
    30  	{"x 'y'", []styling{
    31  		{0, 1, styleForBadCommand.String()},
    32  		{0, 1, styleForPrimary[parse.Bareword].String()},
    33  		{2, 5, styleForPrimary[parse.SingleQuoted].String()},
    34  	}},
    35  }
    36  
    37  func TestEmitAll(t *testing.T) {
    38  	test(t, "form", emitAllTests,
    39  		func(e *Emitter, ps *parse.Parser) {
    40  			e.EmitAll(parse.ParseChunk(ps))
    41  		})
    42  }
    43  
    44  var formTests = []emitTests{
    45  	// Temporary assignments.
    46  	{"a=1 b=2", []styling{
    47  		{0, 1, styleForGoodVariable.String()},
    48  		{4, 5, styleForGoodVariable.String()}}},
    49  	// Normal assignments,
    50  	{"a b = 1 2", []styling{
    51  		{0, 1, styleForGoodVariable.String()},
    52  		{2, 3, styleForGoodVariable.String()}}},
    53  	// Good commands.
    54  	{"a", []styling{{0, 1, styleForGoodCommand.String()}}},
    55  	// Bad commands.
    56  	{"xabc", []styling{{0, 4, styleForBadCommand.String()}}},
    57  	{"'xa'", []styling{{0, 4, styleForBadCommand.String()}}},
    58  
    59  	// "for".
    60  	// Highlighting variable.
    61  	//012345678901
    62  	{"for x [] { }", []styling{
    63  		{0, 3, styleForGoodCommand.String()},
    64  		{4, 5, styleForGoodVariable.String()}}},
    65  	// Highlighting variable, incomplete form.
    66  	//01234
    67  	{"for x", []styling{
    68  		{0, 3, styleForGoodCommand.String()},
    69  		{4, 5, styleForGoodVariable.String()}}},
    70  	// Highlighting variable and "else".
    71  	//012345678901234567890
    72  	{"for x [] { } else { }", []styling{
    73  		{0, 3, styleForGoodCommand.String()},
    74  		{4, 5, styleForGoodVariable.String()},
    75  		{13, 17, styleForSep["else"]}}},
    76  
    77  	// "try".
    78  	// Highlighting except-variable.
    79  	//01234567890123456789
    80  	{"try { } except x { }", []styling{
    81  		{0, 3, styleForGoodCommand.String()},
    82  		{8, 14, styleForSep["except"]},
    83  		{15, 16, styleForGoodVariable.String()},
    84  	}},
    85  	// Highlighting except-variable, incomplete form.
    86  	//0123456789012345
    87  	{"try { } except x", []styling{
    88  		{0, 3, styleForGoodCommand.String()},
    89  		{8, 14, styleForSep["except"]},
    90  		{15, 16, styleForGoodVariable.String()},
    91  	}},
    92  	// Highlighting "else" and "finally".
    93  	//0123456789012345678901234567
    94  	{"try { } else { } finally { }", []styling{
    95  		{0, 3, styleForGoodCommand.String()},
    96  		{8, 12, styleForSep["else"]},
    97  		{17, 24, styleForSep["finally"]},
    98  	}},
    99  }
   100  
   101  func testForm(t *testing.T) {
   102  	test(t, "form", formTests,
   103  		func(e *Emitter, ps *parse.Parser) {
   104  			e.form(parse.ParseForm(ps))
   105  		})
   106  }
   107  
   108  var primaryTests = []emitTests{
   109  	{"what", []styling{{0, 4, styleForPrimary[parse.Bareword].String()}}},
   110  	{"$var", []styling{{0, 4, styleForPrimary[parse.Variable].String()}}},
   111  	{"'a'", []styling{{0, 3, styleForPrimary[parse.SingleQuoted].String()}}},
   112  	{`"x"`, []styling{{0, 3, styleForPrimary[parse.DoubleQuoted].String()}}},
   113  }
   114  
   115  func TestPrimary(t *testing.T) {
   116  	test(t, "primary", primaryTests,
   117  		func(e *Emitter, ps *parse.Parser) {
   118  			e.primary(parse.ParsePrimary(ps, parse.NormalExpr))
   119  		})
   120  }
   121  
   122  var sepTests = []emitTests{
   123  	{">", []styling{{0, 1, styleForSep[">"]}}},
   124  	{"# comment", []styling{{0, 9, styleForComment.String()}}},
   125  }
   126  
   127  func TestSep(t *testing.T) {
   128  	test(t, "sep", sepTests,
   129  		func(e *Emitter, ps *parse.Parser) {
   130  			src := ps.Source()
   131  			e.sep(parse.NewSep(src, 0, len(src)))
   132  		})
   133  }
   134  
   135  func test(t *testing.T, what string,
   136  	tests []emitTests, f func(*Emitter, *parse.Parser)) {
   137  
   138  	for _, test := range tests {
   139  		var stylings []styling
   140  		e := &Emitter{goodFormHead, func(b, e int, s string) {
   141  			stylings = append(stylings, styling{b, e, s})
   142  		}}
   143  		ps := parse.NewParser("<test>", test.source)
   144  
   145  		f(e, ps)
   146  
   147  		if !reflect.DeepEqual(stylings, test.wantStylings) {
   148  			t.Errorf("%s %q gets stylings %v, want %v", what, test.source,
   149  				stylings, test.wantStylings)
   150  		}
   151  	}
   152  }