Skip to content

Commit

Permalink
Merge pull request #43 from fosskers/colin/semver-prerel-zero
Browse files Browse the repository at this point in the history
SemVer Zeroes in prerelease
  • Loading branch information
fosskers authored Jan 24, 2021
2 parents 24a68f6 + 31ee0b3 commit 277251f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
22 changes: 13 additions & 9 deletions Data/Versions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -788,17 +788,21 @@ chunks = chunk `sepBy` char '.'
-- since a version like @1.000.1@ would parse as @1.0.1@.
chunk :: Parsec Void Text VChunk
chunk = try zeroWithLetters <|> oneZero <|> PC.some (iunit <|> sunit)
where oneZero = (:|[]) . Digits . read . T.unpack <$> string "0"
zeroWithLetters = do
z <- Digits . read . T.unpack <$> string "0"
s <- PC.some sunit
c <- optional chunk
case c of
Nothing -> pure $ NEL.cons z s
Just c' -> pure $ NEL.cons z s <> c'
where
oneZero :: Parsec Void Text (NonEmpty VUnit)
oneZero = (Digits 0 :| []) <$ single '0'

zeroWithLetters :: Parsec Void Text (NonEmpty VUnit)
zeroWithLetters = do
z <- Digits 0 <$ single '0'
s <- PC.some sunit
c <- optional chunk
case c of
Nothing -> pure $ NEL.cons z s
Just c' -> pure $ NEL.cons z s <> c'

iunit :: Parsec Void Text VUnit
iunit = Digits . read <$> some digitChar
iunit = Digits <$> ((0 <$ single '0') <|> (read <$> some digitChar))

sunit :: Parsec Void Text VUnit
sunit = Str . T.pack <$> some letterChar
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-16.17
resolver: lts-16.31
8 changes: 6 additions & 2 deletions test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ badSemVs = [ "1", "1.2", "1.2.3+a1b2bc3.1-alpha.2", "a.b.c", "1.01.1"

goodSemVs :: [T.Text]
goodSemVs = [ "0.1.0", "1.2.3", "1.2.3-1", "1.2.3-alpha", "1.2.3-alpha.2"
, "1.2.3+a1b2c3.1", "1.2.3-alpha.2+a1b2c3.1"
, "1.2.3+a1b2c3.1", "1.2.3-alpha.2+a1b2c3.1", "2.2.1-b05"
]

-- | The exact example from `http://semver.org`
Expand Down Expand Up @@ -121,6 +121,10 @@ suite = testGroup "Tests"
(zip semverOrd $ tail semverOrd)
, testGroup "Whitespace Handling"
[ testCase "1.2.3-1[ ]" $ parse semver' "semver whitespace" "1.2.3-1 " @?= Right (SemVer 1 2 3 [[Digits 1]] [])
]
, testGroup "Zero Handling"
[ testCase "2.2.1-b05" $ semver "2.2.1-b05" @?= Right (SemVer 2 2 1 [[Str "b", Digits 0, Digits 5]] [])

]
]
, testGroup "(Haskell) PVP"
Expand Down Expand Up @@ -260,7 +264,7 @@ comp :: Ord b => (T.Text -> Either a b) -> T.Text -> T.Text -> Assertion
comp f a b = check $ (<) <$> f a <*> f b

equal :: Ord r => (T.Text -> Either l r) -> T.Text -> Assertion
equal f a = check $ (\r -> compare r r == EQ) <$> f a
equal f a = check $ (\r -> r == r) <$> f a

check :: Either a Bool -> Assertion
check = assertBool "Some Either-based assertion failed" . either (const False) id
Expand Down

0 comments on commit 277251f

Please sign in to comment.