natspec_test.go 4.0 KB

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