refactor to only have one context per view
This commit is contained in:
@@ -75,6 +75,10 @@ func AsJson(i interface{}) string {
|
||||
|
||||
// used to keep a number n between 0 and max, allowing for wraparounds
|
||||
func ModuloWithWrap(n, max int) int {
|
||||
if max == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
if n >= max {
|
||||
return n % max
|
||||
} else if n < 0 {
|
||||
|
||||
@@ -87,3 +87,45 @@ func TestSafeTruncate(t *testing.T) {
|
||||
assert.EqualValues(t, s.expected, SafeTruncate(s.str, s.limit))
|
||||
}
|
||||
}
|
||||
|
||||
func TestModuloWithWrap(t *testing.T) {
|
||||
type scenario struct {
|
||||
n int
|
||||
max int
|
||||
expected int
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
n: 0,
|
||||
max: 0,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
n: 0,
|
||||
max: 1,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
n: 1,
|
||||
max: 0,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
n: 3,
|
||||
max: 2,
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
n: -1,
|
||||
max: 2,
|
||||
expected: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
if s.expected != ModuloWithWrap(s.n, s.max) {
|
||||
t.Errorf("expected %d, got %d, for n: %d, max: %d", s.expected, ModuloWithWrap(s.n, s.max), s.n, s.max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user