Skip to content

Commit

Permalink
Core(Tests): new rule and tests for it
Browse files Browse the repository at this point in the history
Add a rule that suggest usage of auto-property for property
with getter that only returns some (immutable) value. Add
tests for it.
  • Loading branch information
webwarrior-ws committed Dec 4, 2023
1 parent af75a00 commit 543ba5b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FSharpLint.Core/FSharpLint.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Rules\Conventions\CyclomaticComplexity.fs" />
<Compile Include="Rules\Conventions\FavourReRaise.fs" />
<Compile Include="Rules\Conventions\FavourConsistentThis.fs" />
<Compile Include="Rules\Conventions\SuggestUseAutoProperty.fs" />
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\RaiseWithTooManyArgumentsHelper.fs" />
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\FailwithWithSingleArgument.fs" />
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\RaiseWithSingleArgument.fs" />
Expand Down
18 changes: 18 additions & 0 deletions src/FSharpLint.Core/Rules/Conventions/SuggestUseAutoProperty.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module FSharpLint.Rules.SuggestUseAutoProperty

open System
open FSharpLint.Framework
open FSharpLint.Framework.Suggestion
open FSharp.Compiler.Syntax
open FSharpLint.Framework.Ast
open FSharpLint.Framework.Rules

let private runner (args: AstNodeRuleParams) =

failwith "Not implemented"

let rule =
{ Name = "SuggestUseAutoProperty"
Identifier = Identifiers.FavourConsistentThis
RuleConfig = { AstNodeRuleConfig.Runner = runner; Cleanup = ignore } }
|> AstNodeRule
1 change: 1 addition & 0 deletions src/FSharpLint.Core/Rules/Identifiers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ let FavourReRaise = identifier 73
let FavourConsistentThis = identifier 74
let AvoidTooShortNames = identifier 75
let FavourStaticEmptyFields = identifier 76
let SuggestUseAutoProperty = identifier 77
1 change: 1 addition & 0 deletions tests/FSharpLint.Core.Tests/FSharpLint.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="Rules\Conventions\FavourReRaise.fs" />
<Compile Include="Rules\Conventions\FavourConsistentThis.fs" />
<Compile Include="Rules\Conventions\AvoidTooShortNames.fs" />
<Compile Include="Rules\Conventions\SuggestUseAutoProperty.fs" />
<Compile Include="Rules\Conventions\Naming\NamingHelpers.fs" />
<Compile Include="Rules\Conventions\Naming\InterfaceNames.fs" />
<Compile Include="Rules\Conventions\Naming\ExceptionNames.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module FSharpLint.Core.Tests.Rules.Conventions.SuggestUseAutoProperty

open NUnit.Framework
open FSharpLint.Framework.Rules
open FSharpLint.Rules

[<TestFixture>]
type TestSuggestUseAutoProperty() =
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(SuggestUseAutoProperty.rule)

[<Test>]
member this.``Should suggest usage of auto-property for property that only returns immutable value`` () =
this.Parse """
type Foo(content: int) =
member x.Content = content
"""

Assert.IsTrue(this.ErrorsExist)

[<Test>]
member this.``Should suggest usage of auto-property for property that only returns literal`` () =
this.Parse """
type Foo() =
member x.Content = 42
"""

Assert.IsTrue(this.ErrorsExist)

[<Test>]
member this.``Shouldn't suggest usage of auto-property for non-property member``() =
this.Parse """
type Foo(content: int) =
member x.Content() = content
"""

Assert.IsTrue(this.NoErrorsExist)

[<Test>]
member this.``Shouldn't suggest usage of auto-property for property that does more than just return value``() =
this.Parse """
type Foo(content: int) =
member x.Content = content.ToString()
"""

Assert.IsTrue(this.NoErrorsExist)

[<Test>]
member this.``Shouldn't suggest usage of auto-property for property that returns mutable value``() =
this.Parse """
type Foo(content: int) =
let mutable mutableContent = content
member x.Content = mutableContent
"""

Assert.IsTrue(this.NoErrorsExist)

0 comments on commit 543ba5b

Please sign in to comment.