github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/other_proto_library_test.go (about)

     1  package protoc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bazelbuild/bazel-gazelle/rule"
     7  )
     8  
     9  func TestOtherProtoLibrary(t *testing.T) {
    10  	for name, tc := range map[string]struct {
    11  		rule     *rule.Rule
    12  		wantName string
    13  		wantBase string
    14  		wantSrcs []string
    15  		wantDeps []string
    16  	}{
    17  		"prototypical": {
    18  			rule: withProtoLibraryRule("foo_proto",
    19  				withRuleSrcs("foo.py"),
    20  				withRuleDeps("@a//b:c"),
    21  			),
    22  			wantName: "foo_proto",
    23  			wantBase: "foo",
    24  			wantSrcs: listOf("foo.py"),
    25  			wantDeps: listOf("@a//b:c"),
    26  		},
    27  	} {
    28  		t.Run(name, func(t *testing.T) {
    29  			lib := OtherProtoLibrary{rule: tc.rule}
    30  			name := lib.Name()
    31  			base := lib.BaseName()
    32  			srcs := lib.Srcs()
    33  			deps := lib.Deps()
    34  
    35  			if tc.wantName != name {
    36  				t.Errorf("name: want %s, got %s", tc.wantName, name)
    37  			}
    38  			if tc.wantBase != base {
    39  				t.Errorf("base: want %s, got %s", tc.wantBase, base)
    40  			}
    41  			if len(tc.wantSrcs) != len(srcs) {
    42  				t.Fatalf("srcs: want %d, got %d", len(tc.wantSrcs), len(srcs))
    43  			}
    44  			if len(tc.wantDeps) != len(deps) {
    45  				t.Fatalf("deps: want %d, got %d", len(tc.wantDeps), len(deps))
    46  			}
    47  			for i, got := range srcs {
    48  				want := tc.wantSrcs[i]
    49  				if want != got {
    50  					t.Errorf("srcs %d: want %s, got %s", i, want, got)
    51  				}
    52  			}
    53  			for i, got := range deps {
    54  				want := tc.wantDeps[i]
    55  				if want != got {
    56  					t.Errorf("deps %d: want %s, got %s", i, want, got)
    57  				}
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  type ruleOption func(r *rule.Rule)
    64  
    65  func withProtoLibraryRule(name string, opts ...ruleOption) *rule.Rule {
    66  	r := rule.NewRule("proto_library", name)
    67  	for _, opt := range opts {
    68  		opt(r)
    69  	}
    70  	return r
    71  }
    72  
    73  func withRuleSrcs(srcs ...string) ruleOption {
    74  	return func(r *rule.Rule) {
    75  		r.SetAttr("srcs", srcs)
    76  	}
    77  }
    78  
    79  func withRuleDeps(deps ...string) ruleOption {
    80  	return func(r *rule.Rule) {
    81  		r.SetAttr("deps", deps)
    82  	}
    83  }