Skip to content

Commit e64d3b5

Browse files
oughta: Move test harness Lua prelude to its own file, add comments
1 parent 2561c09 commit e64d3b5

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

oughta/oughta.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ test-suite oughta-tests
154154
build-depends:
155155
bytestring,
156156
directory,
157+
file-embed,
157158
filepath,
158159
tasty,
159160
tasty-hunit,

oughta/test/Test.hs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE TemplateHaskell #-}
23

34
{-
45
Test Oughta, using Oughta.
@@ -14,7 +15,9 @@ is readable and correct (which is non-trivial, especially source span tracking).
1415
module Main (main) where
1516

1617
import Control.Monad qualified as Monad
18+
import Data.ByteString (ByteString)
1719
import Data.ByteString qualified as BS
20+
import Data.FileEmbed (embedFile)
1821
import Data.Function ((&))
1922
import Data.Text qualified as Text
2023
import Data.Text.Encoding qualified as Text
@@ -27,6 +30,9 @@ import System.FilePath qualified as FilePath
2730
import Test.Tasty.HUnit qualified as TTH
2831
import Test.Tasty qualified as TT
2932

33+
prelude :: ByteString
34+
prelude = $(embedFile "test/test.lua")
35+
3036
test :: FilePath -> IO ()
3137
test file = do
3238
content <- Text.IO.readFile file
@@ -43,27 +49,13 @@ test file = do
4349
Text.encodeUtf8 &
4450
Ota.Output
4551

46-
let prelude =
47-
Text.unlines
48-
[ "name = 'Oughta'"
49-
, "function match_on_line(i)"
50-
, " check(string.format('✔️ match at <out>:%d', i))"
51-
, "end"
52-
, "function match_prev()"
53-
, " match_on_line(src_line(1) - 1)"
54-
, "end"
55-
, "function match_from_line(i)"
56-
, " checkln 'stack trace:'"
57-
, " here(string.format(' %s:%d', file(), i))"
58-
, "end"
59-
]
6052
let prog0 = Ota.fromLineComments file comment content
61-
let prog = Ota.addPrefix prelude prog0
53+
let prog = Ota.addPrefix (Text.decodeUtf8Lenient prelude) prog0
6254
result <- Ota.check prog (clearComments content)
6355
TTH.assertBool file (not (Ota.resultNull result))
6456

6557
let prog0' = Ota.fromLineComments file comment' content
66-
let prog' = Ota.addPrefix prelude prog0'
58+
let prog' = Ota.addPrefix (Text.decodeUtf8Lenient prelude) prog0'
6759
let output'@(Ota.Output out) = clearComments (Ota.printResult result)
6860
BS.writeFile (FilePath.replaceExtension file "out") out
6961
Ota.check' prog' output'

oughta/test/test.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name = 'Oughta'
2+
3+
-- Assert that there was a match on line `i`.
4+
function match_on_line(i)
5+
check(string.format('✔️ match at <out>:%d', i))
6+
end
7+
8+
-- Assert that there was a match on the line just before the line of code that
9+
-- calls this function.
10+
function match_prev()
11+
match_on_line(src_line(1) - 1)
12+
end
13+
14+
-- Assert that there was a match from a function call on line `i`.
15+
function match_from_line(i)
16+
checkln 'stack trace:'
17+
here(string.format(' %s:%d', file(), i))
18+
end

0 commit comments

Comments
 (0)