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 15// bpdoc docs. 16package bpdoc 17 18import ( 19 "reflect" 20 "runtime" 21 "testing" 22 23 "github.com/google/blueprint" 24) 25 26// foo docs. 27func fooFactory() (blueprint.Module, []interface{}) { 28 return nil, []interface{}{&props{}} 29} 30 31// props docs. 32type props struct { 33 // A docs. 34 A string 35} 36 37// for properties_test.go 38type tagTestProps struct { 39 A string `tag1:"a,b" tag2:"c"` 40 B string `tag1:"a,c"` 41 C string `tag1:"b,c"` 42} 43 44var pkgPath string 45var pkgFiles map[string][]string 46 47func init() { 48 pc, filename, _, _ := runtime.Caller(0) 49 fn := runtime.FuncForPC(pc) 50 51 var err error 52 pkgPath, err = funcNameToPkgPath(fn.Name()) 53 if err != nil { 54 panic(err) 55 } 56 57 pkgFiles = map[string][]string{ 58 pkgPath: {filename}, 59 } 60} 61 62func TestModuleTypeDocs(t *testing.T) { 63 r := NewReader(pkgFiles) 64 mt, err := r.ModuleType("foo_module", reflect.ValueOf(fooFactory)) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 if mt.Text != "foo docs.\n\n" { 70 t.Errorf("unexpected docs %q", mt.Text) 71 } 72 73 if mt.PkgPath != pkgPath { 74 t.Errorf("expected pkgpath %q, got %q", pkgPath, mt.PkgPath) 75 } 76} 77 78func TestPropertyStruct(t *testing.T) { 79 r := NewReader(pkgFiles) 80 ps, err := r.PropertyStruct(pkgPath, "props", reflect.ValueOf(props{A: "B"})) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 if ps.Text != "props docs.\n" { 86 t.Errorf("unexpected docs %q", ps.Text) 87 } 88 if len(ps.Properties) != 1 { 89 t.Fatalf("want 1 property, got %d", len(ps.Properties)) 90 } 91 92 if ps.Properties[0].Name != "a" || ps.Properties[0].Text != "A docs.\n\n" || ps.Properties[0].Default != "B" { 93 t.Errorf("unexpected property docs %q %q %q", 94 ps.Properties[0].Name, ps.Properties[0].Text, ps.Properties[0].Default) 95 } 96} 97 98func TestPackage(t *testing.T) { 99 r := NewReader(pkgFiles) 100 pkg, err := r.Package(pkgPath) 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 if pkg.Text != "bpdoc docs.\n" { 106 t.Errorf("unexpected docs %q", pkg.Text) 107 } 108} 109 110func TestFuncToPkgPath(t *testing.T) { 111 tests := []struct { 112 f string 113 want string 114 }{ 115 { 116 f: "github.com/google/blueprint/bootstrap.Main", 117 want: "github.com/google/blueprint/bootstrap", 118 }, 119 { 120 f: "android/soong/android.GenruleFactory", 121 want: "android/soong/android", 122 }, 123 { 124 f: "android/soong/android.ModuleFactoryAdapter.func1", 125 want: "android/soong/android", 126 }, 127 { 128 f: "main.Main", 129 want: "main", 130 }, 131 } 132 for _, tt := range tests { 133 t.Run(tt.f, func(t *testing.T) { 134 got, err := funcNameToPkgPath(tt.f) 135 if err != nil { 136 t.Fatal(err) 137 } 138 if got != tt.want { 139 t.Errorf("funcNameToPkgPath(%v) = %v, want %v", tt.f, got, tt.want) 140 } 141 }) 142 } 143} 144