solidity_test.go 1.99 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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 18 19
package compiler

import (
20
	"os/exec"
21 22 23
	"testing"
)

24
const (
25
	testSource = `
26
pragma solidity >0.0.0;
27 28
contract test {
   /// @notice Will multiply ` + "`a`" + ` by 7.
29
   function multiply(uint a) public returns(uint d) {
30 31 32 33 34 35
       return a * 7;
   }
}
`
)

36 37
func skipWithoutSolc(t *testing.T) {
	if _, err := exec.LookPath("solc"); err != nil {
38 39 40
		t.Skip(err)
	}
}
zelig's avatar
zelig committed
41

42
func TestSolidityCompiler(t *testing.T) {
43 44
	skipWithoutSolc(t)

45 46 47 48
	contracts, err := CompileSolidityString("", testSource)
	if err != nil {
		t.Fatalf("error compiling source. result %v: %v", contracts, err)
	}
49
	if len(contracts) != 1 {
Felix Lange's avatar
Felix Lange committed
50
		t.Errorf("one contract expected, got %d", len(contracts))
51
	}
52 53
	c, ok := contracts["test"]
	if !ok {
54 55 56 57
		c, ok = contracts["<stdin>:test"]
		if !ok {
			t.Fatal("info for contract 'test' not present in result")
		}
58
	}
59 60
	if c.Code == "" {
		t.Error("empty code")
61 62 63 64
	}
	if c.Info.Source != testSource {
		t.Error("wrong source")
	}
65 66
	if c.Info.CompilerVersion == "" {
		t.Error("empty version")
zelig's avatar
zelig committed
67
	}
68 69
}

70
func TestSolidityCompileError(t *testing.T) {
71 72
	skipWithoutSolc(t)

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
}