Skip to content

Commit 80f2a98

Browse files
committed
[update] テストで使用する ok, ng 関数を TestCase.Utils へ移動
1 parent d2de527 commit 80f2a98

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

lib/copager2/src/Copager2/Prelude.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Copager2.Prelude
1212
, tokenQQ
1313
, RuleSet(..)
1414
, ruleQQ
15+
, IR
1516
) where
1617

1718
import Copager2.Core.Processor (Processor, ProcessorT(..), runProcessor)
@@ -20,3 +21,4 @@ import Copager2.Core.Language.Token (TokenSet(..))
2021
import Copager2.Core.Language.Token.QuasiQuote (tokenQQ)
2122
import Copager2.Core.Language.Rule (RuleSet(..))
2223
import Copager2.Core.Language.Rule.QuasiQuote (ruleQQ)
24+
import Copager2.Core.Language.IR (IR)

test/src/TestCase/Arithmetic.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module TestCase.Arithmetic (arithmetic) where
22

3+
import Data.Bifunctor (first)
34
import Data.Either.Combinators (swapEither)
45
import Data.Functor.Identity (runIdentity)
56

@@ -8,12 +9,12 @@ import Copager2.IR.Void (Void)
89

910
import Language.Arithmetic (ArithmeticToken, ArithmeticRule)
1011

11-
import Utils (test)
12+
import TestCase.Utils (test, ok', ng')
1213

1314
arithmetic :: Processor ArithmeticToken ArithmeticRule Void -> IO ()
1415
arithmetic processor = do
15-
let ok = runIdentity . runProcessor processor
16-
ng = swapEither . ok
16+
let ok = ok' processor
17+
ng = ng' processor
1718

1819
test $ do
1920
ok "10"
@@ -26,7 +27,6 @@ arithmetic processor = do
2627
ok "10 * 20"
2728
ok "10 / 20"
2829

29-
test $ do
3030
ng "+"
3131
ng "(10"
3232
ng "((((((10)))))"

test/src/TestCase/Json.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module TestCase.Json (json) where
22

3+
import Data.Bifunctor (first)
34
import Data.Either.Combinators (swapEither)
45
import Data.Functor.Identity (runIdentity)
56

@@ -8,19 +9,18 @@ import Copager2.IR.Void (Void)
89

910
import Language.Json (JsonToken, JsonRule)
1011

11-
import Utils (test)
12+
import TestCase.Utils (test, ok', ng')
1213

1314
json :: Processor JsonToken JsonRule Void -> IO ()
1415
json processor = do
15-
let ok = runIdentity . runProcessor processor
16-
ng = swapEither . ok
16+
let ok = ok' processor
17+
ng = ng' processor
1718

1819
-- オブジェクト
1920
test $ do
2021
ok "{ \"key\": 10 }"
2122
ok "{ \"key\": { \"key\": 10 } }"
2223

23-
test $ do
2424
ng "{ \"key\": 10 "
2525
ng "{ key: 10 }"
2626
ng "{ \"key\": 10, }"
@@ -32,7 +32,6 @@ json processor = do
3232
ok "[10, [20, 30]]"
3333
ok "[[[[10]]]]"
3434

35-
test $ do
3635
ng "[10"
3736
ng "[10, 20, ]"
3837
ng "[[[[[10]]]]"

test/src/TestCase/Utils.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module TestCase.Utils (test, ok', ng') where
2+
3+
import Data.Function ((&))
4+
import System.Exit (exitSuccess, exitFailure)
5+
6+
import Data.Bifunctor (first, second)
7+
import Data.Either.Combinators (swapEither)
8+
import Data.Functor.Identity (runIdentity)
9+
10+
import Copager2.Prelude
11+
import Copager2.Utils.Error (Copager2Error)
12+
13+
test :: (Show a) => Either a b -> IO ()
14+
test (Right _) = return ()
15+
test (Left err) = do
16+
putStrLn $ "error: " ++ show err
17+
exitFailure
18+
19+
ok' :: (TokenSet ts, RuleSet ts rs, IR i)
20+
=> Processor ts rs i
21+
-> String
22+
-> Either String ()
23+
ok' processor input =
24+
let f = runIdentity . runProcessor processor
25+
g = first (\a -> "Expected success, but " ++ show a ++ " => " ++ input ++ " ")
26+
h = second (const ())
27+
in h . g . f $ input
28+
29+
ng' :: (TokenSet ts, RuleSet ts rs, IR i)
30+
=> Processor ts rs i
31+
-> String
32+
-> Either String ()
33+
ng' processor input =
34+
let f = swapEither . runIdentity . runProcessor processor
35+
g = first (const $ "Unexpected success => " ++ input ++ " ")
36+
h = second (const ())
37+
in h . g . f $ input

test/src/Utils.hs

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/test.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ test-suite standard
2222
other-modules:
2323
TestCase.Arithmetic,
2424
TestCase.Json,
25-
Utils,
25+
TestCase.Utils,

0 commit comments

Comments
 (0)