JS: More fixes and tests for Amount.negate().

This commit is contained in:
Arthur Britto
2012-10-21 21:45:04 -07:00
committed by Stefan Thomas
parent 1aac962116
commit 02805651cf
2 changed files with 34 additions and 18 deletions

View File

@@ -193,7 +193,7 @@ Amount.prototype.copyTo = function(d, negate) {
if ('object' === typeof this.value)
{
if (this.is_native && negate)
this.value.negate.copyTo(d.value);
this.value.negate().copyTo(d.value);
else
this.value.copyTo(d.value);
}
@@ -204,7 +204,7 @@ Amount.prototype.copyTo = function(d, negate) {
d.offset = this.offset;
d.is_native = this.is_native;
d.is_negative = this.is_negative ? undefined : !this.is_negative;
d.is_negative = this.is_native ? undefined : !this.is_negative;
this.currency.copyTo(d.currency);
this.issuer.copyTo(d.issuer);

View File

@@ -1,55 +1,71 @@
var buster = require("buster");
var amount = require("../js/amount.js");
var Amount = require("../js/amount.js").Amount;
var UInt160 = require("../js/amount.js").UInt160;
buster.testCase("Amount", {
"UInt160" : {
"Parse 0" : function () {
buster.assert.equals(0, amount.UInt160.from_json("0").value);
buster.assert.equals(0, UInt160.from_json("0").value);
},
"Parse 0 export" : function () {
buster.assert.equals(amount.consts.hex_xns, amount.UInt160.from_json("0").to_json());
buster.assert.equals(amount.consts.hex_xns, UInt160.from_json("0").to_json());
},
},
"Amount" : {
"Amount parsing" : {
"Parse native 0" : function () {
buster.assert.equals("0/XNS", amount.Amount.from_json("0").to_text_full());
buster.assert.equals("0/XNS", Amount.from_json("0").to_text_full());
},
"Parse native 0.0" : function () {
buster.assert.equals("0/XNS", amount.Amount.from_json("0.0").to_text_full());
buster.assert.equals("0/XNS", Amount.from_json("0.0").to_text_full());
},
"Parse native -0" : function () {
buster.assert.equals("0/XNS", amount.Amount.from_json("-0").to_text_full());
buster.assert.equals("0/XNS", Amount.from_json("-0").to_text_full());
},
"Parse native -0.0" : function () {
buster.assert.equals("0/XNS", amount.Amount.from_json("-0.0").to_text_full());
buster.assert.equals("0/XNS", Amount.from_json("-0.0").to_text_full());
},
"Parse native 1000" : function () {
buster.assert.equals("1000/XNS", amount.Amount.from_json("1000").to_text_full());
buster.assert.equals("1000/XNS", Amount.from_json("1000").to_text_full());
},
"Parse native 12.3" : function () {
buster.assert.equals("12300000/XNS", amount.Amount.from_json("12.3").to_text_full());
buster.assert.equals("12300000/XNS", Amount.from_json("12.3").to_text_full());
},
"Parse native -12.3" : function () {
buster.assert.equals("-12300000/XNS", amount.Amount.from_json("-12.3").to_text_full());
buster.assert.equals("-12300000/XNS", Amount.from_json("-12.3").to_text_full());
},
"Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", amount.Amount.from_json("123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse 12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", amount.Amount.from_json("12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
buster.assert.equals("12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse 12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", amount.Amount.from_json("12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
buster.assert.equals("12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse 1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("1.23/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", amount.Amount.from_json("1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
buster.assert.equals("1.23/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse -0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", amount.Amount.from_json("-0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse -0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", amount.Amount.from_json("-0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
},
"Amount operations" : {
"Negate native 123" : function () {
buster.assert.equals("-123/XNS", Amount.from_json("123").negate().to_text_full());
},
"Negate native -123" : function () {
buster.assert.equals("123/XNS", Amount.from_json("-123").negate().to_text_full());
},
"Negate non-native 123" : function () {
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").negate().to_text_full());
},
"Negate non-native -123" : function () {
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").negate().to_text_full());
},
}
});