1// Copyright (C) 2024 The Android Open Source Project 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 requirements 16 17import ( 18 "bytes" 19 "testing" 20 21 _ "embed" 22) 23 24// MPC Requirements data from requirements.txtpb 25// 26//go:embed requirements.binbp 27var reqBinary []byte 28 29func TestGensrc(t *testing.T) { 30 reqList, err := UnmarshalRequirementList(reqBinary) 31 if err != nil { 32 t.Fatalf("Failed to unmarshal reqBinary: %v", err) 33 } 34 35 tests := []struct { 36 tmpl string 37 want string 38 }{ 39 { 40 tmpl: "{{- $first := index .ReqList.GetRequirements 0 -}}First requirement is [{{$first.GetId}}].", 41 want: "First requirement is [5.1/H-1-1]."}, 42 { 43 tmpl: "UpperCamelCase foo_bar -> {{UpperCamelCase \"foo_bar\"}}", 44 want: "UpperCamelCase foo_bar -> FooBar", 45 }, 46 } 47 48 for _, tc := range tests { 49 var b bytes.Buffer 50 err = Gensrc(tc.tmpl, reqList, &b) 51 if err != nil { 52 t.Fatalf("Gensrc(%v, ...) failed: %v", tc.tmpl, err) 53 } 54 got := b.String() 55 if got != tc.want { 56 t.Errorf("Gensrc(%q, ...) = %q, want %q", tc.tmpl, got, tc.want) 57 } 58 } 59} 60