File: OsPathSpec.hs

package info (click to toggle)
ghc 9.6.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 158,216 kB
  • sloc: haskell: 648,228; ansic: 81,656; cpp: 11,808; javascript: 8,444; sh: 5,831; fortran: 3,527; python: 3,277; asm: 2,523; makefile: 2,298; yacc: 1,570; lisp: 532; xml: 196; perl: 145; csh: 2
file content (255 lines) | stat: -rw-r--r-- 10,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE QuasiQuotes #-}

module OsPathSpec where

import Data.Maybe

import System.OsPath as OSP
import System.OsString.Internal.Types
import System.OsPath.Posix as Posix
import System.OsPath.Windows as Windows
import System.OsPath.Encoding
import qualified System.OsString.Internal.Types as OS
import System.OsPath.Data.ByteString.Short ( toShort )
import System.OsString.Posix as PosixS
import System.OsString.Windows as WindowsS

import Control.Exception
import Data.ByteString ( ByteString )
import qualified Data.ByteString as BS
import Test.QuickCheck
import qualified Test.QuickCheck.Classes.Base as QC
import GHC.IO.Encoding.UTF8 ( mkUTF8 )
import GHC.IO.Encoding.UTF16 ( mkUTF16le )
import GHC.IO.Encoding ( setFileSystemEncoding )
import GHC.IO.Encoding.Failure ( CodingFailureMode(..) )
import Control.DeepSeq
import Data.Bifunctor ( first )
import qualified Data.ByteString.Char8 as C
import qualified System.OsPath.Data.ByteString.Short.Word16 as BS16
import qualified System.OsPath.Data.ByteString.Short as SBS
import Data.Char ( ord )
import Data.Proxy ( Proxy(..) )

import Arbitrary


fromRight :: b -> Either a b -> b
fromRight _ (Right b) = b
fromRight b _         = b


tests :: [(String, Property)]
tests =
  [ ("OSP.encodeUtf . OSP.decodeUtf == id",
    property $ \(NonNullString str) -> (OSP.decodeUtf . fromJust . OSP.encodeUtf) str == Just str)

  , ("decodeUtf . encodeUtf == id (Posix)",
    property $ \(NonNullString str) -> (Posix.decodeUtf . fromJust . Posix.encodeUtf) str == Just str)
  , ("decodeUtf . encodeUtf == id (Windows)",
    property $ \(NonNullString str) -> (Windows.decodeUtf . fromJust . Windows.encodeUtf) str == Just str)

  , ("encodeWith ucs2le . decodeWith ucs2le == id (Posix)",
    property $ \(padEven -> bs) -> (Posix.encodeWith ucs2le . (\(Right r) -> r) . Posix.decodeWith ucs2le . OS.PS . toShort) bs === Right (OS.PS . toShort $ bs))
  , ("encodeWith ucs2le . decodeWith ucs2le == id (Windows)",
    property $ \(padEven -> bs) -> (Windows.encodeWith ucs2le . (\(Right r) -> r) . Windows.decodeWith ucs2le . OS.WS . toShort) bs
           === Right (OS.WS . toShort $ bs))

  , ("decodeFS . encodeFS == id (Posix)",
    property $ \(NonNullString str) -> ioProperty $ do
      setFileSystemEncoding (mkUTF8 TransliterateCodingFailure)
      r1 <- Posix.encodeFS str
      r2 <- try @SomeException $ Posix.decodeFS r1
      r3 <- evaluate $ force $ first displayException r2
      pure (r3 === Right str)
      )
  , ("decodeFS . encodeFS == id (Windows)",
    property $ \(NonNullString str) -> ioProperty $ do
      r1 <- Windows.encodeFS str
      r2 <- try @SomeException $ Windows.decodeFS r1
      r3 <- evaluate $ force $ first displayException r2
      pure (r3 === Right str)
      )

  , ("fromPlatformString* functions are equivalent under ASCII (Windows)",
    property $ \(WindowsString . BS16.pack . map (fromIntegral . ord) . nonNullAsciiString -> str) -> ioProperty $ do
      r1         <- Windows.decodeFS str
      r2         <- Windows.decodeUtf str
      (Right r3) <- pure $ Windows.decodeWith (mkUTF16le TransliterateCodingFailure) str
      (Right r4) <- pure $ Windows.decodeWith (mkUTF16le RoundtripFailure) str
      (Right r5) <- pure $ Windows.decodeWith (mkUTF16le ErrorOnCodingFailure) str
      pure (    r1 === r2
           .&&. r1 === r3
           .&&. r1 === r4
           .&&. r1 === r5
           )
    )

  , ("fromPlatformString* functions are equivalent under ASCII (Posix)",
    property $ \(PosixString . SBS.toShort . C.pack . nonNullAsciiString -> str) -> ioProperty $ do
      r1         <-        Posix.decodeFS str
      r2         <-        Posix.decodeUtf str
      (Right r3) <- pure $ Posix.decodeWith (mkUTF8 TransliterateCodingFailure) str
      (Right r4) <- pure $ Posix.decodeWith (mkUTF8 RoundtripFailure) str
      (Right r5) <- pure $ Posix.decodeWith (mkUTF8 ErrorOnCodingFailure) str
      pure (    r1 === r2
           .&&. r1 === r3
           .&&. r1 === r4
           .&&. r1 === r5
           )
    )

  , ("toPlatformString* functions are equivalent under ASCII (Windows)",
    property $ \(NonNullAsciiString str) -> ioProperty $ do
      r1         <- Windows.encodeFS str
      r2         <- Windows.encodeUtf str
      (Right r3) <- pure $ Windows.encodeWith (mkUTF16le TransliterateCodingFailure) str
      (Right r4) <- pure $ Windows.encodeWith (mkUTF16le RoundtripFailure) str
      (Right r5) <- pure $ Windows.encodeWith (mkUTF16le ErrorOnCodingFailure) str
      pure (    r1 === r2
           .&&. r1 === r3
           .&&. r1 === r4
           .&&. r1 === r5
           )
    )

  , ("toPlatformString* functions are equivalent under ASCII (Posix)",
    property $ \(NonNullAsciiString str) -> ioProperty $ do
      r1         <-        Posix.encodeFS str
      r2         <-        Posix.encodeUtf str
      (Right r3) <- pure $ Posix.encodeWith (mkUTF8 TransliterateCodingFailure) str
      (Right r4) <- pure $ Posix.encodeWith (mkUTF8 RoundtripFailure) str
      (Right r5) <- pure $ Posix.encodeWith (mkUTF8 ErrorOnCodingFailure) str
      pure (    r1 === r2
           .&&. r1 === r3
           .&&. r1 === r4
           .&&. r1 === r5
           )
    )
  , ("Unit test toPlatformString* (Posix)",
    property $ ioProperty $ do
      let str = "ABcK_(ツ123_&**"
      let expected = PosixString $ SBS.pack [0x41,0x42,0x63,0x4b,0x5f,0x28,0xe3,0x83,0x84,0x31,0x32,0x33,0x5f,0x26,0x2a,0x2a]
      r1         <-        Posix.encodeFS str
      r2         <-        Posix.encodeUtf str
      (Right r3) <- pure $ Posix.encodeWith (mkUTF8 TransliterateCodingFailure) str
      (Right r4) <- pure $ Posix.encodeWith (mkUTF8 RoundtripFailure) str
      (Right r5) <- pure $ Posix.encodeWith (mkUTF8 ErrorOnCodingFailure) str
      pure (    r1 === expected
           .&&. r2 === expected
           .&&. r3 === expected
           .&&. r4 === expected
           .&&. r5 === expected
           )
    )
  , ("Unit test toPlatformString* (WindowsString)",
    property $ ioProperty $ do
      let str = "ABcK_(ツ123_&**"
      let expected = WindowsString $ BS16.pack [0x0041,0x0042,0x0063,0x004b,0x005f,0x0028,0x30c4,0x0031,0x0032,0x0033,0x005f,0x0026,0x002a,0x002a]
      r1         <-        Windows.encodeFS str
      r2         <-        Windows.encodeUtf str
      (Right r3) <- pure $ Windows.encodeWith (mkUTF16le TransliterateCodingFailure) str
      (Right r4) <- pure $ Windows.encodeWith (mkUTF16le RoundtripFailure) str
      (Right r5) <- pure $ Windows.encodeWith (mkUTF16le ErrorOnCodingFailure) str
      pure (    r1 === expected
           .&&. r2 === expected
           .&&. r3 === expected
           .&&. r4 === expected
           .&&. r5 === expected
           )
    )

  , ("Unit test fromPlatformString* (Posix)",
    property $ ioProperty $ do
      let bs = PosixString $ SBS.pack [0x41,0x42,0x63,0x4b,0x5f,0x28,0xe3,0x83,0x84,0x31,0x32,0x33,0x5f,0x26,0x2a,0x2a]
      let expected = "ABcK_(ツ123_&**"
      r1         <-        Posix.decodeFS bs
      r2         <-        Posix.decodeUtf bs
      (Right r3) <- pure $ Posix.decodeWith (mkUTF8 TransliterateCodingFailure) bs
      (Right r4) <- pure $ Posix.decodeWith (mkUTF8 RoundtripFailure) bs
      (Right r5) <- pure $ Posix.decodeWith (mkUTF8 ErrorOnCodingFailure) bs
      pure (    r1 === expected
           .&&. r2 === expected
           .&&. r3 === expected
           .&&. r4 === expected
           .&&. r5 === expected
           )
    )
  , ("Unit test fromPlatformString* (WindowsString)",
    property $ ioProperty $ do
      let bs = WindowsString $ BS16.pack [0x0041,0x0042,0x0063,0x004b,0x005f,0x0028,0x30c4,0x0031,0x0032,0x0033,0x005f,0x0026,0x002a,0x002a]
      let expected = "ABcK_(ツ123_&**"
      r1         <-        Windows.decodeFS bs
      r2         <-        Windows.decodeUtf bs
      (Right r3) <- pure $ Windows.decodeWith (mkUTF16le TransliterateCodingFailure) bs
      (Right r4) <- pure $ Windows.decodeWith (mkUTF16le RoundtripFailure) bs
      (Right r5) <- pure $ Windows.decodeWith (mkUTF16le ErrorOnCodingFailure) bs
      pure (    r1 === expected
           .&&. r2 === expected
           .&&. r3 === expected
           .&&. r4 === expected
           .&&. r5 === expected
           )
    )
  , ("QuasiQuoter (WindowsString)",
    property $ do
      let bs = WindowsString $ BS16.pack [0x0041,0x0042,0x0063,0x004b,0x005f,0x0028,0x30c4,0x0031,0x0032,0x0033,0x005f,0x0026,0x002a,0x002a]
      let expected = [WindowsS.pstr|ABcK_(ツ123_&**|]
      bs === expected
    )
  , ("QuasiQuoter (PosixString)",
    property $ do
      let bs = PosixString $ SBS.pack [0x41,0x42,0x63,0x4b,0x5f,0x28,0xe3,0x83,0x84,0x31,0x32,0x33,0x5f,0x26,0x2a,0x2a]
      let expected = [PosixS.pstr|ABcK_(ツ123_&**|]
      bs === expected
    )
  , ("QuasiQuoter (WindowsPath)",
    property $ do
      let bs = WindowsString $ BS16.pack [0x0041,0x0042,0x0063,0x004b,0x005f]
      let expected = [Windows.pstr|ABcK_|]
      bs === expected
    )
  , ("QuasiQuoter (PosixPath)",
    property $ do
      let bs = PosixString $ SBS.pack [0x41,0x42,0x63,0x4b,0x5f]
      let expected = [Posix.pstr|ABcK_|]
      bs === expected
    )

  , ("pack . unpack == id (Windows)",
    property $ \ws@(WindowsString _) ->
      Windows.pack (Windows.unpack ws) === ws
    )
  , ("pack . unpack == id (Posix)",
    property $ \ws@(PosixString _) ->
      Posix.pack (Posix.unpack ws) === ws
    )
  , ("pack . unpack == id (OsPath)",
    property $ \ws@(OsString _) ->
      OSP.pack (OSP.unpack ws) === ws
    )


  ] ++ QC.lawsProperties (QC.ordLaws (Proxy @OsPath))
    ++ QC.lawsProperties (QC.monoidLaws (Proxy @OsPath))

    ++ QC.lawsProperties (QC.ordLaws (Proxy @OsString))
    ++ QC.lawsProperties (QC.monoidLaws (Proxy @OsString))

    ++ QC.lawsProperties (QC.ordLaws (Proxy @WindowsString))
    ++ QC.lawsProperties (QC.monoidLaws (Proxy @WindowsString))

    ++ QC.lawsProperties (QC.ordLaws (Proxy @PosixString))
    ++ QC.lawsProperties (QC.monoidLaws (Proxy @PosixString))

    ++ QC.lawsProperties (QC.ordLaws (Proxy @PlatformString))
    ++ QC.lawsProperties (QC.monoidLaws (Proxy @PlatformString))


padEven :: ByteString -> ByteString
padEven bs
  | even (BS.length bs) = bs
  | otherwise = bs `BS.append` BS.pack [70]