-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix marshalling of CustomGenesis (#730)
* fixes HexOrDecimal256 marshalling to json * fix Bytes32 marshalling * using default value method * bytes32 nil marshalls to 0x0 instead of nil * nits * license
- Loading branch information
Showing
4 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
package thor | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMarshalUnmarshall(t *testing.T) { | ||
// Example hex string representing the value 100 | ||
originalHex := `"0x00000000000000000000000000000000000000000000000000006d6173746572"` // Note the enclosing double quotes for valid JSON string | ||
|
||
// Unmarshal JSON into HexOrDecimal256 | ||
var unmarshaledValue Bytes32 | ||
|
||
// using direct function | ||
err := unmarshaledValue.UnmarshalJSON([]byte(originalHex)) | ||
assert.NoError(t, err) | ||
|
||
// using json overloading ( satisfies the json.Unmarshal interface ) | ||
err = json.Unmarshal([]byte(originalHex), &unmarshaledValue) | ||
assert.NoError(t, err) | ||
|
||
// Marshal the value back to JSON | ||
// using direct function | ||
directMarshallJson, err := unmarshaledValue.MarshalJSON() | ||
assert.NoError(t, err, "Marshaling should not produce an error") | ||
assert.Equal(t, originalHex, string(directMarshallJson)) | ||
|
||
// using json overloading ( satisfies the json.Unmarshal interface ) | ||
// using value | ||
marshalVal, err := json.Marshal(unmarshaledValue) | ||
assert.NoError(t, err) | ||
assert.Equal(t, originalHex, string(marshalVal)) | ||
|
||
// using json overloading ( satisfies the json.Unmarshal interface ) | ||
// using pointer | ||
marshalPtr, err := json.Marshal(&unmarshaledValue) | ||
assert.NoError(t, err, "Marshaling should not produce an error") | ||
assert.Equal(t, originalHex, string(marshalPtr)) | ||
} |