github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/common/natspec/natspec_test.go (about)

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