Skip to content

Commit 9e646b7

Browse files
committed
Move code execution into a test helper in the kernel_test.
1 parent 8463a09 commit 9e646b7

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

kernel_test.go

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,8 @@ func testEvaluate(t *testing.T, codeIn string) string {
118118
client, closeClient := newTestJupyterClient(t)
119119
defer closeClient()
120120

121-
// Create a message.
122-
request, err := NewMsg("execute_request", ComposedMsg{})
123-
if err != nil {
124-
t.Fatalf("\t%s NewMsg: %s", failure, err)
125-
}
126-
127-
// Fill in remaining header information.
128-
request.Header.Session = sessionID
129-
request.Header.Username = "KernelTester"
130-
131-
// Fill in Metadata.
132-
request.Metadata = make(map[string]interface{})
133-
134-
// Fill in content.
135-
content := make(map[string]interface{})
136-
content["code"] = codeIn
137-
content["silent"] = false
138-
request.Content = content
139-
140-
reply, pub := client.performJupyterRequest(t, request, 10*time.Second)
141-
142-
assertMsgTypeEquals(t, reply, "execute_reply")
121+
content, pub := client.executeCode(t, codeIn)
143122

144-
content = getMsgContentAsJSONObject(t, reply)
145123
status := getString(t, "content", content, "status")
146124

147125
if status != "ok" {
@@ -153,7 +131,7 @@ func testEvaluate(t *testing.T, codeIn string) string {
153131
content = getMsgContentAsJSONObject(t, pubMsg)
154132

155133
bundledMIMEData := getJSONObject(t, "content", content, "data")
156-
textRep := getString(t, "content[\"data\"]", bundledMIMEData, "text/plain")
134+
textRep := getString(t, `content["data"]`, bundledMIMEData, "text/plain")
157135

158136
return textRep
159137
}
@@ -168,30 +146,8 @@ func TestPanicGeneratesError(t *testing.T) {
168146
client, closeClient := newTestJupyterClient(t)
169147
defer closeClient()
170148

171-
// Create a message.
172-
request, err := NewMsg("execute_request", ComposedMsg{})
173-
if err != nil {
174-
t.Fatalf("\t%s NewMsg: %s", failure, err)
175-
}
176-
177-
// Fill in remaining header information.
178-
request.Header.Session = sessionID
179-
request.Header.Username = "KernelTester"
149+
content, pub := client.executeCode(t, `panic("error"`)
180150

181-
// Fill in Metadata.
182-
request.Metadata = make(map[string]interface{})
183-
184-
// Fill in content.
185-
content := make(map[string]interface{})
186-
content["code"] = "panic(\"Error\")"
187-
content["silent"] = false
188-
request.Content = content
189-
190-
reply, pub := client.performJupyterRequest(t, request, 10*time.Second)
191-
192-
assertMsgTypeEquals(t, reply, "execute_reply")
193-
194-
content = getMsgContentAsJSONObject(t, reply)
195151
status := getString(t, "content", content, "status")
196152

197153
if status != "error" {
@@ -387,6 +343,40 @@ func (client *testJupyterClient) performJupyterRequest(t *testing.T, request Com
387343
return
388344
}
389345

346+
// executeCode creates an execute request for the given code and preforms the request. It returns the content of the
347+
// reply as well as all of the messages captured from the IOPub channel during the execution.
348+
func (client *testJupyterClient) executeCode(t *testing.T, code string) (map[string]interface{}, []ComposedMsg) {
349+
t.Helper()
350+
351+
// Create a message.
352+
request, err := NewMsg("execute_request", ComposedMsg{})
353+
if err != nil {
354+
t.Fatalf("\t%s NewMsg: %s", failure, err)
355+
}
356+
357+
// Fill in remaining header information.
358+
request.Header.Session = sessionID
359+
request.Header.Username = "KernelTester"
360+
361+
// Fill in Metadata.
362+
request.Metadata = make(map[string]interface{})
363+
364+
// Fill in content.
365+
content := make(map[string]interface{})
366+
content["code"] = code
367+
content["silent"] = false
368+
request.Content = content
369+
370+
// Make the request.
371+
reply, pub := client.performJupyterRequest(t, request, 10*time.Second)
372+
373+
// Ensure the reply is an execute_reply and extract the content from the reply.
374+
assertMsgTypeEquals(t, reply, "execute_reply")
375+
content = getMsgContentAsJSONObject(t, reply)
376+
377+
return content, pub
378+
}
379+
390380
// assertMsgTypeEquals is a test helper that fails the test if the message header's MsgType is not the
391381
// expectedType.
392382
func assertMsgTypeEquals(t *testing.T, msg ComposedMsg, expectedType string) {

0 commit comments

Comments
 (0)