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. 16 package bpdoc 17 18 import ( 19 "reflect" 20 "runtime" 21 "testing" 22 23 "github.com/google/blueprint" 24 ) 25 26 // foo docs. 27 func fooFactory() (blueprint.Module, []interface{}) { 28 return nil, []interface{}{&props{}} 29 } 30 31 // props docs. 32 type props struct { 33 // A docs. 34 A string 35 } 36 37 var pkgPath string 38 var pkgFiles map[string][]string 39 40 func 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 55 func 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 71 func 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 91 func 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 103 func 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