036271072b30f3bdb10e0968bb90ac11d4b23984
[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
11 def loc_coverage(locations, text):
12     for locname, loc in locations:
13         if loc["description"]["long"] == None or loc["description"]["long"] == '':
14             loc["description"]["long"] = True
15         if loc["description"]["long"] != True:
16             if text.find(loc["description"]["long"]) != -1:
17                 loc["description"]["long"] = True
18         if loc["description"]["short"] == None or loc["description"]["short"] == '':
19             loc["description"]["short"] = True
20         if loc["description"]["short"] != True:
21             if text.find(loc["description"]["short"]) != -1:
22                 loc["description"]["short"] = True
23
24 def arb_coverage(arb_msgs, text):
25     for i, msg in enumerate(arb_msgs):
26         (msg_name, msg_text) = msg
27         if msg_text == None or msg_text == '':
28             arb_msgs[i] = (msg_name, True)
29         elif msg_text != True:
30             if text.find(msg_text) != -1:
31                 arb_msgs[i] = (msg_name, True)
32
33 def obj_coverage(objects, text):
34     for i, objouter in enumerate(objects):
35         (obj_name, obj) = objouter
36         if obj["descriptions"]:
37             for j, desc in enumerate(obj["descriptions"]):
38                 if desc == None or desc == '':
39                     obj["descriptions"][j] = True
40                     objects[i] = (obj_name, obj)
41                 elif desc != True:
42                     if text.find(desc) != -1:
43                         obj["descriptions"][j] = True
44                         objects[i] = (obj_name, obj)
45
46 if __name__ == "__main__":
47     with open(yaml_name, "r") as f:
48         db = yaml.load(f)
49
50     locations = db["locations"]
51     arb_msgs = db["arbitrary_messages"]
52     objects = db["objects"]
53
54     text = ""
55     for filename in os.listdir(test_dir):
56         if filename.endswith(".chk"):
57             with open(filename, "r") as chk:
58                 text = chk.read()
59                 loc_coverage(locations, text)
60                 arb_coverage(arb_msgs, text)
61                 obj_coverage(objects, text)
62
63     for locouter in locations:
64         locname = locouter[0]
65         loc = locouter[1]
66         if loc["description"]["long"] != True:
67             print("%s long description not covered!" % locname)
68         if loc["description"]["short"] != True:
69             print("location: %s short description not covered!" % locname)
70
71     for name, msg in arb_msgs:
72         if msg != True:
73             print("arbitrary message: %s not covered!" % name)
74
75     for (obj_name, obj) in objects:
76         if obj["descriptions"]:
77             for j, desc in enumerate(obj["descriptions"]):
78                 if desc != True:
79                     print("object: %s desctiption #%d not covered!" % (obj_name, j))