github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/common/compiler/solidity_test.go (about)

     1  package compiler
     2  
     3  import (
     4  	"os/exec"
     5  	"testing"
     6  )
     7  
     8  const (
     9  	testSource = `
    10  pragma solidity >0.0.0;
    11  contract test {
    12     /// @notice Will multiply ` + "`a`" + ` by 7.
    13     function multiply(uint a) public returns(uint d) {
    14         return a * 7;
    15     }
    16  }
    17  `
    18  )
    19  
    20  func skipWithoutSolc(t *testing.T) {
    21  	if _, err := exec.LookPath("solc"); err != nil {
    22  		t.Skip(err)
    23  	}
    24  }
    25  
    26  func TestSolidityCompiler(t *testing.T) {
    27  	skipWithoutSolc(t)
    28  
    29  	contracts, err := CompileSolidityString("", testSource)
    30  	if err != nil {
    31  		t.Fatalf("error compiling source. result %v: %v", contracts, err)
    32  	}
    33  	if len(contracts) != 1 {
    34  		t.Errorf("one contract expected, got %d", len(contracts))
    35  	}
    36  	c, ok := contracts["test"]
    37  	if !ok {
    38  		c, ok = contracts["<stdin>:test"]
    39  		if !ok {
    40  			t.Fatal("info for contract 'test' not present in result")
    41  		}
    42  	}
    43  	if c.Code == "" {
    44  		t.Error("empty code")
    45  	}
    46  	if c.Info.Source != testSource {
    47  		t.Error("wrong source")
    48  	}
    49  	if c.Info.CompilerVersion == "" {
    50  		t.Error("empty version")
    51  	}
    52  }
    53  
    54  func TestSolidityCompileError(t *testing.T) {
    55  	skipWithoutSolc(t)
    56  
    57  	contracts, err := CompileSolidityString("", testSource[4:])
    58  	if err == nil {
    59  		t.Errorf("error expected compiling source. got none. result %v", contracts)
    60  	}
    61  	t.Logf("error: %v", err)
    62  }