Skip to content

Commit

Permalink
test: certificate status unmarshal json
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 6, 2024
1 parent 30ee557 commit 0623926
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
10 changes: 6 additions & 4 deletions agglayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"reflect"
"slices"
"strconv"
"strings"

"github.com/0xPolygon/cdk/bridgesync"
Expand Down Expand Up @@ -60,13 +61,14 @@ func (c CertificateStatus) IsOpen() bool {

// UnmarshalJSON is the implementation of the json.Unmarshaler interface
func (c *CertificateStatus) UnmarshalJSON(data []byte) error {
dataStr := string(data)
dataStr, err := strconv.Unquote(string(data))
if err != nil {
return err
}

var status string
status := dataStr
if strings.Contains(dataStr, "InError") {
status = "InError"
} else {
status = string(data)
}

switch status {
Expand Down
65 changes: 65 additions & 0 deletions agglayer/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,68 @@ func TestBridgeExitString(t *testing.T) {
})
}
}

func TestCertificateStatusUnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
expected CertificateStatus
expectError bool
}{
{
name: "Valid status - Pending",
input: `"Pending"`,
expected: Pending,
expectError: false,
},
{
name: "Valid status - Proven",
input: `"Proven"`,
expected: Proven,
expectError: false,
},
{
name: "Valid status - Candidate",
input: `"Candidate"`,
expected: Candidate,
expectError: false,
},
{
name: "Valid status - InError",
input: `"InError"`,
expected: InError,
expectError: false,
},
{
name: "Valid status - Settled",
input: `"Settled"`,
expected: Settled,
expectError: false,
},
{
name: "Invalid status",
input: `"InvalidStatus"`,
expected: 0, // Unchanged (default value of CertificateStatus)
expectError: true,
},
{
name: "Contains 'InError' string",
input: `"SomeStringWithInError"`,
expected: InError,
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var status CertificateStatus
err := json.Unmarshal([]byte(tt.input), &status)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, status)
}
})
}
}

0 comments on commit 0623926

Please sign in to comment.