2019-12-31 07:16:11 +01:00
|
|
|
package s2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestToken(t *testing.T) {
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("germany", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := Token(48.56344833333333, 8.996878333333333)
|
|
|
|
expected := "4799e370"
|
|
|
|
|
|
|
|
assert.True(t, strings.HasPrefix(token, expected))
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("lat_overflow", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := Token(548.56344833333333, 8.996878333333333)
|
|
|
|
expected := ""
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("lng_overflow", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := Token(48.56344833333333, 258.996878333333333)
|
|
|
|
expected := ""
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenLevel(t *testing.T) {
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("level_30", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := TokenLevel(48.56344833333333, 8.996878333333333, 30)
|
|
|
|
expected := "4799e370ca54c8b9"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("level_30_diff", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
plusCode := TokenLevel(48.56344839999999, 8.996878339999999, 30)
|
|
|
|
expected := "4799e370ca54c8b7"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, plusCode)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("level_21", func(t *testing.T) {
|
|
|
|
plusCode := TokenLevel(48.56344839999999, 8.996878339999999, 21)
|
|
|
|
expected := "4799e370ca54"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, plusCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("level_18", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := TokenLevel(48.56344833333333, 8.996878333333333, 18)
|
|
|
|
expected := "4799e370cb"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("level_18_diff", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := TokenLevel(48.56344839999999, 8.996878339999999, 18)
|
|
|
|
expected := "4799e370cb"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("level_15", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
plusCode := TokenLevel(48.56344833333333, 8.996878333333333, 15)
|
|
|
|
expected := "4799e370c"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, plusCode)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("level_10", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := TokenLevel(48.56344833333333, 8.996878333333333, 10)
|
|
|
|
expected := "4799e3"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("lat_overflow", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := TokenLevel(548.56344833333333, 8.996878333333333, 30)
|
|
|
|
expected := ""
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("lng_overflow", func(t *testing.T) {
|
2019-12-31 07:16:11 +01:00
|
|
|
token := TokenLevel(48.56344833333333, 258.996878333333333, 30)
|
|
|
|
expected := ""
|
|
|
|
|
|
|
|
assert.Equal(t, expected, token)
|
|
|
|
})
|
|
|
|
}
|
2020-01-12 14:00:56 +01:00
|
|
|
|
|
|
|
func TestLatLng(t *testing.T) {
|
2020-01-15 04:04:33 +01:00
|
|
|
t.Run("valid", func(t *testing.T) {
|
2020-01-12 14:00:56 +01:00
|
|
|
lat, lng := LatLng("4799e370ca54c8b9")
|
|
|
|
assert.Equal(t, 48.56344835921243, lat)
|
|
|
|
assert.Equal(t, 8.996878323369781, lng)
|
|
|
|
})
|
2020-01-15 04:04:33 +01:00
|
|
|
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
lat, lng := LatLng("4799e370ca5q")
|
|
|
|
assert.Equal(t, 0.0, lat)
|
|
|
|
assert.Equal(t, 0.0, lng)
|
|
|
|
})
|
2020-01-12 14:00:56 +01:00
|
|
|
}
|
2020-01-15 04:04:33 +01:00
|
|
|
|
|
|
|
func TestIsZero(t *testing.T) {
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
lat, lng := LatLng("4799e370ca54c8b9")
|
|
|
|
assert.False(t, IsZero(lat, lng))
|
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
lat, lng := LatLng("4799e370ca5q")
|
|
|
|
assert.True(t, IsZero(lat, lng))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRange(t *testing.T) {
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
min, max := Range("4799e370ca54c8b9", 1)
|
|
|
|
assert.Equal(t, "4799e370ca54c8b4", min)
|
|
|
|
assert.Equal(t, "4799e370ca54c8c4", max)
|
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
min, max := Range("4799e370ca5q", 1)
|
|
|
|
assert.Equal(t, "", min)
|
|
|
|
assert.Equal(t, "", max)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|