Skip to content

Commit 7462437

Browse files
authored
Merge pull request #22 from garyb/fix-ord
Fix Ord instance
2 parents 2824d40 + 05f8ae5 commit 7462437

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

bower.json

+4
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@
2525
"purescript-strings": "^2.0.2",
2626
"purescript-transformers": "^2.0.1",
2727
"purescript-unsafe-coerce": "^2.0.0"
28+
},
29+
"devDependencies": {
30+
"purescript-quickcheck": "^3.1.0",
31+
"purescript-quickcheck-laws": "^2.0.0"
2832
}
2933
}

src/Data/Path/Pathy.purs

+4-1
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,17 @@ instance ordPath :: Ord (Path a b s) where
536536
where
537537
go Current Current = EQ
538538
go Current _ = LT
539+
go _ Current = GT
539540
go Root Root = EQ
540541
go Root _ = LT
542+
go _ Root = GT
541543
go (ParentIn p1') (ParentIn p2') = compare p1' p2'
542544
go (ParentIn _) _ = LT
545+
go _ (ParentIn _) = GT
543546
go (DirIn p1' d1) (DirIn p2' d2) = compare p1' p2' <> compare d1 d2
544547
go (DirIn _ _) _ = LT
548+
go _ (DirIn _ _) = GT
545549
go (FileIn p1' f1) (FileIn p2' f2) = compare p1' p2' <> compare f1 f2
546-
go (FileIn _ _) _ = LT
547550

548551
derive instance genericPath :: Generic (Path a b s)
549552

test/Main.purs

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
module Test.Main where
22

33
import Prelude
4-
import Control.Monad.Eff.Console (CONSOLE, infoShow)
5-
import Control.Monad.Eff(Eff)
4+
import Control.Monad.Eff (Eff)
5+
import Control.Monad.Eff.Console (CONSOLE, info, infoShow)
6+
import Control.Monad.Rec.Class (Step(..), tailRecM)
7+
import Data.Foldable (foldl)
68
import Data.Maybe (Maybe(..), fromJust)
7-
import Data.Path.Pathy (Path, dir, rootDir, parseAbsDir, parseRelDir, currentDir, file, parseAbsFile, parseRelFile, parentDir', depth, sandbox, dropExtension, renameFile, canonicalize, unsandbox, unsafePrintPath, (</>), (<..>), (<.>))
9+
import Data.Path.Pathy (Path, Abs, Rel, Dir, File, Sandboxed, dir, rootDir, parseAbsDir, parseRelDir, currentDir, file, parseAbsFile, parseRelFile, parentDir', depth, sandbox, dropExtension, renameFile, canonicalize, unsandbox, unsafePrintPath, (</>), (<..>), (<.>))
10+
import Data.String as Str
811
import Partial.Unsafe (unsafePartial)
12+
import Test.QuickCheck as QC
13+
import Test.QuickCheck.Gen as Gen
14+
import Test.QuickCheck.Laws.Data as Laws.Data
15+
import Type.Proxy (Proxy(..))
916

10-
test :: forall a. (Show a, Eq a) => String -> a -> a -> Eff (console :: CONSOLE) Unit
17+
test :: forall a eff. (Show a, Eq a) => String -> a -> a -> Eff (console :: CONSOLE | eff) Unit
1118
test name actual expected= do
1219
infoShow $ "Test: " <> name
1320
if expected == actual then infoShow $ "Passed: " <> (show expected) else infoShow $ "Failed: Expected " <> (show expected) <> " but found " <> (show actual)
1421

15-
test' :: forall a b s. String -> Path a b s -> String -> Eff (console :: CONSOLE) Unit
22+
test' :: forall a b s eff. String -> Path a b s -> String -> Eff (console :: CONSOLE | eff) Unit
1623
test' n p s = test n (unsafePrintPath p) s
1724

18-
main :: Eff (console :: CONSOLE) Unit
19-
main = do
20-
infoShow "NEW TEST"
25+
newtype ArbPath = ArbPath (Path Abs File Sandboxed)
26+
27+
derive newtype instance eqArbPath :: Eq ArbPath
28+
derive newtype instance ordArbPath :: Ord ArbPath
29+
30+
runArbPath ArbPath (Path Abs File Sandboxed)
31+
runArbPath (ArbPath p) = p
32+
33+
instance arbitraryArbPathQC.Arbitrary ArbPath where
34+
arbitrary = do
35+
numDirs ← Gen.chooseInt 1 10
36+
dirs ← map dir <$> Gen.vectorOf numDirs pathPart
37+
filename ← file <$> pathPart
38+
pure $ ArbPath $ rootDir </> foldl (flip (</>)) filename (dirs Array (Path Rel Dir Sandboxed))
2139

40+
pathPart Gen.Gen String
41+
pathPart = Gen.suchThat QC.arbitrary (not <<< Str.null)
42+
43+
main :: QC.QC () Unit
44+
main = do
2245
-- Should not compile:
2346
-- test "(</>) - file in dir" (printPath (file "image.png" </> dir "foo")) "./image.png/foo"
2447

@@ -89,3 +112,7 @@ main = do
89112
test "parseAbsDir - /foo/" (parseAbsDir "/foo/") (Just $ rootDir </> dir "foo")
90113

91114
test "parseAbsDir - /foo/bar" (parseAbsDir "/foo/bar/") (Just $ rootDir </> dir "foo" </> dir "bar")
115+
116+
info "Checking typeclass laws..."
117+
Laws.Data.checkEq (Proxy :: Proxy ArbPath)
118+
Laws.Data.checkOrd (Proxy :: Proxy ArbPath)

0 commit comments

Comments
 (0)