-
-
Notifications
You must be signed in to change notification settings - Fork 4
dd analysis for concept exercise bird-count
#99
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
Merged
Merged
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 hidden or 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 hidden or 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,26 @@ | ||
| module Exercise.BirdCount exposing (doNotUseListModule, ruleConfig) | ||
|
|
||
| import Analyzer exposing (CalledExpression(..), CalledFrom(..), Find(..)) | ||
| import Comment exposing (Comment, CommentType(..)) | ||
| import Dict | ||
| import Review.Rule exposing (Rule) | ||
| import RuleConfig exposing (AnalyzerRule(..), RuleConfig) | ||
|
|
||
|
|
||
| ruleConfig : RuleConfig | ||
| ruleConfig = | ||
| { restrictToFiles = Just [ "src/BirdCount.elm" ] | ||
| , rules = | ||
| [ CustomRule doNotUseListModule | ||
| (Comment "elm.bird-count.do_not_use_list" Essential Dict.empty) | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| doNotUseListModule : Comment -> Rule | ||
| doNotUseListModule = | ||
| Analyzer.functionCalls | ||
| { calledFrom = Anywhere | ||
| , findExpressions = [ AnyFromExternalModule [ "List" ] ] | ||
| , find = None | ||
| } |
This file contains hidden or 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 hidden or 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,170 @@ | ||
| module Exercise.BirdCountTest exposing (tests) | ||
|
|
||
| import Comment exposing (Comment, CommentType(..)) | ||
| import Dict | ||
| import Exercise.BirdCount as BirdCount | ||
| import Review.Rule exposing (Rule) | ||
| import Review.Test | ||
| import RuleConfig | ||
| import Test exposing (Test, describe, test) | ||
| import TestHelper | ||
|
|
||
|
|
||
| tests : Test | ||
| tests = | ||
| describe "BirdCountTest" | ||
| [ exemplar | ||
| , usingList | ||
| ] | ||
|
|
||
|
|
||
| rules : List Rule | ||
| rules = | ||
| BirdCount.ruleConfig |> .rules |> List.map RuleConfig.analyzerRuleToRule | ||
|
|
||
|
|
||
| exemplar : Test | ||
| exemplar = | ||
| test "should not report anything for the example solution" <| | ||
| \() -> | ||
| TestHelper.expectNoErrorsForRules rules | ||
| """ | ||
| module BirdCount exposing (busyDays, hasDayWithoutBirds, incrementDayCount, today, total) | ||
|
|
||
|
|
||
| today : List Int -> Maybe Int | ||
| today counts = | ||
| case counts of | ||
| [] -> | ||
| Nothing | ||
|
|
||
| head :: _ -> | ||
| Just head | ||
|
|
||
|
|
||
| incrementDayCount : List Int -> List Int | ||
| incrementDayCount counts = | ||
| case counts of | ||
| [] -> | ||
| [ 1 ] | ||
|
|
||
| head :: tail -> | ||
| (head + 1) :: tail | ||
|
|
||
|
|
||
| hasDayWithoutBirds : List Int -> Bool | ||
| hasDayWithoutBirds counts = | ||
| case counts of | ||
| [] -> | ||
| False | ||
|
|
||
| 0 :: _ -> | ||
| True | ||
|
|
||
| _ :: tail -> | ||
| hasDayWithoutBirds tail | ||
|
|
||
|
|
||
| total : List Int -> Int | ||
| total counts = | ||
| case counts of | ||
| [] -> | ||
| 0 | ||
|
|
||
| head :: tail -> | ||
| head + total tail | ||
|
|
||
|
|
||
| busyDays : List Int -> Int | ||
| busyDays counts = | ||
| case counts of | ||
| [] -> | ||
| 0 | ||
|
|
||
| head :: tail -> | ||
| if head >= 5 then | ||
| 1 + busyDays tail | ||
|
|
||
| else | ||
| busyDays tail | ||
| """ | ||
|
|
||
|
|
||
| usingList : Test | ||
| usingList = | ||
| let | ||
| comment = | ||
| Comment "elm.bird-count.do_not_use_list" Essential Dict.empty | ||
| in | ||
| describe "solutions that use the List function" <| | ||
| [ test "using List.head for today" <| | ||
| \() -> | ||
| """ | ||
| module BirdCount exposing (today) | ||
|
|
||
| today : List Int -> Maybe Int | ||
| today = List.head | ||
| """ | ||
| |> Review.Test.run (BirdCount.doNotUseListModule comment) | ||
| |> Review.Test.expectErrors | ||
| [ TestHelper.createExpectedErrorUnder comment "List.head" ] | ||
| , test "using List.any for hasDayWithoutBirds" <| | ||
| \() -> | ||
| """ | ||
| module BirdCount exposing (hasDayWithoutBirds) | ||
|
|
||
| hasDayWithoutBirds : List Int -> Bool | ||
| hasDayWithoutBirds = List.any ((==) 0) | ||
| """ | ||
| |> Review.Test.run (BirdCount.doNotUseListModule comment) | ||
| |> Review.Test.expectErrors | ||
| [ TestHelper.createExpectedErrorUnder comment "List.any" ] | ||
| , test "using List.sum for total" <| | ||
| \() -> | ||
| """ | ||
| module BirdCount exposing (total) | ||
|
|
||
| total : List Int -> Int | ||
| total = List.sum | ||
| """ | ||
| |> Review.Test.run (BirdCount.doNotUseListModule comment) | ||
| |> Review.Test.expectErrors | ||
| [ TestHelper.createExpectedErrorUnder comment "List.sum" ] | ||
| , test "using List.filter and List.length for busyDays" <| | ||
| \() -> | ||
| """ | ||
| module BirdCount exposing (busyDays) | ||
|
|
||
| busyDays : List Int -> Int | ||
| busyDays = List.filter ((>=) 5) >> List.length | ||
| """ | ||
| |> Review.Test.run (BirdCount.doNotUseListModule comment) | ||
| |> Review.Test.expectErrors | ||
| [ TestHelper.createExpectedErrorUnder comment "List.filter" ] | ||
| , test "using List with an alias" <| | ||
| \() -> | ||
| """ | ||
| module BirdCount exposing (today) | ||
| import List as L | ||
|
|
||
| today : List Int -> Maybe Int | ||
| today = L.head | ||
| """ | ||
| |> Review.Test.run (BirdCount.doNotUseListModule comment) | ||
| |> Review.Test.expectErrors | ||
| [ TestHelper.createExpectedErrorUnder comment "L.head" ] | ||
| , test "using List with exposing" <| | ||
| \() -> | ||
| """ | ||
| module BirdCount exposing (today) | ||
| import List exposing (head) | ||
|
|
||
| today : List Int -> Maybe Int | ||
| today = head | ||
| """ | ||
| |> Review.Test.run (BirdCount.doNotUseListModule comment) | ||
| |> Review.Test.expectErrors | ||
| [ TestHelper.createExpectedErrorUnder comment "head" | ||
| |> Review.Test.atExactly { start = { row = 6, column = 9 }, end = { row = 6, column = 13 } } | ||
| ] | ||
| ] | ||
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.
I don't think I would test all the different uses of
List, being as the rule disallows the module completely, andAnyFromExternalModuleand similar are already tested, but its not a hill I am going to die on.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 point of these tests is not to check that the rule works properly, like you said this has been tested a lot by now, the point is to show examples of "wrong" solutions that justify why we have those rules in place.
With these code examples, you can conclude "right, if someone writes a solution like that, they won't learn anything about recursion." Conversely, if you are not able to come up with an reasonable example of a solution breaking a rule, then maybe the rule isn't meaningful.
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.
We can agree to differ on this, but I do take your point.