github.com/alimy/mir/v4@v4.1.0/internal/parser/parser_test.go (about)

     1  // Copyright 2020 Michael Li <alimy@gility.net>. All rights reserved.
     2  // Use of this source code is governed by Apache License 2.0 that
     3  // can be found in the LICENSE file.
     4  
     5  package parser
     6  
     7  import (
     8  	"testing"
     9  
    10  	. "github.com/alimy/mir/v4"
    11  )
    12  
    13  type AgentInfo struct {
    14  	Platform  string `json:"platform"`
    15  	UserAgent string `json:"user_agent"`
    16  }
    17  
    18  type ServerInfo struct {
    19  	ApiVer string `json:"api_ver"`
    20  }
    21  
    22  type UserInfo struct {
    23  	Name string `json:"name"`
    24  }
    25  
    26  type LoginReq struct {
    27  	AgentInfo AgentInfo `json:"agent_info"`
    28  	Name      string    `json:"name"`
    29  	Passwd    string    `json:"passwd"`
    30  }
    31  
    32  type LoginResp struct {
    33  	UserInfo
    34  	ServerInfo ServerInfo `json:"server_info"`
    35  	JwtToken   string     `json:"jwt_token"`
    36  }
    37  
    38  type site struct {
    39  	Chain    Chain                          `mir:"-"`
    40  	Index    func(Get)                      `mir:"/index/"`
    41  	Articles func(Get)                      `mir:"/articles/:category/"`
    42  	Login    func(Post, LoginReq) LoginResp `mir:"/user/login/"`
    43  	Logout   func(Post)                     `mir:"/user/logout/"`
    44  }
    45  
    46  type siteV1 struct {
    47  	Chain    Chain                          `mir:"-"`
    48  	Group    Group                          `mir:"v1"`
    49  	Index    func(Get)                      `mir:"/index/"`
    50  	Articles func(Get)                      `mir:"/articles/:category/"`
    51  	Login    func(Post, LoginReq) LoginResp `mir:"/user/login/"`
    52  	Logout   func(Post)                     `mir:"/user/logout/"`
    53  }
    54  
    55  type siteV2 struct {
    56  	Group    Group                          `mir:"v2"`
    57  	Index    func(Get)                      `mir:"/index/"`
    58  	Articles func(Get)                      `mir:"/articles/:category/"`
    59  	Login    func(Post, LoginReq) LoginResp `mir:"/user/login/"`
    60  	Logout   func(Post)                     `mir:"/user/logout/"`
    61  }
    62  
    63  func TestMirParser_Parse(t *testing.T) {
    64  	p := &mirParser{tagName: defaultTag}
    65  
    66  	entries := []any{
    67  		new(site),
    68  		new(siteV1),
    69  		new(siteV2),
    70  	}
    71  	ds, err := p.Parse(entries)
    72  	if err != nil {
    73  		t.Errorf("want nil error but got: %s", err)
    74  	}
    75  	if len(ds) != 3 {
    76  		t.Errorf("want 3 item but got: %d", len(ds))
    77  	}
    78  
    79  	iface, exist := ds.Get("")
    80  	if !exist || len(iface) != 1 {
    81  		t.Error("want a correct iface but not")
    82  	}
    83  	site := iface["site"]
    84  	if site == nil || len(site.Fields) != 4 {
    85  		t.Error("want a correct iface but not")
    86  	}
    87  
    88  	iface, exist = ds.Get("v1")
    89  	if !exist || len(iface) != 1 {
    90  		t.Error("want a correct iface but not")
    91  	}
    92  	site = iface["siteV1"]
    93  	if site == nil || len(site.Fields) != 4 {
    94  		t.Error("want a correct iface but not")
    95  	}
    96  
    97  	iface, exist = ds.Get("v2")
    98  	if !exist || len(iface) != 1 {
    99  		t.Error("want a correct iface but not")
   100  	}
   101  	site = iface["siteV2"]
   102  	if site == nil || len(site.Fields) != 4 {
   103  		t.Error("want a correct iface but not")
   104  	}
   105  }