Skip to content

Commit e6f34fc

Browse files
committed
Generate bounded text.
1 parent a59fd16 commit e6f34fc

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cuddle.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.4
22
name: cuddle
3-
version: 0.1.7.0
3+
version: 0.1.8.0
44
synopsis: CDDL Generator and test utilities
55

66
-- description:

src/Codec/CBOR/Cuddle/CBOR/Gen.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import Data.Functor.Identity (Identity (runIdentity))
3737
import Data.List.NonEmpty qualified as NE
3838
import Data.Map.Strict qualified as Map
3939
import Data.Maybe (fromMaybe)
40+
import Data.Text (Text)
41+
import Data.Text qualified as T
4042
import Data.Word (Word64)
4143
import GHC.Generics (Generic)
4244
import System.Random.Stateful
@@ -131,6 +133,9 @@ genRandomM = asksM @"fakeSeed" randomM
131133
genBytes :: forall g. (RandomGen g) => Int -> M g ByteString
132134
genBytes n = asksM @"fakeSeed" $ uniformByteStringM n
133135

136+
genText :: forall g. (RandomGen g) => Int -> M g Text
137+
genText n = pure $ T.pack $ take n ['a' ..]
138+
134139
--------------------------------------------------------------------------------
135140
-- Combinators
136141
--------------------------------------------------------------------------------
@@ -274,6 +279,7 @@ genForCTree (CTree.Control op target controller) = do
274279
ct <- resolveIfRef controller
275280
case (op, ct) of
276281
(CtlOp.Size, CTree.Literal (VUInt n)) -> case tt of
282+
CTree.Postlude PTText -> S . TString <$> genText (fromIntegral n)
277283
CTree.Postlude PTBytes -> S . TBytes <$> genBytes (fromIntegral n)
278284
CTree.Postlude PTUInt -> S . TInteger <$> genUniformRM (0, 2 ^ n - 1)
279285
_ -> error "Cannot apply size operator to target "
@@ -282,6 +288,9 @@ genForCTree (CTree.Control op target controller) = do
282288
t <- resolveIfRef to
283289
case (f, t) of
284290
(CTree.Literal (VUInt f1), CTree.Literal (VUInt t1)) -> case tt of
291+
CTree.Postlude PTText ->
292+
genUniformRM (fromIntegral f1, fromIntegral t1)
293+
>>= (fmap (S . TString) . genText)
285294
CTree.Postlude PTBytes ->
286295
genUniformRM (fromIntegral f1, fromIntegral t1)
287296
>>= (fmap (S . TBytes) . genBytes)

0 commit comments

Comments
 (0)