github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/common/compiler/solidity_test.go (about)

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