diff --git a/integration_test/compiler_test.go b/integration_test/compiler_test.go index 43c177a6..11d01120 100644 --- a/integration_test/compiler_test.go +++ b/integration_test/compiler_test.go @@ -1,6 +1,7 @@ package integrationtest import ( + "reflect" "testing" "github.com/aeternity/aepp-sdk-go/v5/aeternity" @@ -27,6 +28,23 @@ func TestCompiler(t *testing.T) { } golden.Assert(t, compiled, simplestorageBytecode) }) + t.Run("CompileErrorDeserialization", func(t *testing.T) { + // Compiler v4.0.0-rc4 and onwards will return a + // CompileContractForbidden type for compile errors. If the error type + // changes in the future and cannot be deserialized, reflect.TypeOf(err) + // will be of type json.UnmarshalError instead, and this test will fail. + wontcompileSource := `contract Test = + entrypoint some_test(ae_address: address) = + // let test = String.concat("\x19Ethereum Signed Message:\n52", Address.to_str(ae_address)) + ae_addres + ` + + _, err := c.CompileContract(wontcompileSource, aeternity.Config.Compiler.Backend) + errtype := reflect.TypeOf(err).String() + if errtype != "*operations.CompileContractForbidden" { + t.Error(err) + } + }) t.Run("DecodeCallResult", func(t *testing.T) { // taken from contract_test.go _, err := c.DecodeCallResult("ok", "cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACr8s/aY", "main", string(golden.Get(t, identitySource)), aeternity.Config.Compiler.Backend) diff --git a/swagguard/compiler/models/compiler_error.go b/swagguard/compiler/models/compiler_error.go index 7d42ec56..bd70dca2 100644 --- a/swagguard/compiler/models/compiler_error.go +++ b/swagguard/compiler/models/compiler_error.go @@ -6,6 +6,8 @@ package models // Editing this file might prove futile when you re-run the swagger generate command import ( + "fmt" + strfmt "github.com/go-openapi/strfmt" "github.com/go-openapi/errors" @@ -108,3 +110,7 @@ func (m *CompilerError) UnmarshalBinary(b []byte) error { *m = res return nil } + +func (m *CompilerError) String() string { + return fmt.Sprintf("%s, %s, %s", *m.Type, *m.Message, m.Context) +} diff --git a/swagguard/compiler/models/error.go b/swagguard/compiler/models/error.go index 1ca4fc4b..c2b8a818 100644 --- a/swagguard/compiler/models/error.go +++ b/swagguard/compiler/models/error.go @@ -62,3 +62,7 @@ func (m *Error) UnmarshalBinary(b []byte) error { *m = res return nil } + +func (m *Error) String() string { + return *m.Reason +} diff --git a/swagguard/compiler/models/error_pos.go b/swagguard/compiler/models/error_pos.go index 83e1d36f..3ca12449 100644 --- a/swagguard/compiler/models/error_pos.go +++ b/swagguard/compiler/models/error_pos.go @@ -6,6 +6,8 @@ package models // Editing this file might prove futile when you re-run the swagger generate command import ( + "fmt" + strfmt "github.com/go-openapi/strfmt" "github.com/go-openapi/errors" @@ -82,3 +84,7 @@ func (m *ErrorPos) UnmarshalBinary(b []byte) error { *m = res return nil } + +func (m *ErrorPos) String() string { + return fmt.Sprintf("%s:line %v, col %v", m.File, *m.Line, *m.Col) +} diff --git a/swagguard/compiler_error_test.go b/swagguard/compiler_error_test.go index 2bcf1dfa..f006d9d0 100644 --- a/swagguard/compiler_error_test.go +++ b/swagguard/compiler_error_test.go @@ -19,3 +19,21 @@ func TestCompilerErrorModelDereferencing(t *testing.T) { t.Errorf("Expected to find %s when printing out the models.Error: got %s instead", reason, printedError) } } + +func TestCompilerCompilationErrorsModelDereferencing(t *testing.T) { + err1 := &models.CompilerError{} + err1.UnmarshalBinary([]byte(`{"message":"Unbound variable ae_addres at line 4, column 9","pos":{"col":9,"line":4},"type":"type_error"}`)) + err2 := &models.CompilerError{} + err2.UnmarshalBinary([]byte(`{"message":"Also I don't like your face","pos":{"col":0,"line":0},"type":"wrong_programmer_error"}`)) + + compileContractForbidden := operations.CompileContractForbidden{ + Payload: []*models.CompilerError{err1, err2}, + } + printedError := fmt.Sprintf("%s", compileContractForbidden) + lookForError1 := "Unbound variable ae_addres" + lookForError2 := "Also I don't like your face" + + if !(strings.Contains(printedError, lookForError1) && strings.Contains(printedError, lookForError2)) { + t.Errorf("Expected []*models.CompilerError to include the messages %s and %s; got %s instead", lookForError1, lookForError2, printedError) + } +}