aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.3/python-lib/cuddlefish/tests/test_apiparser.py
blob: 1e95f4c5b438e15f0ecb050344ea2e09316989b3 (plain)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

import os
import unittest
from cuddlefish.docs.apiparser import parse_hunks, ParseError

tests_path = os.path.abspath(os.path.dirname(__file__))
static_files_path = os.path.join(tests_path, "static-files")

class ParserTests(unittest.TestCase):
    def pathname(self, filename):
        return os.path.join(static_files_path, "docs", filename)

    def parse_text(self, text):
        return list(parse_hunks(text))

    def parse(self, pathname):
        return self.parse_text(open(pathname).read())

    def test_parser(self):
        parsed = self.parse(self.pathname("APIsample.md"))
        #for i,h in enumerate(parsed):
        #    print i, h
        self.assertEqual(parsed[0],
                         ("version", 4))
        self.assertEqual(parsed[1],
                         ("markdown", "# Title #\n\nSome text here\n\n"))
        self.assertEqual(parsed[2][0], "api-json")
        p_test = parsed[2][1]
        self.assertEqual(p_test["name"], "test")
        self.assertEqual(p_test["type"], "function")
        self.assertEqual(p_test["signature"], "test(argOne, argTwo, \
argThree, options)")
        self.assertEqual(p_test["description"],
                         "This is a function which does nothing in \
particular.")
        r = p_test["returns"]
        self.assertEqual(r["datatype"], "object")
        self.assertEqual(r["description"], "")
        self.assertEqual(len(r["props"]), 2)
        self.assertEqual(r["props"][0]["datatype"], "string")
        self.assertEqual(r["props"][0]["description"], "First string")
        self.assertEqual(r["props"][1]["datatype"], "url")
        self.assertEqual(r["props"][1]["description"], "First URL")

        self.assertEqual(p_test["params"][0],
                         {"name": "argOne",
                          "required": True,
                          "datatype": "string",
                          "description": "This is the first argument.",
                          "line_number": 11,
                          })

        self.assertEqual(p_test["params"][1],
                         {"name": "argTwo",
                          "required": False,
                          "datatype": "bool",
                          "description": "This is the second argument.",
                          "line_number": 12,
                          })

        self.assertEqual(p_test["params"][2],
                         {"name": "argThree",
                          "required": False,
                          "default": "default",
                          "datatype": "uri",
                          "line_number": 13,
                          "description": """\
This is the third and final argument. And this is
a test of the ability to do multiple lines of
text.""",
                          })
        p3 = p_test["params"][3]
        self.assertEqual(p3["name"], "options")
        self.assertEqual(p3["required"], False)
        self.failIf("type" in p3)
        self.assertEqual(p3["description"], "Options Bag")
        self.assertEqual(p3["props"][0],
                         {"name": "style",
                          "required": False,
                          "datatype": "string",
                          "description": "Some style information.",
                          "line_number": 18,
                          })
        self.assertEqual(p3["props"][1],
                         {"name": "secondToLastOption",
                          "required": False,
                          "default": "True",
                          "datatype": "bool",
                          "description": "The last property.",
                          "line_number": 19,
                          })
        self.assertEqual(p3["props"][2]["name"], "lastOption")
        self.assertEqual(p3["props"][2]["required"], False)
        self.assertEqual(p3["props"][2]["datatype"], "uri")
        self.assertEqual(p3["props"][2]["description"], """\
And this time we have
A multiline description
Written as haiku""")

        self.assertEqual(parsed[3][0], "markdown")
        self.assertEqual(parsed[3][1], "\n\nThis text appears between the \
API blocks.\n\n")

        self.assertEqual(parsed[4][0], "api-json")
        p_test = parsed[4][1]

        expected = {'line_number': 28,
 'name': 'append',
 'params': [{'props':[{'line_number': 33,
                       'required': False,
                       'datatype': 'uri',
                       'name': 'icon',
                       'description': 'The HREF of an icon to show as the \
method of accessing your features slideBar'},
                      {'line_number': 34,
                       'required': False,
                       'datatype': 'string/xml',
                       'name': 'html',
                       'description': 'The content of the feature, either \
as an HTML string,\nor an E4X document fragment.'},
                      {'line_number': 37,
                       'required': False,
                       'datatype': 'uri',
                       'name': 'url',
                       'description': 'The url to load into the content area \
of the feature'},
                      {'line_number': 38,
                       'required': False,
                       'datatype': 'int',
                       'name': 'width',
                       'description': 'Width of the content area and the \
selected slide size'},
                      {'line_number': 39,
                       'required': False,
                       'datatype': 'bool',
                       'name': 'persist',
                       'description': 'Default slide behavior when being \
selected as follows:\nIf true: blah; If false: double blah.'},
                      {'line_number': 42,
                       'required': False,
                       'datatype': 'bool',
                       'name': 'autoReload',
                       'description': 'Automatically reload content on \
select'},
                      {'line_number': 43,
                       'required': False,
                       'datatype': 'function',
                       'name': 'onClick',
                       'description': 'Callback when the icon is \
clicked'},
                      {'line_number': 44,
                       'required': False,
                       'datatype': 'function',
                       'name': 'onSelect',
                       'description': 'Callback when the feature is selected'},
                      {'line_number': 45,
                       'required': False,
                       'datatype': 'function',
                       'name': 'onReady',
                       'description':
                       'Callback when featured is loaded'}],
                       'line_number': 31,
             'required': True,
             'name': 'options',
             'description': 'Pass in all of your options here.'}],
 'signature': 'append(options)',
 'type': 'function',
 'description': 'This is a list of options to specify modifications to your \
slideBar instance.'}
        self.assertEqual(p_test, expected)

        self.assertEqual(parsed[6][0], "api-json")
        p_test = parsed[6][1]
        self.assertEqual(p_test["name"], "cool-func.dot")
        self.assertEqual(p_test["signature"], "cool-func.dot(howMuch, double, \
options, onemore, options2)")
        self.assertEqual(p_test["returns"]["description"],
                         """\
A value telling you just how cool you are.
A boa-constructor!
This description can go on for a while, and can even contain
some **realy** fancy things. Like `code`, or even
~~~~{.javascript}
// Some code!
~~~~""")
        self.assertEqual(p_test["params"][2]["props"][0],
                         {"name": "callback",
                          "required": True,
                          "datatype": "function",
                          "line_number": 63,
                          "description": "The callback",
                          })
        self.assertEqual(p_test["params"][2]["props"][1],
                         {"name": "random",
                          "required": False,
                          "datatype": "bool",
                          "line_number": 64,
                          "description": "Do something random?",
                          })

        p_test = parsed[8][1]
        self.assertEqual(p_test["signature"],"random()")

        # tests for classes
        #1) empty class
        p_test = parsed[10][1]
        self.assertEqual(len(p_test), 4)
        self.assertEqual(p_test["name"], "empty-class")
        self.assertEqual(p_test["description"], "This class contains nothing.")
        self.assertEqual(p_test["type"], "class")
        # 2) class with just one ctor
        p_test = parsed[12][1]
        self.assertEqual(len(p_test), 5)
        self.assertEqual(p_test["name"], "only-one-ctor")
        self.assertEqual(p_test["description"], "This class contains only \
one constructor.")
        self.assertEqual(p_test["type"], "class")
        constructors = p_test["constructors"]
        self.assertEqual(len(constructors), 1)
        self._test_class_constructor(constructors[0], "one-constructor")
        # 3) class with 2 ctors
        p_test = parsed[14][1]
        self.assertEqual(len(p_test), 5)
        self.assertEqual(p_test["name"], "two-ctors")
        self.assertEqual(p_test["description"], "This class contains two \
constructors.")
        self.assertEqual(p_test["type"], "class")
        constructors = p_test["constructors"]
        self.assertEqual(len(constructors), 2)
        self._test_class_constructor(constructors[0], "one-constructor")
        self._test_class_constructor(constructors[1], "another-constructor")
        # 4) class with ctor + method
        p_test = parsed[16][1]
        self.assertEqual(len(p_test), 6)
        self.assertEqual(p_test["name"], "ctor-and-method")
        self.assertEqual(p_test["description"], "This class contains one \
constructor and one method.")
        self.assertEqual(p_test["type"], "class")
        constructors = p_test["constructors"]
        self.assertEqual(len(constructors), 1)
        self._test_class_constructor(constructors[0], "one-constructor")
        methods = p_test["methods"]
        self.assertEqual(len(methods), 1)
        self._test_class_method(methods[0])
        # 5) class with ctor + method + property
        p_test = parsed[18][1]
        self.assertEqual(len(p_test), 8)
        self.assertEqual(p_test["name"], "ctor-method-prop-event")
        self.assertEqual(p_test["description"], "This class contains one \
constructor, one method, one property and an event.")
        self.assertEqual(p_test["type"], "class")
        constructors = p_test["constructors"]
        self.assertEqual(len(constructors), 1)
        self._test_class_constructor(constructors[0], "one-constructor")
        methods = p_test["methods"]
        self.assertEqual(len(methods), 1)
        self._test_class_method(methods[0])
        properties = p_test["properties"]
        self.assertEqual(len(properties), 1)
        self._test_class_property(properties[0])
        events = p_test["events"]
        self.assertEqual(len(events), 1)
        self._test_class_event(events[0])

        self.assertEqual(parsed[-1][0], "markdown")
        self.assertEqual(parsed[-1][1], "\n\nSome more text here, \
at the end of the file.\n\n")

    def _test_class_constructor(self, constructor, name):
        self.assertEqual(constructor["type"], "constructor")
        self.assertEqual(constructor["name"], name)
        params = constructor["params"]
        self.assertEqual(len(params), 1)
        self.assertEqual(params[0]["name"], "options")
        self.assertEqual(params[0]["description"], "An object-bag of goodies.")

    def _test_class_method(self, method):
        self.assertEqual(method["type"], "method")
        self.assertEqual(method["name"], "a-method")
        self.assertEqual(method["description"], "Does things.")
        params = method["params"]
        self.assertEqual(len(params), 1)
        self.assertEqual(params[0]["name"], "options")
        self.assertEqual(params[0]["description"], "An argument.")

    def _test_class_property(self, prop):
        self.assertEqual(prop["type"], "property")
        self.assertEqual(prop["name"], "a-property")
        self.assertEqual(prop["description"], "Represents stuff.")
        self.assertEqual(prop["datatype"], "bool")

    def _test_class_event(self, event):
        self.assertEqual(event["type"], "event")
        self.assertEqual(event["name"], "message")
        self.assertEqual(event["description"], "Event emitted when the \
content script sends a message to the add-on.")
        arguments = event["arguments"]
        self.assertEqual(len(arguments), 1)
        argument = arguments[0]
        self.assertEqual(argument["datatype"], "JSON")
        self.assertEqual(argument["description"], "The message itself as a \
JSON-serialized object.")

    def test_missing_return_propname(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns {object}
  @prop {string} First string, but the property name is missing
  @prop {url} First URL, same problem
@param argOne {string} This is the first argument.
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_missing_return_proptype(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns {object}
  @prop untyped It is an error to omit the type of a return property.
@param argOne {string} This is the first argument.
@param [argTwo=True] {bool} This is the second argument.
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_return_propnames(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns {object}
  @prop firststring {string} First string.
  @prop [firsturl] {url} First URL, not always provided.
@param argOne {string} This is the first argument.
@param [argTwo=True] {bool} This is the second argument.
</api>
'''
        parsed = self.parse_text(md)
        r = parsed[1][1]["returns"]
        self.assertEqual(r["props"][0]["name"], "firststring")
        self.assertEqual(r["props"][0],
                         {"name": "firststring",
                          "datatype": "string",
                          "description": "First string.",
                          "required": True,
                          "line_number": 5, # 1-indexed
                          })
        self.assertEqual(r["props"][1],
                         {"name": "firsturl",
                          "datatype": "url",
                          "description": "First URL, not always provided.",
                          "required": False,
                          "line_number": 6,
                          })

    def test_return_description_1(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns {object} A one-line description.
  @prop firststring {string} First string.
  @prop [firsturl] {url} First URL, not always provided.
@param argOne {string} This is the first argument.
@param [argTwo=True] {bool} This is the second argument.
</api>
'''
        parsed = self.parse_text(md)
        r = parsed[1][1]["returns"]
        self.assertEqual(r["description"], "A one-line description.")

    def test_return_description_2(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns {object} A six-line description
  which is consistently indented by two spaces
    except for this line
  and preserves the following empty line
  
  from which a two-space indentation will be removed.
  @prop firststring {string} First string.
  @prop [firsturl] {url} First URL, not always provided.
@param argOne {string} This is the first argument.
@param [argTwo=True] {bool} This is the second argument.
</api>
'''
        parsed = self.parse_text(md)
        r = parsed[1][1]["returns"]
        self.assertEqual(r["description"],
                         "A six-line description\n"
                         "which is consistently indented by two spaces\n"
                         "  except for this line\n"
                         "and preserves the following empty line\n"
                         "\n"
                         "from which a two-space indentation will be removed.")

    def test_return_description_3(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns A one-line untyped description.
@param argOne {string} This is the first argument.
@param [argTwo=True] {bool} This is the second argument.
</api>
'''
        parsed = self.parse_text(md)
        r = parsed[1][1]["returns"]
        self.assertEqual(r["description"], "A one-line untyped description.")

    # if the return value was supposed to be an array, the correct syntax
    # would not have any @prop tags:
    #  @returns {array}
    #   Array consists of two elements, a string and a url...

    def test_return_array(self):
        md = '''\
<api name="test">
@method
This is a function which returns an array.
@returns {array}
  Array consists of two elements, a string and a url.
@param argOne {string} This is the first argument.
@param [argTwo=True] {bool} This is the second argument.
</api>
'''
        parsed = self.parse_text(md)
        r = parsed[1][1]["returns"]
        self.assertEqual(r["description"],
                         "Array consists of two elements, a string and a url.")

    def test_bad_default_on_required_parameter(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@returns something
@param argOne=ILLEGAL {string} Mandatory parameters do not take defaults.
@param [argTwo=Chicago] {string} This is the second argument.
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_missing_apitype(self):
        md = '''\
<api name="test">
Sorry, you must have a @method or something before the description.
Putting it after the description is not good enough
@method
@returns something
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_missing_param_propname(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@param p1 {object} This is a parameter.
  @prop {string} Oops, props must have a name.
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_missing_param_proptype(self):
        md = '''\
<api name="test">
@method
This is a function which does nothing in particular.
@param p1 {object} This is a parameter.
  @prop name Oops, props must have a type.
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_property(self):
        md = '''\
<api name="test">
@property {foo}
An object property named test of type foo.
</api>
'''
        parsed = self.parse_text(md)
        self.assertEqual(parsed[1][0], 'api-json')
        actual_api_json_obj = parsed[1][1]
        expected_api_json_obj = {
            'line_number': 1,
            'datatype': 'foo',
            'type': 'property',
            'name': 'test',
            'description': "An object property named test of type foo."
            }
        self.assertEqual(actual_api_json_obj, expected_api_json_obj)

    def test_property_no_type(self):
        md = '''\
<api name="test">
@property
This property needs to specify a type!
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

    def test_missing_api_closing_tag(self):
        md = '''\
<api name="test">
@class
This is a class with a missing closing tag.
<api name="doStuff"
@method
This method does stuff.
</api>
'''
        self.assertRaises(ParseError, self.parse_text, md)

if __name__ == "__main__":
    unittest.main()