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