-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f77ab7f
commit 4d34d6a
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package logger | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalStructLog(t *testing.T) { | ||
jsonInput := `{"test": "0x64"}` | ||
|
||
var log StructLog | ||
err := json.Unmarshal([]byte(jsonInput), &log) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package logger | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/vechain/thor/v2/vm" | ||
) | ||
|
||
// Implement the StateDB interface partially for testing | ||
type mockStateDB struct { | ||
vm.StateDB | ||
refund uint64 | ||
} | ||
|
||
func (m *mockStateDB) GetRefund() uint64 { | ||
return m.refund | ||
} | ||
|
||
func TestJSONLogger(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
logger := NewJSONLogger(nil, &buf) | ||
env := &vm.EVM{} | ||
stateDB := &mockStateDB{refund: 100} | ||
env.StateDB = stateDB | ||
|
||
logger.CaptureStart(env, common.Address{}, common.Address{}, false, nil, 0, big.NewInt(0)) | ||
|
||
memory := vm.NewMemory() | ||
stack := &vm.Stack{} | ||
contract := vm.NewContract(vm.AccountRef(common.Address{}), vm.AccountRef(common.Address{}), big.NewInt(0), 0) | ||
|
||
logger.CaptureState(0, vm.ADD, 10, 2, memory, stack, contract, nil, 1, nil) | ||
logger.CaptureEnd(nil, 0, nil) | ||
|
||
var logs []json.RawMessage | ||
decoder := json.NewDecoder(&buf) | ||
for { | ||
var log json.RawMessage | ||
if err := decoder.Decode(&log); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
t.Fatalf("Failed to decode log: %v", err) | ||
} | ||
logs = append(logs, log) | ||
} | ||
|
||
if len(logs) != 2 { | ||
t.Errorf("Expected 2 logs, got %d", len(logs)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters