code.vegaprotocol.io/vega@v0.79.0/wallet/api/openrpc_helper_for_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package api_test
    17  
    18  import (
    19  	"sort"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  type nodeType uint16
    26  
    27  const (
    28  	nodeTypeUnknown nodeType = 0
    29  	nodeTypeString  nodeType = 1
    30  	nodeTypeNumber  nodeType = 2
    31  	nodeTypeBoolean nodeType = 3
    32  	nodeTypeObject  nodeType = 4
    33  	nodeTypeArray   nodeType = 5
    34  )
    35  
    36  // astNode represents the object that is going to be walked over.
    37  type astNode struct {
    38  	name             string
    39  	nodeType         nodeType
    40  	nestedProperties []astNode
    41  }
    42  
    43  type methodIODefinition struct {
    44  	Params astNode
    45  	Result astNode
    46  }
    47  
    48  func assertEqualSchema(t *testing.T, method string, params interface{}, result interface{}) {
    49  	t.Helper()
    50  
    51  	paramsAST, err := parseASTFromGo(t, params)
    52  	require.NoError(t, err)
    53  
    54  	resultAST, err := parseASTFromGo(t, result)
    55  	require.NoError(t, err)
    56  
    57  	mioDefinitionFromGo := methodIODefinition{
    58  		Params: paramsAST,
    59  		Result: resultAST,
    60  	}
    61  
    62  	mioDefinitionFromDoc, err := parseASTFromDoc(t, method)
    63  	require.NoError(t, err)
    64  
    65  	require.Equal(t, mioDefinitionFromGo, mioDefinitionFromDoc, "The openRPC and the go code are not in sync!")
    66  }
    67  
    68  func deterministNestedProperties(nodes []astNode) []astNode {
    69  	sort.SliceStable(nodes, func(i, j int) bool {
    70  		return nodes[i].name < nodes[j].name
    71  	})
    72  
    73  	return nodes
    74  }