Skip to content

Commit

Permalink
add tests for the number pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Mar 27, 2024
1 parent afd498e commit 433cec0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
48 changes: 48 additions & 0 deletions pkg/number/number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package number

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestByteCountDecimal(t *testing.T) {
tests := []struct {
name string
n int64
want string
}{
{"bytes", 42, "42 B"},
{"kilobytes", 1024, "1.0 kB"},
{"megabytes", 1048576, "1.0 MB"},
{"gigabytes", 1073741824, "1.1 GB"},
{"terabytes", 1099511627776, "1.1 TB"},
{"petabytes", 1125899906842624, "1.1 PB"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ByteCountDecimal(tt.n)
require.Equal(t, tt.want, got)
})
}
}

func TestWithComma(t *testing.T) {
tests := []struct {
name string
n int64
want string
}{
{"zero", 0, "0"},
{"positive number", 123456789, "123,456,789"},
{"negative number", -987654321, "-987,654,321"},
{"small number", 42, "42"},
{"large number", 1234567890123456789, "1,234,567,890,123,456,789"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := WithComma(tt.n)
require.Equal(t, tt.want, got)
})
}
}
4 changes: 2 additions & 2 deletions pkg/view/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (w *Widget) SetSecondsPerBlock(txt string, opts ...text.WriteOption) error
return w.secondsPerBlock.Write(txt, opts...)
}

func (w *Widget) SetMaxBlockSize(blockSize int64, opts ...text.WriteOption) error {
func (w *Widget) SetMaxBlockSize(txt string, opts ...text.WriteOption) error {
w.maxBlockSize.Reset()
return w.maxBlockSize.Write(strconv.FormatInt(blockSize, 10), opts...)
return w.maxBlockSize.Write(txt, opts...)
}

func (w *Widget) SetValidators(validators int, opts ...text.WriteOption) error {
Expand Down
3 changes: 2 additions & 1 deletion services/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cometbft/cometbft/types"

"github.com/ignite/gex/pkg/client"
"github.com/ignite/gex/pkg/number"
"github.com/ignite/gex/pkg/view"
)

Expand Down Expand Up @@ -45,7 +46,7 @@ func Run(ctx context.Context, host string) error {
if err != nil {
return err
}
if err := v.SetMaxBlockSize(params.ConsensusParams.Block.MaxBytes); err != nil {
if err := v.SetMaxBlockSize(number.ByteCountDecimal(params.ConsensusParams.Block.MaxBytes)); err != nil {
return err
}

Expand Down

0 comments on commit 433cec0

Please sign in to comment.