Skip to content

Commit

Permalink
✅ - Add QuickTest - Start Writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Peelen committed Aug 14, 2022
1 parent eec403b commit 99290c7
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
- containers
- split
- yaml-config
- QuickCheck

library:
source-dirs: src
Expand Down
13 changes: 9 additions & 4 deletions rust-reason.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: d638651666de833bab61ad9a173ef67bf5aabd3290dd88338fa71adc5cd880b5
-- hash: 3eb8f8d21cdd6cc07bf7067e8b9eaebc24c7b9c726abf24c4de7ca8584a419f4

name: rust-reason
version: 0.1.0.0
Expand Down Expand Up @@ -37,7 +37,8 @@ library
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
QuickCheck
, base >=4.7 && <5
, casing
, containers
, parsec
Expand All @@ -54,7 +55,8 @@ executable rust-reason-exe
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
QuickCheck
, base >=4.7 && <5
, casing
, containers
, parsec
Expand All @@ -68,12 +70,15 @@ test-suite rust-reason-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Helpers
SchemaParserSpec
Paths_rust_reason
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
QuickCheck
, base >=4.7 && <5
, casing
, containers
, parsec
Expand Down
8 changes: 7 additions & 1 deletion src/SchemaParser.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{-# LANGUAGE OverloadedStrings #-}

module SchemaParser (parseTypeContainer, parseSchema) where
module SchemaParser
( parseQualifiedType,
parseTypeContainer,
parseType,
parseSchema,
)
where

import Data.Set (member)
import qualified Data.Text as T
Expand Down
20 changes: 20 additions & 0 deletions test/Helpers.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Helpers (genSafeChar, genSafeString, SafeString, toString, checkN) where

import Test.QuickCheck

checkN x = quickCheckWith (stdArgs {maxSuccess = x})

genSafeChar :: Gen Char
genSafeChar = elements $ ['a' .. 'z'] ++ ['A' .. 'Z']

genSafeString :: Gen String
genSafeString = listOf genSafeChar

newtype SafeString = SafeString {toString :: String}
deriving (Show)

instance Arbitrary SafeString where
arbitrary = SafeString <$> genSafeString

genType :: Gen String
genType = listOf genSafeChar
41 changes: 41 additions & 0 deletions test/SchemaParserSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{-# LANGUAGE NumericUnderscores #-}

module SchemaParserSpec (run) where

import qualified Data.Text as T
import Helpers (SafeString, checkN, toString)
import qualified SchemaParser
import Test.QuickCheck
import Text.Parsec

parseError = "Some Parse Error"

-- (String, String) -> Either ParseError (T.Text, T.Text)
parseToEither fn s1 s2 = fn (toString s1) (toString s2) == Right (T.strip $ T.pack $ toString s1, T.strip $ T.pack $ toString s2)

-- parseQualifiedType
parseQualifiedType :: String -> String -> Either ParseError (T.Text, T.Text)
parseQualifiedType s1 s2 = runParser SchemaParser.parseQualifiedType () parseError (T.pack (s1 <> "." <> s2))

parseQualifiedTypeTest :: SafeString -> SafeString -> Bool
parseQualifiedTypeTest = parseToEither parseQualifiedType

-- ParseTypecontainer
parseTypeContainer :: String -> String -> Either ParseError (T.Text, T.Text)
parseTypeContainer s1 s2 = runParser SchemaParser.parseTypeContainer () parseError (T.pack (s1 <> "<" <> s2 <> ">"))

parseTypeContainerTest :: SafeString -> SafeString -> Bool
parseTypeContainerTest = parseToEither parseTypeContainerTest

-- ParseType
parseType :: String -> String -> Either ParseError (T.Text, T.Text)
parseType s1 s2 = runParser SchemaParser.parseType () parseError (T.pack (s1 <> "->" <> s2 <> ","))

parseTypeTest :: SafeString -> SafeString -> Bool
parseTypeTest = parseToEither parseType

run :: IO ()
run = do
checkN 1_000 parseTypeTest
checkN 1_000 parseTypeContainerTest
checkN 1_000 parseQualifiedTypeTest
4 changes: 3 additions & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import qualified SchemaParserSpec

main :: IO ()
main = putStrLn "Test suite not yet implemented"
main = SchemaParserSpec.run

0 comments on commit 99290c7

Please sign in to comment.