Skip to content

Commit

Permalink
Replace test-unit with spec
Browse files Browse the repository at this point in the history
  • Loading branch information
newlandsvalley committed May 6, 2022
1 parent b3e29d2 commit fc93be9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 73 deletions.
2 changes: 2 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
},
"devDependencies": {
"purescript-quickcheck": "^8.0.1",
"purescript-spec": "^7.0.0",
"purescript-spec-quickcheck": "^5.0.0",
"purescript-node-buffer": "^8.0.0",
"purescript-node-fs-aff": "^9.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"pulp": "^16.0.0",
"purescript": "^0.15.0",
"purescript-psa": "^0.6.0",
"spago": "^0.20.8"
"spago": "^0.20.9"
}
}
7 changes: 5 additions & 2 deletions test.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ let conf = ./spago.dhall
in conf // {
sources = conf.sources # [ "test/**/*.purs" ],
dependencies = conf.dependencies #
[ "free"
[ "aff"
, "exceptions"
, "node-buffer"
, "node-path"
, "node-fs-aff"
, "nonempty"
, "test-unit"
, "spec"
, "spec-quickcheck"
, "transformers"
, "quickcheck"
]
}
56 changes: 30 additions & 26 deletions test/FileParser.purs
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
module Test.FileParser (parserSuite) where
module Test.FileParser (parserSpec) where

import Prelude (Unit, bind, discard, ($), (<<<))
import Prelude (Unit, bind, discard, pure, unit, ($), (<<<))
import Control.Monad.Error.Class (class MonadThrow)
import Data.Midi (Recording)
import Data.Midi.Parser (normalise, parse)
import Node.Path as Path
import Effect.Class (liftEffect)
import Control.Monad.Free (Free)
import Effect.Aff (Aff)
import Effect.Exception (Error)
import Data.Either (Either(..))
import Node.Buffer (toString)
import Node.Encoding (Encoding(..))
import Node.FS.Aff (readFile)
import Test.Unit (Test, TestF, test, suite, success, failure)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (fail)

parserSuite :: Free TestF Unit
parserSuite = do
suite "file parser" do
test "lillasystern" do
assertParses "lillasystern.midi"
test "frost" do
assertParses "frost.midi"
test "chord sample" do
assertParses "chordsample.midi"
test "Carolan's receipt" do
assertParses "carolansreceipt.midi"
test "Galway hornpipe" do
assertParses "Galway-Hornpipe.midi"
test "Planxty Burke" do
assertParses "plxburke.midi"
test "The bonny bonny banks of loch Lomond" do
assertParses "lomond.midi"
parserSpec :: Spec Unit
parserSpec = do
describe "midi" do
describe "file parser" do
it "parses lillasystern" do
assertParses "lillasystern.midi"
it "parses frost" do
assertParses "frost.midi"
it "parses a chord sample" do
assertParses "chordsample.midi"
it "parses Carolan's receipt" do
assertParses "carolansreceipt.midi"
it "parses the Galway hornpipe" do
assertParses "Galway-Hornpipe.midi"
it "parses Planxty Burke" do
assertParses "plxburke.midi"
it "parses the bonny banks of Loch Lomond" do
assertParses "lomond.midi"

-- | tunnel a binary MIDI file as text and parse it
assertParses :: String -> Test
assertParses :: String -> Aff Unit
assertParses fileName =
do
let
Expand All @@ -40,15 +44,15 @@ assertParses fileName =
estr <- liftEffect $ (toString Binary) buffer
canParse estr

canParse :: String -> Test
canParse :: forall m. MonadThrow Error m => String -> m Unit
canParse str =
let
fullParse :: String -> Either String Recording
fullParse s =
(parse <<< normalise) s
in
case fullParse str of
Right _ ->
success
Right _ ->
pure unit
Left err ->
failure (err)
fail err
25 changes: 13 additions & 12 deletions test/Instrument.purs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Test.Instrument (instrumentChecksSuite) where
module Test.Instrument (instrumentChecksSpec) where


import Prelude (Unit, ($), (<$>), discard, map)
import Data.List (toUnfoldable)
import Data.Array.NonEmpty (fromNonEmpty)
import Data.NonEmpty ((:|))
import Data.Maybe (Maybe(..))
import Control.Monad.Free (Free)
import Test.Unit (TestF, test, suite)
import Test.Unit.Assert (equal) as Assert
import Test.QuickCheck.Arbitrary (class Arbitrary)
import Test.QuickCheck (Result(), (===))
import Test.Unit.QuickCheck (quickCheck)

import Test.Spec.QuickCheck (quickCheck)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.QuickCheck.Gen (Gen, elements)
import Data.Midi.Instrument (InstrumentName(..), gleitzmanName, gleitzmanNames,
instrumentNames, readGleitzman)
Expand Down Expand Up @@ -58,12 +58,13 @@ roundTripGleitzmanProperty (TestGleitzman s) =
in
(Just s :: Maybe String) === name

instrumentChecksSuite :: Free TestF Unit
instrumentChecksSuite = do
suite "instrument" do
test "unknown" do
Assert.equal Nothing (readGleitzman "unknown")
test "round trip instrument name" do
instrumentChecksSpec :: Spec Unit
instrumentChecksSpec = do
describe "instrument" do
it "handles an unknown instrument name" do
Nothing `shouldEqual` (readGleitzman "unknown")
it "round trips instrument names" do
quickCheck roundTripInstrumentProperty
test "round trip Gleitzman name" do
it "round trips Gleitzman names" do
quickCheck roundTripGleitzmanProperty

23 changes: 11 additions & 12 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
module Test.Main where

import Prelude (Unit, discard)
import Prelude (Unit, discard, ($))
import Effect (Effect)
import Test.Unit (suite)
import Test.Unit.Main (runTest)
import Test.FileParser (parserSuite)
import Test.Instrument (instrumentChecksSuite)
import Test.Parser (parserChecksSuite)
import Effect.Aff (launchAff_)
import Test.Spec.Reporter (specReporter)
import Test.Spec.Runner (runSpec)
import Test.FileParser (parserSpec)
import Test.Instrument (instrumentChecksSpec)
import Test.Parser (parserChecksSpec)

main :: Effect Unit
main =
runTest do
suite "midi" do
parserSuite
instrumentChecksSuite
parserChecksSuite
main = launchAff_ $ runSpec [ specReporter] do
parserSpec
instrumentChecksSpec
parserChecksSpec
40 changes: 20 additions & 20 deletions test/Parser.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module Test.Parser (parserChecksSuite) where
module Test.Parser (parserChecksSpec) where


import Data.Midi

import Control.Monad.Free (Free)
import Data.Array (singleton, fromFoldable) as Array
import Data.Array.NonEmpty (NonEmptyArray, fromNonEmpty)
import Data.Array.NonEmpty (NonEmptyArray, fromNonEmpty, fromFoldable1)
import Data.Char (fromCharCode)
import Data.Either (Either(..))
import Data.List (List(..), (:), fromFoldable, toUnfoldable)
Expand All @@ -20,8 +19,9 @@ import Prelude (Unit, ($), (<$>), (<*>), (<>), (+), (<<<), bind, discard, map, n
import Test.QuickCheck (Result, (===))
import Test.QuickCheck.Arbitrary (class Arbitrary)
import Test.QuickCheck.Gen (Gen, chooseInt, elements, frequency, listOf, oneOf)
import Test.Unit (TestF, test, suite)
import Test.Unit.QuickCheck (quickCheck)

import Test.Spec.QuickCheck (quickCheck)
import Test.Spec (Spec, describe, it)

-- | quickcheck-style tests adapted from the elm-comidi tests courtesy of @rhofour

Expand Down Expand Up @@ -348,9 +348,10 @@ allEvents :: Generate.Context -> NonEmptyArray (Gen Event)
allEvents ctx =
fromNonEmpty $ arbNoteOn :| (channelEvents <> Array.singleton (arbSysEx ctx) <> metaEvents)

weightedEvents :: Generate.Context -> NonEmptyList (Tuple Number (Gen Event))

weightedEvents :: Generate.Context -> NonEmptyArray (Tuple Number (Gen Event))
weightedEvents ctx =
NonEmptyList $
fromFoldable1 $ NonEmptyList $
(Tuple 1.0 arbNoteOn)
:|
( weightedChannelEvents
Expand Down Expand Up @@ -381,9 +382,7 @@ arbTestMessage =
arbTrack :: Gen Track
arbTrack =
do
count <- chooseInt 0 100
-- this doesn't seem to terminate when testing with spago
-- count <- chooseInt 0 250
count <- chooseInt 0 250
Track <$> listOf count arbMessage

arbTracks :: Int -> Gen (List Track)
Expand Down Expand Up @@ -450,13 +449,14 @@ unsafeFromCharCode :: Int -> Char
unsafeFromCharCode i =
fromMaybe 'a' $ fromCharCode i

-- | the test suite
parserChecksSuite :: Free TestF Unit
parserChecksSuite = do
suite "parser" do
test "round trip (stream) event" do
quickCheck roundTripStreamEventProperty
test "round trip message" do
quickCheck roundTripMessageProperty
test "round trip recording" do
quickCheck roundTripRecordingProperty
-- | the test spec
parserChecksSpec :: Spec Unit
parserChecksSpec = do
describe "midi" do
describe "parser" do
it "round trips (stream) event" do
quickCheck roundTripStreamEventProperty
it "round trips message" do
quickCheck roundTripMessageProperty
it "round trips recording" do
quickCheck roundTripRecordingProperty

0 comments on commit fc93be9

Please sign in to comment.