|
| 1 | +package kernel |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNewContext(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + setupOption func() *ContextOptions |
| 12 | + wantErr bool |
| 13 | + errType error |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "Valid context options", |
| 17 | + setupOption: func() *ContextOptions { |
| 18 | + opts, err := NewContextOptions() |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to create context options: %v", err) |
| 21 | + } |
| 22 | + params, err := NewChainParameters(ChainTypeMainnet) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Failed to create chain parameters: %v", err) |
| 25 | + } |
| 26 | + defer params.Destroy() |
| 27 | + opts.SetChainParams(params) |
| 28 | + return opts |
| 29 | + }, |
| 30 | + wantErr: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Nil context options", |
| 34 | + setupOption: func() *ContextOptions { |
| 35 | + return nil |
| 36 | + }, |
| 37 | + wantErr: true, |
| 38 | + errType: errors.New("context options cannot be nil"), |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + for _, tt := range tests { |
| 43 | + t.Run(tt.name, func(t *testing.T) { |
| 44 | + opts := tt.setupOption() |
| 45 | + if opts != nil { |
| 46 | + defer opts.Destroy() |
| 47 | + } |
| 48 | + |
| 49 | + ctx, err := NewContext(opts) |
| 50 | + |
| 51 | + if tt.wantErr { |
| 52 | + if err == nil { |
| 53 | + t.Errorf("NewContext() error = nil, wantErr %v", tt.wantErr) |
| 54 | + return |
| 55 | + } |
| 56 | + if tt.errType != nil && err.Error() != tt.errType.Error() { |
| 57 | + t.Errorf("NewContext() error = %v, want %v", err, tt.errType) |
| 58 | + } |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + t.Errorf("NewContext() error = %v, wantErr %v", err, tt.wantErr) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if ctx == nil { |
| 68 | + t.Error("NewContext() returned nil Context") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if ctx.ptr == nil { |
| 73 | + t.Error("Context has nil pointer") |
| 74 | + } |
| 75 | + |
| 76 | + // Clean up |
| 77 | + ctx.Destroy() |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestNewDefaultContext(t *testing.T) { |
| 83 | + ctx, err := NewDefaultContext() |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + t.Errorf("NewDefaultContext() error = %v, want nil", err) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + if ctx == nil { |
| 91 | + t.Error("NewDefaultContext() returned nil Context") |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + if ctx.ptr == nil { |
| 96 | + t.Error("Context has nil pointer") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestContextInterrupt(t *testing.T) { |
| 101 | + ctx, err := NewDefaultContext() |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("Failed to create default context: %v", err) |
| 104 | + } |
| 105 | + defer ctx.Destroy() |
| 106 | + |
| 107 | + // Test interrupt on valid context |
| 108 | + result := ctx.Interrupt() |
| 109 | + // Result can be true or false, both could be valid |
| 110 | + t.Logf("Context interrupt result: %v", result) |
| 111 | + |
| 112 | + // Destroy and test interrupt on destroyed context |
| 113 | + ctx.Destroy() |
| 114 | + result = ctx.Interrupt() |
| 115 | + if result { |
| 116 | + t.Error("Interrupt() should return false after context is destroyed") |
| 117 | + } |
| 118 | +} |
0 commit comments