Skip to content
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

fix(utils) nil values in choose(cond, val1, val2) #447

Merged
merged 6 commits into from
Dec 28, 2022
Merged

fix(utils) nil values in choose(cond, val1, val2) #447

merged 6 commits into from
Dec 28, 2022

Conversation

broma0
Copy link
Contributor

@broma0 broma0 commented Dec 19, 2022

The original implementation of choose would fail if val1 was falsy. I have fixed this by replacing the "and/or" ternary logic with an if statement.

Example of failure:

choose(true, nil, "hello") -- returns "hello"
choose(true, false, "hello") -- returns "hello"

Internally, the evaluated expressions were as follows:

true and nil or "hello" -- returns "hello"
true and false or "hello" -- returns "hello"

This commit updates the logic to be as follows:

if true then return nil else return "hello" end -- returns nil
if true then return false else return "hello" end -- returns false

broma0 and others added 2 commits December 19, 2022 17:54
The original implementation of choose would fail if val1 was falsy. I have
fixed this by replacing the "and/or" ternary logic with an if statement.

Example of failure:

  choose(true, nil, "hello") -- returns "hello"
  choose(true, false, "hello") -- returns "hello"

Internally, the evaluated expressions were as follows:

  true and nil or "hello"  -- returns "hello"
  true and false or "hello" -- returns "hello"

This commit updates the logic to be as follows:

  if true then return nil else return "hello" end  -- returns nil
  if true then return false else return "hello" end -- returns false
Copy link
Member

@Tieske Tieske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! thx for the busted based tests. Can you please add a changelog entry?

tests/test-utils.lua Outdated Show resolved Hide resolved
@broma0
Copy link
Contributor Author

broma0 commented Dec 24, 2022

Hey @Tieske - Just updated the changelog and removed the old tests that I added. Let me know if you'd like for me to change anything else.

@Tieske Tieske merged commit ca34942 into lunarmodules:master Dec 28, 2022
@Tieske
Copy link
Member

Tieske commented Dec 28, 2022

@broma0 thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants