-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
162 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,148 @@ | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Main where | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
import CofreeBot.Bot (fixBot) | ||
import CofreeBot.Bot.Behaviors | ||
import CofreeBot.Bot (Behavior, Bot (..), fixBot) | ||
import CofreeBot.Bot.Behaviors.Calculator | ||
( calculatorBot, | ||
helloSimpleBot, | ||
printCalcOutput, | ||
simplifyCalculatorBot, | ||
) | ||
import CofreeBot.Bot.Behaviors.Calculator.Language (statementP) | ||
import CofreeBot.Bot.Behaviors.Hello (helloSimpleBot) | ||
import CofreeBot.Bot.Context (sessionize, simplifySessionBot) | ||
import Scripts (mkScript) | ||
import Test.Hspec (Spec, describe, hspec, it, shouldBe) | ||
import TestServer (runTestScript) | ||
import Data.Text (Text, pack) | ||
import Scripts (Script, mkScript) | ||
import Test.Hspec (Spec, describe, hspec, it, shouldNotBe) | ||
import TestServer (Completion (..), conformsToScript, conformsToScript') | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
main :: IO () | ||
main = hspec $ do | ||
scriptedTestsSpec | ||
helloBotSpec | ||
calculatorBotSpec | ||
sessionizedBotSpec | ||
|
||
scriptedTestsSpec :: Spec | ||
scriptedTestsSpec = describe "Scripted tests" $ do | ||
let myBehavior :: forall m. Monad m => Behavior m Text Text | ||
myBehavior = flip fixBot True $ Bot $ \s _ -> return (pack $ show s, not s) | ||
|
||
it "can deal with bots that respond correctly" $ do | ||
myBehavior | ||
`conformsToScript` [mkScript| | ||
>>>hello | ||
<<<True | ||
>>>whatever | ||
<<<False | ||
|] | ||
|
||
it "can deal with bots that respond incorrectly" $ do | ||
let script :: Script | ||
script = | ||
[mkScript| | ||
>>>hello | ||
<<<True | ||
>>>whatever | ||
<<<True | ||
|] | ||
result <- myBehavior `conformsToScript'` script | ||
result `shouldNotBe` Passed | ||
|
||
helloBotSpec :: Spec | ||
helloBotSpec = | ||
describe "Hello Bot" $ do | ||
let bot = helloSimpleBot | ||
it "responds to precisely its trigger phrase" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>cofree-bot | ||
<<<Are you talking to me, punk? | ||
|] | ||
result <- runTestScript scenario $ fixBot bot () | ||
result `shouldBe` scenario | ||
fixBot bot () | ||
`conformsToScript` [mkScript| | ||
>>>cofree-bot | ||
<<<Are you talking to me, punk? | ||
|] | ||
|
||
it "responds to its trigger phrase embedded in a sentence" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>hows it going cofree-bot | ||
<<<Are you talking to me, punk? | ||
|] | ||
result <- runTestScript scenario $ fixBot bot () | ||
result `shouldBe` scenario | ||
fixBot bot () | ||
`conformsToScript` [mkScript| | ||
>>>hows it going cofree-bot | ||
<<<Are you talking to me, punk? | ||
|] | ||
|
||
calculatorBotSpec :: Spec | ||
calculatorBotSpec = | ||
describe "Calculator Bot" $ do | ||
let bot = simplifyCalculatorBot calculatorBot | ||
it "performs arithmetic" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>(1 + 2) | ||
<<<1 + 2 = 3 | ||
>>>(2 * 3) | ||
<<<2 * 3 = 6 | ||
>>>((2 * 3) + 1) | ||
<<<2 * 3 + 1 = 7 | ||
|] | ||
result <- runTestScript scenario $ fixBot bot mempty | ||
result `shouldBe` scenario | ||
fixBot bot mempty | ||
`conformsToScript` [mkScript| | ||
>>>(1 + 2) | ||
<<<1 + 2 = 3 | ||
>>>(2 * 3) | ||
<<<2 * 3 = 6 | ||
>>>((2 * 3) + 1) | ||
<<<2 * 3 + 1 = 7 | ||
|] | ||
|
||
it "can store values in state" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>x := (1 + 2) | ||
<<<*variable saved* | ||
>>>x | ||
<<<"x" = 3 | ||
|] | ||
result <- runTestScript scenario $ fixBot bot mempty | ||
result `shouldBe` scenario | ||
fixBot bot mempty | ||
`conformsToScript` [mkScript| | ||
>>>x := (1 + 2) | ||
<<<*variable saved* | ||
>>>x | ||
<<<"x" = 3 | ||
|] | ||
|
||
sessionizedBotSpec :: Spec | ||
sessionizedBotSpec = | ||
describe "Sessionized Bot" $ do | ||
let bot = simplifySessionBot printCalcOutput statementP $ sessionize mempty $ calculatorBot | ||
it "can instantiate a session" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
|] | ||
result <- runTestScript scenario $ fixBot bot mempty | ||
result `shouldBe` scenario | ||
fixBot bot mempty | ||
`conformsToScript` [mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
|] | ||
|
||
it "can delete a session" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
>>>end 0 | ||
<<<Session Ended: '0'. | ||
|] | ||
result <- runTestScript scenario $ fixBot bot mempty | ||
result `shouldBe` scenario | ||
fixBot bot mempty | ||
`conformsToScript` [mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
>>>end 0 | ||
<<<Session Ended: '0'. | ||
|] | ||
|
||
it "preserves bot behavior" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
>>>use 0: (1 + 2) | ||
<<<Session '0' Output: | ||
1 + 2 = 3 | ||
|] | ||
result <- runTestScript scenario $ fixBot bot mempty | ||
result `shouldBe` scenario | ||
fixBot bot mempty | ||
`conformsToScript` [mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
>>>use 0: (1 + 2) | ||
<<<Session '0' Output: | ||
1 + 2 = 3 | ||
|] | ||
|
||
it "tracks multiple sessions" $ do | ||
let scenario = | ||
[mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
>>>new | ||
<<<Session Started: '1'. | ||
>>>use 0: x := (1 + 2) | ||
<<<Session '0' Output: | ||
*variable saved* | ||
>>>use 1: x := 42 | ||
<<<Session '1' Output: | ||
*variable saved* | ||
>>>use 0: x | ||
<<<Session '0' Output: | ||
"x" = 3 | ||
>>>use 1: x | ||
<<<Session '1' Output: | ||
"x" = 42 | ||
|] | ||
result <- runTestScript scenario $ fixBot bot mempty | ||
result `shouldBe` scenario | ||
fixBot bot mempty | ||
`conformsToScript` [mkScript| | ||
>>>new | ||
<<<Session Started: '0'. | ||
>>>new | ||
<<<Session Started: '1'. | ||
>>>use 0: x := (1 + 2) | ||
<<<Session '0' Output: | ||
*variable saved* | ||
>>>use 1: x := 42 | ||
<<<Session '1' Output: | ||
*variable saved* | ||
>>>use 0: x | ||
<<<Session '0' Output: | ||
"x" = 3 | ||
>>>use 1: x | ||
<<<Session '1' Output: | ||
"x" = 42 | ||
|] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters