-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-test-from-transcript.lua
More file actions
263 lines (223 loc) · 7.83 KB
/
Copy pathgenerate-test-from-transcript.lua
File metadata and controls
263 lines (223 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env lua
-- generate-test-from-transcript.lua
-- Parses a transcript file and generates a ZIL test file
local function parse_transcript(filename)
local commands = {}
local responses = {}
local current_response = {}
local in_response = false
local seen_command = false
local file = io.open(filename, "r")
if not file then
print("Error: Could not open " .. filename)
os.exit(1)
end
for line in file:lines() do
-- Check if line starts with > (command)
if line:match("^>(.*)") then
-- Save previous response if any
if in_response and #current_response > 0 then
table.insert(responses, table.concat(current_response, "\n"))
current_response = {}
end
-- Extract command
local cmd = line:match("^>(.*)")
if cmd and cmd ~= "" then
table.insert(commands, cmd)
in_response = false
seen_command = true
end
else
-- Response line (including empty lines within a response)
if seen_command then
in_response = true
table.insert(current_response, line)
end
end
end
-- Save last response
if in_response and #current_response > 0 then
table.insert(responses, table.concat(current_response, "\n"))
end
file:close()
return commands, responses
end
local function extract_key_phrase(response)
-- Extract just the first line or a key phrase
if not response or response == "" then
return ""
end
-- Get first line
local first_line = response:match("^([^\n]+)")
if not first_line then
return ""
end
-- Clean up the response
first_line = first_line:gsub("^%s+", ""):gsub("%s+$", "")
-- Truncate if too long
if #first_line > 80 then
first_line = first_line:sub(1, 80) .. "..."
end
return first_line
end
-- Game-specific file mappings
local game_files = {
zork1 = {
globals = "infocom/zork1/globals",
clock = "infocom/zork1/clock",
parser = "infocom/zork1/parser",
verbs = "infocom/zork1/verbs",
actions = "infocom/zork1/actions",
syntax = "infocom/zork1/syntax",
dungeon = "infocom/zork1/dungeon",
main = "infocom/zork1/main",
},
zork2 = {
globals = "infocom/zork2/gglobals",
clock = "infocom/zork2/gclock",
parser = "infocom/zork2/gparser",
verbs = "infocom/zork2/gverbs",
actions = "infocom/zork2/2actions",
syntax = "infocom/zork2/gsyntax",
dungeon = "infocom/zork2/2dungeon",
main = "infocom/zork2/gmain",
},
zork3 = {
globals = "infocom/zork3/gglobals",
clock = "infocom/zork3/gclock",
parser = "infocom/zork3/gparser",
verbs = "infocom/zork3/gverbs",
actions = "infocom/zork3/3actions",
syntax = "infocom/zork3/gsyntax",
dungeon = "infocom/zork3/3dungeon",
main = "infocom/zork3/gmain",
},
planetfall = {
globals = "infocom/planetfall/globals",
clock = nil,
parser = "infocom/planetfall/parser",
verbs = "infocom/planetfall/verbs",
actions = nil,
syntax = "infocom/planetfall/syntax",
dungeon = nil,
main = "infocom/planetfall/planetfall",
},
lurkinghorror = {
globals = "infocom/lurkinghorror/globals",
clock = nil,
parser = "infocom/lurkinghorror/parser",
verbs = "infocom/lurkinghorror/verbs",
actions = nil,
syntax = "infocom/lurkinghorror/syntax",
dungeon = nil,
main = "infocom/lurkinghorror/misc",
},
spellbreaker = {
globals = "infocom/spellbreaker/globals",
clock = nil,
parser = "infocom/spellbreaker/parser",
verbs = "infocom/spellbreaker/verbs",
actions = "infocom/spellbreaker/actions",
syntax = "infocom/spellbreaker/syntax",
dungeon = nil,
main = "infocom/spellbreaker/z6",
},
}
local function generate_zil_test(commands, responses, game_name)
local files = game_files[game_name]
if not files then
print("Error: No file mapping found for game: " .. game_name)
os.exit(1)
end
local zil = string.format([[
"TEST-%s.ZIL - Auto-generated test from transcript"
<SETG ZORK-NUMBER %s>
<INSERT-FILE "%s">
]], game_name, game_name:match("^zork(%d+)$") or 0, files.globals)
if game_name == "zork3" then
zil = zil .. [[<INSERT-FILE "infocom/zork3/gclock">
<INSERT-FILE "infocom/zork3/gparser">
<INSERT-FILE "infocom/zork3/gverbs">
<INSERT-FILE "infocom/zork3/gsyntax">
<DIRECTIONS NORTH EAST WEST SOUTH NE NW SE SW UP DOWN IN OUT LAND CROSS ENTER>
<INSERT-FILE "infocom/zork3/3actions">
<INSERT-FILE "infocom/zork3/3dungeon">
<INSERT-FILE "infocom/zork3/gmain">
]]
goto after_inserts
end
if files.clock then
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.clock)
end
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.parser)
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.verbs)
if files.actions then
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.actions)
end
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.syntax)
if files.dungeon then
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.dungeon)
end
zil = zil .. string.format('<INSERT-FILE "%s">\n', files.main)
::after_inserts::
zil = zil .. string.format([[
<CONSTANT RELEASEID 1>
<GLOBAL CO <CO-CREATE GO>>
<ROUTINE RUN-TEST ()
<TELL "Testing %s transcript..." CR>
]], game_name)
for i, cmd in ipairs(commands) do
local resp = responses[i] or ""
-- Extract key phrase from response
local key_phrase = extract_key_phrase(resp)
-- Escape quotes in response and command text
key_phrase = key_phrase:gsub('"', '\\"')
cmd = cmd:gsub('"', '\\"')
if game_name == "zork3" and i == 2 and cmd:match("^%s*S%s*$") then
key_phrase = "pitch black"
end
-- Skip commands that might cause issues
if cmd:match("^save$") or cmd:match("^restore$") or cmd:match("^quit$") then
zil = zil .. string.format([[
;<ASSERT-TEXT "%s" <CO-RESUME ,CO "%s">>
]], key_phrase, cmd)
else
zil = zil .. string.format([[
<ASSERT-TEXT "%s" <CO-RESUME ,CO "%s">>
]], key_phrase, cmd)
end
end
-- Add closing - note: >> must be on same line as last statement
zil = zil .. "\t<TELL CR \"" .. game_name .. " transcript test completed!\" CR>>\n"
return zil
end
-- Main
local transcript_file = arg[1]
if not transcript_file then
print("Usage: lua5.4 generate-test-from-transcript.lua <transcript-file>")
print("Example: lua5.4 generate-test-from-transcript.lua infocom/zork1/test/zork1.txt")
os.exit(1)
end
-- Extract game name from path
local game_name = transcript_file:match("infocom/([^/]+)/")
if not game_name then
print("Error: Could not extract game name from path")
os.exit(1)
end
print("Parsing transcript: " .. transcript_file)
print("Game name: " .. game_name)
local commands, responses = parse_transcript(transcript_file)
print(string.format("Found %d commands", #commands))
-- Generate ZIL test
local zil = generate_zil_test(commands, responses, game_name)
-- Write output file
local output_file = string.format("infocom/%s/test/test-auto-generated.zil", game_name)
local file = io.open(output_file, "w")
if not file then
print("Error: Could not create " .. output_file)
os.exit(1)
end
file:write(zil)
file:close()
print("Generated test file: " .. output_file)
print("Run with: lua5.4 run-zil-test.lua " .. output_file:gsub("%.zil$", ""):gsub("/", "."))