package hbrtl import ( "five/hbrt" "testing" ) func TestLastKey(t *testing.T) { _, th := setupVM() SetLastKey(27) // ESC th.PendingParams2(0) LastKey(th) if r := th.GetRetValue().AsLong(); r != 27 { t.Errorf("LASTKEY() = %d, want 27", r) } } func TestKeyboardBuffer(t *testing.T) { _, th := setupVM() // Stuff keys via KEYBOARD th.PushString("ABC") th.PendingParams2(1) Keyboard(th) // NEXTKEY should return 'A' without consuming th.PendingParams2(0) NextKey(th) if r := th.GetRetValue().AsLong(); r != 65 { t.Errorf("NEXTKEY() = %d, want 65 ('A')", r) } // PopKeyBuffer should consume k := PopKeyBuffer() if k != 65 { t.Errorf("PopKeyBuffer() = %d, want 65", k) } k = PopKeyBuffer() if k != 66 { t.Errorf("PopKeyBuffer() = %d, want 66 ('B')", k) } k = PopKeyBuffer() if k != 67 { t.Errorf("PopKeyBuffer() = %d, want 67 ('C')", k) } k = PopKeyBuffer() if k != -1 { t.Errorf("PopKeyBuffer() = %d, want -1 (empty)", k) } } func TestHbKeyPut(t *testing.T) { _, th := setupVM() // Clear buffer first th.PushString("") th.PendingParams2(1) Keyboard(th) // Put single key th.PushInt(13) // Enter th.PendingParams2(1) HbKeyPut(th) k := PopKeyBuffer() if k != 13 { t.Errorf("HB_KEYPUT(13) → PopKeyBuffer = %d, want 13", k) } } func TestSetKey(t *testing.T) { _, th := setupVM() // Set a key action block := hbrt.MakeBlock(func(t *hbrt.Thread) { t.Frame(0, 0) defer t.EndProc() t.RetNil() }, 0) th.PushInt(28) // F1 th.PushValue(block) th.PendingParams2(2) SetKeyFunc(th) // Old action should be NIL (first time) if !th.GetRetValue().IsNil() { t.Error("SETKEY first call should return NIL") } // Query key action th.PushInt(28) th.PendingParams2(1) SetKeyFunc(th) if th.GetRetValue().IsNil() { t.Error("SETKEY should return previous block") } // Clear key action th.PushInt(28) th.PushNil() th.PendingParams2(2) SetKeyFunc(th) th.PushInt(28) th.PendingParams2(1) SetKeyFunc(th) if !th.GetRetValue().IsNil() { t.Error("SETKEY after clear should return NIL") } }