solidity_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. contract, err := sol.Compile(source)
  31. if err != nil {
  32. t.Errorf("error compiling source. result %v: %v", contract, err)
  33. return
  34. }
  35. if contract.Code != code {
  36. t.Errorf("wrong code, expected\n%s, got\n%s", code, contract.Code)
  37. }
  38. }
  39. func TestCompileError(t *testing.T) {
  40. sol, err := New("")
  41. if err != nil || sol.version != solcVersion {
  42. t.Skip("solc not found: skip")
  43. } else if sol.Version() != solcVersion {
  44. t.Logf("WARNING: a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion)
  45. }
  46. contract, err := sol.Compile(source[2:])
  47. if err == nil {
  48. t.Errorf("error expected compiling source. got none. result %v", contract)
  49. return
  50. }
  51. }
  52. func TestNoCompiler(t *testing.T) {
  53. _, err := New("/path/to/solc")
  54. if err != nil {
  55. t.Log("solidity quits with error: %v", err)
  56. } else {
  57. t.Errorf("no solc installed, but got no error")
  58. }
  59. }
  60. func TestExtractInfo(t *testing.T) {
  61. var cinfo ContractInfo
  62. err := json.Unmarshal([]byte(info), &cinfo)
  63. if err != nil {
  64. t.Errorf("%v", err)
  65. }
  66. contract := &Contract{
  67. Code: "",
  68. Info: cinfo,
  69. }
  70. filename := "/tmp/solctest.info.json"
  71. os.Remove(filename)
  72. cinfohash, err := ExtractInfo(contract, filename)
  73. if err != nil {
  74. t.Errorf("error extracting info: %v", err)
  75. }
  76. got, err := ioutil.ReadFile(filename)
  77. if err != nil {
  78. t.Errorf("error reading '%v': %v", filename, err)
  79. }
  80. if string(got) != info {
  81. t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", info, string(got))
  82. }
  83. if cinfohash != infohash {
  84. t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex())
  85. }
  86. }