natspec_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // +build ignore
  17. package natspec
  18. import (
  19. "testing"
  20. )
  21. func makeInfoDoc(desc string) []byte {
  22. return []byte(`
  23. {
  24. "source": "contract test { }",
  25. "language": "Solidity",
  26. "compilerVersion": "1",
  27. "userDoc": {
  28. "methods": {
  29. "multiply(uint256)": {
  30. "notice": "` + desc + `"
  31. },
  32. "balance(address)": {
  33. "notice": "` + "`(balanceInmGAV / 1000).fixed(0,3)`" + ` GAV is the total funds available to ` + "`who.address()`." + `"
  34. }
  35. },
  36. "invariants": [
  37. { "notice": "The sum total amount of GAV in the system is 1 million." }
  38. ],
  39. "construction": [
  40. { "notice": "Endows ` + "`message.caller.address()`" + ` with 1m GAV." }
  41. ]
  42. },
  43. "abiDefinition": [{
  44. "name": "multiply",
  45. "constant": false,
  46. "type": "function",
  47. "inputs": [{
  48. "name": "a",
  49. "type": "uint256"
  50. }],
  51. "outputs": [{
  52. "name": "d",
  53. "type": "uint256"
  54. }]
  55. }]
  56. }`)
  57. }
  58. var data = "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
  59. var tx = `
  60. {
  61. "params": [{
  62. "to": "0x8521742d3f456bd237e312d6e30724960f72517a",
  63. "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
  64. }],
  65. }
  66. `
  67. func TestNotice(t *testing.T) {
  68. desc := "Will multiply `a` by 7 and return `a * 7`."
  69. expected := "Will multiply 122 by 7 and return 854."
  70. infodoc := makeInfoDoc(desc)
  71. ns, err := NewWithDocs(infodoc, tx, data)
  72. if err != nil {
  73. t.Errorf("New: error: %v", err)
  74. return
  75. }
  76. notice, err := ns.Notice()
  77. if err != nil {
  78. t.Errorf("expected no error, got %v", err)
  79. }
  80. if notice != expected {
  81. t.Errorf("incorrect notice. expected %v, got %v", expected, notice)
  82. }
  83. }
  84. // test missing method
  85. func TestMissingMethod(t *testing.T) {
  86. desc := "Will multiply `a` by 7 and return `a * 7`."
  87. expected := "natspec.js error evaluating expression: Natspec evaluation failed, method does not exist"
  88. infodoc := makeInfoDoc(desc)
  89. ns, err := NewWithDocs(infodoc, tx, data)
  90. if err != nil {
  91. t.Errorf("New: error: %v", err)
  92. }
  93. notice, err := ns.noticeForMethod(tx, "missing_method", "")
  94. if err == nil {
  95. t.Errorf("expected error, got nothing (notice: '%v')", notice)
  96. } else {
  97. if err.Error() != expected {
  98. t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
  99. }
  100. }
  101. }
  102. // test invalid desc
  103. func TestInvalidDesc(t *testing.T) {
  104. desc := "Will multiply 122 by \"7\" and return 854."
  105. expected := "invalid character '7' after object key:value pair"
  106. infodoc := makeInfoDoc(desc)
  107. _, err := NewWithDocs(infodoc, tx, data)
  108. if err == nil {
  109. t.Errorf("expected error, got nothing", err)
  110. } else {
  111. if err.Error() != expected {
  112. t.Errorf("expected error '%s' got '%v'", expected, err)
  113. }
  114. }
  115. }
  116. // test wrong input params
  117. func TestWrongInputParams(t *testing.T) {
  118. desc := "Will multiply `e` by 7 and return `a * 7`."
  119. expected := "natspec.js error evaluating expression: Natspec evaluation failed, wrong input params"
  120. infodoc := makeInfoDoc(desc)
  121. ns, err := NewWithDocs(infodoc, tx, data)
  122. if err != nil {
  123. t.Errorf("New: error: %v", err)
  124. }
  125. notice, err := ns.Notice()
  126. if err == nil {
  127. t.Errorf("expected error, got nothing (notice: '%v')", notice)
  128. } else {
  129. if err.Error() != expected {
  130. t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
  131. }
  132. }
  133. }