Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/Data/URI/Common.js

This file was deleted.

21 changes: 11 additions & 10 deletions src/Data/URI/Common.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Data.URI.Common where
import Prelude

import Control.Alt ((<|>))
import Data.Array (fromFoldable)
import Control.MonadZero (guard)
import Data.Array (fromFoldable, head)
import Data.Either (Either(..), fromRight)
import Data.List (List)
import Data.Maybe (Maybe(..))
Expand Down Expand Up @@ -78,12 +79,12 @@ anyMatch rx = Parser \{ str: str, pos: i } → case match1From rx i str of
Nothing → Left { error: (ParseError $ "Expected " <> show rx), pos: i }

match1From ∷ RX.Regex → Int → String → Maybe String
match1From = match1FromImpl Just Nothing

foreign import match1FromImpl
∷ (∀ a. a → Maybe a)
→ (∀ a. Maybe a)
RX.Regex
→ Int
→ String
→ (Maybe String)
match1From rx' n str' =
case RX.regex (RX.source rx') (RXF.global <> RX.flags rx') of
Left _ -> Nothing
Right rx -> do
let str = S.drop n str'
i <- RX.search rx str
guard $ i == 0
matches <- RX.match rx str
join $ head matches
30 changes: 30 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import Data.List (List(..), singleton, (:))
import Data.Maybe (Maybe(Nothing, Just))
import Data.Monoid (mempty)
import Data.Path.Pathy (currentDir, parentDir', file, dir, rootDir, (</>))
import Data.String.Regex (Regex, regex)
import Data.String.Regex.Flags (global, noFlags)
import Data.Tuple (Tuple(..))
import Data.URI (AbsoluteURI(..), Authority(..), Fragment(..), HierarchicalPart(..), Host(..), Port(..), Query(..), RelativePart(..), RelativeRef(..), Scheme(..), URI(..), UserInfo(..))
import Data.URI.AbsoluteURI as AbsoluteURI
import Data.URI.Authority as Authority
import Data.URI.Common as Common
import Data.URI.Host as Host
import Data.URI.Host.Gen as Host.Gen
import Data.URI.Port as Port
Expand Down Expand Up @@ -74,6 +77,22 @@ testParseQueryParses uri query =
("parses: \"" <> uri <> "\"")
(equal (Right query) (runParser Query.parser uri))

testMatch1FromMatches :: forall a. Either String Regex -> Int -> String -> Maybe String -> TestSuite a
testMatch1FromMatches rx' n str expected = case rx' of
Left error -> test "faulty regex given" (assert error false)
Right rx ->
test
("matches: " <> show rx <> " at " <> show n <> " in " <> show str)
(equal expected $ Common.match1From rx n str)

testMatch1FromMisses :: forall a. Either String Regex -> Int -> String -> TestSuite a
testMatch1FromMisses rx' n str = case rx' of
Left error -> test "faulty regex given" (assert error false)
Right rx ->
test
("does not match: " <> show rx <> " at " <> show n <> " in " <> show str)
(equal Nothing $ Common.match1From rx n str)

main :: forall eff. Eff (console :: CONSOLE, testOutput :: TESTOUTPUT, avar :: AVAR, exception :: EXCEPTION, random :: RANDOM | eff) Unit
main = runTest $ suite "Data.URI" do

Expand Down Expand Up @@ -443,6 +462,17 @@ main = runTest $ suite "Data.URI" do
"key1=&key2="
(Query (Tuple "key1" (Just "") : Tuple "key2" (Just "") : Nil))

suite "Common.match1From" do
testMatch1FromMisses (regex "key1" noFlags) 0 ""
testMatch1FromMisses (regex "key1" noFlags) 1 "key1"
testMatch1FromMisses (regex "key1" noFlags) 1 "key1=&key1="
testMatch1FromMisses (regex "key1" global) 1 "key1=&key1="
testMatch1FromMisses (regex "key1|key2" noFlags) 1 "key1=&key2="

testMatch1FromMatches (regex "key1" noFlags) 0 "key1" (Just "key1")
testMatch1FromMatches (regex "key1" noFlags) 6 "key1=&key1=" (Just "key1")
testMatch1FromMatches (regex "key1|key2" noFlags) 6 "key1=&key2=" (Just "key2")

forAll :: forall eff prop. QC.Testable prop => QCG.Gen prop -> Test (console :: CONSOLE, random :: RANDOM, exception :: EXCEPTION | eff)
forAll = quickCheck

Expand Down