Skip to content

Fix not working for ints and floats of sizes other than default #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elvis.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import options

#true if float not 0 or NaN
template truthy*(val: float): bool = (val < 0 or val > 0)
template truthy*(val: SomeFloat): bool = (val < 0 or val > 0)

#true if int not 0
template truthy*(val: int): bool = (val != 0)
template truthy*(val: SomeInteger): bool = (val != 0)

#try if char not \0
template truthy*(val: char): bool = (val != '\0')
Expand Down
9 changes: 9 additions & 0 deletions tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,24 @@ suite "truthy":
test "empty string": check(not(?""))
test "zero float": check(not ?0.0)
test "NaN float": check(not ?NaN)
test "zero float32": check(not ?float32(0.0))
test "NaN float32": check(not ?float32(NaN))
test "\0 char": check(not ?cha0)
test "not \0 char": check(?'0')
test "zero int": check(not ?0)
test "zero int32": check(not ?int32(0))
test "zero uint32": check(not ?uint32(0))
test "empty array": check(not ?seq0)
test "empty seq lit": check(?seq1)
test "none option": check(not ?none(string))
test "not empty string": check(?"1")
test "not zero float": check(?1.1)
test "not zero negative": check(?(-1.1))
test "not zero float32": check(?float32(1.1))
test "not zero int": check(?1)
test "not zero int32": check(?int32(1))
test "not zero uint32": check(?uint32(1))
test "not zero uint8": check(?uint8(1))
test "not empty array": check(?[0])
test "not empty seq lit": check(?seq1)

Expand Down