1// Copyright 2018 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 15package liteparse 16 17import ( 18 "bytes" 19 "context" 20 "reflect" 21 "testing" 22 23 "src/tools/ak/res/res" 24 "src/tools/ak/res/respipe/respipe" 25 "src/tools/ak/res/resxml/resxml" 26) 27 28func TestResNonValuesParse(t *testing.T) { 29 tests := []struct { 30 doc string 31 wanted []string 32 }{ 33 { 34 `<?xml version="1.0" encoding="utf-8"?> 35 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 36 android:layout_width="match_parent"> 37 <!-- a comment --> 38 <TextView android:id="@+id/new_tv" android:layout_height="match_parent"/> 39 <LinearLayout android:id="@+id/bad_layouts" android:layout_height="match_parent"> 40 <Something myAttr="@+id/id_here_too"/> 41 <LinearLayout android:id="@+id/really_bad_layouts"/> 42 </LinearLayout> 43 44 </LinearLayout> 45 `, 46 []string{ 47 "res-auto:id/new_tv", 48 "res-auto:id/bad_layouts", 49 "res-auto:id/id_here_too", 50 "res-auto:id/really_bad_layouts", 51 }, 52 }, 53 } 54 55 for _, tc := range tests { 56 ctx, cancel := context.WithCancel(context.Background()) 57 defer cancel() 58 xmlC, xmlErrC := resxml.StreamDoc(ctx, bytes.NewBufferString(tc.doc)) 59 resC, parseErrC := nonValuesParse(ctx, xmlC) 60 errC := respipe.MergeErrStreams(ctx, []<-chan error{xmlErrC, parseErrC}) 61 var parsedNames []string 62 for resC != nil || errC != nil { 63 select { 64 case r, ok := <-resC: 65 if !ok { 66 resC = nil 67 continue 68 } 69 pn, err := res.ParseName(r.GetName(), res.Type(r.ResourceType)) 70 if err != nil { 71 t.Errorf("res.ParseName(%s, %v) unexpected err: %v", r.GetName(), r.ResourceType, err) 72 } 73 parsedNames = append(parsedNames, pn.String()) 74 case e, ok := <-errC: 75 if !ok { 76 errC = nil 77 continue 78 } 79 t.Errorf("unexpected error: %v", e) 80 } 81 } 82 83 if !reflect.DeepEqual(parsedNames, tc.wanted) { 84 t.Errorf("nonValuesParse of: %s got: %s wanted: %s", tc.doc, parsedNames, tc.wanted) 85 } 86 } 87 88} 89