github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/compiler/vyper_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package compiler
    19  
    20  import (
    21  	"os/exec"
    22  	"testing"
    23  )
    24  
    25  func skipWithoutVyper(t *testing.T) {
    26  	if _, err := exec.LookPath("vyper"); err != nil {
    27  		t.Skip(err)
    28  	}
    29  }
    30  
    31  func TestVyperCompiler(t *testing.T) {
    32  	skipWithoutVyper(t)
    33  
    34  	testSource := []string{"test.v.py"}
    35  	source, err := slurpFiles(testSource)
    36  	if err != nil {
    37  		t.Error("couldn't read test files")
    38  	}
    39  	contracts, err := CompileVyper("", testSource...)
    40  	if err != nil {
    41  		t.Fatalf("error compiling test.v.py. result %v: %v", contracts, err)
    42  	}
    43  	if len(contracts) != 1 {
    44  		t.Errorf("one contract expected, got %d", len(contracts))
    45  	}
    46  	c, ok := contracts["test.v.py"]
    47  	if !ok {
    48  		c, ok = contracts["<stdin>:test"]
    49  		if !ok {
    50  			t.Fatal("info for contract 'test.v.py' not present in result")
    51  		}
    52  	}
    53  	if c.Code == "" {
    54  		t.Error("empty code")
    55  	}
    56  	if c.Info.Source != source {
    57  		t.Error("wrong source")
    58  	}
    59  	if c.Info.CompilerVersion == "" {
    60  		t.Error("empty version")
    61  	}
    62  }
    63  
    64  func TestVyperCompileError(t *testing.T) {
    65  	skipWithoutVyper(t)
    66  
    67  	contracts, err := CompileVyper("", "test_bad.v.py")
    68  	if err == nil {
    69  		t.Errorf("error expected compiling test_bad.v.py. got none. result %v", contracts)
    70  	}
    71  	t.Logf("error: %v", err)
    72  }