solidity_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package compiler
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/common"
  8. )
  9. var (
  10. source = `
  11. contract test {
  12. /// @notice Will multiply ` + "`a`" + ` by 7.
  13. function multiply(uint a) returns(uint d) {
  14. return a * 7;
  15. }
  16. }
  17. `
  18. code = "605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056"
  19. 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.13","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":{}}}`
  20. infohash = common.HexToHash("0xfdb031637e8a1c1891143f8d129ebc7f7c4e4b41ecad8c85abe1756190f74204")
  21. )
  22. func TestCompiler(t *testing.T) {
  23. sol, err := New("")
  24. if err != nil {
  25. t.Skip("no solc installed")
  26. }
  27. contract, err := sol.Compile(source)
  28. if err != nil {
  29. t.Errorf("error compiling source. result %v: %v", contract, err)
  30. return
  31. }
  32. if contract.Code != code {
  33. t.Errorf("wrong code, expected\n%s, got\n%s", code, contract.Code)
  34. }
  35. }
  36. func TestCompileError(t *testing.T) {
  37. sol, err := New("")
  38. if err != nil {
  39. t.Skip("no solc installed")
  40. }
  41. contract, err := sol.Compile(source[2:])
  42. if err == nil {
  43. t.Errorf("error expected compiling source. got none. result %v", contract)
  44. return
  45. }
  46. }
  47. func TestNoCompiler(t *testing.T) {
  48. _, err := New("/path/to/solc")
  49. if err != nil {
  50. t.Log("solidity quits with error: %v", err)
  51. } else {
  52. t.Errorf("no solc installed, but got no error")
  53. }
  54. }
  55. func TestExtractInfo(t *testing.T) {
  56. var cinfo ContractInfo
  57. err := json.Unmarshal([]byte(info), &cinfo)
  58. if err != nil {
  59. t.Errorf("%v", err)
  60. }
  61. contract := &Contract{
  62. Code: "",
  63. Info: cinfo,
  64. }
  65. filename := "/tmp/solctest.info.json"
  66. os.Remove(filename)
  67. cinfohash, err := ExtractInfo(contract, filename)
  68. if err != nil {
  69. t.Errorf("%v", err)
  70. }
  71. got, err := ioutil.ReadFile(filename)
  72. if err != nil {
  73. t.Errorf("%v", err)
  74. }
  75. if string(got) != info {
  76. t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", info, string(got))
  77. }
  78. if cinfohash != infohash {
  79. t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex())
  80. }
  81. }