Skip to content
Snippets Groups Projects
TEquivalenceClasses.hs 1.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • module TEquivalenceClasses where
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
    import Control.Monad
    
    import Data.List          (elemIndex)
    import Data.List.Split    (splitOn)
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
    import Data.Maybe
    
    import System.IO          (stderr, stdout)
    import System.IO.Silently (hSilence)
    import System.IO.Unsafe   (unsafePerformIO)
    
    import Test.HUnit
    
    
    import API
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
    import Javawlp.Engine.HelperFunctions (parseMethodIds)
    
    import Model
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
    testEquiv :: Response -> String -> String -> String -> Assertion
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
    testEquiv b src s s' = do
      res <- hSilence [stdout, stderr] $ compareSpec Debug File (src, s) (src, s')
      (case res of
    
        NotEquivalent _ _ -> NotEquivalent emptyModel defFeedback'
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
        x               -> x) @?= b
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
    (.==) = testEquiv Equivalent
    
    (.!=) = testEquiv $ NotEquivalent emptyModel defFeedback'
    
    genEquivTests edslSrc =
    
      let methodIds = unsafePerformIO (hSilence [stdout, stderr] $ parseMethodIds edslSrc)
    
          getClass = last . splitOn "_"
    
          tailFrom :: Eq a => [a] -> a -> [a]
          tailFrom xs x = case elemIndex x xs of Just i  -> snd $ splitAt i xs
                                                 Nothing -> []
    
      in [ a `op` b | a <- methodIds
    
                    , b <- methodIds `tailFrom` a
    
                    , a /= b
    
                    , let op = unsafePerformIO $ do
    
    Orestis Melkonian's avatar
    Orestis Melkonian committed
                            putStrLn $ "  (" ++ a ++ testOpS ++ b ++ ")"
                            return $ testOp edslSrc
                            where [clA, clB] = getClass <$> [a, b]
                                  eq = clA == clB
                                  (testOp, testOpS) =
                                    if eq then ((.==), " == ") else ((.!=), " != ")