github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/edit/fix_test.go (about)

     1  package edit
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/cheshirekow/buildtools/build"
     8  )
     9  
    10  func TestMovePackageDeclarationToTheTop(t *testing.T) {
    11  	tests := []struct {
    12  		input, expected string
    13  		shouldMove      bool
    14  	}{
    15  		{`"""Docstring."""
    16  
    17  load(":path.bzl", "x")
    18  
    19  foo()
    20  
    21  package(attr = "val")`,
    22  			`"""Docstring."""
    23  
    24  load(":path.bzl", "x")
    25  
    26  package(attr = "val")
    27  
    28  foo()`,
    29  			true},
    30  		{`"""Docstring."""
    31  
    32  load(":path.bzl", "x")
    33  
    34  package(attr = "val")
    35  
    36  foo()`,
    37  			`"""Docstring."""
    38  
    39  load(":path.bzl", "x")
    40  
    41  package(attr = "val")
    42  
    43  foo()`,
    44  			false},
    45  		{`"""Docstring."""
    46  
    47  load(":path.bzl", "x")
    48  
    49  foo()`,
    50  			`"""Docstring."""
    51  
    52  load(":path.bzl", "x")
    53  
    54  foo()`,
    55  			false,
    56  		},
    57  	}
    58  
    59  	for _, tst := range tests {
    60  		bld, err := build.Parse("BUILD", []byte(tst.input))
    61  		if err != nil {
    62  			t.Error(err)
    63  			continue
    64  		}
    65  		if result := movePackageDeclarationToTheTop(bld); result != tst.shouldMove {
    66  			t.Errorf("TestMovePackageDeclarationToTheTop: expected %v, got %v", tst.shouldMove, result)
    67  		}
    68  
    69  		got := strings.TrimSpace(string(build.Format(bld)))
    70  		want := strings.TrimSpace(tst.expected)
    71  
    72  		if got != want {
    73  			t.Errorf("TestMovePackageDeclarationToTheTop: got:\n%s\nexpected:\n%s", got, want)
    74  		}
    75  	}
    76  }