b65094a5089f8d24a071d0290f05bc061e0c6a76
[open-adventure.git] / tests / coverage_dungeon.py
1 #!/usr/bin/python3
2
3 import os
4 import yaml
5
6 import pprint
7
8 test_dir = "."
9 yaml_name = "../adventure.yaml"
10 html_template_path = "coverage_dungeon.html.tpl"
11 html_output_path = "../coverage/adventure.yaml.html"
12
13 location_row = """
14     <tr>
15         <td class="coverFile">{}</td>
16         <td class="{}">&nbsp;</td>
17         <td class="{}">&nbsp;</td>
18     </tr>
19 """
20
21 arb_msg_row = """
22     <tr>
23         <td class="coverFile">{}</td>
24         <td class="{}">&nbsp;</td>
25     </tr>
26 """
27
28 object_row = """
29     <tr>
30         <td class="coverFile">{}</td>
31         <td class="{}">&nbsp;</td>
32     </tr>
33 """
34
35 def loc_coverage(locations, text):
36     for locname, loc in locations:
37         if loc["description"]["long"] == None or loc["description"]["long"] == '':
38             loc["description"]["long"] = True
39         if loc["description"]["long"] != True:
40             if text.find(loc["description"]["long"]) != -1:
41                 loc["description"]["long"] = True
42         if loc["description"]["short"] == None or loc["description"]["short"] == '':
43             loc["description"]["short"] = True
44         if loc["description"]["short"] != True:
45             if text.find(loc["description"]["short"]) != -1:
46                 loc["description"]["short"] = True
47
48 def arb_coverage(arb_msgs, text):
49     for i, msg in enumerate(arb_msgs):
50         (msg_name, msg_text) = msg
51         if msg_text == None or msg_text == '':
52             arb_msgs[i] = (msg_name, True)
53         elif msg_text != True:
54             if text.find(msg_text) != -1:
55                 arb_msgs[i] = (msg_name, True)
56
57 def obj_coverage(objects, text):
58     for i, objouter in enumerate(objects):
59         (obj_name, obj) = objouter
60         if obj["descriptions"]:
61             for j, desc in enumerate(obj["descriptions"]):
62                 if desc == None or desc == '':
63                     obj["descriptions"][j] = True
64                     objects[i] = (obj_name, obj)
65                 elif desc != True:
66                     if text.find(desc) != -1:
67                         obj["descriptions"][j] = True
68                         objects[i] = (obj_name, obj)
69
70 if __name__ == "__main__":
71     with open(yaml_name, "r") as f:
72         db = yaml.load(f)
73
74     with open(html_template_path, "r") as f:
75         html_template = f.read()
76
77     locations = db["locations"]
78     arb_msgs = db["arbitrary_messages"]
79     objects = db["objects"]
80
81     text = ""
82     for filename in os.listdir(test_dir):
83         if filename.endswith(".chk"):
84             with open(filename, "r") as chk:
85                 text = chk.read()
86                 loc_coverage(locations, text)
87                 arb_coverage(arb_msgs, text)
88                 obj_coverage(objects, text)
89
90     print("\nadventure.yaml coverage rate:")
91
92     location_html = ""
93     location_total = len(locations) * 2
94     location_covered = 0
95     for locouter in locations:
96         locname = locouter[0]
97         loc = locouter[1]
98         long_success = "covered"
99         short_success = "covered"
100         if loc["description"]["long"] != True:
101             long_success = "uncovered"
102         else:
103             location_covered += 1
104
105         if loc["description"]["short"] != True:
106             short_success = "uncovered"
107         else:
108             location_covered += 1
109
110         location_html += location_row.format(locname, long_success, short_success)
111     location_percent = round((location_covered / location_total) * 100, 1)
112     print("  locations..........: {}% covered ({} of {})".format(location_percent, location_covered, location_total))
113
114     arb_msg_html = ""
115     arb_total = len(arb_msgs)
116     arb_covered = 0
117     for name, msg in arb_msgs:
118         success = "covered"
119         if msg != True:
120             success = "uncovered"
121         else:
122             arb_covered += 1
123         arb_msg_html += arb_msg_row.format(name, success)
124     arb_percent = round((arb_covered / arb_total) * 100, 1)
125     print("  arbitrary_messages.: {}% covered ({} of {})".format(arb_percent, arb_covered, arb_total))
126
127     object_html = ""
128     objects_total = 0
129     objects_covered = 0
130     for (obj_name, obj) in objects:
131         if obj["descriptions"]:
132             for j, desc in enumerate(obj["descriptions"]):
133                 objects_total += 1
134                 success = "covered"
135                 if desc != True:
136                     success = "uncovered"
137                 else:
138                     objects_covered += 1
139                 object_html += object_row.format("%s[%d]" % (obj_name, j), success)
140     objects_percent = round((objects_covered / objects_total) * 100, 1)
141     print("  objects............: {}% covered ({} of {})".format(objects_percent, objects_covered, objects_total))
142
143     with open(html_output_path, "w") as f:
144         f.write(html_template.format(
145                 location_total, location_covered, location_percent,
146                 arb_total, arb_covered, arb_percent,
147                 objects_total, objects_covered, objects_percent,
148                 location_html, arb_msg_html, object_html
149         ))