github.com/palcoin-project/palcd@v1.0.0/btcjson/btcdextresults_test.go (about)

     1  // Copyright (c) 2016-2017 The btcsuite developers
     2  // Copyright (c) 2015-2016 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package btcjson_test
     7  
     8  import (
     9  	"encoding/json"
    10  	"testing"
    11  
    12  	"github.com/palcoin-project/palcd/btcjson"
    13  )
    14  
    15  // TestBtcdExtCustomResults ensures any results that have custom marshalling
    16  // work as inteded.
    17  // and unmarshal code of results are as expected.
    18  func TestBtcdExtCustomResults(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	tests := []struct {
    22  		name     string
    23  		result   interface{}
    24  		expected string
    25  	}{
    26  		{
    27  			name: "versionresult",
    28  			result: &btcjson.VersionResult{
    29  				VersionString: "1.0.0",
    30  				Major:         1,
    31  				Minor:         0,
    32  				Patch:         0,
    33  				Prerelease:    "pr",
    34  				BuildMetadata: "bm",
    35  			},
    36  			expected: `{"versionstring":"1.0.0","major":1,"minor":0,"patch":0,"prerelease":"pr","buildmetadata":"bm"}`,
    37  		},
    38  	}
    39  
    40  	t.Logf("Running %d tests", len(tests))
    41  	for i, test := range tests {
    42  		marshalled, err := json.Marshal(test.result)
    43  		if err != nil {
    44  			t.Errorf("Test #%d (%s) unexpected error: %v", i,
    45  				test.name, err)
    46  			continue
    47  		}
    48  		if string(marshalled) != test.expected {
    49  			t.Errorf("Test #%d (%s) unexpected marhsalled data - "+
    50  				"got %s, want %s", i, test.name, marshalled,
    51  				test.expected)
    52  			continue
    53  		}
    54  	}
    55  }