From 9eeb3faba33d97050ad5c67e39c7b3d5ff84fca7 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Thu, 12 Sep 2013 14:15:34 -0700 Subject: [PATCH] Improve test coverage --- src/js/ripple/currency.js | 2 +- test/currency-test.js | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/currency-test.js diff --git a/src/js/ripple/currency.js b/src/js/ripple/currency.js index 9e215ca9..64bfbcfc 100644 --- a/src/js/ripple/currency.js +++ b/src/js/ripple/currency.js @@ -52,7 +52,7 @@ Currency.prototype.equals = function (d) { // this._value = NaN on error. Currency.prototype.parse_json = function (j) { if (j instanceof Currency) { - this._value = j; + j.copyTo(this); } else if (typeof j === 'string') { if (j === "" || j === "0" || j === "XRP") { // XRP is never allowed as a Currency object diff --git a/test/currency-test.js b/test/currency-test.js new file mode 100644 index 00000000..07cbf879 --- /dev/null +++ b/test/currency-test.js @@ -0,0 +1,55 @@ +var assert = require('assert'); +var utils = require('./testutils'); +var currency = utils.load_module('currency').Currency; + +describe('Currency', function() { + describe('json_rewrite', function() { + it('json_rewrite("USD") == "USD"', function() { + assert.strictEqual('USD', currency.json_rewrite('USD')); + }); + it('json_rewrite("NaN") == "XRP"', function() { + assert.strictEqual('XRP', currency.json_rewrite(NaN)); + }); + }); + describe('from_json', function() { + it('from_json(NaN).to_json() == "XRP"', function() { + var r = currency.from_json(NaN); + assert(!r.is_valid()); + assert.strictEqual('XRP', r.to_json()); + }); + it('from_json("XRP").to_json() == "XRP"', function() { + var r = currency.from_json('XRP'); + assert.strictEqual(0, r._value); + assert(r.is_valid()); + assert.strictEqual('XRP', r.to_json()); + }); + }); + describe('parse_json(currency obj)', function() { + assert.strictEqual('USD', new currency().parse_json(currency.from_json('USD')).to_json()); + }); + describe('is_valid', function() { + it('Currency.is_valid("XRP")', function() { + assert(currency.is_valid('XRP')); + }); + it('!Currency.is_valid(NaN)', function() { + assert(!currency.is_valid(NaN)); + }); + it('from_json("XRP").is_valid()', function() { + assert(currency.from_json('XRP').is_valid()); + }); + it('!from_json(NaN).is_valid()', function() { + assert(!currency.from_json(NaN).is_valid()); + }); + }); + describe('clone', function() { + it('should clone currency object', function() { + var c = currency.from_json('XRP'); + assert.strictEqual('XRP', c.clone().to_json()); + }); + }); + describe('to_human', function() { + it('should generate human string', function() { + assert.strictEqual('XRP', currency.from_json('XRP').to_human()); + }); + }); +});