-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaning up the function guards in the dot component
Adding additional unit tests to go back towards 100% coverage. Fixing a bug in the LedStrip. We shouldn't raise an exception, but only return an error if the arguments are wrong BREAKING CHANGE: Removing the default strip_name from the LedStrip. You have to explicitly pass a name. You can simply pass in LedStrip instead of nothing to get the same behaviour
- Loading branch information
Showing
8 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2024, Matthias Reik <fledex@reik.org> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
defmodule Fledex.Utils.Guards do | ||
@moduledoc """ | ||
This module collects all useful guards. If you want to use one of them | ||
you need to `import` this module. | ||
""" | ||
|
||
@doc """ | ||
This guard checks whether the value is within the given range. | ||
Notes: | ||
* The lower bound is excluded (except if `inverse_bounds` is true) | ||
* The upper bound is included (except if `inverse_bounds` is true) | ||
* This guard is not fully tested, so be careful when using that it works for you. | ||
""" | ||
# @spec is_in_range(integer, boolean, integer, integer) :: boolean | ||
defguard is_in_range(value, inverse_bounds, min, max) when | ||
is_integer(value) and | ||
( | ||
(inverse_bounds and value + 1 > min and value + 1 <= max) or | ||
(not inverse_bounds and value > min and value <= max) | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2024, Matthias Reik <fledex@reik.org> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
defmodule Fledex.Utils.GuardsTest do | ||
use ExUnit.Case | ||
|
||
import Fledex.Utils.Guards | ||
|
||
def create_in_range_case(line) do | ||
# IO.puts("line: #{inspect line}") | ||
line | ||
|> String.split(",") | ||
|> Enum.map(fn item -> convert!(item) end) | ||
|> List.to_tuple() | ||
end | ||
|
||
def convert!("t"), do: true | ||
def convert!("true"), do: true | ||
def convert!("f"), do: false | ||
def convert!("false"), do: false | ||
def convert!(num), do: String.to_integer(num) | ||
|
||
describe "Test is_in_range" do | ||
setup do | ||
cases = File.read!("test/fledex/utils/in_range_guard_cases.csv") | ||
|> String.split("\n") | ||
|> Enum.reject(fn line -> | ||
String.length(String.trim(line)) == 0 | ||
end) | ||
|> Enum.map(fn line -> create_in_range_case(line) end) | ||
%{cases: cases} | ||
end | ||
test "test all combinations", %{cases: cases} do | ||
Enum.each(cases, fn {value, inverted, min, max, expected} -> | ||
# IO.puts("case #{inspect {value, inverted, min, max, expected}}") | ||
case value do | ||
_x when is_in_range(value, inverted, min, max) -> | ||
# IO.puts("in_range") | ||
assert(expected) | ||
_x when not is_in_range(value, inverted, min, max) -> | ||
# IO.puts("NOT in_range") | ||
assert(not expected) | ||
end | ||
end) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
3,f,0,5,t | ||
0,f,0,5,f | ||
0,t,0,5,t | ||
5,f,0,5,t | ||
5,t,0,5,f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Copyright 2024, Matthias Reik <fledex@reik.org> | ||
|
||
SPDX-License-Identifier: Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters