github.com/podhmo/reflect-shape@v0.4.3/metadata/lookup_test.go (about)

     1  package metadata
     2  
     3  import (
     4  	"context"
     5  	"go/token"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  )
    11  
    12  // Person is person
    13  type Person struct {
    14  
    15  	// name is the name of person
    16  	Name string
    17  }
    18  
    19  func TestType(t *testing.T) {
    20  	type result struct {
    21  		Name          string
    22  		Doc           string
    23  		FieldComments map[string]string
    24  	}
    25  
    26  	want := result{
    27  		Name: "Person",
    28  		Doc:  "Person is person",
    29  		FieldComments: map[string]string{
    30  			"Name": "name is the name of person",
    31  		},
    32  	}
    33  
    34  	fset := token.NewFileSet()
    35  	l := NewLookup(fset)
    36  	l.IncludeGoTestFiles = true // for test
    37  
    38  	metadata, err := l.LookupFromType(Person{})
    39  	if err != nil {
    40  		t.Fatalf("unexpected error: %+v", err)
    41  	}
    42  	got := result{
    43  		Name:          metadata.Name(),
    44  		Doc:           metadata.Doc(),
    45  		FieldComments: metadata.FieldComments(),
    46  	}
    47  
    48  	if diff := cmp.Diff(want, got); diff != "" {
    49  		t.Errorf("LookupFromType() mismatch (-want +got):\n%s", diff)
    50  	}
    51  }
    52  
    53  // Hello is function returns greeting message
    54  func Hello(name string) string {
    55  	return "Hello " + name
    56  }
    57  
    58  func TestFunc(t *testing.T) {
    59  	type result struct {
    60  		Name string
    61  		Doc  string
    62  		Args []string
    63  	}
    64  
    65  	want := result{
    66  		Name: "Hello",
    67  		Doc:  "Hello is function returns greeting message",
    68  		Args: []string{"name"},
    69  	}
    70  
    71  	fset := token.NewFileSet()
    72  	l := NewLookup(fset)
    73  	l.IncludeGoTestFiles = true
    74  
    75  	metadata, err := l.LookupFromFunc(Hello)
    76  	if err != nil {
    77  		t.Fatalf("unexpected error: %+v", err)
    78  	}
    79  	var args []string
    80  	for _, p := range metadata.Args() {
    81  		args = append(args, p.Name)
    82  	}
    83  	got := result{
    84  		Name: metadata.Name(),
    85  		Doc:  metadata.Doc(),
    86  		Args: args,
    87  	}
    88  
    89  	if diff := cmp.Diff(want, got); diff != "" {
    90  		t.Errorf("LookupFromFunc() mismatch (-want +got):\n%s", diff)
    91  	}
    92  }
    93  
    94  type S struct{}
    95  
    96  // Method1 is one of S
    97  func (s *S) Method1(name string) error { return nil }
    98  
    99  // Method2 is one of S
   100  func (s S) Method2(ctx context.Context, name string) (result int, err error) { return 0, nil }
   101  
   102  func TestMethod(t *testing.T) {
   103  	type result struct {
   104  		Name    string
   105  		Doc     string
   106  		Args    []string
   107  		Returns []string
   108  	}
   109  
   110  	cases := []struct {
   111  		msg    string
   112  		want   result
   113  		target interface{}
   114  	}{
   115  		{
   116  			msg: "pointer",
   117  			want: result{
   118  				Name:    "Method1",
   119  				Doc:     "Method1 is one of S",
   120  				Args:    []string{"name"},
   121  				Returns: []string{""},
   122  			},
   123  			target: (&S{}).Method1,
   124  		},
   125  		{
   126  			msg: "value",
   127  			want: result{
   128  				Name:    "Method2",
   129  				Doc:     "Method2 is one of S",
   130  				Args:    []string{"ctx", "name"},
   131  				Returns: []string{"result", "err"},
   132  			},
   133  			target: (S{}).Method2,
   134  		},
   135  	}
   136  
   137  	fset := token.NewFileSet()
   138  	l := NewLookup(fset)
   139  	l.IncludeGoTestFiles = true
   140  
   141  	for _, c := range cases {
   142  		c := c
   143  		t.Run(c.msg, func(t *testing.T) {
   144  			metadata, err := l.LookupFromFunc(c.target)
   145  			if err != nil {
   146  				t.Fatalf("unexpected error: %+v", err)
   147  			}
   148  
   149  			var args []string
   150  			for _, p := range metadata.Args() {
   151  				args = append(args, p.Name)
   152  			}
   153  			var returns []string
   154  			for _, p := range metadata.Returns() {
   155  				returns = append(returns, p.Name)
   156  			}
   157  
   158  			got := result{
   159  				Name:    metadata.Name(),
   160  				Doc:     metadata.Doc(),
   161  				Args:    args,
   162  				Returns: returns,
   163  			}
   164  
   165  			if diff := cmp.Diff(c.want, got); diff != "" {
   166  				t.Errorf("LookupFromFunc() mismatch (-want +got):\n%s", diff)
   167  			}
   168  
   169  		})
   170  	}
   171  }
   172  
   173  // I is I
   174  type I interface {
   175  	// Foo is Foo
   176  	Foo()
   177  }
   178  
   179  func TestInterface(t *testing.T) {
   180  	type result struct {
   181  		Name string
   182  		Doc  string
   183  	}
   184  
   185  	var iface I
   186  	cases := []struct {
   187  		msg    string
   188  		want   result
   189  		target interface{}
   190  	}{
   191  		{
   192  			msg: "value",
   193  			want: result{
   194  				Name: "I",
   195  				Doc:  "I is I",
   196  			},
   197  			target: iface,
   198  		},
   199  	}
   200  
   201  	fset := token.NewFileSet()
   202  	l := NewLookup(fset)
   203  	l.IncludeGoTestFiles = true
   204  
   205  	for _, c := range cases {
   206  		c := c
   207  		t.Run(c.msg, func(t *testing.T) {
   208  			metadata, err := l.LookupFromTypeForReflectType(reflect.TypeOf(func() I { return nil }).Out(0))
   209  			if err != nil {
   210  				t.Fatalf("unexpected error: %+v", err)
   211  			}
   212  			got := result{
   213  				Name: metadata.Name(),
   214  				Doc:  metadata.Doc(),
   215  			}
   216  
   217  			if diff := cmp.Diff(c.want, got); diff != "" {
   218  				t.Errorf("LookupFromType() mismatch (-want +got):\n%s", diff)
   219  			}
   220  		})
   221  	}
   222  }