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