-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add PacmanRules #30
Merged
Merged
Add PacmanRules #30
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule ElixirAnalyzer.ExerciseTest.PacmanRules do | ||
@dialyzer generated: true | ||
@moduledoc """ | ||
This is an exercise analyzer extension module for the concept exercise Pacman Rules | ||
""" | ||
|
||
alias ElixirAnalyzer.Constants | ||
|
||
use ElixirAnalyzer.ExerciseTest | ||
|
||
feature "requires using strictly boolean operators" do | ||
find :none | ||
on_fail :disapprove | ||
comment Constants.pacman_rules_use_strictly_boolean_operators() | ||
|
||
form do | ||
_ignore && _ignore | ||
end | ||
|
||
form do | ||
Kernel.&&(_ignore, _ignore) | ||
end | ||
|
||
form do | ||
_ignore || _ignore | ||
end | ||
|
||
form do | ||
Kernel.||(_ignore, _ignore) | ||
end | ||
|
||
form do | ||
!_ignore | ||
end | ||
|
||
form do | ||
Kernel.!(_ignore) | ||
end | ||
end | ||
end |
104 changes: 104 additions & 0 deletions
104
test/elixir_analyzer/exercise_test/pacman_rules_test.exs
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,104 @@ | ||
defmodule ElixirAnalyzer.ExerciseTest.PacmanRulesTest do | ||
use ElixirAnalyzer.ExerciseTestCase, | ||
exercise_test_module: ElixirAnalyzer.ExerciseTest.PacmanRules | ||
|
||
test_exercise_analysis "example solution", | ||
comments: [], | ||
status: :approve do | ||
defmodule Rules do | ||
def eat_ghost?(power_pellet_active, touching_ghost) do | ||
power_pellet_active and touching_ghost | ||
end | ||
|
||
def score?(touching_power_pellet, touching_dot) do | ||
touching_power_pellet or touching_dot | ||
end | ||
|
||
def lose?(power_pellet_active, touching_ghost) do | ||
not power_pellet_active and touching_ghost | ||
end | ||
|
||
def win?(has_eaten_all_dots, power_pellet_active, touching_ghost) do | ||
has_eaten_all_dots and not lose?(power_pellet_active, touching_ghost) | ||
end | ||
end | ||
end | ||
|
||
describe "requires strictly boolean operators" do | ||
test_exercise_analysis "detects &&", | ||
comments_include: [Constants.pacman_rules_use_strictly_boolean_operators()] do | ||
[ | ||
defmodule Rules do | ||
def eat_ghost?(power_pellet_active, touching_ghost) do | ||
power_pellet_active && touching_ghost | ||
end | ||
end, | ||
defmodule Rules do | ||
def eat_ghost?(power_pellet_active, touching_ghost) do | ||
power_pellet_active and touching_ghost | ||
end | ||
|
||
def lose?(power_pellet_active, touching_ghost) do | ||
not power_pellet_active && touching_ghost | ||
end | ||
end, | ||
defmodule Rules do | ||
def eat_ghost?(power_pellet_active, touching_ghost) do | ||
Kernel.&&(power_pellet_active, touching_ghost) | ||
end | ||
end | ||
] | ||
end | ||
|
||
test_exercise_analysis "detects ||", | ||
comments_include: [Constants.pacman_rules_use_strictly_boolean_operators()] do | ||
[ | ||
defmodule Rules do | ||
def score?(touching_power_pellet, touching_dot) do | ||
touching_power_pellet || touching_dot | ||
end | ||
end, | ||
defmodule Rules do | ||
def score?(touching_power_pellet, touching_dot) do | ||
Kernel.||(touching_power_pellet, touching_dot) | ||
end | ||
end | ||
] | ||
end | ||
|
||
test_exercise_analysis "detects !", | ||
comments_include: [Constants.pacman_rules_use_strictly_boolean_operators()] do | ||
[ | ||
defmodule Rules do | ||
def lose?(power_pellet_active, touching_ghost) do | ||
!power_pellet_active and touching_ghost | ||
end | ||
end, | ||
defmodule Rules do | ||
def lose?(power_pellet_active, touching_ghost) do | ||
touching_ghost and !power_pellet_active | ||
end | ||
end, | ||
defmodule Rules do | ||
def lose?(power_pellet_active, touching_ghost) do | ||
not power_pellet_active and touching_ghost | ||
end | ||
|
||
def win?(has_eaten_all_dots, power_pellet_active, touching_ghost) do | ||
has_eaten_all_dots and !lose?(power_pellet_active, touching_ghost) | ||
end | ||
end, | ||
defmodule Rules do | ||
def eat_ghost?(power_pellet_active, touching_ghost) do | ||
!!power_pellet_active and !!touching_ghost | ||
end | ||
end, | ||
defmodule Rules do | ||
def lose?(power_pellet_active, touching_ghost) do | ||
Kernel.!(power_pellet_active) and touching_ghost | ||
end | ||
end | ||
] | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first three test cases fail, while the last two work and I have no idea why. I would think I cannot go wrong with such a simple form:
But apparently I can...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting behavior. Off the top of my head I don't know either.
AST of the form:
AST of the module:
I would suspect that somewhere when the
form
is compiled, it isn't forming the correct pattern. Maybe I didn't account for unary functions with the pattern matching? Maybe it is making the wrong pattern to match against?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting an inspect after this line here would let you output the pattern generated by each form when you compile.
The output might be a bit noisy, so it might be worthwhile to comment out other cases and temporarily remove the other analyzer modules that might get compiled to isolate the pattern generated by the form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you're saying that
should get reduced to
But in reality, it gets reduced to:
So it can only ever match if it's used on its own in a single line of code. This is unlike regular function calls, which aren't wrapped in a list and thus behave like expected:
I'm not sure at the moment how to work around that...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't if that is the pattern that is made by the analyzer. it is a missed case in my design. It should have resulted as I said. So now we know where the bug is. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay, bug resolved! 👢 🪲