solidity_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package compiler
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/common"
  8. )
  9. const solcVersion = "0.9.23"
  10. var (
  11. source = `
  12. contract test {
  13. /// @notice Will multiply ` + "`a`" + ` by 7.
  14. function multiply(uint a) returns(uint d) {
  15. return a * 7;
  16. }
  17. }
  18. `
  19. code = "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"
  20. info = `{"source":"\ncontract test {\n /// @notice Will multiply ` + "`a`" + ` by 7.\n function multiply(uint a) returns(uint d) {\n return a * 7;\n }\n}\n","language":"Solidity","languageVersion":"0","compilerVersion":"0.9.23","abiDefinition":[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}],"userDoc":{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}},"developerDoc":{"methods":{}}}`
  21. infohash = common.HexToHash("0xea782f674eb898e477c20e8a7cf11c2c28b09fa68b5278732104f7a101aed255")
  22. )
  23. func TestCompiler(t *testing.T) {
  24. sol, err := New("")
  25. if err != nil {
  26. t.Skip("solc not found: skip")
  27. } else if sol.Version() != solcVersion {
  28. t.Logf("WARNING: a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion)
  29. }
  30. contracts, err := sol.Compile(source)
  31. if err != nil {
  32. t.Errorf("error compiling source. result %v: %v", contracts, err)
  33. return
  34. }
  35. if len(contracts) != 1 {
  36. t.Errorf("one contract expected, got\n%s", len(contracts))
  37. }
  38. if contracts["test"].Code != code {
  39. t.Errorf("wrong code, expected\n%s, got\n%s", code, contracts["test"].Code)
  40. }
  41. }
  42. func TestCompileError(t *testing.T) {
  43. sol, err := New("")
  44. if err != nil || sol.version != solcVersion {
  45. t.Skip("solc not found: skip")
  46. } else if sol.Version() != solcVersion {
  47. t.Logf("WARNING: a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion)
  48. }
  49. contracts, err := sol.Compile(source[2:])
  50. if err == nil {
  51. t.Errorf("error expected compiling source. got none. result %v", contracts)
  52. return
  53. }
  54. }
  55. func TestNoCompiler(t *testing.T) {
  56. _, err := New("/path/to/solc")
  57. if err != nil {
  58. t.Log("solidity quits with error: %v", err)
  59. } else {
  60. t.Errorf("no solc installed, but got no error")
  61. }
  62. }
  63. func TestExtractInfo(t *testing.T) {
  64. var cinfo ContractInfo
  65. err := json.Unmarshal([]byte(info), &cinfo)
  66. if err != nil {
  67. t.Errorf("%v", err)
  68. }
  69. contract := &Contract{
  70. Code: "",
  71. Info: cinfo,
  72. }
  73. filename := "/tmp/solctest.info.json"
  74. os.Remove(filename)
  75. cinfohash, err := ExtractInfo(contract, filename)
  76. if err != nil {
  77. t.Errorf("error extracting info: %v", err)
  78. }
  79. got, err := ioutil.ReadFile(filename)
  80. if err != nil {
  81. t.Errorf("error reading '%v': %v", filename, err)
  82. }
  83. if string(got) != info {
  84. t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", info, string(got))
  85. }
  86. if cinfohash != infohash {
  87. t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex())
  88. }
  89. }