github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/androidmk/parser/make_strings_test.go (about)

     1  // Copyright 2017 Google Inc. 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  package parser
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  var splitNTestCases = []struct {
    23  	in       *MakeString
    24  	expected []*MakeString
    25  	sep      string
    26  	n        int
    27  }{
    28  	{
    29  		in: &MakeString{
    30  			Strings: []string{
    31  				"a b c",
    32  				"d e f",
    33  				" h i j",
    34  			},
    35  			Variables: []Variable{
    36  				Variable{Name: SimpleMakeString("var1", NoPos)},
    37  				Variable{Name: SimpleMakeString("var2", NoPos)},
    38  			},
    39  		},
    40  		sep: " ",
    41  		n:   -1,
    42  		expected: []*MakeString{
    43  			SimpleMakeString("a", NoPos),
    44  			SimpleMakeString("b", NoPos),
    45  			&MakeString{
    46  				Strings: []string{"c", "d"},
    47  				Variables: []Variable{
    48  					Variable{Name: SimpleMakeString("var1", NoPos)},
    49  				},
    50  			},
    51  			SimpleMakeString("e", NoPos),
    52  			&MakeString{
    53  				Strings: []string{"f", ""},
    54  				Variables: []Variable{
    55  					Variable{Name: SimpleMakeString("var2", NoPos)},
    56  				},
    57  			},
    58  			SimpleMakeString("h", NoPos),
    59  			SimpleMakeString("i", NoPos),
    60  			SimpleMakeString("j", NoPos),
    61  		},
    62  	},
    63  	{
    64  		in: &MakeString{
    65  			Strings: []string{
    66  				"a b c",
    67  				"d e f",
    68  				" h i j",
    69  			},
    70  			Variables: []Variable{
    71  				Variable{Name: SimpleMakeString("var1", NoPos)},
    72  				Variable{Name: SimpleMakeString("var2", NoPos)},
    73  			},
    74  		},
    75  		sep: " ",
    76  		n:   3,
    77  		expected: []*MakeString{
    78  			SimpleMakeString("a", NoPos),
    79  			SimpleMakeString("b", NoPos),
    80  			&MakeString{
    81  				Strings: []string{"c", "d e f", " h i j"},
    82  				Variables: []Variable{
    83  					Variable{Name: SimpleMakeString("var1", NoPos)},
    84  					Variable{Name: SimpleMakeString("var2", NoPos)},
    85  				},
    86  			},
    87  		},
    88  	},
    89  }
    90  
    91  func TestMakeStringSplitN(t *testing.T) {
    92  	for _, test := range splitNTestCases {
    93  		got := test.in.SplitN(test.sep, test.n)
    94  		gotString := dumpArray(got)
    95  		expectedString := dumpArray(test.expected)
    96  		if gotString != expectedString {
    97  			t.Errorf("expected:\n%s\ngot:\n%s", expectedString, gotString)
    98  		}
    99  	}
   100  }
   101  
   102  func dumpArray(a []*MakeString) string {
   103  	ret := make([]string, len(a))
   104  
   105  	for i, s := range a {
   106  		ret[i] = s.Dump()
   107  	}
   108  
   109  	return strings.Join(ret, "|||")
   110  }