|
| 1 | +module FSharpLint.Core.Tests.Rules.Conventions.SynchronousFunctionNames |
| 2 | + |
| 3 | +open NUnit.Framework |
| 4 | + |
| 5 | +open FSharpLint.Rules |
| 6 | +open FSharpLint.Core.Tests |
| 7 | + |
| 8 | +[<TestFixture>] |
| 9 | +type TestSynchronousFunctionNames() = |
| 10 | + inherit TestAstNodeRuleBase.TestAstNodeRuleBase(SynchronousFunctionNames.rule) |
| 11 | + |
| 12 | + [<Test>] |
| 13 | + member this.``Non-asynchronous function named Async* should give violations offering removing Async prefix``() = |
| 14 | + this.Parse """ |
| 15 | +module Foo = |
| 16 | + let AsyncBar(): int = |
| 17 | + 1 |
| 18 | +""" |
| 19 | + |
| 20 | + Assert.IsTrue this.ErrorsExist |
| 21 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 22 | + |
| 23 | + [<Test>] |
| 24 | + member this.``Non-asynchronous function named *Async should give violations offering removing Async suffix``() = |
| 25 | + this.Parse """ |
| 26 | +module Foo = |
| 27 | + let BarAsync(): int = |
| 28 | + 1 |
| 29 | +""" |
| 30 | + |
| 31 | + Assert.IsTrue this.ErrorsExist |
| 32 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 33 | + |
| 34 | + [<Test>] |
| 35 | + member this.``Non-asynchronous nested function named *Async should give violations offering removing Async suffix``() = |
| 36 | + this.Parse """ |
| 37 | +module Foo = |
| 38 | + let Bar(): int = |
| 39 | + let BazAsync(): int = |
| 40 | + 0 |
| 41 | + 1 |
| 42 | +""" |
| 43 | + |
| 44 | + Assert.IsTrue this.ErrorsExist |
| 45 | + StringAssert.Contains("Baz", this.ErrorMsg) |
| 46 | + |
| 47 | + [<Test>] |
| 48 | + member this.``Private non-asynchronous function named Async* should give violations offering removing Async suffix``() = |
| 49 | + this.Parse """ |
| 50 | +module Foo = |
| 51 | + let private AsyncBar(): int = |
| 52 | + 1 |
| 53 | +""" |
| 54 | + |
| 55 | + Assert.IsTrue this.ErrorsExist |
| 56 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 57 | + |
| 58 | + [<Test>] |
| 59 | + member this.``Private non-asynchronous function named async* should give violations offering removing Async suffix``() = |
| 60 | + this.Parse """ |
| 61 | +module Foo = |
| 62 | + let private asyncBar(): int = |
| 63 | + 1 |
| 64 | +""" |
| 65 | + |
| 66 | + Assert.IsTrue this.ErrorsExist |
| 67 | + StringAssert.Contains("bar", this.ErrorMsg) |
| 68 | + |
| 69 | + [<Test>] |
| 70 | + member this.``Internal non-asynchronous function named *Async should give violations offering removing Async suffix``() = |
| 71 | + this.Parse """ |
| 72 | +module Foo = |
| 73 | + let internal AsyncBar(): int = |
| 74 | + 1 |
| 75 | +""" |
| 76 | + |
| 77 | + Assert.IsTrue this.ErrorsExist |
| 78 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 79 | + |
| 80 | + [<Test>] |
| 81 | + member this.``Async functions with Async prefix should give no violations``() = |
| 82 | + this.Parse """ |
| 83 | +let AsyncFoo(): Async<int> = |
| 84 | + async { return 1 } |
| 85 | +let AsyncBar(): Async<unit> = |
| 86 | + async { return () } |
| 87 | +let AsyncBaz(): Async<unit> = |
| 88 | + async { do! Async.Sleep(1000) } |
| 89 | +""" |
| 90 | + |
| 91 | + Assert.IsTrue this.NoErrorsExist |
| 92 | + |
| 93 | + [<Test>] |
| 94 | + member this.``Functions that return Task with Async suffix should give no violations``() = |
| 95 | + this.Parse """ |
| 96 | +let FooAsync(): Task = |
| 97 | + null |
| 98 | +let BarAsync(): Task<int> = |
| 99 | + null |
| 100 | +""" |
| 101 | + |
| 102 | + Assert.IsTrue this.NoErrorsExist |
| 103 | + |
| 104 | + |
| 105 | + [<Test>] |
| 106 | + member this.``Non-asynchronous method named Async* should give violations offering removing Async prefix``() = |
| 107 | + this.Parse """ |
| 108 | +type Foo() = |
| 109 | + member this.AsyncBar(): int = |
| 110 | + 1 |
| 111 | +""" |
| 112 | + |
| 113 | + Assert.IsTrue this.ErrorsExist |
| 114 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 115 | + |
| 116 | + [<Test>] |
| 117 | + member this.``Non-asynchronous method named *Async should give violations offering removing Async suffix``() = |
| 118 | + this.Parse """ |
| 119 | +type Foo() = |
| 120 | + member this.BarAsync(): int = |
| 121 | + 1 |
| 122 | +""" |
| 123 | + |
| 124 | + Assert.IsTrue this.ErrorsExist |
| 125 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 126 | + |
| 127 | + [<Test>] |
| 128 | + member this.``Private non-asynchronous method named *Async should give violations offering removing Async suffix``() = |
| 129 | + this.Parse """ |
| 130 | +type Foo() = |
| 131 | + member private this.AsyncBar(): int = |
| 132 | + 1 |
| 133 | +""" |
| 134 | + |
| 135 | + Assert.IsTrue this.ErrorsExist |
| 136 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 137 | + |
| 138 | + [<Test>] |
| 139 | + member this.``Internal non-asynchronous method named *Async should give violations offering removing Async suffix``() = |
| 140 | + this.Parse """ |
| 141 | +type Foo() = |
| 142 | + member internal this.AsyncBar(): int = |
| 143 | + 1 |
| 144 | +""" |
| 145 | + |
| 146 | + Assert.IsTrue this.ErrorsExist |
| 147 | + StringAssert.Contains("Bar", this.ErrorMsg) |
| 148 | + |
| 149 | + [<Test>] |
| 150 | + member this.``Async methods with Async prefix should give no violations``() = |
| 151 | + this.Parse """ |
| 152 | +type Foo() = |
| 153 | + member this.AsyncFoo(): Async<int> = |
| 154 | + async { return 1 } |
| 155 | + member this.AsyncBar(): Async<unit> = |
| 156 | + async { return () } |
| 157 | + member this.AsyncBaz(): Async<unit> = |
| 158 | + async { do! Async.Sleep(1000) } |
| 159 | +""" |
| 160 | + |
| 161 | + Assert.IsTrue this.NoErrorsExist |
| 162 | + |
| 163 | + [<Test>] |
| 164 | + member this.``Methods that return Task with Async suffix should give no violations``() = |
| 165 | + this.Parse """ |
| 166 | +type Foo() = |
| 167 | + member this.FooAsync(): Task = |
| 168 | + null |
| 169 | + member this.BarAsync(): Task<int> = |
| 170 | + null |
| 171 | +""" |
| 172 | + |
| 173 | + Assert.IsTrue this.NoErrorsExist |
0 commit comments