Skip to content

Commit

Permalink
fix negative case
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Feb 21, 2020
1 parent 2348034 commit 63f6fde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/Grid.elm
Expand Up @@ -159,7 +159,11 @@ cropWrapping bounds (Grid grid) =

get : { row : Int, column : Int } -> Grid a -> Maybe a
get coords (Grid { items, width }) =
Array.get (coords.row * width + coords.column) items
if coords.row < 0 || coords.column < 0 then
Nothing

else
Array.get (coords.row * width + coords.column) items


{-| Still a maybe because the grid could be empty
Expand Down
30 changes: 13 additions & 17 deletions tests/GridTests.elm
Expand Up @@ -9,25 +9,21 @@ import Test exposing (..)
getTest : Test
getTest =
let
grid =
Grid.fromRowsAndColumns [ [ () ] ]
overInUnderFuzzer =
Fuzz.intRange -1 1
in
describe "get"
[ test "in-bounds coordinates should return the value at the coordinates" <|
\_ ->
Expect.equal (Just ()) (Grid.get { row = 0, column = 0 } grid)
, test "coordinates above the top row should return Nothing" <|
\_ ->
Expect.equal Nothing (Grid.get { row = -1, column = 0 } grid)
, test "coordinates below the bottom row should return Nothing" <|
\_ ->
Expect.equal Nothing (Grid.get { row = 1, column = 0 } grid)
, test "coordinates to the left of the leftmost column should return Nothing" <|
\_ ->
Expect.equal Nothing (Grid.get { row = 0, column = -1 } grid)
, test "coordinates to the right of the rightmost row should return Nothing" <|
\_ ->
Expect.equal Nothing (Grid.get { row = 0, column = 1 } grid)
[ fuzz2 overInUnderFuzzer overInUnderFuzzer "only returns values in bounds" <|
\row column ->
let
grid =
Grid.initialize { rows = 1, columns = 1 } (always ())
in
if row == 0 && column == 0 then
Expect.equal (Just ()) (Grid.get { row = row, column = column } grid)

else
Expect.equal Nothing (Grid.get { row = row, column = column } grid)
]


Expand Down

0 comments on commit 63f6fde

Please sign in to comment.