Skip to content

Commit

Permalink
[chore]: enable float-compare rule from testifylint (open-telemetry#1…
Browse files Browse the repository at this point in the history
…1149)

#### Description

Testifylint is a linter that provides best practices with the use of
testify.

This PR enables
[float-compare](https://github.com/Antonboom/testifylint?tab=readme-ov-file#float-compare)
rule from [testifylint](https://github.com/Antonboom/testifylint)

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Sep 18, 2024
1 parent ec4a7f1 commit 012cf86
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 58 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ linters-settings:
testifylint:
# TODO: enable all rules
disable:
- float-compare
- require-error
enable-all: true

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SEMCONVGEN := $(TOOLS_BIN_DIR)/semconvgen
SEMCONVKIT := $(TOOLS_BIN_DIR)/semconvkit
TESTIFYLINT := $(TOOLS_BIN_DIR)/testifylint

TESTIFYLINT_OPT?= --enable-all --disable=float-compare,require-error
TESTIFYLINT_OPT?= --enable-all --disable=require-error

.PHONY: install-tools
install-tools: $(TOOLS_BIN_NAMES)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/mdatagen/internal/templates/metrics_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ func TestMetricsBuilder(t *testing.T) {
assert.Equal(t, start, dp.StartTimestamp())
assert.Equal(t, ts, dp.Timestamp())
assert.Equal(t, pmetric.NumberDataPointValueType{{ $metric.Data.MetricValueType }}, dp.ValueType())
{{- if eq $metric.Data.MetricValueType.BasicType "float64" }}
assert.InDelta(t, {{ $metric.Data.MetricValueType.BasicType }}(1), dp.{{ $metric.Data.MetricValueType }}Value(), 0.01)
{{- else }}
assert.Equal(t, {{ $metric.Data.MetricValueType.BasicType }}(1), dp.{{ $metric.Data.MetricValueType }}Value())
{{- end }}

{{- range $i, $attr := $metric.Attributes }}
attrVal, ok {{ if eq $i 0 }}:{{ end }}= dp.Attributes().Get("{{ (attributeInfo $attr).Name }}")
Expand Down
24 changes: 22 additions & 2 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,17 @@ func (ms {{ .structName }}) Set{{ .accessorFieldName }}(v {{ .returnType }}) {

const accessorsOneOfPrimitiveTestTemplate = `func Test{{ .structName }}_{{ .accessorFieldName }}(t *testing.T) {
ms := New{{ .structName }}()
{{- if eq .returnType "float64"}}
assert.InDelta(t, {{ .defaultVal }}, ms.{{ .accessorFieldName }}(), 0.01)
{{- else }}
assert.Equal(t, {{ .defaultVal }}, ms.{{ .accessorFieldName }}())
{{- end }}
ms.Set{{ .accessorFieldName }}({{ .testValue }})
{{- if eq .returnType "float64" }}
assert.InDelta(t, {{ .testValue }}, ms.{{ .accessorFieldName }}(), 0.01)
{{- else }}
assert.Equal(t, {{ .testValue }}, ms.{{ .accessorFieldName }}())
{{- end }}
assert.Equal(t, {{ .typeName }}, ms.{{ .originOneOfTypeFuncName }}())
sharedState := internal.StateReadOnly
assert.Panics(t, func() { new{{ .structName }}(&{{ .originStructName }}{}, &sharedState).Set{{ .accessorFieldName }}({{ .testValue }}) })
Expand All @@ -179,13 +187,17 @@ const accessorsPrimitiveTestTemplate = `func Test{{ .structName }}_{{ .fieldName
ms := New{{ .structName }}()
{{- if eq .returnType "bool" }}
assert.{{- if eq .defaultVal "true" }}True{{- else }}False{{- end }}(t, ms.{{ .fieldName }}())
{{- else }}
{{- else if eq .returnType "float64" }}
assert.InDelta(t, {{ .defaultVal }}, ms.{{ .fieldName }}(), 0.01)
{{- else }}
assert.Equal(t, {{ .defaultVal }}, ms.{{ .fieldName }}())
{{- end }}
ms.Set{{ .fieldName }}({{ .testValue }})
{{- if eq .returnType "bool" }}
assert.{{- if eq .testValue "true" }}True{{- else }}False{{- end }}(t, ms.{{ .fieldName }}())
{{- else }}
{{- else if eq .returnType "float64"}}
assert.InDelta(t, {{ .testValue }}, ms.{{ .fieldName }}(), 0.01)
{{- else }}
assert.Equal(t, {{ .testValue }}, ms.{{ .fieldName }}())
{{- end }}
sharedState := internal.StateReadOnly
Expand Down Expand Up @@ -243,10 +255,18 @@ func (ms {{ .structName }}) Remove{{ .fieldName }}() {

const accessorsOptionalPrimitiveTestTemplate = `func Test{{ .structName }}_{{ .fieldName }}(t *testing.T) {
ms := New{{ .structName }}()
{{- if eq .returnType "float64" }}
assert.InDelta(t, {{ .defaultVal }}, ms.{{ .fieldName }}() , 0.01)
{{- else }}
assert.Equal(t, {{ .defaultVal }}, ms.{{ .fieldName }}())
{{- end }}
ms.Set{{ .fieldName }}({{ .testValue }})
assert.True(t, ms.Has{{ .fieldName }}())
{{- if eq .returnType "float64" }}
assert.InDelta(t, {{.testValue }}, ms.{{ .fieldName }}(), 0.01)
{{- else }}
assert.Equal(t, {{ .testValue }}, ms.{{ .fieldName }}())
{{- end }}
ms.Remove{{ .fieldName }}()
assert.False(t, ms.Has{{ .fieldName }}())
}`
Expand Down
40 changes: 36 additions & 4 deletions pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,49 @@ const immutableSliceTestTemplate = `func TestNew{{ .structName }}(t *testing.T)
assert.Equal(t, []{{ .itemType }}{ {{ .testNewVal }} }, ms.AsRaw())
ms.FromRaw([]{{ .itemType }}{ {{ index .testInterfaceOrigVal 2 }} })
assert.Equal(t, 1, ms.Len())
assert.Equal(t, {{ .itemType }}( {{ index .testInterfaceOrigVal 2 }} ), ms.At(0))
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{ index .testInterfaceOrigVal 2 }}), ms.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 2 }}), ms.At(0))
{{- end }}
cp := New{{ .structName }}()
ms.CopyTo(cp)
ms.SetAt(0, {{ .itemType }}( {{ index .testInterfaceOrigVal 1 }} ))
assert.Equal(t, {{ .itemType }}( {{ index .testInterfaceOrigVal 1 }} ), ms.At(0))
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), ms.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), ms.At(0))
{{- end }}
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{ index .testInterfaceOrigVal 2 }}), cp.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 2 }}), cp.At(0))
{{- end }}
ms.CopyTo(cp)
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), cp.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), cp.At(0))
{{- end }}
mv := New{{ .structName }}()
ms.MoveTo(mv)
assert.Equal(t, 0, ms.Len())
assert.Equal(t, 1, mv.Len())
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), mv.At(0))
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{index .testInterfaceOrigVal 1 }}), mv.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{index .testInterfaceOrigVal 1 }}), mv.At(0))
{{- end }}
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
ms.MoveTo(mv)
assert.Equal(t, 3, mv.Len())
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 0 }}), mv.At(0))
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{index .testInterfaceOrigVal 0 }}), mv.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{index .testInterfaceOrigVal 0 }}), mv.At(0))
{{- end }}
}
func Test{{ .structName }}ReadOnly(t *testing.T) {
Expand All @@ -143,7 +167,11 @@ func Test{{ .structName }}ReadOnly(t *testing.T) {
ms := {{ .structName }}(internal.New{{ .structName }}(&raw, &state))
assert.Equal(t, 3, ms.Len())
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}( {{index .testInterfaceOrigVal 0 }} ), ms.At(0), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 0 }}), ms.At(0))
{{- end }}
assert.Panics(t, func() { ms.Append({{ index .testInterfaceOrigVal 0 }}) })
assert.Panics(t, func() { ms.EnsureCapacity(2) })
assert.Equal(t, raw, ms.AsRaw())
Expand All @@ -163,7 +191,11 @@ func Test{{ .structName }}Append(t *testing.T) {
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
ms.Append({{ .testSetVal }}, {{ .testSetVal }})
assert.Equal(t, 5, ms.Len())
{{- if eq .itemType "float64" }}
assert.InDelta(t, {{ .itemType }}({{ .testSetVal }} ), ms.At(4), 0.01)
{{- else }}
assert.Equal(t, {{ .itemType }}({{ .testSetVal }}), ms.At(4))
{{- end }}
}
func Test{{ .structName }}EnsureCapacity(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pdata/internal/json/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestReadFloat64(t *testing.T) {
return
}
assert.NoError(t, iter.Error)
assert.Equal(t, tt.want, val)
assert.InDelta(t, tt.want, val, 0.01)
})
}
}
16 changes: 8 additions & 8 deletions pdata/pcommon/generated_float64slice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pdata/pcommon/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestMapWithEmpty(t *testing.T) {
val, exist = sm.Get("other_key_double")
assert.True(t, exist)
assert.EqualValues(t, ValueTypeDouble, val.Type())
assert.EqualValues(t, 1.23, val.Double())
assert.InDelta(t, 1.23, val.Double(), 0.01)

sm.PutBool("other_key_bool", true)
val, exist = sm.Get("other_key_bool")
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestMapWithEmpty(t *testing.T) {
val, exist = sm.Get("another_key_double")
assert.True(t, exist)
assert.EqualValues(t, ValueTypeDouble, val.Type())
assert.EqualValues(t, 4.56, val.Double())
assert.InDelta(t, 4.56, val.Double(), 0.01)

sm.PutBool("another_key_bool", false)
val, exist = sm.Get("another_key_bool")
Expand Down Expand Up @@ -357,7 +357,7 @@ func TestMap_FromRaw(t *testing.T) {
assert.Equal(t, int64(123), v.Int())
v, ok = am.Get("k_double")
assert.True(t, ok)
assert.Equal(t, 1.23, v.Double())
assert.InDelta(t, 1.23, v.Double(), 0.01)
v, ok = am.Get("k_null")
assert.True(t, ok)
assert.Equal(t, ValueTypeEmpty, v.Type())
Expand Down
8 changes: 4 additions & 4 deletions pdata/pcommon/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestValue(t *testing.T) {

v = NewValueDouble(3.4)
assert.EqualValues(t, ValueTypeDouble, v.Type())
assert.EqualValues(t, 3.4, v.Double())
assert.InDelta(t, 3.4, v.Double(), 0.01)

v = NewValueBool(true)
assert.EqualValues(t, ValueTypeBool, v.Type())
Expand All @@ -52,7 +52,7 @@ func TestValueReadOnly(t *testing.T) {
assert.EqualValues(t, ValueTypeStr, v.Type())
assert.EqualValues(t, "v", v.Str())
assert.EqualValues(t, 0, v.Int())
assert.EqualValues(t, 0, v.Double())
assert.InDelta(t, 0, v.Double(), 0.01)
assert.False(t, v.Bool())
assert.EqualValues(t, ByteSlice{}, v.Bytes())
assert.EqualValues(t, Map{}, v.Map())
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestNilOrigSetValue(t *testing.T) {

av = NewValueEmpty()
av.SetDouble(1.23)
assert.EqualValues(t, 1.23, av.Double())
assert.InDelta(t, 1.23, av.Double(), 0.01)

av = NewValueEmpty()
av.SetEmptyBytes().FromRaw([]byte{1, 2, 3})
Expand Down Expand Up @@ -560,7 +560,7 @@ func TestInvalidValue(t *testing.T) {
v := Value{}
assert.False(t, v.Bool())
assert.Equal(t, int64(0), v.Int())
assert.Equal(t, float64(0), v.Double())
assert.InDelta(t, float64(0), v.Double(), 0.01)
assert.Equal(t, "", v.Str())
assert.Equal(t, ByteSlice{}, v.Bytes())
assert.Equal(t, Map{}, v.Map())
Expand Down
4 changes: 2 additions & 2 deletions pdata/pmetric/generated_exemplar_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions pdata/pmetric/generated_exponentialhistogramdatapoint_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 012cf86

Please sign in to comment.