decred.org/dcrwallet/v3@v3.1.0/internal/rpc/jsonrpc/rpchelp_test.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Decred developers
     3  //
     4  // Permission to use, copy, modify, and distribute this software for any
     5  // purpose with or without fee is hereby granted, provided that the above
     6  // copyright notice and this permission notice appear in all copies.
     7  //
     8  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     9  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    11  // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12  // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13  // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    14  // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15  
    16  package jsonrpc
    17  
    18  import (
    19  	"strings"
    20  	"testing"
    21  
    22  	"decred.org/dcrwallet/v3/internal/rpchelp"
    23  	"decred.org/dcrwallet/v3/rpc/jsonrpc/types"
    24  	"github.com/decred/dcrd/dcrjson/v4"
    25  )
    26  
    27  func serverMethods() map[string]struct{} {
    28  	m := make(map[string]struct{})
    29  	for method, handlerData := range handlers {
    30  		if !handlerData.noHelp {
    31  			m[method] = struct{}{}
    32  		}
    33  	}
    34  	return m
    35  }
    36  
    37  // TestRPCMethodHelpGeneration ensures that help text can be generated for every
    38  // method of the RPC server for every supported locale.
    39  func TestRPCMethodHelpGeneration(t *testing.T) {
    40  	needsGenerate := false
    41  
    42  	defer func() {
    43  		if needsGenerate && !t.Failed() {
    44  			t.Error("Generated help texts are out of date: run 'go generate'")
    45  			return
    46  		}
    47  		if t.Failed() {
    48  			t.Log("Regenerate help texts with 'go generate' after fixing")
    49  		}
    50  	}()
    51  
    52  	for i := range rpchelp.HelpDescs {
    53  		svrMethods := serverMethods()
    54  		locale := rpchelp.HelpDescs[i].Locale
    55  		generatedDescs := localeHelpDescs[locale]()
    56  		for _, m := range rpchelp.Methods {
    57  			delete(svrMethods, m.Method)
    58  
    59  			helpText, err := dcrjson.GenerateHelp(types.Method(m.Method), rpchelp.HelpDescs[i].Descs, m.ResultTypes...)
    60  			if err != nil {
    61  				t.Errorf("Cannot generate '%s' help for method '%s': missing description for '%s'",
    62  					locale, m.Method, err)
    63  				continue
    64  			}
    65  			if !needsGenerate && helpText != generatedDescs[m.Method] {
    66  				needsGenerate = true
    67  			}
    68  		}
    69  
    70  		for m := range svrMethods {
    71  			t.Errorf("Missing '%s' help for method '%s'", locale, m)
    72  		}
    73  	}
    74  }
    75  
    76  // TestRPCMethodUsageGeneration ensures that single line usage text can be
    77  // generated for every supported request of the RPC server.
    78  func TestRPCMethodUsageGeneration(t *testing.T) {
    79  	needsGenerate := false
    80  
    81  	defer func() {
    82  		if needsGenerate && !t.Failed() {
    83  			t.Error("Generated help usages are out of date: run 'go generate'")
    84  			return
    85  		}
    86  		if t.Failed() {
    87  			t.Log("Regenerate help usage with 'go generate' after fixing")
    88  		}
    89  	}()
    90  
    91  	svrMethods := serverMethods()
    92  	usageStrs := make([]string, 0, len(rpchelp.Methods))
    93  	for _, m := range rpchelp.Methods {
    94  		delete(svrMethods, m.Method)
    95  
    96  		usage, err := dcrjson.MethodUsageText(types.Method(m.Method))
    97  		if err != nil {
    98  			t.Errorf("Cannot generate single line usage for method '%s': %v",
    99  				m.Method, err)
   100  		}
   101  
   102  		if !t.Failed() {
   103  			usageStrs = append(usageStrs, usage)
   104  		}
   105  	}
   106  
   107  	if !t.Failed() {
   108  		usages := strings.Join(usageStrs, "\n")
   109  		needsGenerate = usages != requestUsages
   110  	}
   111  }