File: QNameTest.hs

package info (click to toggle)
haskell-swish 0.9.0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,584 kB
  • ctags: 14
  • sloc: haskell: 20,812; makefile: 42
file content (413 lines) | stat: -rw-r--r-- 13,590 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
{-# LANGUAGE OverloadedStrings #-}

--------------------------------------------------------------------------------
--  See end of this file for licence information.
--------------------------------------------------------------------------------
-- |
--  Module      :  QNameTest
--  Copyright   :  (c) 2003, Graham Klyne, 2009 Vasili I Galchin, 2011, 2013 Douglas Burke
--  License     :  GPL V2
--
--  Maintainer  :  Douglas Burke
--  Stability   :  experimental
--  Portability :  OverloadedStrings
--
--  This module defines test cases for QName data. It also throws in a few
--  tests for the Namespace module.
--
--------------------------------------------------------------------------------

module Main where

import qualified Data.Text as T

import qualified Test.Framework as TF

import Swish.Namespace (makeQNameScopedName, getQName, getScopedNameURI)
import Swish.QName
    ( QName
    , LName
    , newQName
    , qnameFromURI
    , getNamespace
    , getLocalName
    , getQNameURI
    , getLName
    )

import Data.Maybe (fromJust)

import Network.URI (URI, parseURIReference)

import Test.HUnit (Test(TestList))

import TestHelpers (conv, testCompare, testCompareEq)

------------------------------------------------------------
--  Define some common values
------------------------------------------------------------

toURI :: String -> URI
toURI = fromJust . parseURIReference

base1, base2, base3, base4, base5, base6, base7 :: URI
base1  = toURI "http://id.ninebynine.org/wip/2003/test/graph1/node#"
base2  = toURI "http://id.ninebynine.org/wip/2003/test/graph2/node/"
base3  = toURI "http://id.ninebynine.org/wip/2003/test/graph3/node"
base4  = toURI "http://id.ninebynine.org/wip/2003/test/graph3/nodebase"
base5  = toURI "http://id.ninebynine.org/wip/2003/test/graph5/"
base6  = toURI "file://home/swish/"
base7  = toURI "urn:long:separator:path" -- should this really be "urn:"?
  
qb1s1, qb2s2, qb3s3, qb3, qb3bm, qb4m, qb5, qb5s5, qb6, qb7 :: QName
qb1s1  = newQName base1 "s1"
qb2s2  = newQName base2 "s2"
qb3s3  = newQName base3 "s3"
qb3    = newQName base3 ""
qb3bm  = newQName base3 "basemore"
qb4m   = newQName base4 "more"
qb5    = newQName base5 ""
qb5s5  = newQName base5 "s5"
qb6    = newQName base6 "file.dat"
qb7    = newQName base7 ""

qb1st1, qb2st2, qb3st3 :: QName
qb1st1 = newQName base1 "st1"
qb2st2 = newQName base2 "st2"
qb3st3 = newQName base3 "st3"

------------------------------------------------------------
--  QName equality tests
------------------------------------------------------------

testQNameEq :: String -> Bool -> QName -> QName -> Test
testQNameEq = testCompareEq "QNameEq"
-- testQNameEq lbl eq n1 n2 = testIsEq "QNameEq" lbl eq (n1==n2)

qnlist :: [(String, QName)]
qnlist =
  [ ("qb1s1", qb1s1)
  , ("qb2s2", qb2s2)
  , ("qb3s3", qb3s3)
  , ("qb3",   qb3)
  , ("qb3bm", qb3bm)
  , ("qb4m",  qb4m)
  , ("qb5",   qb5)
  , ("qb5s5", qb5s5)
  , ("qb1st1",qb1st1)
  , ("qb2st2",qb2st2)
  , ("qb3st3",qb3st3)
  ]

qneqlist :: [(String, String)]
qneqlist =
  [ ("qb3bm","qb4m")
  ]

testQNameEqSuite :: Test
testQNameEqSuite = TestList
  [ testQNameEq (testLab l1 l2) (testEq  l1 l2) n1 n2
      | (l1,n1) <- qnlist , (l2,n2) <- qnlist ]
    where
    testLab l1 l2 = l1 ++ "-" ++ l2
    testEq  l1 l2 = (l1 == l2)        ||
            (l1,l2) `elem` qneqlist ||
            (l2,l1) `elem` qneqlist

------------------------------------------------------------
--  Alternative constructors
------------------------------------------------------------

nq1, nq2 :: QName
nq1 = newQName base1 "s1"
nq2 = newQName base1 "s2"

toQN :: String -> QName
toQN = fromJust . qnameFromURI . toURI

qu1, qu2, qu3, qu4, qu5, qu6, qu7 :: QName
qu1 = toQN "http://id.ninebynine.org/wip/2003/test/graph1/node#s1"
qu2 = toQN "http://id.ninebynine.org/wip/2003/test/graph2/node/s2"
qu3 = "http://id.ninebynine.org/wip/2003/test/graph3/node"
qu4 = "http://id.ninebynine.org/wip/2003/test/graph5/"
qu5 = "http://id.ninebynine.org/wip/2003/test/graph5/s5"
qu6 = "file://home/swish/file.dat"
qu7 = "urn:long:separator:path"

testMakeQNameSuite :: Test
testMakeQNameSuite = 
  TestList
  [ testQNameEq "testnq01" True  nq1 qb1s1
  , testQNameEq "testnq02" False nq2 qb1s1
  , testQNameEq "testqu01" True qb1s1 qu1
  , testQNameEq "testqu02" True qb2s2 qu2
  , testQNameEq "testqu03" True qb3   qu3
  , testQNameEq "testqu04" True qb5   qu4
  , testQNameEq "testqu05" True qb5s5 qu5
  , testQNameEq "testqu06" True qb6   qu6
  , testQNameEq "testqu07" True qb7   qu7
  ]

------------------------------------------------------------
--  Extract components
------------------------------------------------------------

testStringEq :: String -> String -> String -> Test
testStringEq = testCompare "StringEq"

testTextEq :: String -> T.Text -> T.Text -> Test
testTextEq = testCompare "TextEq"

testURIEq :: String -> String -> URI -> Test
testURIEq lbl uri = testCompare "URIEq" lbl (toURI uri)

testPartQNameSuite :: Test
testPartQNameSuite = 
  TestList
  [ testURIEq "testGetNamespace01"
        "http://id.ninebynine.org/wip/2003/test/graph1/node#" 
        (getNamespace qb1s1)
  , testURIEq "testGetNamespace02"
        "http://id.ninebynine.org/wip/2003/test/graph2/node/"
        (getNamespace qb2s2)
  , testURIEq "testGetNamespace03"
        "http://id.ninebynine.org/wip/2003/test/graph3/node"
        (getNamespace qb3s3)
  , testURIEq "testGetNamespace04"
        "http://id.ninebynine.org/wip/2003/test/graph3/node"
        (getNamespace qb3)
  , testTextEq "testGetLocalName01"
        "s1" (getLName (getLocalName qb1s1))
  , testTextEq "testGetLocalName02"
        "s2" (getLName (getLocalName qb2s2))
  , testTextEq "testGetLocalName03"
      "s3" (getLName (getLocalName qb3s3))
  , testTextEq "testGetLocalName04"
      "" (getLName (getLocalName qb3))
  , testURIEq "testGetQNameURI01"
      "http://id.ninebynine.org/wip/2003/test/graph1/node#s1"
      (getQNameURI qb1s1)
  , testURIEq "testGetQNameURI02"
      "http://id.ninebynine.org/wip/2003/test/graph2/node/s2"
      (getQNameURI qb2s2)
  , testURIEq "testGetQNameURI03"
      "http://id.ninebynine.org/wip/2003/test/graph3/nodes3"
      (getQNameURI qb3s3)
  , testURIEq "testGetQNameURI04"
      "http://id.ninebynine.org/wip/2003/test/graph3/node"
      (getQNameURI qb3)
  ]

------------------------------------------------------------
--  Maybe Qname comparison
------------------------------------------------------------

testMaybeQNameEq :: String -> Bool -> Maybe QName -> Maybe QName -> Test
testMaybeQNameEq = testCompareEq "MaybeQName"

testMaybeQNameEqSuite :: Test
testMaybeQNameEqSuite = 
  TestList
  [ testMaybeQNameEq "testMaybeQNameEq01" True
      (Just qb1s1) (Just qb1s1)
  , testMaybeQNameEq "testMaybeQNameEq02" False
      (Just qb1s1) (Just qb2s2)
  , testMaybeQNameEq "testMaybeQNameEq03" False
      (Just qb1s1) Nothing
  , testMaybeQNameEq "testMaybeQNameEq04" False
      Nothing (Just qb1s1)
  , testMaybeQNameEq "testMaybeQNameEq05" True
      Nothing Nothing
  ]

------------------------------------------------------------
--  QName ordering
------------------------------------------------------------

testQNameLe :: String -> Bool -> QName -> QName -> Test
testQNameLe lbl le n1 n2 = testCompare "QNameLE" lbl le (n1 <= n2)

testQNameLeSuite :: Test
testQNameLeSuite = 
  TestList
  [testQNameLe "testQNameLe01" True  qb3bm qb4m
  , testQNameLe "testQNameLe02" True  qb4m  qb3bm
  , testQNameLe "testQNameLe03" True  qb1s1 qb2s2
  , testQNameLe "testQNameLe04" False qb2s2 qb1s1
  ]
  
------------------------------------------------------------
--  Show QName
------------------------------------------------------------

testShowQNameSuite :: Test
testShowQNameSuite = 
  TestList
  [testStringEq "testShowQName01"
      "<http://id.ninebynine.org/wip/2003/test/graph1/node#s1>"
      (show qb1s1)
  , testStringEq "testShowQName02"
    "<http://id.ninebynine.org/wip/2003/test/graph2/node/s2>"
    (show qb2s2)
  , testStringEq "testShowQName03"
    "<http://id.ninebynine.org/wip/2003/test/graph3/node>"
    (show qb3)
  , testStringEq "testShowQName04"
    "<http://id.ninebynine.org/wip/2003/test/graph5/>"
    (show qb5)
  , testStringEq "testShowQName06"
    "<file://home/swish/file.dat>"
    (show qb6)
  , testStringEq "testShowQName07"
    "<urn:long:separator:path>"
    (show qb7)
  ]

------------------------------------------------------------
--  Split URI string into QName parts
------------------------------------------------------------

-- splitURI :: String -> ( String, String )
    -- splitURI "http://example.org/aaa#bbb" = ("http://example.org/aaa#","bbb")
    -- splitURI "http://example.org/aaa/bbb" = ("http://example.org/aaa/","bbb")
    -- splitURI "http://example.org/aaa/"    = ("http://example.org/aaa/","")

{-
testSplitURI :: String -> String -> ( String, String ) -> Test
testSplitURI label input ans =
    TestCase ( assertEqual label ans ( splitURI input ) )

as splitURI has now been moved into qnameFromURI we change the
test somewhat and also include a check of the
URI combination done by newQName (may be tested elsewhere).
-}

testSplitURI :: String -> String -> (String, LName) -> Test
testSplitURI lbl input (a,b) =
  let qn = newQName (toURI a) b
  in 
   TestList
   [ testCompare lbl ":split" qn ((fromJust . qnameFromURI . toURI) input)
   , testCompare lbl ":show"  input (show (getQNameURI qn))
   ]

testSplitURISuite :: Test
testSplitURISuite = 
  TestList
  [ testSplitURI "testSplitURI01"
      "http://example.org/aaa#bbb"
      ( "http://example.org/aaa#", "bbb" )
  , testSplitURI "testSplitURI02"
     "http://example.org/aaa/bbb"
     ( "http://example.org/aaa/", "bbb" )
  , testSplitURI "testSplitURI03"
     "http://example.org/aaa#"
     ( "http://example.org/aaa#", "" )
  , testSplitURI "testSplitURI04"
     "http://example.org/aaa/"
     ( "http://example.org/aaa/", "" )
     
  {- REMOVE the relative URI tests since it is not clear they make sense
        for QNames.

  , testSplitURI "testSplitURI05"
     "//example.org/aaa#bbb"
     ( "//example.org/aaa#", "bbb" )
  , testSplitURI "testSplitURI06"
     "aaa/bbb"
     ( "aaa/", "bbb" )
  , testSplitURI "testSplitURI07"
     "aaa/bbb/"
     ( "aaa/bbb/", "" )
     
     -- Thanks to Ian Dickinson of the HP Jena team for spotting this one:
     -- So what *is* the correct split here?
  , testSplitURI "testSplitURI08"
      "mortal"
      ( "", "mortal" )
  
    -}
  ]

------------------------------------------------------------
--  Scoped Name tests, via QName and URI
--  In reality this is testing qnameFromURI (or at least
--  that was the original motivation).
------------------------------------------------------------

-- simple round-trip tests
testSQRoundTrip :: String -> String -> Test
testSQRoundTrip lbl uri = 
  let u = (fromJust . parseURIReference) uri
      qn = (fromJust . qnameFromURI) u
      sn = makeQNameScopedName Nothing qn
  in TestList
     [ testCompare "SQ:URI"   lbl u  (getScopedNameURI sn)
     , testCompare "SQ:Qname" lbl qn (getQName sn)
     ]

testSNameTTSuite :: Test
testSNameTTSuite =
  TestList
  [ testSQRoundTrip "null" ""
  , testSQRoundTrip "frag1"  "/" -- Should relative fragments be supported?
  , testSQRoundTrip "frag2a"  "/foo"
  , testSQRoundTrip "frag2b"  "/foo/"
  , testSQRoundTrip "frag3"  "/foo/bar"
  , testSQRoundTrip "frag4a"  "/foo/bar#"
  , testSQRoundTrip "frag4b"  "/foo/bar#fragid"
  , testSQRoundTrip "http1a" "http://example.com"
  , testSQRoundTrip "http1b" "http://example.com/"
  , testSQRoundTrip "http2" "http://example.com/foo/bar/"
  , testSQRoundTrip "http3" "http://example.com/foo/bar/bar"
  , testSQRoundTrip "http4a" "http://example.com/foo/bar/bar#"
  , testSQRoundTrip "http4b" "http://example.com/foo/bar/bar#fragid"
  , testSQRoundTrip "https1" "https://joeuser@example.com/foo/bar"
  , testSQRoundTrip "file1"  "file:///dev/null"
  , testSQRoundTrip "urn1"   "URN:foo:a123,456"
  , testSQRoundTrip "urn2"   "urn:foo:a123%2C456"
  ]

------------------------------------------------------------
--  All tests
------------------------------------------------------------

allTests :: [TF.Test]
allTests = 
  [ conv "QNameEq" testQNameEqSuite
  , conv "MakeQName" testMakeQNameSuite
  , conv "PartQName" testPartQNameSuite
  , conv "MaybeQNameEq" testMaybeQNameEqSuite
  , conv "QNameLe" testQNameLeSuite
  , conv "ShowQName" testShowQNameSuite
  , conv "SplitURI" testSplitURISuite
  , conv "SNameTT" testSNameTTSuite
  ]

main :: IO ()
main = TF.defaultMain allTests

--------------------------------------------------------------------------------
--
--  Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,
--      2011, 2013 Douglas Burke
--  All rights reserved.
--
--  This file is part of Swish.
--
--  Swish 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 2 of the License, or
--  (at your option) any later version.
--
--  Swish 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 Swish; if not, write to:
--    The Free Software Foundation, Inc.,
--    59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
--
--------------------------------------------------------------------------------