From 0ec53588d36683e38f08ca723c46f2525c7f13cb Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 3 Nov 2024 20:31:45 +0800 Subject: [PATCH] test RunString, CompileString, None, Nil, MainModule, With --- python_test.go | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/python_test.go b/python_test.go index ff0ef48..f625737 100644 --- a/python_test.go +++ b/python_test.go @@ -11,3 +11,153 @@ func TestMain(m *testing.M) { Finalize() os.Exit(code) } + +func TestRunString(t *testing.T) { + tests := []struct { + name string + code string + wantErr bool + }{ + { + name: "valid python code", + code: "x = 1 + 1", + wantErr: false, + }, + { + name: "invalid python code", + code: "x = ", + wantErr: true, + }, + { + name: "syntax error", + code: "for i in range(10) print(i)", // missing : + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := RunString(tt.code) + if (err != nil) != tt.wantErr { + t.Errorf("RunString() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestCompileString(t *testing.T) { + tests := []struct { + name string + code string + filename string + start InputType + wantNil bool + }{ + { + name: "compile expression", + code: "1 + 1", + filename: "", + start: EvalInput, + wantNil: false, + }, + { + name: "compile invalid code", + code: "x =", + filename: "", + start: EvalInput, + wantNil: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + obj := CompileString(tt.code, tt.filename, tt.start) + if obj.Nil() != tt.wantNil { + t.Errorf("CompileString() returned nil = %v, want %v", obj.Nil(), tt.wantNil) + } + }) + } +} + +func TestNone(t *testing.T) { + none := None() + if none.Nil() { + t.Error("None() returned nil object") + } +} + +func TestNil(t *testing.T) { + nil_ := Nil() + if !nil_.Nil() { + t.Error("Nil() did not return nil object") + } +} + +func TestMainModule(t *testing.T) { + main := MainModule() + if main.Nil() { + t.Error("MainModule() returned nil") + } +} + +func TestWith(t *testing.T) { + // First create a simple Python context manager class + code := ` +class TestContextManager: + def __init__(self): + self.entered = False + self.exited = False + + def __enter__(self): + self.entered = True + return self + + def __exit__(self, *args): + self.exited = True + return None +` + if err := RunString(code); err != nil { + t.Fatalf("Failed to create test context manager: %v", err) + } + + // Get the context manager class and create an instance + main := MainModule() + cmClass := main.AttrFunc("TestContextManager") + if cmClass.Nil() { + t.Fatal("Failed to get TestContextManager class") + } + + cm := cmClass.Call() + if cm.Nil() { + t.Fatal("Failed to create context manager instance") + } + + // Test the With function + called := false + With(cm, func(obj Object) { + called = true + + // Check that __enter__ was called + entered := obj.AttrBool("entered") + if entered.Nil() { + t.Error("Could not get entered attribute") + } + if !entered.Bool() { + t.Error("__enter__ was not called") + } + }) + + // Verify the callback was called + if !called { + t.Error("With callback was not called") + } + + // Check that __exit__ was called + exited := cm.AttrBool("exited") + if exited.Nil() { + t.Error("Could not get exited attribute") + } + if !exited.Bool() { + t.Error("__exit__ was not called") + } +}