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.
7 # Currently, only the location descriptions, arbitrary messages, and object
8 # descriptions are supported. This may be expanded in the future.
15 yaml_name = "../adventure.yaml"
16 html_template_path = "coverage_dungeon.html.tpl"
17 html_output_path = "../coverage/adventure.yaml.html"
21 <td class="coverFile">{}</td>
22 <td class="{}"> </td>
23 <td class="{}"> </td>
29 <td class="coverFile">{}</td>
30 <td class="{}"> </td>
36 <td class="coverFile">{}</td>
37 <td class="{}"> </td>
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", ".*") \
51 return re.search(needle, haystack)
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
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)
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)
85 if search(desc, text):
86 obj["descriptions"][j] = True
87 objects[i] = (obj_name, obj)
89 if __name__ == "__main__":
90 with open(yaml_name, "r") as f:
93 with open(html_template_path, "r") as f:
94 html_template = f.read()
96 locations = db["locations"]
97 arb_msgs = db["arbitrary_messages"]
98 objects = db["objects"]
101 for filename in os.listdir(test_dir):
102 if filename.endswith(".chk"):
103 with open(filename, "r") as chk:
105 loc_coverage(locations, text)
106 arb_coverage(arb_msgs, text)
107 obj_coverage(objects, text)
110 location_total = len(locations) * 2
112 for locouter in locations:
113 locname = locouter[0]
115 if loc["description"]["long"] != True:
116 long_success = "uncovered"
118 long_success = "covered"
119 location_covered += 1
121 if loc["description"]["short"] != True:
122 short_success = "uncovered"
124 short_success = "covered"
125 location_covered += 1
127 location_html += location_row.format(locname, long_success, short_success)
128 location_percent = round((location_covered / location_total) * 100, 1)
131 arb_total = len(arb_msgs)
133 for name, msg in arb_msgs:
135 success = "uncovered"
139 arb_msg_html += arb_msg_row.format(name, success)
140 arb_percent = round((arb_covered / arb_total) * 100, 1)
145 for (obj_name, obj) in objects:
146 if obj["descriptions"]:
147 for j, desc in enumerate(obj["descriptions"]):
150 success = "uncovered"
154 object_html += object_row.format("%s[%d]" % (obj_name, j), success)
155 objects_percent = round((objects_covered / objects_total) * 100, 1)
157 # output some quick report stats
158 print("\nadventure.yaml coverage rate:")
159 print(" locations..........: {}% covered ({} of {})".format(location_percent, location_covered, location_total))
160 print(" arbitrary_messages.: {}% covered ({} of {})".format(arb_percent, arb_covered, arb_total))
161 print(" objects............: {}% covered ({} of {})".format(objects_percent, objects_covered, objects_total))
164 with open(html_output_path, "w") as f:
165 f.write(html_template.format(
166 location_total, location_covered, location_percent,
167 arb_total, arb_covered, arb_percent,
168 objects_total, objects_covered, objects_percent,
169 location_html, arb_msg_html, object_html