implemented SubFix.encode function

This commit is contained in:
Jonathan Lamothe 2020-11-10 15:42:07 -05:00
parent 0310142814
commit 2d3713d0c9
4 changed files with 122 additions and 3 deletions

View File

@ -35,7 +35,7 @@ import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State (StateT, evalStateT, get, put)
import Data.Char (chr)
import SubFix.Internal (decodeTime)
import SubFix.Internal (decodeTime, encodeTime)
-- | Defines a caption group
data Caption = Caption
@ -64,7 +64,9 @@ decode = evalStateT decodeLoop . lines
-- | Encodes a list of caption groups
encode :: [Caption] -> String
encode = undefined
encode [] = ""
encode [c] = encodeCaption c
encode (c:cs) = encodeCaption c ++ "\n" ++ encode cs
editText :: String -> String
editText = checkCaret . checkNotes
@ -132,4 +134,10 @@ nextLine = get >>= \case
return line
[] -> lift $ Left "missing line"
encodeCaption :: Caption -> String
encodeCaption c = unlines $
[ show $ capID c
, encodeTime (capStart c) ++ " --> " ++ encodeTime (capEnd c)
] ++ lines (capText c)
--jl

View File

@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 0d6580c85ab3588e866980c3fede7319b7d6e699369dc88af2ec75c093c399cf
-- hash: 2e1458e73e6f887df0b932b43aeb47cccc4d9f13cdadafa835caae628e2ae291
name: subfix
version: 0.0.0
@ -59,6 +59,7 @@ test-suite subfix-test
other-modules:
SubFix.ConvertSpec
SubFix.DecodeSpec
SubFix.EncodeSpec
SubFix.Internal.DecodeTimeSpec
SubFix.Internal.EncodeTimeSpec
SubFix.Internal.TimestampSpec

108
test/SubFix/EncodeSpec.hs Normal file
View File

@ -0,0 +1,108 @@
{-
subfix
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
module SubFix.EncodeSpec (spec) where
import Test.Hspec (Spec, context, describe, it, shouldBe)
import SubFix (Caption (..), encode)
import SubFix.Internal (timestamp)
spec :: Spec
spec = describe "encode" $ mapM_
( \(label, input, expected) ->
context label $
it ("should encode " ++ show (length input) ++ " records") $
lines (encode input) `shouldBe` expected
)
-- label, input, expected
[ ( "no records", [], [] )
, ( "one record", oneRecord, oneRecord' )
, ( "two records", twoRecords, twoRecords' )
, ( "three records", threeRecords, threeRecords' )
, ( "split caption", multiLine, multiLine' )
]
where
oneRecord =
[ Caption
{ capID = 1
, capStart = timestamp 1 2 3 4
, capEnd = timestamp 5 6 7 8
, capText = "foo"
}
]
oneRecord' =
[ "1"
, "01:02:03,004 --> 05:06:07,008"
, "foo"
]
twoRecords = oneRecord ++
[ Caption
{ capID = 2
, capStart = timestamp 9 10 11 12
, capEnd = timestamp 13 14 15 16
, capText = "bar"
}
]
twoRecords' = oneRecord' ++
[ ""
, "2"
, "09:10:11,012 --> 13:14:15,016"
, "bar"
]
threeRecords = twoRecords ++
[ Caption
{ capID = 3
, capStart = timestamp 17 18 19 20
, capEnd = timestamp 21 22 23 24
, capText = "baz"
}
]
threeRecords' = twoRecords' ++
[ ""
, "3"
, "17:18:19,020 --> 21:22:23,024"
, "baz"
]
multiLine =
[ Caption
{ capID = 1
, capStart = timestamp 1 2 3 4
, capEnd = timestamp 5 6 7 8
, capText = "foo\nbar"
}
]
multiLine' =
[ "1"
, "01:02:03,004 --> 05:06:07,008"
, "foo"
, "bar"
]
--jl

View File

@ -25,11 +25,13 @@ import Test.Hspec (Spec, describe)
import qualified SubFix.InternalSpec as Internal
import qualified SubFix.DecodeSpec as Decode
import qualified SubFix.ConvertSpec as Convert
import qualified SubFix.EncodeSpec as Encode
spec :: Spec
spec = describe "SubFix" $ do
Internal.spec
Decode.spec
Convert.spec
Encode.spec
--jl