handle dimension
This commit is contained in:
72
internal/entity/dimension_test.go
Normal file
72
internal/entity/dimension_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package entity_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.loyso.art/frx/eway/internal/entity"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDimension_AdjustTo(t *testing.T) {
|
||||
// Test adjusting from smaller dimension to larger one
|
||||
d := entity.Dimension{Value: 5.0, Kind: entity.DimensionKindCentimeter}
|
||||
expected := entity.Dimension{Value: 0.05, Kind: entity.DimensionKindMeter}
|
||||
actual := d.AdjustTo(entity.DimensionKindMeter)
|
||||
|
||||
assert.EqualValues(t, expected.Value, actual.Value)
|
||||
assert.Equal(t, expected.Kind, actual.Kind)
|
||||
|
||||
// Test adjusting from larger dimension to smaller one
|
||||
d = entity.Dimension{Value: 0.05, Kind: entity.DimensionKindMeter}
|
||||
expected = entity.Dimension{Value: 50.0, Kind: entity.DimensionKindMilimeter}
|
||||
actual = d.AdjustTo(entity.DimensionKindMilimeter)
|
||||
|
||||
assert.EqualValues(t, expected.Value, actual.Value)
|
||||
assert.Equal(t, expected.Kind, actual.Kind)
|
||||
}
|
||||
|
||||
func TestParseDimension_Success(t *testing.T) {
|
||||
// Test parsing a valid dimension string with RU locale
|
||||
input := "10 см"
|
||||
expected := entity.Dimension{Value: 10.0, Kind: entity.DimensionKindCentimeter}
|
||||
|
||||
actual, err := entity.ParseDimention(input, entity.DimensionLocalRU)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, expected.Value, actual.Value)
|
||||
assert.Equal(t, expected.Kind, actual.Kind)
|
||||
}
|
||||
|
||||
func TestParseDimensionComplex_Success(t *testing.T) {
|
||||
// Test parsing a valid dimension string with RU locale
|
||||
input := "10 256.20 см"
|
||||
expected := entity.Dimension{Value: 10256.20, Kind: entity.DimensionKindCentimeter}
|
||||
|
||||
actual, err := entity.ParseDimention(input, entity.DimensionLocalRU)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, expected.Value, actual.Value)
|
||||
assert.Equal(t, expected.Kind, actual.Kind)
|
||||
}
|
||||
|
||||
func TestParseDimension_InvalidInputFormat(t *testing.T) {
|
||||
// Test parsing an invalid dimension string with RU locale
|
||||
input := "invalid value 2"
|
||||
expectedErr := errors.New("expected 2 values after split for value invalid value 2")
|
||||
|
||||
_, err := entity.ParseDimention(input, entity.DimensionLocalRU)
|
||||
assert.Error(t, err)
|
||||
assert.EqualError(t, err, expectedErr.Error())
|
||||
}
|
||||
|
||||
func TestParseDimension_InvalidLocale(t *testing.T) {
|
||||
// Test parsing a dimension string with an unsupported locale
|
||||
input := "10 мм"
|
||||
expectedErr := errors.New("unknown locale for parse")
|
||||
|
||||
_, err := entity.ParseDimention(input, 3) // An invalid locale value is used here for demonstration purposes
|
||||
assert.EqualError(t, err, expectedErr.Error())
|
||||
}
|
||||
Reference in New Issue
Block a user