| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // Copyright 2015 The go-ethereum Authors | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // This file is part of the go-ethereum library. | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2015-07-23 18:35:11 +02:00
										 |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 03:43:34 +01:00
										 |  |  | package natspec | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | func makeInfoDoc(desc string) []byte { | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 	return []byte(` | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  |   "source": "contract test { }", | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  |   "language": "Solidity", | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  |   "compilerVersion": "1", | 
					
						
							|  |  |  |   "userDoc": { | 
					
						
							|  |  |  |     "methods": { | 
					
						
							|  |  |  |       "multiply(uint256)": { | 
					
						
							|  |  |  |         "notice":  "` + desc + `" | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       "balance(address)": { | 
					
						
							|  |  |  |         "notice": "` + "`(balanceInmGAV / 1000).fixed(0,3)`" + ` GAV is the total funds available to ` + "`who.address()`." + `" | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  |     "invariants": [ | 
					
						
							|  |  |  |       { "notice": "The sum total amount of GAV in the system is 1 million." } | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     "construction": [ | 
					
						
							|  |  |  |       { "notice": "Endows ` + "`message.caller.address()`" + ` with 1m GAV." } | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  |   "abiDefinition": [{ | 
					
						
							|  |  |  |     "name": "multiply", | 
					
						
							|  |  |  |     "constant": false, | 
					
						
							|  |  |  |     "type": "function", | 
					
						
							|  |  |  |     "inputs": [{ | 
					
						
							|  |  |  |       "name": "a", | 
					
						
							|  |  |  |       "type": "uint256" | 
					
						
							|  |  |  |     }], | 
					
						
							|  |  |  |     "outputs": [{ | 
					
						
							|  |  |  |       "name": "d", | 
					
						
							|  |  |  |       "type": "uint256" | 
					
						
							|  |  |  |     }] | 
					
						
							|  |  |  |   }] | 
					
						
							|  |  |  | }`) | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-06 03:43:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | var data = "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var tx = ` | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   "params": [{ | 
					
						
							|  |  |  |       "to": "0x8521742d3f456bd237e312d6e30724960f72517a", | 
					
						
							|  |  |  |       "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" | 
					
						
							|  |  |  |   }], | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestNotice(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	desc := "Will multiply `a` by 7 and return `a * 7`." | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 	expected := "Will multiply 122 by 7 and return 854." | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 	infodoc := makeInfoDoc(desc) | 
					
						
							|  |  |  | 	ns, err := NewWithDocs(infodoc, tx, data) | 
					
						
							| 
									
										
										
										
											2015-03-06 03:43:34 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 		t.Errorf("New: error: %v", err) | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2015-03-06 03:43:34 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 08:28:12 +02:00
										 |  |  | 	notice, err := ns.Notice() | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 		t.Errorf("expected no error, got %v", err) | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-06 03:43:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if notice != expected { | 
					
						
							|  |  |  | 		t.Errorf("incorrect notice. expected %v, got %v", expected, notice) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | // test missing method | 
					
						
							|  |  |  | func TestMissingMethod(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 	desc := "Will multiply `a` by 7 and return `a * 7`." | 
					
						
							|  |  |  | 	expected := "natspec.js error evaluating expression: Natspec evaluation failed, method does not exist" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 	infodoc := makeInfoDoc(desc) | 
					
						
							|  |  |  | 	ns, err := NewWithDocs(infodoc, tx, data) | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("New: error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	notice, err := ns.noticeForMethod(tx, "missing_method", "") | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 		t.Errorf("expected error, got nothing (notice: '%v')", notice) | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		if err.Error() != expected { | 
					
						
							|  |  |  | 			t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | // test invalid desc | 
					
						
							| 
									
										
										
										
											2015-03-31 16:02:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | func TestInvalidDesc(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 	desc := "Will multiply 122 by \"7\" and return 854." | 
					
						
							| 
									
										
										
										
											2015-03-31 16:02:55 +01:00
										 |  |  | 	expected := "invalid character '7' after object key:value pair" | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 	infodoc := makeInfoDoc(desc) | 
					
						
							|  |  |  | 	_, err := NewWithDocs(infodoc, tx, data) | 
					
						
							| 
									
										
										
										
											2015-03-12 19:08:30 +07:00
										 |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2015-03-31 16:02:55 +01:00
										 |  |  | 		t.Errorf("expected error, got nothing", err) | 
					
						
							| 
									
										
										
										
											2015-03-12 19:08:30 +07:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		if err.Error() != expected { | 
					
						
							| 
									
										
										
										
											2015-03-31 16:02:55 +01:00
										 |  |  | 			t.Errorf("expected error '%s' got '%v'", expected, err) | 
					
						
							| 
									
										
										
										
											2015-03-12 19:08:30 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // test wrong input params | 
					
						
							|  |  |  | func TestWrongInputParams(t *testing.T) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	desc := "Will multiply `e` by 7 and return `a * 7`." | 
					
						
							|  |  |  | 	expected := "natspec.js error evaluating expression: Natspec evaluation failed, wrong input params" | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 	infodoc := makeInfoDoc(desc) | 
					
						
							|  |  |  | 	ns, err := NewWithDocs(infodoc, tx, data) | 
					
						
							| 
									
										
										
										
											2015-03-31 00:49:38 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("New: error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 08:28:12 +02:00
										 |  |  | 	notice, err := ns.Notice() | 
					
						
							| 
									
										
										
										
											2015-03-08 20:09:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Errorf("expected error, got nothing (notice: '%v')", notice) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if err.Error() != expected { | 
					
						
							|  |  |  | 			t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 03:43:34 +01:00
										 |  |  | } |