|
1
|
|
|
""" |
|
2
|
|
|
SDoc |
|
3
|
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
|
5
|
|
|
|
|
6
|
|
|
Licence MIT |
|
7
|
|
|
""" |
|
8
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
9
|
|
|
import glob |
|
10
|
|
|
import os |
|
11
|
|
|
import ast |
|
12
|
|
|
import csv |
|
13
|
|
|
import unittest |
|
14
|
|
|
|
|
15
|
|
|
from sdoc.SDoc import SDoc |
|
16
|
|
|
from sdoc.sdoc2 import in_scope |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class SDoc2EnumerationTest(unittest.TestCase): |
|
20
|
|
|
""" |
|
21
|
|
|
Test cases for SDoc2 enumerations. |
|
22
|
|
|
""" |
|
23
|
|
|
|
|
24
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
25
|
|
|
def create_list_of_items(self, nodes): |
|
26
|
|
|
""" |
|
27
|
|
|
Takes a list of lists/tuples, converts to string, removes extra nesting and creates a tuple. |
|
28
|
|
|
|
|
29
|
|
|
:param list nodes: The list of nested lists/tuples of items. |
|
30
|
|
|
|
|
31
|
|
|
:rtype: tuple[tuple] |
|
32
|
|
|
""" |
|
33
|
|
|
items_string = str(nodes) |
|
34
|
|
|
items_string = items_string.replace('[', '') |
|
35
|
|
|
items_string = items_string.replace(']', '') |
|
36
|
|
|
|
|
37
|
|
|
return ast.literal_eval(items_string) |
|
38
|
|
|
|
|
39
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
40
|
|
|
def csv_to_tuple(self, file_name): |
|
41
|
|
|
""" |
|
42
|
|
|
Reads a csv file, creates a tuple of csv objects. |
|
43
|
|
|
|
|
44
|
|
|
:param str file_name: The path to file. |
|
45
|
|
|
|
|
46
|
|
|
:rtype: tuple[tuple] |
|
47
|
|
|
""" |
|
48
|
|
|
list_for_items = [] |
|
49
|
|
|
|
|
50
|
|
|
with open(file_name, 'r') as f: |
|
51
|
|
|
reader = csv.reader(f) |
|
52
|
|
|
for row in reader: |
|
53
|
|
|
list_for_items.append(tuple(row)) |
|
54
|
|
|
|
|
55
|
|
|
items_tuple = tuple(list_for_items) |
|
56
|
|
|
|
|
57
|
|
|
return items_tuple |
|
58
|
|
|
|
|
59
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
60
|
|
|
def testNumbering(self): |
|
61
|
|
|
""" |
|
62
|
|
|
Runs all test cases in the test/enumeration directory. |
|
63
|
|
|
""" |
|
64
|
|
|
test_file_names = glob.glob(os.path.dirname(os.path.abspath(__file__)) + "/enumeration/*.sdoc") |
|
65
|
|
|
|
|
66
|
|
|
for test_file_name in sorted(test_file_names): |
|
67
|
|
|
with self.subTest(test_file_name=test_file_name): |
|
68
|
|
|
pre, ext = os.path.splitext(test_file_name) |
|
69
|
|
|
csv_file_name = pre + '.csv' |
|
70
|
|
|
|
|
71
|
|
|
sdoc = SDoc() |
|
72
|
|
|
sdoc.test_sdoc2(test_file_name) |
|
73
|
|
|
|
|
74
|
|
|
root = in_scope(1) |
|
75
|
|
|
numbers = root.get_enumerated_items() |
|
76
|
|
|
|
|
77
|
|
|
actual = self.create_list_of_items(numbers) |
|
78
|
|
|
expected = self.csv_to_tuple(csv_file_name) |
|
79
|
|
|
|
|
80
|
|
|
self.assertEqual(actual, expected) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
84
|
|
|
|