|
| 1 | +module FSharpLint.Core.Tests.Rules.Conventions.DisallowShadowing |
| 2 | + |
| 3 | +open NUnit.Framework |
| 4 | + |
| 5 | +open FSharpLint.Rules |
| 6 | +open FSharpLint.Core.Tests |
| 7 | + |
| 8 | +[<TestFixture>] |
| 9 | +type TestConventionsDisallowShadowing() = |
| 10 | + inherit TestAstNodeRuleBase.TestAstNodeRuleBase(DisallowShadowing.rule) |
| 11 | + |
| 12 | + [<Test>] |
| 13 | + member this.``Should produce error for shadowed variable``() = |
| 14 | + this.Parse """ |
| 15 | +let foo = 0 |
| 16 | +
|
| 17 | +module Foo = |
| 18 | + let foo = 1""" |
| 19 | + |
| 20 | + Assert.IsTrue this.ErrorsExist |
| 21 | + |
| 22 | + [<Test>] |
| 23 | + member this.``Should produce error for shadowed variable, inside function``() = |
| 24 | + this.Parse """ |
| 25 | +let bar () = |
| 26 | + let foo = 0 |
| 27 | + let foo = 1 |
| 28 | + foo""" |
| 29 | + |
| 30 | + Assert.IsTrue this.ErrorsExist |
| 31 | + |
| 32 | + [<Test>] |
| 33 | + member this.``Should produce error for shadowed variable (function argument)``() = |
| 34 | + this.Parse """ |
| 35 | +let foo = 0 |
| 36 | +let bar foo = foo + 1""" |
| 37 | + |
| 38 | + Assert.IsTrue this.ErrorsExist |
| 39 | + |
| 40 | + [<Test>] |
| 41 | + member this.``Should produce error for shadowed variable (function argument, inside function)``() = |
| 42 | + this.Parse """ |
| 43 | +let baz () = |
| 44 | + let foo = 0 |
| 45 | + let bar foo = foo + 1 |
| 46 | + ()""" |
| 47 | + |
| 48 | + Assert.IsTrue this.ErrorsExist |
| 49 | + |
| 50 | + [<Test>] |
| 51 | + member this.``Should produce error for shadowed variable (function argument, nested)``() = |
| 52 | + this.Parse """ |
| 53 | +let baz foo = |
| 54 | + let bar foo = foo + 1 |
| 55 | + ()""" |
| 56 | + |
| 57 | + Assert.IsTrue this.ErrorsExist |
| 58 | + |
| 59 | + [<Test>] |
| 60 | + member this.``Should produce error for shadowed variable (function argument, tuple)``() = |
| 61 | + this.Parse """ |
| 62 | +let foo = 0 |
| 63 | +let bar (foo, baz) = foo + baz""" |
| 64 | + |
| 65 | + Assert.IsTrue this.ErrorsExist |
| 66 | + |
| 67 | + [<Test>] |
| 68 | + member this.``Should produce error for shadowed variable (lambda function argument)``() = |
| 69 | + this.Parse """ |
| 70 | +let foo = 0 |
| 71 | +(fun foo -> foo + 1) 0 |> ignore""" |
| 72 | + |
| 73 | + Assert.IsTrue this.ErrorsExist |
| 74 | + |
| 75 | + [<Test>] |
| 76 | + member this.``Should produce error for shadowed variable inside lambda function``() = |
| 77 | + this.Parse """ |
| 78 | +(fun foo -> |
| 79 | + let foo = foo + 1 |
| 80 | + foo) 0 |> ignore""" |
| 81 | + |
| 82 | + Assert.IsTrue this.ErrorsExist |
| 83 | + |
| 84 | + [<Test>] |
| 85 | + member this.``Should produce error for shadowed variable (match pattern)``() = |
| 86 | + this.Parse """ |
| 87 | +let foo = 0 |
| 88 | +match 1 with |
| 89 | +| foo -> foo""" |
| 90 | + |
| 91 | + Assert.IsTrue this.ErrorsExist |
| 92 | + |
| 93 | + [<Test>] |
| 94 | + member this.``Should produce error for shadowed variable (as match pattern)``() = |
| 95 | + this.Parse """ |
| 96 | +let foo = 0 |
| 97 | +match (1, 2) with |
| 98 | +| (x, y) as foo -> foo""" |
| 99 | + |
| 100 | + Assert.IsTrue this.ErrorsExist |
| 101 | + |
| 102 | + [<Test>] |
| 103 | + member this.``Should produce error for shadowed variable inside match pattern``() = |
| 104 | + this.Parse """ |
| 105 | +match 1 with |
| 106 | +| foo -> |
| 107 | + let foo = foo + 1 |
| 108 | + foo""" |
| 109 | + |
| 110 | + Assert.IsTrue this.ErrorsExist |
| 111 | + |
| 112 | + [<Test>] |
| 113 | + member this.``Should produce error for shadowed variable inside type definition``() = |
| 114 | + this.Parse """ |
| 115 | +type Foo(foo) = |
| 116 | + let foo = foo + 1 |
| 117 | + |
| 118 | + member this.Bar = foo""" |
| 119 | + |
| 120 | + Assert.IsTrue this.ErrorsExist |
| 121 | + |
| 122 | + [<Test>] |
| 123 | + member this.``Should produce error for shadowed variable inside member definition``() = |
| 124 | + this.Parse """ |
| 125 | +type Foo() = |
| 126 | + member this.Bar(foo) = |
| 127 | + let foo = foo + 1 |
| 128 | + foo""" |
| 129 | + |
| 130 | + Assert.IsTrue this.ErrorsExist |
| 131 | + |
| 132 | + [<Test>] |
| 133 | + member this.``Should not produce error when variable with same name exists in another module``() = |
| 134 | + this.Parse """ |
| 135 | +module Foo = |
| 136 | + let foo = 0 |
| 137 | +let foo = 1""" |
| 138 | + |
| 139 | + Assert.IsTrue this.NoErrorsExist |
0 commit comments