solidity_test.go 2.6 KB

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