Cosmetic text fix.
[open-adventure.git] / tests / coverage_dungeon.py
1 #!/usr/bin/env python
2
3 # This is the open-adventure dungeon text coverage report generator. It
4 # consumes a YAML description of the dungeon and determines whether the
5 # various strings contained are present within the test check files.
6 #
7 # Currently, only the location descriptions, arbitrary messages, and object
8 # descriptions are supported. This may be expanded in the future.
9
10 import os
11 import yaml
12 import re
13
14 test_dir = "."
15 yaml_name = "../adventure.yaml"
16 html_template_path = "coverage_dungeon.html.tpl"
17 html_output_path = "../coverage/adventure.yaml.html"
18
19 location_row = """
20     <tr>
21         <td class="coverFile">{}</td>
22         <td class="{}">&nbsp;</td>
23         <td class="{}">&nbsp;</td>
24     </tr>
25 """
26
27 arb_msg_row = """
28     <tr>
29         <td class="coverFile">{}</td>
30         <td class="{}">&nbsp;</td>
31     </tr>
32 """
33
34 object_row = """
35     <tr>
36         <td class="coverFile">{}</td>
37         <td class="{}">&nbsp;</td>
38     </tr>
39 """
40
41 def search(needle, haystack):
42     # Search for needle in haystack, first escaping needle for regex, then
43     # replacing %s, %d, etc. with regex wildcards, so the variable messages
44     # within the dungeon definition will actually match
45     needle = re.escape(needle) \
46              .replace("\%S", ".*") \
47              .replace("\%s", ".*") \
48              .replace("\%d", ".*") \
49              .replace("\%V", ".*")
50
51     return re.search(needle, haystack)
52
53 def loc_coverage(locations, text):
54     for locname, loc in locations:
55         if loc["description"]["long"] == None or loc["description"]["long"] == '':
56             loc["description"]["long"] = True
57         if loc["description"]["long"] != True:
58             if search(loc["description"]["long"], text):
59                 loc["description"]["long"] = True
60         if loc["description"]["short"] == None or loc["description"]["short"] == '':
61             loc["description"]["short"] = True
62         if loc["description"]["short"] != True:
63             #if text.find(loc["description"]["short"]) != -1:
64             if search(loc["description"]["short"], text):
65                 loc["description"]["short"] = True
66
67 def arb_coverage(arb_msgs, text):
68     for i, msg in enumerate(arb_msgs):
69         (msg_name, msg_text) = msg
70         if msg_text == None or msg_text == '':
71             arb_msgs[i] = (msg_name, True)
72         elif msg_text != True:
73             if search(msg_text, text):
74                 arb_msgs[i] = (msg_name, True)
75
76 def obj_coverage(objects, text):
77     for i, objouter in enumerate(objects):
78         (obj_name, obj) = objouter
79         if obj["descriptions"]:
80             for j, desc in enumerate(obj["descriptions"]):
81                 if desc == None or desc == '':
82                     obj["descriptions"][j] = True
83                     objects[i] = (obj_name, obj)
84                 elif desc != True:
85                     if search(desc, text):
86                         obj["descriptions"][j] = True
87                         objects[i] = (obj_name, obj)
88
89 if __name__ == "__main__":
90     with open(yaml_name, "r") as f:
91         db = yaml.load(f)
92
93     with open(html_template_path, "r") as f:
94         html_template = f.read()
95
96     locations = db["locations"]
97     arb_msgs = db["arbitrary_messages"]
98     objects = db["objects"]
99
100     text = ""
101     for filename in os.listdir(test_dir):
102         if filename.endswith(".chk"):
103             with open(filename, "r") as chk:
104                 text = chk.read()
105                 loc_coverage(locations, text)
106                 arb_coverage(arb_msgs, text)
107                 obj_coverage(objects, text)
108
109     location_html = ""
110     location_total = len(locations) * 2
111     location_covered = 0
112     locations.sort()
113     for locouter in locations:
114         locname = locouter[0]
115         loc = locouter[1]
116         if loc["description"]["long"] != True:
117             long_success = "uncovered"
118         else:
119             long_success = "covered"
120             location_covered += 1
121
122         if loc["description"]["short"] != True:
123             short_success = "uncovered"
124         else:
125             short_success = "covered"
126             location_covered += 1
127
128         location_html += location_row.format(locname, long_success, short_success)
129     location_percent = round((location_covered / float(location_total)) * 100, 1)
130
131     arb_msgs.sort()
132     arb_msg_html = ""
133     arb_total = len(arb_msgs)
134     arb_covered = 0
135     for name, msg in arb_msgs:
136         if msg != True:
137             success = "uncovered"
138         else:
139             success = "covered"
140             arb_covered += 1
141         arb_msg_html += arb_msg_row.format(name, success)
142     arb_percent = round((arb_covered / float(arb_total)) * 100, 1)
143
144     object_html = ""
145     objects_total = 0
146     objects_covered = 0
147     objects.sort()
148     for (obj_name, obj) in objects:
149         if obj["descriptions"]:
150             for j, desc in enumerate(obj["descriptions"]):
151                 objects_total += 1
152                 if desc != True:
153                     success = "uncovered"
154                 else:
155                     success = "covered"
156                     objects_covered += 1
157                 object_html += object_row.format("%s[%d]" % (obj_name, j), success)
158     objects_percent = round((objects_covered / float(objects_total)) * 100, 1)
159
160     # output some quick report stats
161     print("\nadventure.yaml coverage rate:")
162     print("  locations..........: {}% covered ({} of {})".format(location_percent, location_covered, location_total))
163     print("  arbitrary_messages.: {}% covered ({} of {})".format(arb_percent, arb_covered, arb_total))
164     print("  objects............: {}% covered ({} of {})".format(objects_percent, objects_covered, objects_total))
165
166     # render HTML report
167     with open(html_output_path, "w") as f:
168         f.write(html_template.format(
169                 location_total, location_covered, location_percent,
170                 arb_total, arb_covered, arb_percent,
171                 objects_total, objects_covered, objects_percent,
172                 location_html, arb_msg_html, object_html
173         ))