2019-08-04 12:42:59 +03:00
|
|
|
module FixTest exposing (all)
|
|
|
|
|
|
|
|
import Elm.Syntax.Range exposing (Range)
|
2019-08-04 13:27:17 +03:00
|
|
|
import Expect
|
2019-08-04 12:42:59 +03:00
|
|
|
import Lint.Fix as Fix
|
|
|
|
import Test exposing (Test, describe, test)
|
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
|
|
|
describe "Fix"
|
|
|
|
[ mergeRangesTest
|
2019-08-04 13:04:31 +03:00
|
|
|
, fixTest
|
2019-08-04 12:42:59 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
mergeRangesTest : Test
|
|
|
|
mergeRangesTest =
|
|
|
|
describe "mergeRanges"
|
|
|
|
[ test "should take the start of the one before and the end of the one after if ranges are distinct" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
a : Range
|
|
|
|
a =
|
|
|
|
{ start = { row = 2, column = 10 }
|
|
|
|
, end = { row = 2, column = 15 }
|
|
|
|
}
|
|
|
|
|
|
|
|
b : Range
|
|
|
|
b =
|
|
|
|
{ start = { row = 20, column = 1 }
|
|
|
|
, end = { row = 22, column = 5 }
|
|
|
|
}
|
|
|
|
|
|
|
|
expected : Range
|
|
|
|
expected =
|
|
|
|
{ start = { row = 2, column = 10 }
|
|
|
|
, end = { row = 22, column = 5 }
|
|
|
|
}
|
|
|
|
in
|
|
|
|
Expect.all
|
|
|
|
[ \() ->
|
|
|
|
Fix.mergeRanges a b
|
|
|
|
|> Expect.equal expected
|
|
|
|
, \() ->
|
|
|
|
Fix.mergeRanges b a
|
|
|
|
|> Expect.equal expected
|
|
|
|
]
|
|
|
|
()
|
|
|
|
, test "should take the start of the one that starts first and the end of the one ends last if ranges are shared" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
a : Range
|
|
|
|
a =
|
|
|
|
{ start = { row = 2, column = 10 }
|
|
|
|
, end = { row = 10, column = 15 }
|
|
|
|
}
|
|
|
|
|
|
|
|
b : Range
|
|
|
|
b =
|
|
|
|
{ start = { row = 5, column = 1 }
|
|
|
|
, end = { row = 22, column = 5 }
|
|
|
|
}
|
|
|
|
|
|
|
|
expected : Range
|
|
|
|
expected =
|
|
|
|
{ start = { row = 2, column = 10 }
|
|
|
|
, end = { row = 22, column = 5 }
|
|
|
|
}
|
|
|
|
in
|
|
|
|
Expect.all
|
|
|
|
[ \() ->
|
|
|
|
Fix.mergeRanges a b
|
|
|
|
|> Expect.equal expected
|
|
|
|
, \() ->
|
|
|
|
Fix.mergeRanges b a
|
|
|
|
|> Expect.equal expected
|
|
|
|
]
|
|
|
|
()
|
|
|
|
, test "should take the bigger one if one is included in the other" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
a : Range
|
|
|
|
a =
|
|
|
|
{ start = { row = 2, column = 10 }
|
|
|
|
, end = { row = 10, column = 15 }
|
|
|
|
}
|
|
|
|
|
|
|
|
b : Range
|
|
|
|
b =
|
|
|
|
{ start = { row = 3, column = 1 }
|
|
|
|
, end = { row = 4, column = 5 }
|
|
|
|
}
|
|
|
|
in
|
|
|
|
Expect.all
|
|
|
|
[ \() ->
|
|
|
|
Fix.mergeRanges a b
|
|
|
|
|> Expect.equal a
|
|
|
|
, \() ->
|
|
|
|
Fix.mergeRanges b a
|
|
|
|
|> Expect.equal a
|
|
|
|
]
|
|
|
|
()
|
|
|
|
]
|
2019-08-04 13:04:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
fixTest : Test
|
|
|
|
fixTest =
|
|
|
|
describe "fix"
|
|
|
|
[ test "should apply a removal on a single line" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
a = Debug.log "foo" 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.removeRange
|
|
|
|
{ start = { row = 2, column = 5 }
|
|
|
|
, end = { row = 2, column = 20 }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:04:31 +03:00
|
|
|
a = 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:04:31 +03:00
|
|
|
, test "should apply a replacement on a single line" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
some_var = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.replaceRangeBy
|
|
|
|
{ start = { row = 2, column = 1 }
|
|
|
|
, end = { row = 2, column = 9 }
|
|
|
|
}
|
|
|
|
"someVar"
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:04:31 +03:00
|
|
|
someVar = 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:04:31 +03:00
|
|
|
, test "should insert something on a single line" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.insertAt
|
|
|
|
{ row = 2, column = 5 }
|
|
|
|
"""Debug.log "foo" """
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:04:31 +03:00
|
|
|
a = Debug.log "foo" 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:10:54 +03:00
|
|
|
, test "should apply multiple fixes regardless of the order" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.replaceRangeBy
|
|
|
|
{ start = { row = 2, column = 1 }
|
|
|
|
, end = { row = 2, column = 2 }
|
|
|
|
}
|
|
|
|
"someVar"
|
|
|
|
, Fix.insertAt
|
|
|
|
{ row = 2, column = 5 }
|
|
|
|
"""Debug.log "foo" """
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Expect.all
|
|
|
|
[ \() ->
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:10:54 +03:00
|
|
|
someVar = Debug.log "foo" 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:10:54 +03:00
|
|
|
, \() ->
|
|
|
|
Fix.fix (List.reverse fixes) source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:10:54 +03:00
|
|
|
someVar = Debug.log "foo" 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:10:54 +03:00
|
|
|
]
|
|
|
|
()
|
2019-08-04 13:27:17 +03:00
|
|
|
, test "should apply a removal on multiple lines" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (someCode)
|
|
|
|
someCode = 2
|
|
|
|
|
|
|
|
a : Int
|
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.removeRange
|
|
|
|
{ start = { row = 4, column = 1 }
|
|
|
|
, end = { row = 5, column = 6 }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (someCode)
|
2019-08-04 13:27:17 +03:00
|
|
|
someCode = 2
|
|
|
|
|
|
|
|
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:27:17 +03:00
|
|
|
, test "should apply a replacement whose content is on multiple lines" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
some_var = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.replaceRangeBy
|
|
|
|
{ start = { row = 2, column = 1 }
|
|
|
|
, end = { row = 2, column = 13 }
|
|
|
|
}
|
|
|
|
"someVar =\n 1"
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:27:17 +03:00
|
|
|
someVar =
|
|
|
|
1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:27:17 +03:00
|
|
|
, test "should apply a replacement on multiple lines" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
some_var =
|
|
|
|
1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.replaceRangeBy
|
|
|
|
{ start = { row = 2, column = 1 }
|
|
|
|
, end = { row = 3, column = 4 }
|
|
|
|
}
|
|
|
|
"someVar = 1"
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:27:17 +03:00
|
|
|
someVar = 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:27:17 +03:00
|
|
|
, test "should apply a replacement on multiple lines with something on multiple lines" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (a)
|
|
|
|
some_var =
|
|
|
|
1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.replaceRangeBy
|
|
|
|
{ start = { row = 2, column = 1 }
|
|
|
|
, end = { row = 3, column = 4 }
|
|
|
|
}
|
|
|
|
"foo =\n 2"
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (a)
|
2019-08-04 13:27:17 +03:00
|
|
|
foo =
|
|
|
|
2
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:27:17 +03:00
|
|
|
, test "should apply an insertion on multiple lines" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
source : String
|
|
|
|
source =
|
|
|
|
"""module A exposing (someCode)
|
|
|
|
someCode = 2
|
|
|
|
|
|
|
|
a : Int
|
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixes =
|
|
|
|
[ Fix.insertAt
|
|
|
|
{ row = 4, column = 1 }
|
|
|
|
"b =\n 2\n"
|
|
|
|
]
|
|
|
|
in
|
|
|
|
Fix.fix fixes source
|
2019-08-04 15:48:57 +03:00
|
|
|
|> Expect.equal (Fix.Successful """module A exposing (someCode)
|
2019-08-04 13:27:17 +03:00
|
|
|
someCode = 2
|
|
|
|
|
|
|
|
b =
|
|
|
|
2
|
|
|
|
a : Int
|
|
|
|
a = 1
|
2019-08-04 15:48:57 +03:00
|
|
|
""")
|
2019-08-04 13:04:31 +03:00
|
|
|
]
|