solidity_test.go 1.94 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 27 28 29 30 31 32 33 34
contract test {
   /// @notice Will multiply ` + "`a`" + ` by 7.
   function multiply(uint a) returns(uint d) {
       return a * 7;
   }
}
`
)

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

41
func TestCompiler(t *testing.T) {
42 43
	skipWithoutSolc(t)

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

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

72
	contracts, err := CompileSolidityString("", testSource[4:])
73
	if err == nil {
74
		t.Errorf("error expected compiling source. got none. result %v", contracts)
75
	}
76
	t.Logf("error: %v", err)
77
}