1// Copyright 2019 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 15package bpdoc 16 17import ( 18 "reflect" 19 "testing" 20) 21 22func TestExcludeByTag(t *testing.T) { 23 r := NewReader(pkgFiles) 24 ps, err := r.PropertyStruct(pkgPath, "tagTestProps", reflect.ValueOf(tagTestProps{})) 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 ps.ExcludeByTag("tag1", "a") 30 31 expected := []string{"c", "d", "g"} 32 actual := actualProperties(t, ps.Properties) 33 if !reflect.DeepEqual(expected, actual) { 34 t.Errorf("unexpected ExcludeByTag result, expected: %q, actual: %q", expected, actual) 35 } 36} 37 38func TestIncludeByTag(t *testing.T) { 39 r := NewReader(pkgFiles) 40 ps, err := r.PropertyStruct(pkgPath, "tagTestProps", reflect.ValueOf(tagTestProps{A: "B"})) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 ps.IncludeByTag("tag1", "c") 46 47 expected := []string{"b", "c", "d", "f", "g"} 48 actual := actualProperties(t, ps.Properties) 49 if !reflect.DeepEqual(expected, actual) { 50 t.Errorf("unexpected IncludeByTag result, expected: %q, actual: %q", expected, actual) 51 } 52} 53 54func actualProperties(t *testing.T, props []Property) []string { 55 t.Helper() 56 57 actual := []string{} 58 for _, p := range props { 59 actual = append(actual, p.Name) 60 actual = append(actual, actualProperties(t, p.Properties)...) 61 } 62 return actual 63} 64