Skip to content

Commit

Permalink
chore: enable float-compare rule from testifylint (aquasecurity#6967)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
Co-authored-by: knqyf263 <[email protected]>
  • Loading branch information
2 people authored and skahn007gl committed Jul 23, 2024
1 parent f0f2852 commit d50ed41
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ linters-settings:
ignore-generated-header: true
testifylint:
enable-all: true
disable:
- float-compare

linters:
disable-all: true
enable:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_Number_IntToFloat(t *testing.T) {
metadata := types.NewTestMetadata()
err := Unmarshal(example, &output, &metadata)
require.NoError(t, err)
assert.Equal(t, 123.0, output)
assert.InEpsilon(t, 123.0, output, 0.0001)
}

func Test_Number_FloatToFloat(t *testing.T) {
Expand All @@ -33,7 +33,7 @@ func Test_Number_FloatToFloat(t *testing.T) {
metadata := types.NewTestMetadata()
err := Unmarshal(example, &output, &metadata)
require.NoError(t, err)
assert.Equal(t, 123.456, output)
assert.InEpsilon(t, 123.456, output, 0.0001)
}

func Test_Number_FloatToInt(t *testing.T) {
Expand All @@ -42,7 +42,7 @@ func Test_Number_FloatToInt(t *testing.T) {
metadata := types.NewTestMetadata()
err := Unmarshal(example, &output, &metadata)
require.NoError(t, err)
assert.Equal(t, 123, output)
assert.InEpsilon(t, 123, output, 0.0001)
}

func Test_Number_FloatWithExponent(t *testing.T) {
Expand Down Expand Up @@ -70,7 +70,7 @@ func Test_Number_FloatWithExponent(t *testing.T) {
metadata := types.NewTestMetadata()
err := Unmarshal(example, &output, &metadata)
require.NoError(t, err)
assert.Equal(t, test.out, output)
assert.InEpsilon(t, test.out, output, 0.0001)

})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_Object(t *testing.T) {
metadata := types.NewTestMetadata()
require.NoError(t, Unmarshal(example, &target, &metadata))
assert.Equal(t, "testing", target.Name)
assert.Equal(t, 3.14, target.Balance)
assert.InEpsilon(t, 3.14, target.Balance, 0.0001)
}

func Test_ObjectWithPointers(t *testing.T) {
Expand All @@ -36,7 +36,7 @@ func Test_ObjectWithPointers(t *testing.T) {
metadata := types.NewTestMetadata()
require.NoError(t, Unmarshal(example, &target, &metadata))
assert.Equal(t, "testing", *target.Name)
assert.Equal(t, 3.14, *target.Balance)
assert.InEpsilon(t, 3.14, *target.Balance, 0.0001)
}

type nestedParent struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac/scanners/cloudformation/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func TestJsonWithNumbers(t *testing.T) {
file := files[0]

assert.Equal(t, 1, file.Parameters["SomeIntParam"].Default())
assert.Equal(t, 1.1, file.Parameters["SomeFloatParam"].Default())
assert.InEpsilon(t, 1.1, file.Parameters["SomeFloatParam"].Default(), 0.0001)

res := file.GetResourcesByType("Test::Resource")
assert.NotNil(t, res)
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac/scanners/json/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_Parser(t *testing.T) {
y, ok := yRaw.(float64)
require.True(t, ok)

assert.Equal(t, 123.0, y)
assert.InEpsilon(t, 123.0, y, 0.0001)

zRaw, ok := xMsi["z"]
require.True(t, ok)
Expand Down
51 changes: 25 additions & 26 deletions pkg/parallel/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package parallel_test
import (
"context"
"fmt"
"math"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,21 +14,21 @@ import (
func TestPipeline_Do(t *testing.T) {
type field struct {
numWorkers int
items []float64
onItem func(context.Context, float64) (float64, error)
items []int
onItem func(context.Context, int) (int, error)
}
type testCase struct {
name string
field field
want float64
want int
wantErr require.ErrorAssertionFunc
}
tests := []testCase{
{
name: "pow",
field: field{
numWorkers: 5,
items: []float64{
items: []int{
1,
2,
3,
Expand All @@ -41,44 +40,44 @@ func TestPipeline_Do(t *testing.T) {
9,
10,
},
onItem: func(_ context.Context, f float64) (float64, error) {
return math.Pow(f, 2), nil
onItem: func(_ context.Context, i int) (int, error) {
return i * i, nil
},
},
want: 385,
wantErr: require.NoError,
},
{
name: "ceil",
name: "double",
field: field{
numWorkers: 3,
items: []float64{
1.1,
2.2,
3.3,
4.4,
5.5,
-1.1,
-2.2,
-3.3,
items: []int{
1,
2,
3,
4,
5,
-1,
-2,
-3,
},
onItem: func(_ context.Context, f float64) (float64, error) {
return math.Round(f), nil
onItem: func(_ context.Context, i int) (int, error) {
return i * 2, nil
},
},
want: 10,
want: 18,
wantErr: require.NoError,
},
{
name: "error in series",
field: field{
numWorkers: 1,
items: []float64{
items: []int{
1,
2,
3,
},
onItem: func(_ context.Context, f float64) (float64, error) {
onItem: func(_ context.Context, _ int) (int, error) {
return 0, fmt.Errorf("error")
},
},
Expand All @@ -88,11 +87,11 @@ func TestPipeline_Do(t *testing.T) {
name: "error in parallel",
field: field{
numWorkers: 3,
items: []float64{
items: []int{
1,
2,
},
onItem: func(_ context.Context, f float64) (float64, error) {
onItem: func(_ context.Context, _ int) (int, error) {
return 0, fmt.Errorf("error")
},
},
Expand All @@ -101,8 +100,8 @@ func TestPipeline_Do(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got float64
p := parallel.NewPipeline(tt.field.numWorkers, false, tt.field.items, tt.field.onItem, func(f float64) error {
var got int
p := parallel.NewPipeline(tt.field.numWorkers, false, tt.field.items, tt.field.onItem, func(f int) error {
got += f
return nil
})
Expand Down

0 comments on commit d50ed41

Please sign in to comment.