diff --git a/src/Grid.elm b/src/Grid.elm index 532b6f8..0c65737 100644 --- a/src/Grid.elm +++ b/src/Grid.elm @@ -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 diff --git a/tests/GridTests.elm b/tests/GridTests.elm index 68cae4f..1359774 100644 --- a/tests/GridTests.elm +++ b/tests/GridTests.elm @@ -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) ]