Minor fix to dhall diff (#1006)

`dhall diff` slightly misbehaves when diffing the following expression with
itself:

```dhall
λ(f : List Bool -> Bool) → f ([] : List Bool)
```

... producing the following diff:

```
  λ(… :   …
        → …)
→ …@…
  - [ … ] : List …
  + [ … ] : List …
```

This is because there are two places in the `Dhall.Diff` module responsible
for comparing lists:

* Once in `diffApplicationExpression`, which compares two lists with at least
  one type annotation between them
* Once in `diffPrimitiveExpression`, which compares two lists if neither one has
  a type annotation

Those cases exhaustively cover all possible pairs of lists, but there was a
third (incorrect) fallback case that prematurely gave up and displayed them as
different.  This fallback would trigger when applying a function to an empty
list, since the diffing algorithm wouldn't have a chance to return back to the
top-level `diffExpression` and try to compare the lists correctly in
`diffApplicationExpression`.

After this change, the diff now doesn't include a spurious difference:

```
  λ(… :   …
        → …)
→ …@…
  …
```
This commit is contained in:
Gabriel Gonzalez 2019-06-14 09:43:30 -07:00 committed by GitHub
parent 53f5fa158e
commit b001a61a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 4 deletions

View File

@ -370,6 +370,8 @@ Extra-Source-Files:
dhall-lang/tests/typecheck/success/simple/*.dhall
tests/format/*.dhall
tests/lint/success/*.dhall
tests/diff/*.dhall
tests/diff/*.txt
tests/regression/*.dhall
tests/tutorial/*.dhall
@ -520,6 +522,7 @@ Test-Suite tasty
GHC-Options: -Wall
Other-Modules:
Dhall.Test.Dhall
Dhall.Test.Diff
Dhall.Test.Format
Dhall.Test.Import
Dhall.Test.Lint

View File

@ -1139,10 +1139,6 @@ diffPrimitiveExpression l r@List =
diffPrimitiveExpression (ListLit Nothing bL) (ListLit Nothing bR) = align doc
where
doc = format " " (diffList bL bR)
diffPrimitiveExpression l@(ListLit {}) r =
mismatch l r
diffPrimitiveExpression l r@(ListLit {}) =
mismatch l r
diffPrimitiveExpression ListBuild ListBuild =
""
diffPrimitiveExpression l@ListBuild r =

View File

@ -0,0 +1,62 @@
{-# LANGUAGE OverloadedStrings #-}
module Dhall.Test.Diff where
import Data.Text (Text)
import Prelude hiding (FilePath)
import Test.Tasty (TestTree)
import Turtle (FilePath)
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
import qualified Dhall.Core as Core
import qualified Dhall.Diff as Diff
import qualified Dhall.Parser as Parser
import qualified Dhall.Test.Util as Test.Util
import qualified Test.Tasty as Tasty
import qualified Test.Tasty.HUnit as Tasty.HUnit
import qualified Turtle
diffDirectory :: FilePath
diffDirectory = "./tests/diff"
getTests :: IO TestTree
getTests = do
diffTests <- Test.Util.discover (Turtle.chars <* "A.dhall") diffTest (Turtle.lstree diffDirectory)
let testTree = Tasty.testGroup "diff tests" [ diffTests ]
return testTree
diffTest :: Text -> TestTree
diffTest prefix =
Tasty.HUnit.testCase (Text.unpack prefix) $ do
let leftFile = Text.unpack (prefix <> "A.dhall")
let rightFile = Text.unpack (prefix <> "B.dhall")
let diffFile = Text.unpack (prefix <> ".txt")
let toInput file = do
text <- Text.IO.readFile file
Core.throws (Parser.exprFromText mempty text)
leftInput <- toInput leftFile
rightInput <- toInput rightFile
expectedDiffText <- Text.IO.readFile diffFile
let actualDiffDocument =
Diff.diffNormalized leftInput rightInput <> "\n"
let options =
Pretty.LayoutOptions
{ Pretty.layoutPageWidth = Pretty.Unbounded }
let actualDiffText =
Pretty.Text.renderStrict
(Pretty.layoutPretty options actualDiffDocument)
let message = "The diffed expressions did not match the expected output"
Tasty.HUnit.assertEqual message expectedDiffText actualDiffText

View File

@ -4,6 +4,7 @@ import System.FilePath ((</>))
import Test.Tasty (TestTree)
import qualified Dhall.Test.Dhall
import qualified Dhall.Test.Diff
import qualified Dhall.Test.Format
import qualified Dhall.Test.Import
import qualified Dhall.Test.Lint
@ -33,6 +34,8 @@ getAllTests = do
lintTests <- Dhall.Test.Lint.getTests
diffTests <- Dhall.Test.Diff.getTests
let testTree =
Test.Tasty.testGroup "Dhall Tests"
[ normalizationTests
@ -41,6 +44,7 @@ getAllTests = do
, typecheckingTests
, formattingTests
, lintTests
, diffTests
, Dhall.Test.Regression.tests
, Dhall.Test.Tutorial.tests
, Dhall.Test.QuickCheck.tests

View File

@ -0,0 +1,4 @@
λ(… : …
→ …)
→ …@…

View File

@ -0,0 +1 @@
λ(f : List Bool -> Bool) → f ([] : List Bool)

View File

@ -0,0 +1 @@
λ(f : List Bool -> Bool) → f ([] : List Bool)