golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gorebuild/main_test.go (about)

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	gorebuild "golang.org/x/build/cmd/gorebuild"
    11  )
    12  
    13  func TestDiffArchive(t *testing.T) {
    14  	type match bool // A named type for readability of test cases below.
    15  	for _, tc := range [...]struct {
    16  		name string
    17  		a, b map[string]string
    18  		want match
    19  	}{
    20  		{
    21  			name: "empty",
    22  			a:    map[string]string{},
    23  			b:    map[string]string{},
    24  			want: match(true),
    25  		},
    26  		{
    27  			name: "equal",
    28  			a:    map[string]string{"file1": "content 1", "file2": "content 2"},
    29  			b:    map[string]string{"file1": "content 1", "file2": "content 2"},
    30  			want: match(true),
    31  		},
    32  		{
    33  			name: "different content",
    34  			a:    map[string]string{"file1": "content 1", "file2": "content 2"},
    35  			b:    map[string]string{"file1": "content 3", "file2": "content 4"},
    36  			want: match(false),
    37  		},
    38  		{
    39  			name: "missing file", // file2 in a, but missing in b.
    40  			a:    map[string]string{"file1": "", "file2": ""},
    41  			b:    map[string]string{"file1": ""},
    42  			want: match(false),
    43  		},
    44  		{
    45  			name: "unexpected file", // file3 not in a, but unexpectedly there in b.
    46  			a:    map[string]string{"file1": "", "file2": ""},
    47  			b:    map[string]string{"file1": "", "file2": "", "file3": ""},
    48  			want: match(false),
    49  		},
    50  	} {
    51  		t.Run(tc.name, func(t *testing.T) {
    52  			var log gorebuild.Log
    53  			got := gorebuild.DiffArchive(&log, tc.a, tc.b, func(_ *gorebuild.Log, a, b string) bool { return a == b })
    54  			if got != bool(tc.want) {
    55  				t.Errorf("got match = %v, want %v", got, tc.want)
    56  			}
    57  		})
    58  	}
    59  }