github.com/rayrapetyan/go-ethereum@v1.8.21/common/compiler/solidity_test.go (about)

     1  // Copyright 2015 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  const (
    25  	testSource = `
    26  contract test {
    27     /// @notice Will multiply ` + "`a`" + ` by 7.
    28     function multiply(uint a) returns(uint d) {
    29         return a * 7;
    30     }
    31  }
    32  `
    33  )
    34  
    35  func skipWithoutSolc(t *testing.T) {
    36  	if _, err := exec.LookPath("solc"); err != nil {
    37  		t.Skip(err)
    38  	}
    39  }
    40  
    41  func TestCompiler(t *testing.T) {
    42  	skipWithoutSolc(t)
    43  
    44  	contracts, err := CompileSolidityString("", testSource)
    45  	if err != nil {
    46  		t.Fatalf("error compiling source. result %v: %v", contracts, err)
    47  	}
    48  	if len(contracts) != 1 {
    49  		t.Errorf("one contract expected, got %d", len(contracts))
    50  	}
    51  	c, ok := contracts["test"]
    52  	if !ok {
    53  		c, ok = contracts["<stdin>:test"]
    54  		if !ok {
    55  			t.Fatal("info for contract 'test' not present in result")
    56  		}
    57  	}
    58  	if c.Code == "" {
    59  		t.Error("empty code")
    60  	}
    61  	if c.Info.Source != testSource {
    62  		t.Error("wrong source")
    63  	}
    64  	if c.Info.CompilerVersion == "" {
    65  		t.Error("empty version")
    66  	}
    67  }
    68  
    69  func TestCompileError(t *testing.T) {
    70  	skipWithoutSolc(t)
    71  
    72  	contracts, err := CompileSolidityString("", testSource[4:])
    73  	if err == nil {
    74  		t.Errorf("error expected compiling source. got none. result %v", contracts)
    75  	}
    76  	t.Logf("error: %v", err)
    77  }