File: Testing.hs

package info (click to toggle)
haskell-graphviz 2999.17.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,488 kB
  • sloc: haskell: 12,152; makefile: 2
file content (429 lines) | stat: -rw-r--r-- 14,942 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
{-# LANGUAGE Rank2Types, FlexibleContexts #-}

{- |
   Module      : Data.GraphViz.Testing
   Description : Test-suite for graphviz.
   Copyright   : (c) Ivan Lazar Miljenovic
   License     : 3-Clause BSD-style
   Maintainer  : Ivan.Miljenovic@gmail.com

   This defines a test-suite for the graphviz library.

   Limitations of the test suite are as follows:

   * For the most part, this library lets you use arbitrary numbers
     for String values.  However, this is not tested due to too many
     corner cases for special parsers that don't take arbitrary
     Strings.  As the Dot standard is ambiguous over whether you can
     or can't use numbers as Strings (more specifically, if they
     should be quoted or not), this is a user beware situation.

   * Same goes for empty Strings; sometimes they're allowed, sometimes
     they're not.  Thus, to simplify matters they're not generated.

   * The generated Strings are very simple, only composed of lower
     case letters, digits and some symbols.  This is because too many
     tests were \"failing\" due to some corner case; e.g. lower-case
     letters only because the parser parses Strings as lowercase, so
     if a particular String isn't valid (e.g. @\"all\"@ for 'LayerID',
     then the 'Arbitrary' instance has to ensure that all possible
     ways of capitalising that String isn't generated as a random
     'LRName'.

   * The generated 'DotRepr's are not guaranteed to be valid.

   * To avoid needless endless recursion, sub-graphs do not have their
     own internal sub-graphs.

   * This test suite isn't perfect: if you deliberately try to stuff
     something up, you probably can.
-}
module Data.GraphViz.Testing
       ( -- * Running the test suite.
         runChosenTests
       , runTests
       , runTest
         -- ** The tests themselves
       , Test(..)
       , defaultTests
       , test_printParseID_Attributes
       , test_generalisedSameDot
       , test_printParseID
       , test_preProcessingID
       , test_dotizeAugment
       , test_dotizeAugmentUniq
       , test_canonicalise
       , test_canonicaliseNodes
       , test_canonicaliseEdges
       , test_transitive
       , test_transitiveNodes
        -- * Re-exporting modules for manual testing.
       , module Data.GraphViz
       , module Data.GraphViz.Testing.Properties
         -- * Debugging printing
       , PrintDot(..)
       , printIt
       , renderDot
         -- * Debugging parsing
       , ParseDot(..)
       , parseIt
       , parseIt'
       , runParser
       , preProcess
       ) where

import Test.QuickCheck

import Data.GraphViz.Testing.Instances()
import Data.GraphViz.Testing.Properties

import Data.GraphViz
import Data.GraphViz.Algorithms(CanonicaliseOptions)
import Data.GraphViz.Parsing(parseIt, parseIt', runParser)
import Data.GraphViz.PreProcessing(preProcess)
import Data.GraphViz.Printing(printIt, renderDot)
import qualified Data.GraphViz.Types.Generalised as G
import qualified Data.GraphViz.Types.Graph as Gr
-- Can't use PatriciaTree because a Show instance is needed.
import Data.Graph.Inductive.Tree(Gr)

import System.Exit(ExitCode(..), exitWith)
import System.IO(hPutStrLn, stderr)

-- -----------------------------------------------------------------------------

runChosenTests       :: [Test] -> IO ()
runChosenTests tsts = do putStrLn msg
                         blankLn
                         runTests tsts
                         spacerLn
                         putStrLn successMsg
  where
    msg = "This is the test suite for the graphviz library.\n\
           \If any of these tests fail, please inform the maintainer,\n\
           \including full output of this test suite."

    successMsg = "All tests were successful!"


-- -----------------------------------------------------------------------------
-- Defining a Test structure and how to run tests.

-- | Defines the test structure being used.
data Test = Test { name       :: String
                 , lookupName :: String      -- ^ Should be lowercase
                 , desc       :: String
                 , tests      :: [IO Result] -- ^ QuickCheck test.
                 }

-- | Run all of the provided tests.
runTests :: [Test] -> IO ()
runTests = mapM_ ((>>) spacerLn . runTest)

-- | Run the provided test.
runTest     :: Test -> IO ()
runTest tst = do putStrLn title
                 blankLn
                 putStrLn $ desc tst
                 blankLn
                 run $ tests tst
                 blankLn
  where
    nm = '"' : name tst ++ "\""
    title = "Running test: " ++ nm ++ "."
    successMsg = "All tests for " ++ nm ++ " were successful!"
    gaveUpMsg = "Too many sample inputs for " ++ nm ++ " were rejected;\n\
                 \tentatively marking this as successful."
    failMsg = "The tests for " ++ nm ++ " failed!\n\
               \Not attempting any further tests."

    run [] = putStrLn successMsg
    run (t:ts) = do r <- t
                    case r of
                       Success{} -> run ts
                       GaveUp{}  -> putStrLn gaveUpMsg >> run ts
                       _         -> die failMsg

spacerLn :: IO ()
spacerLn = putStrLn (replicate 70 '=') >> blankLn

blankLn :: IO ()
blankLn = putStrLn ""

die     :: String -> IO a
die msg = do hPutStrLn stderr msg
             exitWith (ExitFailure 1)

qCheck :: (Testable prop) => prop -> IO Result
qCheck = quickCheckWithResult (stdArgs { maxSize = 50, maxSuccess = 200 })

-- -----------------------------------------------------------------------------
-- Defining the tests to use.

-- | The tests to run by default.
defaultTests :: [Test]
defaultTests = [ test_printParseID_Attributes
               , test_generalisedSameDot
               , test_printParseID
               , test_preProcessingID
               , test_dotizeAugment
               , test_dotizeHasAugment
               , test_dotizeAugmentUniq
               , test_findAllNodes
               , test_findAllNodesE
               , test_findAllEdges
               , test_noGraphInfo
               , test_canonicalise
               , test_canonicaliseNodes
               , test_canonicaliseEdges
               , test_transitive
               , test_transitiveNodes
               ]

-- | Test that 'Attributes' can be printed and then parsed back.
test_printParseID_Attributes :: Test
test_printParseID_Attributes
  = Test { name       = "Printing and parsing of Attributes"
         , lookupName = "attributes"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: Attributes -> Property
    prop = prop_printParseListID

    dsc = "The most common source of errors in printing and parsing are for\n\
          \Attributes."

test_generalisedSameDot :: Test
test_generalisedSameDot
  = Test { name       = "Printing generalised Dot code"
         , lookupName = "makegeneralised"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
    where
      prop :: DotGraph Int -> Bool
      prop = prop_generalisedSameDot

      dsc = "When generalising \"DotGraph\" values to other \"DotRepr\" values,\n\
             \the generated Dot code should be identical."

test_printParseID :: Test
test_printParseID
  = Test { name       = "Printing and Parsing DotReprs"
         , lookupName = "printparseid"
         , desc       = dsc
         , tests      = tsts
         }
  where
    tsts :: [IO Result]
    tsts = [ qCheck (prop_printParseID :: DotGraph    Int -> Bool)
           , qCheck (prop_printParseID :: G.DotGraph  Int -> Bool)
           , qCheck (prop_printParseID :: Gr.DotGraph Int -> Bool)
           ]

    dsc = "The graphviz library should be able to parse back in its own\n\
           \generated Dot code for any \"DotRepr\" instance"

test_preProcessingID :: Test
test_preProcessingID
  = Test { name       = "Pre-processing Dot code"
         , lookupName = "preprocessing"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: DotGraph Int -> Bool
    prop = prop_preProcessingID

    dsc = "When parsing Dot code, some pre-processing is done to remove items\n\
           \such as comments and to join together multi-line strings.  This\n\
           \test verifies that this pre-processing doesn't affect actual\n\
           \Dot code by running the pre-processor on generated Dot code.\n\n\
           \This test is not run on generalised Dot graphs as if it works for\n\
           \normal dot graphs then it should also work for generalised ones."

test_dotizeAugment :: Test
test_dotizeAugment
  = Test { name       = "Augmenting FGL Graphs"
         , lookupName = "augment"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: Gr Char Double -> Bool
    prop = prop_dotizeAugment

    dsc = "The various Graph to Graph functions in Data.GraphViz should\n\
           \only _augment_ the graph labels and not change the graphs\n\
           \themselves.  This test compares the original graphs to these\n\
           \augmented graphs and verifies that they are the same."

test_dotizeHasAugment :: Test
test_dotizeHasAugment
  = Test { name       = "Ensuring augmentation of FGL Graphs"
         , lookupName = "hasaugment"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: Gr Char Double -> Bool
    prop = prop_dotizeHasAugment

    dsc = "The various Graph to Graph functions in Data.GraphViz should\n\
           \actually agument the graph labels; this ensures that all labels\n\
           \actually have attached Attributes after augmentation."

test_dotizeAugmentUniq :: Test
test_dotizeAugmentUniq
  = Test { name       = "Unique edges in augmented FGL Graphs"
         , lookupName = "augmentuniq"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: Gr Char Double -> Bool
    prop = prop_dotizeAugmentUniq

    dsc = "When augmenting a graph with multiple edges, as long as no\n\
           \Attributes are provided that override the default settings,\n\
           \then each edge between two nodes should have a unique position\n\
           \Attribute, etc."

test_findAllNodes :: Test
test_findAllNodes
  = Test { name       = "Ensure all nodes are found in a DotRepr"
         , lookupName = "findnodes"
         , desc       = dsc
         , tests      = map qCheck props
         }
  where
    props :: [Gr () () -> Bool]
    props = testAllGraphTypes prop_findAllNodes

    dsc = "nodeInformation should find all nodes in a DotRepr;\n\
           \this is tested by converting an FGL graph and comparing\n\
           \the nodes it should have to those that are found."

test_findAllNodesE :: Test
test_findAllNodesE
  = Test { name       = "Ensure all nodes are found in a node-less DotRepr"
         , lookupName = "findedgelessnodes"
         , desc       = dsc
         , tests      = map qCheck props
         }
  where
    props :: [Gr () () -> Bool]
    props = testAllGraphTypes prop_findAllNodesE

    dsc = "nodeInformation should find all nodes in a DotRepr,\n\
           \even if there are no explicit nodes in that graph.\n\
           \This is tested by converting an FGL graph and comparing\n\
           \the nodes it should have to those that are found."

test_findAllEdges :: Test
test_findAllEdges
  = Test { name       = "Ensure all edges are found in a DotRepr"
         , lookupName = "findedges"
         , desc       = dsc
         , tests      = map qCheck props
         }
  where
    props :: [Gr () () -> Bool]
    props = testAllGraphTypes prop_findAllEdges

    dsc = "nodeInformation should find all edges in a DotRepr;\n\
           \this is tested by converting an FGL graph and comparing\n\
           \the edges it should have to those that are found."

test_noGraphInfo :: Test
test_noGraphInfo
  = Test { name       = "Plain DotReprs should have no structural information"
         , lookupName = "nographinfo"
         , desc       = dsc
         , tests      = map qCheck props
         }
  where
    props :: [Gr () () -> Bool]
    props = testAllGraphTypes prop_noGraphInfo

    dsc = "When converting a Graph to a DotRepr, there should be no\n\
           \clusters or global attributes."

test_canonicalise :: Test
test_canonicalise
  = Test { name       = "Canonicalisation should be idempotent"
         , lookupName = "canonicalise"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: CanonicaliseOptions -> DotGraph Int -> Bool
    prop = prop_canonicalise

    dsc = "Repeated application of canonicalise shouldn't have any further affect."

test_canonicaliseNodes :: Test
test_canonicaliseNodes
  = Test { name       = "Canonicalisation shouldn't change any nodes"
         , lookupName = "canonicalisenodes"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: CanonicaliseOptions -> DotGraph Int -> Bool
    prop = prop_canonicaliseNodes

    dsc = "Canonicalisation shouldn't change or remove any nodes."

test_canonicaliseEdges :: Test
test_canonicaliseEdges
  = Test { name       = "Canonicalisation shouldn't change any edges"
         , lookupName = "canonicaliseedges"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: CanonicaliseOptions -> DotGraph Int -> Bool
    prop = prop_canonicaliseEdges

    dsc = "Canonicalisation shouldn't change or remove any edges."

test_transitive :: Test
test_transitive
  = Test { name       = "Transitive reduction should be idempotent"
         , lookupName = "transitive"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: CanonicaliseOptions -> DotGraph Int -> Bool
    prop = prop_transitive

    dsc = "Repeated application of transitiveReduction shouldn't have any further affect."

test_transitiveNodes :: Test
test_transitiveNodes
  = Test { name       = "Transitive reduction shouldn't change any nodes"
         , lookupName = "transitivenodes"
         , desc       = dsc
         , tests      = [qCheck prop]
         }
  where
    prop :: CanonicaliseOptions -> DotGraph Int -> Bool
    prop = prop_transitiveNodes

    dsc = "Transitive reduction shouldn't change or remove any nodes."

-- -----------------------------------------------------------------------------

-- | Used when a property takes in a DotRepr as the first argument to
--   indicate which instance it should test via 'fromCanonical'.
testAllGraphTypes      :: (Testable prop)
                          => (forall dg. (Eq (dg Int), DotRepr dg Int) => dg Int -> prop)
                          -> [prop]
testAllGraphTypes prop = [ prop (undefined :: DotGraph Int)
                         , prop (undefined :: G.DotGraph Int)
                         , prop (undefined :: Gr.DotGraph Int)
                         ]