github.com/klaytn/klaytn@v1.12.1/common/compiler/solidity_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  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  const (
    27  	testSource = `
    28  pragma solidity >0.0.0;
    29  contract test {
    30     /// @notice Will multiply ` + "`a`" + ` by 7.
    31     function multiply(uint a) public returns(uint d) {
    32         return a * 7;
    33     }
    34  }
    35  `
    36  )
    37  
    38  func skipWithoutSolc(t *testing.T) {
    39  	if _, err := exec.LookPath("solc"); err != nil {
    40  		t.Skip(err)
    41  	}
    42  }
    43  
    44  func TestSolidityCompiler(t *testing.T) {
    45  	skipWithoutSolc(t)
    46  
    47  	contracts, err := CompileSolidityString("", testSource)
    48  	if err != nil {
    49  		t.Fatalf("error compiling source. result %v: %v", contracts, err)
    50  	}
    51  	if len(contracts) != 1 {
    52  		t.Errorf("one contract expected, got %d", len(contracts))
    53  	}
    54  	c, ok := contracts["test"]
    55  	if !ok {
    56  		c, ok = contracts["<stdin>:test"]
    57  		if !ok {
    58  			t.Fatal("info for contract 'test' not present in result")
    59  		}
    60  	}
    61  	if c.Code == "" {
    62  		t.Error("empty code")
    63  	}
    64  	if c.Info.Source != testSource {
    65  		t.Error("wrong source")
    66  	}
    67  	if c.Info.CompilerVersion == "" {
    68  		t.Error("empty version")
    69  	}
    70  }
    71  
    72  func TestSolidityCompileError(t *testing.T) {
    73  	skipWithoutSolc(t)
    74  
    75  	// Force syntax error by removing some characters.
    76  	contracts, err := CompileSolidityString("", testSource[4:])
    77  	if err == nil {
    78  		t.Errorf("error expected compiling source. got none. result %v", contracts)
    79  	}
    80  	t.Logf("error: %v", err)
    81  }
    82  
    83  func TestCompileSolidityOrLoad(t *testing.T) {
    84  	// Usually only one version of solc is installed, if any.
    85  	// But CompileSolidityOrLoad will work in all cases.
    86  
    87  	versions := []string{"0.4.24", "0.8.13"}
    88  	for _, version := range versions {
    89  		t.Logf("testing version %s", version)
    90  
    91  		path := "../../contracts/test/UnsafeMultiply_" + version + ".sol"
    92  
    93  		contracts, err := CompileSolidityOrLoad("", path)
    94  		assert.Nil(t, err)
    95  		assert.Equal(t, 1, len(contracts))
    96  
    97  		for _, contract := range contracts {
    98  			assert.NotNil(t, contract.Code)
    99  			assert.NotNil(t, contract.Info.AbiDefinition)
   100  			assert.Equal(t, version, contract.Info.CompilerVersion)
   101  		}
   102  	}
   103  }