From 4ed3aca2fca392af64fdeabb38490e194fa28bec Mon Sep 17 00:00:00 2001 From: Andrew Binstock <920630+platypusguy@users.noreply.github.com> Date: Wed, 6 Nov 2024 21:47:50 -0800 Subject: [PATCH] JACOBIN-592 Cleaning up last few of the new unit tests that refer to runFrame() --- src/config/buildno.go | 2 +- src/jvm/interpreter_A-E_test.go | 14 +++++++++----- src/jvm/interpreter_II-LD_test.go | 29 +++++++++++++++++------------ src/jvm/interpreter_LL-end_test.go | 17 ++++++++++++++--- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/config/buildno.go b/src/config/buildno.go index f137b038..05494169 100644 --- a/src/config/buildno.go +++ b/src/config/buildno.go @@ -8,4 +8,4 @@ package config -var BuildNo = 3195 \ No newline at end of file +var BuildNo = 3196 \ No newline at end of file diff --git a/src/jvm/interpreter_A-E_test.go b/src/jvm/interpreter_A-E_test.go index db5482f7..7f94b3ff 100644 --- a/src/jvm/interpreter_A-E_test.go +++ b/src/jvm/interpreter_A-E_test.go @@ -7,6 +7,7 @@ package jvm import ( + "io" "jacobin/classloader" "jacobin/frames" "jacobin/globals" @@ -404,7 +405,7 @@ func TestNewCheckcastOfInvalidReference(t *testing.T) { // redirect stderr to avoid printing error message to console normalStderr := os.Stderr - _, w, _ := os.Pipe() + r, w, _ := os.Pipe() os.Stderr = w f := newFrame(opcodes.CHECKCAST) @@ -412,11 +413,15 @@ func TestNewCheckcastOfInvalidReference(t *testing.T) { fs := frames.CreateFrameStack() fs.PushFront(&f) // push the new frame - err := runFrame(fs) + interpret(fs) + + _ = w.Close() + msg, _ := io.ReadAll(r) + os.Stderr = normalStderr - os.Stderr = normalStderr // restore stderr + errMsg := string(msg) - if err == nil { + if errMsg == "" { t.Errorf("CHECKCAST: Expected an error, but did not get one") } @@ -424,7 +429,6 @@ func TestNewCheckcastOfInvalidReference(t *testing.T) { t.Errorf("CHECKCAST: Expected TOS to be 0, got %d", f.TOS) } - errMsg := err.Error() if !strings.Contains(errMsg, "CHECKCAST: Invalid class reference") { t.Errorf("CHECKCAST: Expected different error message. Got: %s", errMsg) } diff --git a/src/jvm/interpreter_II-LD_test.go b/src/jvm/interpreter_II-LD_test.go index 7eb14956..fa6a925b 100644 --- a/src/jvm/interpreter_II-LD_test.go +++ b/src/jvm/interpreter_II-LD_test.go @@ -7,6 +7,7 @@ package jvm import ( + "io" "jacobin/classloader" "jacobin/frames" "jacobin/globals" @@ -940,7 +941,7 @@ func TestNewLdcInvalidDouble(t *testing.T) { // hide the error message to stderr normalStderr := os.Stderr - _, w, _ := os.Pipe() + r, w, _ := os.Pipe() os.Stderr = w f := newFrame(opcodes.LDC) @@ -964,15 +965,17 @@ func TestNewLdcInvalidDouble(t *testing.T) { fs := frames.CreateFrameStack() fs.PushFront(&f) // push the new frame - ret := runFrame(fs) + interpret(fs) - // restore stderr _ = w.Close() + msg, _ := io.ReadAll(r) os.Stderr = normalStderr - if ret != nil { - if !strings.Contains(ret.Error(), "LDC: Invalid type") { - t.Errorf("Did not get expected error from LDC with double value, got: %s", ret.Error()) + errMsg := string(msg) + + if errMsg != "" { + if !strings.Contains(errMsg, "LDC: Invalid type") { + t.Errorf("Did not get expected error from LDC with double value, got: %s", errMsg) } } else { t.Errorf("Did not get expected error from LDC with double value") @@ -1123,7 +1126,7 @@ func TestNewLdc2wInvalidForString(t *testing.T) { // hide the error message to stderr normalStderr := os.Stderr - _, w, _ := os.Pipe() + r, w, _ := os.Pipe() os.Stderr = w f := newFrame(opcodes.LDC2_W) @@ -1148,15 +1151,17 @@ func TestNewLdc2wInvalidForString(t *testing.T) { fs := frames.CreateFrameStack() fs.PushFront(&f) // push the new frame - ret := runFrame(fs) + interpret(fs) - // restore stderr _ = w.Close() + msg, _ := io.ReadAll(r) os.Stderr = normalStderr - if ret != nil { - if !strings.Contains(ret.Error(), "LDC2_W: Invalid type") { - t.Errorf("Did not get expected error from LDC with double value, got: %s", ret.Error()) + errMsg := string(msg) + + if errMsg != "" { + if !strings.Contains(errMsg, "LDC2_W: Invalid type") { + t.Errorf("Did not get expected error from LDC with double value, got: %s", errMsg) } } else { t.Errorf("Did not get expected error message in TestLdc2wInvalidForString()") diff --git a/src/jvm/interpreter_LL-end_test.go b/src/jvm/interpreter_LL-end_test.go index ba79d23a..6f6fe210 100644 --- a/src/jvm/interpreter_LL-end_test.go +++ b/src/jvm/interpreter_LL-end_test.go @@ -506,6 +506,12 @@ func TestNewMonitorExit(t *testing.T) { // NEW: Instantiate object -- here with an error func TestNewNewWithError(t *testing.T) { + globals.InitGlobals("test") + + normalStderr := os.Stderr + r, w, _ := os.Pipe() + os.Stderr = w + f := newFrame(opcodes.NEW) f.Meth = append(f.Meth, 0x00) f.Meth = append(f.Meth, 0x01) // Go to slot 0x0001 in the CP @@ -521,13 +527,18 @@ func TestNewNewWithError(t *testing.T) { fs := frames.CreateFrameStack() fs.PushFront(&f) // push the new frame - err := runFrame(fs) + interpret(fs) - if err == nil { + _ = w.Close() + msg, _ := io.ReadAll(r) + os.Stderr = normalStderr + + errMsg := string(msg) + + if errMsg == "" { t.Errorf("NEW: Expected error message, but got none") } - errMsg := err.Error() if !strings.Contains(errMsg, "Invalid type for new object") { t.Errorf("NEW: got unexpected error message: %s", errMsg) }