From 7084925e4b34d6673818f24645b5aa8a09ca9e7e Mon Sep 17 00:00:00 2001 From: Vsevolod Brekelov Date: Tue, 14 Jan 2020 17:33:04 +0300 Subject: [PATCH] compiler: add test for compile and save --- pkg/compiler/compiler_test.go | 69 ++++++++++++++++++++------- pkg/compiler/testdata/compile/test.go | 7 +++ 2 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 pkg/compiler/testdata/compile/test.go diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index e49a31f30..0f3903860 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -7,30 +7,63 @@ import ( "testing" "github.com/CityOfZion/neo-go/pkg/compiler" + "github.com/stretchr/testify/require" ) const examplePath = "../../examples" +const exampleCompilePath = "testdata/compile" +const exampleSavePath = exampleCompilePath + "/save" -func TestExamplesFolder(t *testing.T) { - infos, err := ioutil.ReadDir(examplePath) - if err != nil { - t.Fatal(err) +type compilerTestCase struct { + name string + function func() +} + +func TestCompiler(t *testing.T) { + testCases := []compilerTestCase{ + { + name: "TestCompile", + function: func() { + infos, err := ioutil.ReadDir(examplePath) + require.NoError(t, err) + for _, info := range infos { + infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name())) + require.NoError(t, err) + if len(infos) == 0 { + t.Fatal("detected smart contract folder with no contract in it") + } + + filename := filterFilename(infos) + targetPath := path.Join(examplePath, info.Name(), filename) + if err := compileFile(targetPath); err != nil { + t.Fatal(err) + } + } + }, + }, + { + name: "TestCompileAndSave", + function: func() { + infos, err := ioutil.ReadDir(exampleCompilePath) + require.NoError(t, err) + err = os.MkdirAll(exampleSavePath, os.ModePerm) + require.NoError(t, err) + outfile := exampleSavePath + "/test.avm" + if _, err := compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile}); err != nil { + t.Fatal(err) + } + defer func() { + err := os.RemoveAll(exampleSavePath) + require.NoError(t, err) + }() + }, + }, } - for _, info := range infos { - infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name())) - if err != nil { - t.Fatal(err) - } - if len(infos) == 0 { - t.Fatal("detected smart contract folder with no contract in it") - } - - filename := filterFilename(infos) - targetPath := path.Join(examplePath, info.Name(), filename) - if err := compileFile(targetPath); err != nil { - t.Fatal(err) - } + for _, tcase := range testCases { + t.Run(tcase.name, func(t *testing.T) { + tcase.function() + }) } } diff --git a/pkg/compiler/testdata/compile/test.go b/pkg/compiler/testdata/compile/test.go new file mode 100644 index 000000000..4290d67c4 --- /dev/null +++ b/pkg/compiler/testdata/compile/test.go @@ -0,0 +1,7 @@ +package compile + +import "github.com/CityOfZion/neo-go/pkg/interop/runtime" + +func Main() { + runtime.Notify("Hello world!") +}