Skip to content

Commit

Permalink
Basic unit tests for syntax errors (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 authored Jul 2, 2023
1 parent 12d640e commit 4051f39
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
48 changes: 48 additions & 0 deletions contextual_solidity_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package solgo

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPushContext(t *testing.T) {
parser := &ContextualSolidityParser{
contextStack: []string{},
}

parser.PushContext("ContractDefinition")

assert.Equal(t, 1, len(parser.contextStack), "Expected context stack length to be 1")
assert.Equal(t, "ContractDefinition", parser.contextStack[0], "Expected context to be 'ContractDefinition'")
}

func TestPopContext(t *testing.T) {
parser := &ContextualSolidityParser{
contextStack: []string{"ContractDefinition"},
}

parser.PopContext()

assert.Equal(t, 0, len(parser.contextStack), "Expected context stack length to be 0")
}

func TestCurrentContext(t *testing.T) {
parser := &ContextualSolidityParser{
contextStack: []string{"ContractDefinition", "FunctionDeclaration"},
}

context := parser.CurrentContext()

assert.Equal(t, "FunctionDeclaration", context, "Expected current context to be 'FunctionDeclaration'")
}

func TestCurrentContextEmpty(t *testing.T) {
parser := &ContextualSolidityParser{
contextStack: []string{},
}

context := parser.CurrentContext()

assert.Equal(t, "", context, "Expected current context to be an empty string")
}
34 changes: 34 additions & 0 deletions syntax_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package solgo

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewSyntaxErrorListener(t *testing.T) {
listener := NewSyntaxErrorListener()

assert.NotNil(t, listener.DefaultErrorListener, "Expected DefaultErrorListener to be initialized")
assert.NotNil(t, listener.Errors, "Expected Errors slice to be initialized")
assert.Equal(t, 0, len(listener.Errors), "Expected Errors slice to be empty")
}

func TestSyntaxErrorListener_SyntaxError(t *testing.T) {
listener := NewSyntaxErrorListener()

listener.SyntaxError(nil, nil, 1, 1, "missing ';'", nil)

assert.Equal(t, 1, len(listener.Errors), "Expected one error to be recorded")
assert.Equal(t, 1, listener.Errors[0].Line, "Expected error line to be 1")
assert.Equal(t, 1, listener.Errors[0].Column, "Expected error column to be 1")
assert.Equal(t, "missing ';'", listener.Errors[0].Message, "Expected error message to be 'missing ';''")
assert.Equal(t, SeverityHigh, listener.Errors[0].Severity, "Expected error severity to be SeverityHigh")
assert.Equal(t, "", listener.Errors[0].Context, "Expected error context to be an empty string")
}

func TestDetermineSeverity(t *testing.T) {
assert.Equal(t, SeverityHigh, determineSeverity("missing ';'", "FunctionDeclaration"), "Expected severity to be SeverityHigh for missing token in FunctionDeclaration context")
assert.Equal(t, SeverityMedium, determineSeverity("mismatched input", "VariableDeclaration"), "Expected severity to be SeverityMedium for mismatched input in VariableDeclaration context")
assert.Equal(t, SeverityMedium, determineSeverity("extraneous input", "Expression"), "Expected severity to be SeverityLow for extraneous input in Expression context")
}

0 comments on commit 4051f39

Please sign in to comment.