gopkg.in/tools/godep.v21@v21.0.0-20151104013723-2cf1d6e3f557/diff_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  const (
     9  	d1 = `--- Godeps/Godeps.json
    10  +++ $GOPATH
    11  @@ -1,12 +1,12 @@
    12   {
    13          "ImportPath": "C",
    14          "GoVersion": "go1.2",
    15          "Deps": [
    16                  {
    17                          "ImportPath": "D101",
    18  -                       "Comment": "D202",
    19  +                       "Comment": "D303",
    20                          "Rev": ""
    21                  }
    22          ]
    23   }
    24  `
    25  
    26  	d2 = `--- Godeps/Godeps.json
    27  +++ $GOPATH
    28  @@ -1,12 +1,17 @@
    29   {
    30          "ImportPath": "C",
    31          "GoVersion": "go1.2",
    32          "Deps": [
    33                  {
    34                          "ImportPath": "D101",
    35                          "Comment": "D202",
    36                          "Rev": ""
    37  +               },
    38  +               {
    39  +                       "ImportPath": "D102",
    40  +                       "Comment": "D203",
    41  +                       "Rev": ""
    42                  }
    43          ]
    44   }
    45  `
    46  )
    47  
    48  var (
    49  	dep1 = Godeps{
    50  		ImportPath: "C",
    51  		GoVersion:  "go1.2",
    52  		Deps: []Dependency{
    53  			{ImportPath: "D101", Comment: "D202"},
    54  		},
    55  	}
    56  
    57  	dep2 = Godeps{
    58  		ImportPath: "C",
    59  		GoVersion:  "go1.2",
    60  		Deps: []Dependency{
    61  			{ImportPath: "D101", Comment: "D202"},
    62  		},
    63  	}
    64  )
    65  
    66  func TestDiff(t *testing.T) {
    67  	// Equiv Godeps, should yield an empty diff.
    68  	diff, _ := diffStr(&dep1, &dep2)
    69  	if diff != "" {
    70  		t.Errorf("Diff is %v want ''", diff)
    71  	}
    72  
    73  	// Test modifications in packages make it to the diff.
    74  	dep2.Deps[0].Comment = "D303"
    75  	diff, _ = diffStr(&dep1, &dep2)
    76  	if !diffsEqual(strings.Fields(diff), strings.Fields(d1)) {
    77  		t.Errorf("Expecting diffs to be equal. Obs <%q>. Exp <%q>", diff, d1)
    78  	}
    79  
    80  	// Test additional packages in new Godeps
    81  	dep2.Deps[0].Comment = "D202"
    82  	dep2.Deps = append(dep2.Deps, Dependency{ImportPath: "D102", Comment: "D203"})
    83  	diff, _ = diffStr(&dep1, &dep2)
    84  
    85  	if !diffsEqual(strings.Fields(diff), strings.Fields(d2)) {
    86  		t.Errorf("Expecting diffs to be equal. Obs <%v>. Exp <%v>", diff, d2)
    87  	}
    88  }
    89  
    90  // diffsEqual asserts that two slices are equivalent.
    91  func diffsEqual(a, b []string) bool {
    92  	if len(a) != len(b) {
    93  		return false
    94  	}
    95  
    96  	for i := range a {
    97  		if a[i] != b[i] {
    98  			return false
    99  		}
   100  	}
   101  	return true
   102  }