Skip to content

Commit

Permalink
fix: lint and UT
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Nov 8, 2024
1 parent 54ecedd commit 7915df1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
41 changes: 21 additions & 20 deletions agglayer/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
func TestErrorVectors(t *testing.T) {
t.Parallel()

// we define an alias to avoid infinite recursion
type testCase struct {
TestName string `json:"test_name"`
ExpectedError string `json:"expected_error"`
Expand Down Expand Up @@ -106,6 +105,7 @@ func TestConvertMapValue_String(t *testing.T) {
}
}

//nolint:dupl
func TestConvertMapValue_Uint32(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -160,41 +160,42 @@ func TestConvertMapValue_Uint32(t *testing.T) {
}
}

func TestConvertMapValue_Bool(t *testing.T) {
//nolint:dupl
func TestConvertMapValue_Uint64(t *testing.T) {
t.Parallel()

tests := []struct {
name string
data map[string]interface{}
key string
want bool
want uint64
errString string
}{
{
name: "Key exists and type matches",
data: map[string]interface{}{
"key1": true,
"key1": uint64(3411),
},
key: "key1",
want: true,
want: uint64(3411),
},
{
name: "Key exists but type does not match",
data: map[string]interface{}{
"key1": "value1",
"key1": "not a number",
},
key: "key1",
want: false,
want: 0,
errString: "is not of type",
},
{
name: "Key does not exist",
data: map[string]interface{}{
"key1": true,
"key1": uint64(123555),
},
key: "key2",
want: false,
errString: "key key2 not found in map",
key: "key22",
want: 0,
errString: "key key22 not found in map",
},
}

Expand All @@ -204,7 +205,7 @@ func TestConvertMapValue_Bool(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := convertMapValue[bool](tt.data, tt.key)
got, err := convertMapValue[uint64](tt.data, tt.key)
if tt.errString != "" {
require.ErrorContains(t, err, tt.errString)
} else {
Expand All @@ -214,40 +215,40 @@ func TestConvertMapValue_Bool(t *testing.T) {
}
}

func TestConvertMapValue_Uint64(t *testing.T) {
func TestConvertMapValue_Bool(t *testing.T) {
t.Parallel()

tests := []struct {
name string
data map[string]interface{}
key string
want uint64
want bool
errString string
}{
{
name: "Key exists and type matches",
data: map[string]interface{}{
"key1": uint64(123456789),
"key1": true,
},
key: "key1",
want: uint64(123456789),
want: true,
},
{
name: "Key exists but type does not match",
data: map[string]interface{}{
"key1": "value1",
},
key: "key1",
want: 0,
want: false,
errString: "is not of type",
},
{
name: "Key does not exist",
data: map[string]interface{}{
"key1": uint64(123456789),
"key1": true,
},
key: "key2",
want: 0,
want: false,
errString: "key key2 not found in map",
},
}
Expand All @@ -258,7 +259,7 @@ func TestConvertMapValue_Uint64(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := convertMapValue[uint64](tt.data, tt.key)
got, err := convertMapValue[bool](tt.data, tt.key)
if tt.errString != "" {
require.ErrorContains(t, err, tt.errString)
} else {
Expand Down
4 changes: 2 additions & 2 deletions agglayer/proof_generation_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,9 @@ func convertMapValue[T any](data map[string]interface{}, key string) (T, error)
if floatValue, ok := value.(float64); ok && targetType.Kind() >= reflect.Int && targetType.Kind() <= reflect.Uint64 {
convertedValue, err := convertNumeric(floatValue, targetType)
if err != nil {
return target, fmt.Errorf("conversion error for key %s: %v", key, err)
return target, fmt.Errorf("conversion error for key %s: %w", key, err)
}
return convertedValue.(T), nil
return convertedValue.(T), nil //nolint:forcetypeassert
}

return target, fmt.Errorf("value of key %s is not of type %T", key, target)
Expand Down
2 changes: 1 addition & 1 deletion agglayer/proof_verification_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (p *ProofVerificationError) Unmarshal(data interface{}) error {
dataMap, ok := data.(map[string]interface{})
if !ok {
// it can be a single error
return getAndAddInnerErrorFn(data.(string), nil)
return getAndAddInnerErrorFn(data.(string), nil) //nolint:forcetypeassert
}

for key, value := range dataMap {
Expand Down
2 changes: 1 addition & 1 deletion agglayer/type_conversion_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (p *TypeConversionError) Unmarshal(data interface{}) error {
errorSourceMap, ok := data.(map[string]interface{})
if !ok {
// it can be a single error
return getAndAddInnerErrorFn(data.(string), nil)
return getAndAddInnerErrorFn(data.(string), nil) //nolint:forcetypeassert
}

for key, value := range errorSourceMap {
Expand Down
7 changes: 6 additions & 1 deletion agglayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,13 @@ type CertificateHeader struct {
}

func (c CertificateHeader) String() string {
errors := ""
if c.Error != nil {
errors = c.Error.String()
}

return fmt.Sprintf("Height: %d, CertificateID: %s, NewLocalExitRoot: %s. Status: %s. Errors: %s",
c.Height, c.CertificateID.String(), c.NewLocalExitRoot.String(), c.Status.String(), c.Error.String())
c.Height, c.CertificateID.String(), c.NewLocalExitRoot.String(), c.Status.String(), errors)
}

func (c *CertificateHeader) UnmarshalJSON(data []byte) error {
Expand Down
4 changes: 2 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ sonar.projectName=cdk
sonar.organization=0xpolygon

sonar.sources=.
sonar.exclusions=**/test/**,**/vendor/**,**/mocks/**,**/build/**,**/target/**,**/proto/include/**,**/*.pb.go,**/docs/**,**/*.sql,**/mocks_*/*,scripts/**,**/mock_*.go,**/agglayer/**,**/cmd/**
sonar.exclusions=**/test/**,**/vendor/**,**/mocks/**,**/build/**,**/target/**,**/proto/include/**,**/*.pb.go,**/docs/**,**/*.sql,**/mocks_*/*,scripts/**,**/mock_*.go,**/cmd/**

sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**,**/docs/**,**/mocks/**,**/*.pb.go,**/*.yml,**/*.yaml,**/*.json,**/*.xml,**/*.toml,**/mocks_*/*,**/mock_*.go,**/agglayer/**,**/cmd/**
sonar.test.exclusions=**/vendor/**,**/docs/**,**/mocks/**,**/*.pb.go,**/*.yml,**/*.yaml,**/*.json,**/*.xml,**/*.toml,**/mocks_*/*,**/mock_*.go,**/cmd/**
sonar.issue.enforceSemantic=true

# =====================================================
Expand Down

0 comments on commit 7915df1

Please sign in to comment.