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