github.com/tdcblockchain/tdcblockchain@v0.0.0-20191111034745-805c65ade158/common/compiler/vyper_test.go (about)

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