github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/packages/package_test.go (about)

     1  /* Copyright 2017 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 packages
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/bazelbuild/bazel-gazelle/internal/config"
    23  )
    24  
    25  func TestAddPlatformStrings(t *testing.T) {
    26  	c := &config.Config{}
    27  	for _, tc := range []struct {
    28  		desc, filename string
    29  		tags           []tagLine
    30  		want           PlatformStrings
    31  	}{
    32  		{
    33  			desc:     "generic",
    34  			filename: "foo.go",
    35  			want: PlatformStrings{
    36  				Generic: []string{"foo.go"},
    37  			},
    38  		}, {
    39  			desc:     "os",
    40  			filename: "foo_linux.go",
    41  			want: PlatformStrings{
    42  				OS: map[string][]string{"linux": []string{"foo_linux.go"}},
    43  			},
    44  		}, {
    45  			desc:     "arch",
    46  			filename: "foo_amd64.go",
    47  			want: PlatformStrings{
    48  				Arch: map[string][]string{"amd64": []string{"foo_amd64.go"}},
    49  			},
    50  		}, {
    51  			desc:     "os and arch",
    52  			filename: "foo_linux_amd64.go",
    53  			want: PlatformStrings{
    54  				Platform: map[config.Platform][]string{
    55  					config.Platform{OS: "linux", Arch: "amd64"}: []string{"foo_linux_amd64.go"},
    56  				},
    57  			},
    58  		}, {
    59  			desc:     "os not arch",
    60  			filename: "foo.go",
    61  			tags:     []tagLine{{{"solaris", "!arm"}}},
    62  			want: PlatformStrings{
    63  				Platform: map[config.Platform][]string{
    64  					config.Platform{OS: "solaris", Arch: "amd64"}: []string{"foo.go"},
    65  				},
    66  			},
    67  		},
    68  	} {
    69  		t.Run(tc.desc, func(t *testing.T) {
    70  			fi := fileNameInfo("", "", tc.filename)
    71  			fi.tags = tc.tags
    72  			var sb platformStringsBuilder
    73  			add := getPlatformStringsAddFunction(c, fi, nil)
    74  			add(&sb, tc.filename)
    75  			got := sb.build()
    76  			if !reflect.DeepEqual(got, tc.want) {
    77  				t.Errorf("got %#v ; want %#v", got, tc.want)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestDuplicatePlatformStrings(t *testing.T) {
    84  	for _, tc := range []struct {
    85  		desc string
    86  		add  func(sb *platformStringsBuilder)
    87  		want PlatformStrings
    88  	}{
    89  		{
    90  			desc: "both generic",
    91  			add: func(sb *platformStringsBuilder) {
    92  				sb.addGenericString("a")
    93  				sb.addGenericString("a")
    94  			},
    95  			want: PlatformStrings{
    96  				Generic: []string{"a"},
    97  			},
    98  		}, {
    99  			desc: "os generic",
   100  			add: func(sb *platformStringsBuilder) {
   101  				sb.addOSString("a", []string{"linux"})
   102  				sb.addGenericString("a")
   103  			},
   104  			want: PlatformStrings{
   105  				Generic: []string{"a"},
   106  			},
   107  		}, {
   108  			desc: "os arch",
   109  			add: func(sb *platformStringsBuilder) {
   110  				sb.addOSString("a", []string{"solaris"})
   111  				sb.addArchString("a", []string{"mips"})
   112  			},
   113  			want: PlatformStrings{
   114  				Platform: map[config.Platform][]string{
   115  					config.Platform{OS: "solaris", Arch: "amd64"}: {"a"},
   116  					config.Platform{OS: "linux", Arch: "mips"}:    {"a"},
   117  				},
   118  			},
   119  		}, {
   120  			desc: "platform os",
   121  			add: func(sb *platformStringsBuilder) {
   122  				sb.addPlatformString("a", []config.Platform{{OS: "linux", Arch: "mips"}})
   123  				sb.addOSString("a", []string{"solaris"})
   124  			},
   125  			want: PlatformStrings{
   126  				Platform: map[config.Platform][]string{
   127  					config.Platform{OS: "solaris", Arch: "amd64"}: {"a"},
   128  					config.Platform{OS: "linux", Arch: "mips"}:    {"a"},
   129  				},
   130  			},
   131  		},
   132  	} {
   133  		t.Run(tc.desc, func(t *testing.T) {
   134  			var sb platformStringsBuilder
   135  			tc.add(&sb)
   136  			got := sb.build()
   137  			if !reflect.DeepEqual(got, tc.want) {
   138  				t.Errorf("got %#v ; want %#v", got, tc.want)
   139  			}
   140  		})
   141  	}
   142  }