unused/test/Unused/ProjectionSpec.hs
Joshua Clayton 7fe32edc4d
Support projections-style transformations to reduce false-positives
Basic aliases (e.g. `admin?`/`be_admin`) can be represented easily with simple
wildcards, but more complex transformations require a different mechanism.

Instead of using `%s` to represent strings that can be replaced 1:1, this
introduces a syntax inspired by https://github.com/tpope/vim-projectionist, as
such:

    - name: Rails
      aliases:
      - from: "*Validator"
        to: "{snakecase}"

This would find `AbsoluteUriValidator` and also match `absolute_uri`, which
would be found if the validation was in use.

This currently supports the `camelcase` and `snakecase` transformations,
as well as no transformation.

Closes #18
2016-07-19 15:14:34 -04:00

31 lines
936 B
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Unused.ProjectionSpec where
import Data.Text (Text)
import Test.Hspec
import Unused.Projection
main :: IO ()
main = hspec spec
spec :: Spec
spec = parallel $
describe "translate" $ do
it "replaces the text without transforms" $
translate' "foo_{}" "bar" `shouldBe` "foo_bar"
it "handles text transformations" $ do
translate' "{camelcase}Validator" "proper_email" `shouldBe` "ProperEmailValidator"
translate' "{snakecase}" "ProperEmail" `shouldBe` "proper_email"
translate' "{camelcase}Validator" "AlreadyCamelcase" `shouldBe` "AlreadyCamelcaseValidator"
it "handles unknown transformations" $
translate' "{unknown}Validator" "proper_email" `shouldBe` "proper_email"
translate' :: Text -> Text -> Text
translate' template v =
case translate template of
Right f -> f v
Left _ -> v