hslua-module-text 0.3.0.1 → 1.0.0
raw patch · 7 files changed
+206/−221 lines, 7 filesdep +hslua-coredep +hslua-marshallingdep +hslua-packagingdep −bytestringdep −hsluadep ~basedep ~tastydep ~tasty-hunitPVP ok
version bump matches the API change (PVP)
Dependencies added: hslua-core, hslua-marshalling, hslua-packaging
Dependencies removed: bytestring, hslua
Dependency ranges changed: base, tasty, tasty-hunit, tasty-lua, text
API changes (from Hackage documentation)
- Foreign.Lua.Module.Text: description :: Text
- Foreign.Lua.Module.Text: documentedModule :: Module
- Foreign.Lua.Module.Text: functions :: [(Text, HaskellFunction)]
- Foreign.Lua.Module.Text: preloadModule :: String -> Lua ()
- Foreign.Lua.Module.Text: preloadTextModule :: String -> Lua ()
- Foreign.Lua.Module.Text: pushModule :: Lua NumResults
- Foreign.Lua.Module.Text: pushModuleText :: Lua NumResults
+ HsLua.Module.Text: documentedModule :: Module e
+ HsLua.Module.Text: len :: DocumentedFunction e
+ HsLua.Module.Text: lower :: DocumentedFunction e
+ HsLua.Module.Text: reverse :: DocumentedFunction e
+ HsLua.Module.Text: sub :: DocumentedFunction e
+ HsLua.Module.Text: upper :: DocumentedFunction e
Files
- ChangeLog.md +6/−0
- LICENSE +1/−1
- hslua-module-text.cabal +40/−21
- src/Foreign/Lua/Module/Text.hs +0/−157
- src/HsLua/Module/Text.hs +125/−0
- test/test-hslua-module-text.hs +32/−40
- test/test-text.lua +2/−2
ChangeLog.md view
@@ -1,5 +1,11 @@ # Revision history for hslua-module-text +## 1.0.0++Release pending.++- Use hslua 2.0.+ ## 0.3.0.1 -- 2020-10-16 - Relaxed upper bound for hslua, allow `hslua-1.3.*`.
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2017-2020 Albert Krewinkel+Copyright (c) 2017-2021 Albert Krewinkel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
hslua-module-text.cabal view
@@ -1,49 +1,68 @@+cabal-version: 2.2 name: hslua-module-text-version: 0.3.0.1+version: 1.0.0 synopsis: Lua module for text description: UTF-8 aware subset of Lua's `string` module.-homepage: https://github.com/hslua/hslua-module-text+ .+ This package is part of HsLua, a Haskell framework+ built around the embeddable scripting language+ <https://lua.org Lua>.+homepage: https://github.com/hslua/hslua license: MIT license-file: LICENSE author: Albert Krewinkel maintainer: albert+hslua@zeitkraut.de-copyright: © 2017–2020 Albert Krewinkel+copyright: © 2017–2021 Albert Krewinkel category: Foreign-build-type: Simple extra-source-files: ChangeLog.md test/test-text.lua-cabal-version: >=1.10 tested-with: GHC == 8.0.2 , GHC == 8.2.2 , GHC == 8.4.4 , GHC == 8.6.5 , GHC == 8.8.3- , GHC == 8.10.1+ , GHC == 8.10.4+ , GHC == 9.0.1 source-repository head type: git- location: https://github.com/hslua/hslua-module-text.git+ location: https://github.com/hslua/hslua.git+ subdir: hslua-module-text +common common-options+ default-language: Haskell2010+ build-depends: base >= 4.8 && < 5+ , hslua-core >= 2.0 && < 2.1+ , hslua-packaging >= 2.0 && < 2.1+ , text >= 1.0 && < 1.3+ ghc-options: -Wall+ -Wincomplete-record-updates+ -Wnoncanonical-monad-instances+ -Wredundant-constraints+ if impl(ghc >= 8.2)+ ghc-options: -Wcpp-undef+ -Werror=missing-home-modules+ if impl(ghc >= 8.4)+ ghc-options: -Widentities+ -Wincomplete-uni-patterns+ -Wpartial-fields+ -fhide-source-paths+ library- exposed-modules: Foreign.Lua.Module.Text- build-depends: base >= 4.8 && < 5- , bytestring >= 0.10.2 && < 0.11- , hslua >= 1.2 && < 1.4- , text >= 1 && < 1.3+ import: common-options+ exposed-modules: HsLua.Module.Text hs-source-dirs: src- default-language: Haskell2010+ build-depends: hslua-marshalling >= 2.0 && < 2.1 other-extensions: OverloadedStrings -test-suite test-hslua- default-language: Haskell2010+test-suite test-hslua-module-text+ import: common-options type: exitcode-stdio-1.0 main-is: test-hslua-module-text.hs hs-source-dirs: test ghc-options: -Wall -threaded- build-depends: base- , hslua- , hslua-module-text- , tasty- , tasty-hunit- , tasty-lua >= 0.2 && < 0.3+ build-depends: hslua-module-text+ , tasty >= 0.11+ , tasty-hunit >= 0.9+ , tasty-lua >= 1.0 && < 1.1 , text
− src/Foreign/Lua/Module/Text.hs
@@ -1,157 +0,0 @@-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE TypeApplications #-}-{-|-Module : Foreign.Lua.Module.Text-Copyright : © 2017–2020 Albert Krewinkel-License : MIT-Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>-Stability : alpha-Portability : ForeignFunctionInterface--Provide a lua module containing a selection of useful Text functions.--}-module Foreign.Lua.Module.Text-- ( -- * Module- pushModule- , preloadModule- , documentedModule- , description- , functions-- -- * Legacy- , pushModuleText- , preloadTextModule- ) where--import Prelude hiding (reverse)-import Control.Applicative ((<$>))-import Data.ByteString (ByteString)-import Data.Text (Text)-import Data.Maybe (fromMaybe)-import Foreign.Lua (NumResults, Lua, Peekable, Pushable, ToHaskellFunction)-import Foreign.Lua.Call-import Foreign.Lua.Module hiding (preloadModule, pushModule)-import Foreign.Lua.Peek (Peeker, peekIntegral, peekText)-import Foreign.Lua.Push (pushIntegral, pushText)-import qualified Foreign.Lua as Lua-import qualified Data.Text as T--import qualified Foreign.Lua.Module as Module------ Module------- | Textual description of the "text" module.-description :: Text-description =- "UTF-8 aware text manipulation functions, implemented in Haskell."--documentedModule :: Module-documentedModule = Module- { moduleName = "paths"- , moduleFields = []- , moduleDescription = description- , moduleFunctions = functions- }---- | Pushes the @text@ module to the Lua stack.-pushModule :: Lua NumResults-pushModule = 1 <$ Module.pushModule documentedModule--pushModuleText :: Lua NumResults-pushModuleText = 1 <$ Module.pushModule documentedModule---- | Add the @text@ module under the given name to the table of--- preloaded packages.-preloadModule :: String -> Lua ()-preloadModule name = Module.preloadModule $- documentedModule { moduleName = T.pack name }---- | Add the text module under the given name to the table of preloaded--- packages.-preloadTextModule :: String -> Lua ()-preloadTextModule = flip Lua.preloadhs pushModuleText------- Functions-----functions :: [(Text, HaskellFunction)]-functions =- [ ("len", len)- , ("lower", lower)- , ("reverse", reverse)- , ("sub", sub)- , ("upper", upper)- ]---- | Wrapper for @'T.length'@.-len :: HaskellFunction-len = toHsFnPrecursor T.length- <#> textParam "s"- =#> intResult "length"- #? "Determines the number of characters in a string."---- | Wrapper for @'T.toLower'@.-lower :: HaskellFunction-lower = toHsFnPrecursor T.toLower- <#> textParam "s"- =#> textResult "Lowercase copy of `s`"- #? "Convert a string to lower case"---- | Wrapper for @'T.reverse'@.-reverse :: HaskellFunction-reverse = toHsFnPrecursor T.reverse- <#> textParam "s"- =#> textResult "Reversed `s`"- #? "Reverses a string."---- | Returns a substring, using Lua's string indexing rules.-sub :: HaskellFunction-sub = toHsFnPrecursor substring- <#> textParam "s"- <#> textIndex "i" "substring start position"- <#> textOptionalIndex "j" "substring end position"- =#> textResult "text substring"- #? "Returns a substring, using Lua's string indexing rules."- where- substring :: Text -> Int -> Maybe Int -> Text- substring s i jopt =- let j = fromMaybe (-1) jopt- fromStart = if i >= 0 then i - 1 else T.length s + i- fromEnd = if j < 0 then -j - 1 else T.length s - j- in T.dropEnd fromEnd . T.drop fromStart $ s---- | Wrapper for @'T.toUpper'@.-upper :: HaskellFunction-upper = toHsFnPrecursor T.toUpper- <#> textParam "s"- =#> textResult "Lowercase copy of `s`"- #? "Convert a string to lower case"------- Parameters-----textParam :: Text -> Parameter Text-textParam name =- parameter peekText "string" name "UTF-8 encoded string"--textIndex :: Text -> Text -> Parameter Int-textIndex = parameter (peekIntegral @Int) "integer"--textOptionalIndex :: Text -> Text -> Parameter (Maybe Int)-textOptionalIndex = optionalParameter (peekIntegral @Int) "integer"------- Results-----textResult :: Text -- ^ Description- -> FunctionResults Text-textResult = functionResult pushText "string"--intResult :: Text -- ^ Description- -> FunctionResults Int-intResult = functionResult (pushIntegral @Int) "integer"
+ src/HsLua/Module/Text.hs view
@@ -0,0 +1,125 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}+{-|+Module : HsLua.Module.Text+Copyright : © 2017–2021 Albert Krewinkel+License : MIT+Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>+Stability : alpha+Portability : ForeignFunctionInterface++Provides a Lua module containing a selection of useful Text functions.+-}+module HsLua.Module.Text+ ( -- * Module+ documentedModule+ -- ** Functions+ , len+ , lower+ , reverse+ , sub+ , upper+ ) where++import Prelude hiding (reverse)+import Data.Text (Text)+import Data.Maybe (fromMaybe)+import HsLua.Marshalling (peekIntegral, peekText, pushIntegral, pushText)+import HsLua.Packaging+import qualified Data.Text as T++-- | The @text@ module.+documentedModule :: Module e+documentedModule = Module+ { moduleName = "text"+ , moduleOperations = []+ , moduleFields = []+ , moduleFunctions =+ [ len+ , lower+ , reverse+ , sub+ , upper+ ]+ , moduleDescription =+ "UTF-8 aware text manipulation functions, implemented in Haskell."+ }++--+-- Functions+--++-- | Wrapper for @'T.length'@.+len :: DocumentedFunction e+len = defun "len"+ ### liftPure T.length+ <#> textParam "s"+ =#> intResult "length"+ #? "Determines the number of characters in a string."++-- | Wrapper for @'T.toLower'@.+lower :: DocumentedFunction e+lower = defun "lower"+ ### liftPure T.toLower+ <#> textParam "s"+ =#> textResult "Lowercase copy of `s`"+ #? "Converts a string to lower case."++-- | Wrapper for @'T.reverse'@.+reverse :: DocumentedFunction e+reverse = defun "reverse"+ ### liftPure T.reverse+ <#> textParam "s"+ =#> textResult "Reversed `s`"+ #? "Reverses a string."++-- | Returns a substring, using Lua's string indexing rules.+sub :: DocumentedFunction e+sub = defun "sub"+ ### liftPure3 substring+ <#> textParam "s"+ <#> textIndex "i" "substring start position"+ <#> textOptionalIndex "j" "substring end position"+ =#> textResult "text substring"+ #? "Returns a substring, using Lua's string indexing rules."+ where+ substring :: Text -> Int -> Maybe Int -> Text+ substring s i jopt =+ let j = fromMaybe (-1) jopt+ fromStart = if i >= 0 then i - 1 else T.length s + i+ fromEnd = if j < 0 then -j - 1 else T.length s - j+ in T.dropEnd fromEnd . T.drop fromStart $ s++-- | Wrapper for @'T.toUpper'@.+upper :: DocumentedFunction e+upper = defun "upper"+ ### liftPure T.toUpper+ <#> textParam "s"+ =#> textResult "Uppercase copy of `s`"+ #? "Converts a string to upper case."++--+-- Parameters+--++textParam :: Text -> Parameter e Text+textParam name =+ parameter peekText "string" name "UTF-8 encoded string"++textIndex :: Text -> Text -> Parameter e Int+textIndex = parameter (peekIntegral @Int) "integer"++textOptionalIndex :: Text -> Text -> Parameter e (Maybe Int)+textOptionalIndex = optionalParameter (peekIntegral @Int) "integer"++--+-- Results+--++textResult :: Text -- ^ Description+ -> FunctionResults e Text+textResult = functionResult pushText "string"++intResult :: Text -- ^ Description+ -> FunctionResults e Int+intResult = functionResult (pushIntegral @Int) "integer"
test/test-hslua-module-text.hs view
@@ -1,40 +1,32 @@-{--Copyright © 2017–2020 Albert Krewinkel--Permission is hereby granted, free of charge, to any person obtaining a copy-of this software and associated documentation files (the "Software"), to deal-in the Software without restriction, including without limitation the rights-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell-copies of the Software, and to permit persons to whom the Software is-furnished to do so, subject to the following conditions:--The above copyright notice and this permission notice shall be included in-all copies or substantial portions of the Software.+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}+{-|+Module : Main+Copyright : © 2017-2021 Albert Krewinkel+License : MIT+Maintainer : Albert Krewinkel <albert+hslua@zeitkraut.de>+Stability : stable+Portability : Requires language extensions ForeignFunctionInterface,+ OverloadedStrings. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN-THE SOFTWARE.+Tests for the `text` Lua module. -}-{-# LANGUAGE OverloadedStrings #-}+module Main (main) where import Control.Monad (void)-import Foreign.Lua (Lua)-import Foreign.Lua.Module.Text (preloadTextModule, pushModuleText)+import HsLua.Core as Lua+import HsLua.Packaging+import HsLua.Module.Text (documentedModule) import Test.Tasty (TestTree, defaultMain, testGroup) import Test.Tasty.HUnit (assertEqual, testCase) import Test.Tasty.Lua (translateResultsFromFile) -import qualified Foreign.Lua as Lua- main :: IO () main = do- luaTest <- Lua.run $ do- Lua.openlibs- Lua.requirehs "text" (void pushModuleText)+ luaTest <- run @Lua.Exception $ do+ openlibs+ registerModule documentedModule+ pop 1 translateResultsFromFile "test/test-text.lua" defaultMain $ testGroup "hslua-module-text" [tests, luaTest] @@ -42,21 +34,21 @@ tests :: TestTree tests = testGroup "FromLuaStack" [ testCase "text module can be pushed to the stack" $- Lua.run (void pushModuleText)+ run (void (pushModule documentedModule) :: Lua ()) - , testCase "text module can be added to the preloader" . Lua.run $ do- Lua.openlibs- preloadTextModule "hstext"- assertEqual' "function not added to preloader" Lua.TypeFunction =<< do- Lua.getglobal' "package.preload.hstext"- Lua.ltype (-1)+ , testCase "text module can be added to the preloader" . run $ do+ openlibs+ preloadModule documentedModule+ assertEqual' "function not added to preloader" TypeFunction =<< do+ _ <- dostring "return package.preload.text"+ ltype top - , testCase "text module can be loaded as hstext" . Lua.run $ do- Lua.openlibs- preloadTextModule "hstext"- assertEqual' "loading the module fails " Lua.OK =<<- Lua.dostring "require 'hstext'"+ , testCase "text module can be loaded as hstext" . run $ do+ openlibs+ preloadModuleWithName documentedModule "hstext"+ assertEqual' "loading the module fails " OK =<<+ dostring "require 'hstext'" ] assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua ()-assertEqual' msg expected = Lua.liftIO . assertEqual msg expected+assertEqual' msg expected = liftIO . assertEqual msg expected
test/test-text.lua view
@@ -46,13 +46,13 @@ test('capitalized word with umlaut', function () assert.are_equal(text.upper 'Lübeck', 'LÜBECK') end),- test('German ß becomes double S', function ()+ test('German eszett becomes double S', function () assert.are_equal(text.upper 'Spaß', 'SPASS') end), }, group 'reverse' {- test('être becomes ertê', function ()+ test('reverse word with accent circumflex', function () assert.are_equal(text.reverse 'être', 'ertê') end) },