@@ -1,5 +1,6 @@
11module Main (main) where
22
3+import Control.Monad
34import Data.ByteString qualified as BS
45import Data.ByteString.Lazy qualified as BSL
56import Data.Char qualified as Char
@@ -18,97 +19,32 @@ import Nock.Jam
1819import Nock.Parser
1920import Nock.Parser qualified
2021import Nock.Printer
22+import System.FilePath.Posix
2123import System.Process.Typed
2224import Test.Hspec
2325import Test.Hspec.QuickCheck
2426import Test.QuickCheck
2527import Test.Tasty
26−import Test.Tasty.Golden (goldenVsFile, goldenVsString)
28+import Test.Tasty.Golden
2729import Test.Tasty.Hspec
2830
2931main :: IO ()
3032main = do
31− spec <- testSpec "nock" specs
32− defaultMain $
33− testGroup
34− "nock"
35− [ -- spec
36− goldenTest "add",
37− goldenTest "dec"
38− -- goldenVsString "add" "golden/add.nock" "golden/add.result.nock" $
39− -- file <-
40− -- TL.writeFile "golden/add.result.nock" $ Nock.Printer.pretty
41− ]
42−
43−goldenTest :: String -> TestTree
44−goldenTest name = do
45− let source = "tests/golden/" <> name <> ".hoon"
46− let target = "tests/golden/" <> name <> ".nock"
47− goldenVsString name target $ do
48− hoon <- BSL.readFile source
49− (_, BSL.dropEnd 6 . BSL.drop 11 -> output) <-
50− readProcessStdout $
51− proc "/run/current-system/sw/bin/urbit" ["eval"]
52− & setStdin (byteStringInput hoon)
53− let result = TL.encodeUtf8 $ Nock.Printer.pretty $ tar $ Nock.Parser.decode $ T.decodeUtf8 $ BS.toStrict ("[0 " <> output <> "]")
54− print result
55− pure result
56−
57−-- TL.writeFile target (Nock.Printer.pretty . tar $ Nock.Parser.decode inp)
58−
59−specs :: Spec
60−specs = describe "nock" $ do
61− tarTest "[77 [2 [1 42] [1 1 153 218]]]" "[153 218]"
62− tarTest "[42 8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1]" "41"
63− tarTest "[4 9 2.398 0 2.047]" "3"
64− tarTest "[2 [1 1 3] 9 36 0 2.047]" "4"
65− tarTest "[[1 2] [7 [9 36 0 2.047] 9 2 0 1]]" "3"
66− tarTest "[50 [4 0 1]]" "51"
67− tarTest "[8 [8 [1 0] [1 6 [5 [1 0] 0 6] [0 0] 8 [1 0] 8 [1 6 [5 [0 30] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1] 0 1] 9 2 [0 4] [7 [0 3] 1 10] 0 11]" "9"
68− tarTest "[[[4 5] [6 14 15]] [0 7]]" "[14 15]"
69− tarTest "[77 [2 [1 42] [1 1 153 218]]]" "[153 218]"
70− tarTest "[[42 43] [8 [1 0] [8 [1 [6 [5 [0 6] [0 14]] [1 0] [6 [5 [0 6] [0 15]] [1 1] [9 2 [[0 2] [4 0 6] [0 7]]]]]] [9 2 0 1]]]]" "0"
71− tarTest "[8 [9 36 0 2.047] 9 2 10 [6 [7 [0 3] 1 1] 7 [0 3] 1 1] 0 2]" "2"
72− tarTest "[[132 19] [4 0 3]]" "20"
73− fasTest "[1 [531 25 99]]" "[531 25 99]"
74− fasTest "[2 [531 25 99]]" "531"
75− fasTest "[3 [531 25 99]]" "[25 99]"
76− fasTest "[6 [531 25 99]]" "25"
77− haxTest "[2 11 [22 33]]" "[11 33]"
78− haxTest "[3 11 [22 33]]" "[22 11]"
79− haxTest "[4 11 [[22 33] 44]]" "[[11 33] 44]"
80− haxTest "[5 11 [[22 33] 44]]" "[[22 11] 44]"
81− tarTest "[7 [9 36 0 2.047] 9 2 0 1]" "[[22 11] 44]"
82− tarTest "[[[4 5] [6 14 15]] [0 7]]" "[14 15]"
83− tarTest "[[1 3][7 [9 36 0 2.047] 9 2 0 1]]" "4"
84− -- tarTest "[9 36 0 2.047]" "0"
85−
86− prop
87− "rub (mat atom) = atom"
88− ( \(NonNegative (a :: Integer)) (NonNegative (b :: Integer)) (NonNegative (c :: Integer)) ->
89− withMaxSuccess 10000 $
90− let atom = fromInteger (a * b * c * 11)
91− in rub (mat atom) === atom
92− )
93− describe "cue (jam noun) = noun" $ do
94− testJam "[77 [2 [1 42] [1 1 153 218]]]"
95−
96−tarTest :: T.Text -> T.Text -> Spec
97−tarTest source target =
98− it (T.unpack ("*" <> source <> " = " <> target)) $
99− tar (decode source) === decode target
100−
101−haxTest :: T.Text -> T.Text -> Spec
102−haxTest source target =
103− it (T.unpack ("#" <> source <> " = " <> target)) $
104− hax (decode source) == decode target
105−
106−fasTest :: T.Text -> T.Text -> Spec
107−fasTest source target =
108− it (T.unpack ("/" <> source <> " = " <> target)) $
109− fas (decode source) == decode target
110−
111−testJam :: T.Text -> Spec
112−testJam source =
113− it ("cue (jam " ++ T.unpack source ++ ") = source") $
114− cue (jam (decode source)) == decode source
33+ spec <- goldenTest
34+ defaultMain $ spec
35+
36+goldenTest :: IO TestTree
37+goldenTest = do
38+ files <- findByExtension [".hoon"] "tests/golden"
39+ fmap (testGroup "nock") $ forM files $ \source -> do
40+ let target = replaceExtension source ".nock"
41+ pure $ goldenVsString (takeBaseName source) target $ do
42+ hoon <- BSL.readFile source
43+ nock <-
44+ fmap (Nock.Parser.noun . T.decodeUtf8 . BS.toStrict . BSL.dropEnd 6 . BSL.drop 11 . snd)
45+ . readProcessStdout
46+ $ proc "urbit" ["eval"]
47+ & setStdin (byteStringInput hoon)
48+ & setStderr nullStream
49+ let result = TL.encodeUtf8 . Nock.Printer.pretty . tar $ cell (atom 0) nock
50+ pure result