solidity_test.go 1.9 KB

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