Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 5003ae5

Browse files
committed
Use tasty-lua for Lua tests
1 parent f62b8b9 commit 5003ae5

File tree

5 files changed

+88
-39
lines changed

5 files changed

+88
-39
lines changed

hslua-module-text.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ copyright: © 2017–2019 Albert Krewinkel
1111
category: Foreign
1212
build-type: Simple
1313
extra-source-files: ChangeLog.md
14-
test/hstext-test.lua
14+
test/test-text.lua
1515
cabal-version: >=1.10
1616
tested-with: GHC == 7.10.3
1717
, GHC == 8.0.2
@@ -44,4 +44,5 @@ test-suite test-hslua
4444
, hslua-module-text
4545
, tasty
4646
, tasty-hunit
47+
, tasty-lua >= 0.2 && < 0.3
4748
, text

stack.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ packages: ['.']
33
flags: {}
44
extra-deps:
55
- hslua-1.0.3
6+
- tasty-lua-0.2.0

test/hstext-test.lua

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

test/test-hslua-module-text.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ import Foreign.Lua (Lua)
2626
import Foreign.Lua.Module.Text (preloadTextModule, pushModuleText)
2727
import Test.Tasty (TestTree, defaultMain, testGroup)
2828
import Test.Tasty.HUnit (assertEqual, testCase)
29+
import Test.Tasty.Lua (translateResultsFromFile)
2930

3031
import qualified Foreign.Lua as Lua
3132

3233
main :: IO ()
33-
main = defaultMain $ testGroup "hslua-module-text" [tests]
34+
main = do
35+
luaTest <- Lua.run $ do
36+
Lua.openlibs
37+
Lua.requirehs "text" (void pushModuleText)
38+
translateResultsFromFile "test/test-text.lua"
39+
defaultMain $ testGroup "hslua-module-text" [tests, luaTest]
3440

3541
-- | HSpec tests
3642
tests :: TestTree
@@ -50,14 +56,6 @@ tests = testGroup "FromLuaStack"
5056
preloadTextModule "hstext"
5157
assertEqual' "loading the module fails " Lua.OK =<<
5258
Lua.dostring "require 'hstext'"
53-
54-
, testCase "Lua tests pass" . Lua.run $ do
55-
Lua.openlibs
56-
preloadTextModule "hstext"
57-
assertEqual' "error while running lua tests" Lua.OK =<< do
58-
st <- Lua.loadfile "test/hstext-test.lua"
59-
when (st == Lua.OK) $ Lua.call 0 0
60-
return st
6159
]
6260

6361
assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua ()

test/test-text.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--
2+
-- Tests for the text module
3+
--
4+
local text = require 'text'
5+
local tasty = require 'tasty'
6+
7+
local group = tasty.test_group
8+
local test = tasty.test_case
9+
local assert = tasty.assert
10+
11+
return {
12+
group 'len' {
13+
test('ASCII', function ()
14+
tasty.assert.are_equal(text.len 'five!', 5)
15+
end),
16+
test('German sz', function ()
17+
tasty.assert.are_equal(text.len 'Straße', 6)
18+
end),
19+
test('string with small letter e accute', function ()
20+
tasty.assert.are_equal(text.len 'Chartie', 7)
21+
end),
22+
test('Unicode snowman', function ()
23+
tasty.assert.are_equal(text.len '', 1)
24+
end)
25+
},
26+
27+
group 'lower' {
28+
test('uppercase ASCII', function ()
29+
assert.are_equal(text.lower 'YELLING', 'yelling')
30+
end),
31+
test('lowercase ASCII', function ()
32+
assert.are_equal(text.lower 'talking', 'talking')
33+
end),
34+
test('capitalized word with umlaut', function ()
35+
assert.are_equal(text.lower 'Lübeck', 'lübeck')
36+
end),
37+
},
38+
39+
group 'upper' {
40+
test('uppercase ASCII', function ()
41+
assert.are_equal(text.upper 'YELLING', 'YELLING')
42+
end),
43+
test('lowercase ASCII', function ()
44+
assert.are_equal(text.upper 'silence', 'SILENCE')
45+
end),
46+
test('capitalized word with umlaut', function ()
47+
assert.are_equal(text.upper 'Lübeck', 'LÜBECK')
48+
end),
49+
test('German ß becomes double S', function ()
50+
assert.are_equal(text.upper 'Spaß', 'SPASS')
51+
end),
52+
},
53+
54+
group 'reverse' {
55+
test('être becomes ertê', function ()
56+
assert.are_equal(text.reverse 'être', 'ertê')
57+
end)
58+
},
59+
60+
group 'sub' {
61+
test('behaves like string.sub for ASCII text', function ()
62+
local hw = 'Hello, World'
63+
assert.are_equal(text.sub(hw, 6), string.sub(hw, 6))
64+
assert.are_equal(text.sub(hw, -1, -1), string.sub(hw, -1, -1))
65+
assert.are_equal(text.sub(hw, -7, -2), string.sub(hw, -7, -2))
66+
assert.are_equal(text.sub(hw, 7, -2), string.sub(hw, 7, -2))
67+
assert.are_equal(text.sub(hw, 1, 5), string.sub(hw, 1, 5))
68+
assert.are_equal(text.sub(hw, 5, 0), string.sub(hw, 5, 0))
69+
assert.are_equal(text.sub(hw, 0, 2), string.sub(hw, 0, 2))
70+
assert.are_equal(text.sub(hw, -19, 5), string.sub(hw, -19, 5))
71+
end),
72+
test('respects UTF-8', function ()
73+
assert.are_equal(text.sub('Für dich', 5), 'dich')
74+
assert.are_equal(text.sub('☢ radioactive', 0, 1), '')
75+
assert.are_equal(text.sub('☢ radioactive', -11, -1), 'radioactive')
76+
end)
77+
}
78+
}

0 commit comments

Comments
 (0)