solidity_test.go 2.0 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. pragma solidity >0.0.0;
  24. contract test {
  25. /// @notice Will multiply ` + "`a`" + ` by 7.
  26. function multiply(uint a) public returns(uint d) {
  27. return a * 7;
  28. }
  29. }
  30. `
  31. )
  32. func skipWithoutSolc(t *testing.T) {
  33. if _, err := exec.LookPath("solc"); err != nil {
  34. t.Skip(err)
  35. }
  36. }
  37. func TestSolidityCompiler(t *testing.T) {
  38. skipWithoutSolc(t)
  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. func TestSolidityCompileError(t *testing.T) {
  64. skipWithoutSolc(t)
  65. contracts, err := CompileSolidityString("", testSource[4:])
  66. if err == nil {
  67. t.Errorf("error expected compiling source. got none. result %v", contracts)
  68. }
  69. t.Logf("error: %v", err)
  70. }