goparser_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package goparser
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. var dpath = "/Users/weicheng/Go/src/go-common/app/service/account/service"
  7. //var dpath = "/Users/weicheng/Go/src/playground/testgen/service"
  8. func TestParse(t *testing.T) {
  9. spec, err := Parse("account", dpath, "Service", dpath)
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. for _, method := range spec.Methods {
  14. t.Logf("method %s", method.Name)
  15. for _, param := range method.Parameters {
  16. t.Logf(">> param %s", param)
  17. }
  18. for _, result := range method.Results {
  19. t.Logf("<< result %s", result)
  20. }
  21. }
  22. }
  23. func TestExtractProtoFile(t *testing.T) {
  24. comment := "// source: article.proto\n"
  25. protoFile := extractProtoFile(comment)
  26. if protoFile != "article.proto" {
  27. t.Errorf("expect %s get %s", "article.proto", protoFile)
  28. }
  29. }
  30. func TestGoPackage(t *testing.T) {
  31. os.Setenv("GOPATH", "/go:/go1:/go3")
  32. type args struct {
  33. dpath string
  34. }
  35. tests := []struct {
  36. name string
  37. args args
  38. want string
  39. wantErr bool
  40. }{
  41. {
  42. name: "test1",
  43. args: args{"/go/src/hello/hello.go"},
  44. want: "hello",
  45. },
  46. {
  47. name: "test2",
  48. args: args{"/go3/src/hello/foo/hello.go"},
  49. want: "hello/foo",
  50. },
  51. {
  52. name: "test3",
  53. args: args{"/g/src/hello/foo/hello.go"},
  54. wantErr: true,
  55. },
  56. }
  57. for _, tt := range tests {
  58. t.Run(tt.name, func(t *testing.T) {
  59. got, err := GoPackage(tt.args.dpath)
  60. if (err != nil) != tt.wantErr {
  61. t.Errorf("GoPackage() error = %v, wantErr %v", err, tt.wantErr)
  62. return
  63. }
  64. if got != tt.want {
  65. t.Errorf("GoPackage() = %v, want %v", got, tt.want)
  66. }
  67. })
  68. }
  69. }