Skip to content

Commit

Permalink
WIP V2
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Jun 6, 2023
1 parent 12caee5 commit b89bdb9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 117 deletions.
3 changes: 3 additions & 0 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ func (e *EmulatorBackend) Events(
panic(err)
}
for _, event := range sdkEvents {
if strings.Contains(event.Type, "flow.") {
continue
}
value, err := runtime.ImportValue(
inter,
interpreter.EmptyLocationRange,
Expand Down
34 changes: 5 additions & 29 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3210,6 +3210,8 @@ func TestGetEventsFromIntegrationTests(t *testing.T) {
Test.assert(events.length == 1)
let evts = blockchain.events()
log(evts[9])
Test.assert(evts.length == 10)
}
`

Expand Down Expand Up @@ -3245,39 +3247,13 @@ func TestGetEventsFromIntegrationTests(t *testing.T) {
return "", nil
}

runner := NewTestRunner().WithFileResolver(fileResolver).WithImportResolver(importResolver)
runner := NewTestRunner().
WithFileResolver(fileResolver).
WithImportResolver(importResolver)

results, err := runner.RunTests(testCode)
require.NoError(t, err)
for _, result := range results {
require.NoError(t, result.Error)
}

// events := runner.backend.Events("A.01cf0e2f2f715450.FooContract.RandomEvent")
// assert.Equal(t, 0, len(events))

// events = runner.backend.Events("A.01cf0e2f2f715450.FooContract.ContractInitialized")
// assert.Equal(t, 1, len(events))

// events = runner.backend.Events("A.01cf0e2f2f715450.FooContract.NumberAdded")
// assert.Equal(t, 1, len(events))

// n := cadence.NewInt(78557)
// trait, err := cadence.NewString("Sierpinski")
// require.NoError(t, err)

// event := events[0]
// assert.ElementsMatch(
// t,
// []cadence.Value{n, trait},
// event.Fields,
// )
// assert.ElementsMatch(
// t,
// []cadence.Field{
// cadence.NewField("n", cadence.NewIntType()),
// cadence.NewField("trait", cadence.NewStringType()),
// },
// event.EventType.Fields,
// )
}
138 changes: 50 additions & 88 deletions test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,78 +307,7 @@ func (r *TestRunner) parseCheckAndInterpret(script string) (*interpreter.Program
env.CheckerConfig.ContractValueHandler = contractValueHandler

// Interpreter configs
// env.InterpreterConfig.ImportLocationHandler = r.interpreterImportHandler(ctx)
interpreterImportHandler := func(ctx runtime.Context) func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
return func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
switch location {
case stdlib.CryptoCheckerLocation:
cryptoChecker := stdlib.CryptoChecker()
program := interpreter.ProgramFromChecker(cryptoChecker)
subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}

case stdlib.TestContractLocation:
testChecker := stdlib.GetTestContractType().Checker
program := interpreter.ProgramFromChecker(testChecker)
subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}

default:
addressLocation, ok := location.(common.AddressLocation)
if ok {
account, _ := r.backend.blockchain.GetAccount(
flow.Address(addressLocation.Address),
)
programCode := account.Contracts[addressLocation.Name]
program, err := env.ParseAndCheckProgram(
programCode, location, true,
)
fmt.Println("GOT HERE for: ", location)
if err != nil {
fmt.Println("GOT AN err")
panic(err)
}

subInterpreter, err := inter.NewSubInterpreter(program, addressLocation)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
}
importedProgram, importedElaboration, err := r.parseAndCheckImport(location, ctx)
if err != nil {
panic(err)
}

program := &interpreter.Program{
Program: importedProgram,
Elaboration: importedElaboration,
}

subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}

return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
}
}
}
env.InterpreterConfig.ImportLocationHandler = interpreterImportHandler(ctx)
env.InterpreterConfig.ImportLocationHandler = r.interpreterImportHandler(ctx)

// It is safe to use the test-runner's environment as the standard library handler
// in the test framework, since it is only used for value conversions (i.e: values
Expand Down Expand Up @@ -450,7 +379,6 @@ func contractValueHandler(
declaration *ast.CompositeDeclaration,
compositeType *sema.CompositeType,
) sema.ValueDeclaration {
fmt.Println("In contractValueHandler for: ", compositeType.Location)

constructorType, constructorArgumentLabels := sema.CompositeLikeConstructorType(
checker.Elaboration,
Expand Down Expand Up @@ -542,6 +470,55 @@ func (r *TestRunner) interpreterImportHandler(ctx runtime.Context) interpreter.I
}

default:
addressLocation, ok := location.(common.AddressLocation)
if ok {
account, _ := r.backend.blockchain.GetAccount(
flow.Address(addressLocation.Address),
)
programCode := account.Contracts[addressLocation.Name]
env := runtime.NewBaseInterpreterEnvironment(runtime.Config{})
newCtx := runtime.Context{
Interface: newScriptEnvironment(),
Location: addressLocation,
Environment: env,
}
env.CheckerConfig.ImportHandler = func(
checker *sema.Checker,
importedLocation common.Location,
importRange ast.Range,
) (sema.Import, error) {
addressLoc, ok := importedLocation.(common.AddressLocation)
if !ok {
return nil, fmt.Errorf("no address location given")
}
account, _ := r.backend.blockchain.GetAccount(
flow.Address(addressLoc.Address),
)
programCode := account.Contracts[addressLoc.Name]
program, err := env.ParseAndCheckProgram(
programCode, addressLoc, true,
)
if err != nil {
panic(err)
}

return sema.ElaborationImport{
Elaboration: program.Elaboration,
}, nil
}
program, err := r.testRuntime.ParseAndCheckProgram(programCode, newCtx)
if err != nil {
panic(err)
}

subInterpreter, err := inter.NewSubInterpreter(program, addressLocation)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
}
importedProgram, importedElaboration, err := r.parseAndCheckImport(location, ctx)
if err != nil {
panic(err)
Expand All @@ -556,7 +533,6 @@ func (r *TestRunner) interpreterImportHandler(ctx runtime.Context) interpreter.I
if err != nil {
panic(err)
}

return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
Expand Down Expand Up @@ -615,20 +591,6 @@ func (r *TestRunner) parseAndCheckImport(

env.CheckerConfig.ContractValueHandler = contractValueHandler

addressLocation, ok := location.(common.AddressLocation)
if ok {
account, _ := r.backend.blockchain.GetAccount(
flow.Address(addressLocation.Address),
)
programCode := account.Contracts[addressLocation.Name]
program, err := r.testRuntime.ParseAndCheckProgram(programCode, ctx)
if err != nil {
panic(err)
}

return program.Program, program.Elaboration, nil
}

code, err := r.importResolver(location)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit b89bdb9

Please sign in to comment.