github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/rule/value_test.go (about)

     1  /* Copyright 2023 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package rule
    17  
    18  import (
    19  	"testing"
    20  
    21  	bzl "github.com/bazelbuild/buildtools/build"
    22  	"github.com/bazelbuild/bazel-gazelle/label"
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestExprFromValue(t *testing.T) {
    27  	for name, tt := range map[string]struct {
    28  		val  interface{}
    29  		want bzl.Expr
    30  	}{
    31  		"glob value": {
    32  			val: GlobValue{
    33  				Patterns: []string{"a", "b"},
    34  			},
    35  			want: &bzl.CallExpr{
    36  				X: &bzl.LiteralExpr{Token: "glob"},
    37  				List: []bzl.Expr{
    38  					&bzl.ListExpr{
    39  						List: []bzl.Expr{
    40  							&bzl.StringExpr{Value: "a"},
    41  							&bzl.StringExpr{Value: "b"},
    42  						},
    43  					},
    44  				},
    45  			},
    46  		},
    47  		"glob value with excludes": {
    48  			val: GlobValue{
    49  				Patterns: []string{"a", "b"},
    50  				Excludes: []string{"c", "d"},
    51  			},
    52  			want: &bzl.CallExpr{
    53  				X: &bzl.LiteralExpr{Token: "glob"},
    54  				List: []bzl.Expr{
    55  					&bzl.ListExpr{
    56  						List: []bzl.Expr{
    57  							&bzl.StringExpr{Value: "a"},
    58  							&bzl.StringExpr{Value: "b"},
    59  						},
    60  					},
    61  					&bzl.AssignExpr{
    62  						LHS: &bzl.LiteralExpr{Token: "exclude"},
    63  						Op:  "=",
    64  						RHS: &bzl.ListExpr{
    65  							List: []bzl.Expr{
    66  								&bzl.StringExpr{Value: "c"},
    67  								&bzl.StringExpr{Value: "d"},
    68  							},
    69  						},
    70  					},
    71  				},
    72  			},
    73  		},
    74  		"sorted strings": {
    75  			val: SortedStrings{"@b", ":a", "//:target"},
    76  			want: &bzl.ListExpr{
    77  				List: []bzl.Expr{
    78  					&bzl.StringExpr{Value: ":a"},
    79  					&bzl.StringExpr{Value: "//:target"},
    80  					&bzl.StringExpr{Value: "@b"},
    81  				},
    82  			},
    83  		},
    84  		"unsorted strings": {
    85  			val: UnsortedStrings{"@d", ":a", "//:b"},
    86  			want: &bzl.ListExpr{
    87  				List: []bzl.Expr{
    88  					&bzl.StringExpr{Value: "@d"},
    89  					&bzl.StringExpr{Value: ":a"},
    90  					&bzl.StringExpr{Value: "//:b"},
    91  				},
    92  			},
    93  		},
    94  		"labels": {
    95  			val:  label.New("repo", "pkg", "name"),
    96  			want: &bzl.StringExpr{Value: "@repo//pkg:name"},
    97  		},
    98  	} {
    99  		t.Run(name, func(t *testing.T) {
   100  			got := ExprFromValue(tt.val)
   101  			if diff := cmp.Diff(tt.want, got); diff != "" {
   102  				t.Errorf("ExprFromValue() mismatch (-want +got):\n%s", diff)
   103  			}
   104  		})
   105  	}
   106  }