github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/compiler/solidity_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package compiler
    13  
    14  import (
    15  	"os/exec"
    16  	"testing"
    17  )
    18  
    19  const (
    20  	testSource = `
    21  contract test {
    22     /// @notice Will multiply ` + "`a`" + ` by 7.
    23     function multiply(uint a) returns(uint d) {
    24         return a * 7;
    25     }
    26  }
    27  `
    28  )
    29  
    30  func skipWithoutSolc(t *testing.T) {
    31  	if _, err := exec.LookPath("solc"); err != nil {
    32  		t.Skip(err)
    33  	}
    34  }
    35  
    36  func TestCompiler(t *testing.T) {
    37  	skipWithoutSolc(t)
    38  
    39  	contracts, err := CompileSolidityString("", testSource)
    40  	if err != nil {
    41  		t.Fatalf("error compiling source. result %v: %v", contracts, err)
    42  	}
    43  	if len(contracts) != 1 {
    44  		t.Errorf("one contract expected, got %d", len(contracts))
    45  	}
    46  	c, ok := contracts["test"]
    47  	if !ok {
    48  		c, ok = contracts["<stdin>:test"]
    49  		if !ok {
    50  			t.Fatal("info for contract 'test' not present in result")
    51  		}
    52  	}
    53  	if c.Code == "" {
    54  		t.Error("empty code")
    55  	}
    56  	if c.Info.Source != testSource {
    57  		t.Error("wrong source")
    58  	}
    59  	if c.Info.CompilerVersion == "" {
    60  		t.Error("empty version")
    61  	}
    62  }
    63  
    64  func TestCompileError(t *testing.T) {
    65  	skipWithoutSolc(t)
    66  
    67  	contracts, err := CompileSolidityString("", testSource[4:])
    68  	if err == nil {
    69  		t.Errorf("error expected compiling source. got none. result %v", contracts)
    70  	}
    71  	t.Logf("error: %v", err)
    72  }