github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/cmd/lgo-internal/liner/liner_test.go (about)

     1  package liner
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestContinueLine(t *testing.T) {
     8  	tests := []struct {
     9  		lines  []string
    10  		expect bool
    11  		indent int
    12  	}{{
    13  		lines:  []string{"x +"},
    14  		expect: true,
    15  	}, {
    16  		lines:  []string{" y +"},
    17  		expect: true,
    18  	}, {
    19  		lines:  []string{"func"},
    20  		expect: true,
    21  	}, {
    22  		lines:  []string{"     func"},
    23  		expect: true,
    24  	}, {
    25  		lines:  []string{"if"},
    26  		expect: true,
    27  	}, {
    28  		// syntax error.
    29  		lines:  []string{"if {"},
    30  		expect: false,
    31  	}, {
    32  		lines:  []string{"for {"},
    33  		expect: true,
    34  		indent: 1,
    35  	}, {
    36  		lines:  []string{"for{for{for{{"},
    37  		expect: true,
    38  		indent: 4,
    39  	}, {
    40  		lines:  []string{"for{for{for{{}"},
    41  		expect: true,
    42  		indent: 3,
    43  	}, {
    44  		lines: []string{"func main()"},
    45  		// This is false because `func f()<newline>{}` is invalid in Go`
    46  		expect: false,
    47  	}, {
    48  		// Don't return true even if "missing function body" occurrs.
    49  		lines:  []string{"func main()", "func main2(){}"},
    50  		expect: false,
    51  	}, {
    52  		lines: []string{"import fmt"},
    53  		// This must be true because `import fmt<newline>"fmt"` is invalid.
    54  		// TODO: Fix this
    55  		expect: true,
    56  	}, {
    57  		lines:  []string{"func main("},
    58  		expect: true,
    59  	}, {
    60  		lines:  []string{"func main(x,"},
    61  		expect: true,
    62  	}, {
    63  		lines: []string{"func main(x"},
    64  		// Strickly speaking, this should be false if there is no possible valid statement with this.
    65  		expect: true,
    66  	}, {
    67  		lines:  []string{"func ("},
    68  		expect: true,
    69  	}, {
    70  		lines:  []string{"func (r interface{"},
    71  		expect: true,
    72  		indent: 1,
    73  	}, {
    74  		lines:  []string{"/* comment "},
    75  		expect: true,
    76  	}, {
    77  		lines:  []string{"`raw string"},
    78  		expect: true,
    79  	}, {
    80  		lines:  []string{"for {", "for {"},
    81  		expect: true,
    82  		indent: 2,
    83  	}, {
    84  		lines:  []string{"for {", " "},
    85  		expect: true,
    86  		indent: 1,
    87  	}, {
    88  		lines:  []string{"for {", "\t"},
    89  		expect: true,
    90  		indent: 1,
    91  	}, {
    92  		// This tests dropEmptyLine.
    93  		lines:  []string{"for {", ""},
    94  		expect: true,
    95  		indent: 1,
    96  	}, {
    97  		lines:  []string{"type s struct {}"},
    98  		expect: true,
    99  	}, {
   100  		lines:  []string{"type s struct {}", ""},
   101  		expect: false,
   102  	}, {
   103  		lines:  []string{"type s struct {}", "   "},
   104  		expect: false,
   105  	}, {
   106  		lines:  []string{"func (s) f(){}"},
   107  		expect: true,
   108  	}, {
   109  		lines:  []string{"func (s) f(){}", ""},
   110  		expect: false,
   111  	},
   112  	}
   113  
   114  	for _, tc := range tests {
   115  		t.Run("", func(t *testing.T) {
   116  			var lines []string
   117  			for _, l := range tc.lines {
   118  				lines = append(lines, l)
   119  			}
   120  			cont, indent := continueLine(lines)
   121  			if cont != tc.expect {
   122  				t.Errorf("Expected %v but got %v for %#v", tc.expect, cont, tc.lines)
   123  				return
   124  			}
   125  			if indent != tc.indent {
   126  				t.Errorf("Expected %d but got %d for %#v", tc.indent, indent, tc.lines)
   127  			}
   128  		})
   129  	}
   130  }