Skip to content

Commit

Permalink
fix rewriter eq checks on Windows by skipping \r s
Browse files Browse the repository at this point in the history
  • Loading branch information
raehik committed Aug 11, 2021
1 parent ec4e8f7 commit 9045cfd
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion test/Language/Fortran/RewriterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,42 @@ compareFile expected actual = do
c2 <- BC.readFile actual
compareByteString c1 c2

-- XXX: It appears the rewriter has different behaviour on Windows and Linux --
-- specifically relating to newlines. So we use a custom equality check
-- that skips Windows newlines (@\r@ characters are skipped).
--
-- (This is morally grey: the issue is unlikely to rear its head in
-- practical usage, _but_ if you were to be comparing rewriter output from
-- different platforms, you may encounter it. Most cross-platform text
-- tooling recognises/ignores Windows newlines, however.)
compareByteString :: BC.ByteString -> BC.ByteString -> IO Bool
compareByteString expected actual = if expected == actual
compareByteString expected actual = if expected `eqSkipWinNewlines` actual
then return True
else do
BC.putStrLn ">>>>>>> EXPECTED"
BC.putStrLn expected
BC.putStrLn ">>>>>>> ACTUAL"
BC.putStrLn actual
return False

eqSkipWinNewlines :: BC.ByteString -> BC.ByteString -> Bool
eqSkipWinNewlines x1 x2 = go (BC.uncons x1) (BC.uncons x2)
where
go :: Maybe (Char, BC.ByteString) -> Maybe (Char, BC.ByteString) -> Bool

-- both reached EOF simultaneously: identical
go Nothing Nothing = True

-- next character in either bytestring is @\r@: skip
go (Just ('\r', bs1)) bs2 = go (BC.uncons bs1) bs2
go bs1 (Just ('\r', bs2)) = go bs1 (BC.uncons bs2)

-- only one bytestring is EOF: different
go Nothing (Just _) = False
go (Just _) Nothing = False

-- main case: check equality of next words
go (Just (b1, bs1)) (Just (b2, bs2)) =
if b1 == b2
then go (BC.uncons bs1) (BC.uncons bs2)
else False

0 comments on commit 9045cfd

Please sign in to comment.