addon-sdk/source/test/test-base64.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-base64.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const base64 = require("sdk/base64");
    1.11 +
    1.12 +const text = "Awesome!";
    1.13 +const b64text = "QXdlc29tZSE=";
    1.14 +
    1.15 +const utf8text = "✓ à la mode";
    1.16 +const b64utf8text = "4pyTIMOgIGxhIG1vZGU=";
    1.17 +
    1.18 +exports["test base64.encode"] = function (assert) {
    1.19 +  assert.equal(base64.encode(text), b64text, "encode correctly")
    1.20 +}
    1.21 +
    1.22 +exports["test base64.decode"] = function (assert) {
    1.23 +  assert.equal(base64.decode(b64text), text, "decode correctly")
    1.24 +}
    1.25 +
    1.26 +exports["test base64.encode Unicode"] = function (assert) {
    1.27 +
    1.28 +  assert.equal(base64.encode(utf8text, "utf-8"), b64utf8text,
    1.29 +    "encode correctly Unicode strings.")
    1.30 +}
    1.31 +
    1.32 +exports["test base64.decode Unicode"] = function (assert) {
    1.33 +
    1.34 +  assert.equal(base64.decode(b64utf8text, "utf-8"), utf8text,
    1.35 +    "decode correctly Unicode strings.")
    1.36 +}
    1.37 +
    1.38 +exports["test base64.encode with wrong charset"] = function (assert) {
    1.39 +
    1.40 +  assert.throws(function() {
    1.41 +    base64.encode(utf8text, "utf-16");
    1.42 +  }, "The charset argument can be only 'utf-8'");
    1.43 +
    1.44 +  assert.throws(function() {
    1.45 +    base64.encode(utf8text, "");
    1.46 +  }, "The charset argument can be only 'utf-8'");
    1.47 +
    1.48 +  assert.throws(function() {
    1.49 +    base64.encode(utf8text, 8);
    1.50 +  }, "The charset argument can be only 'utf-8'");
    1.51 +
    1.52 +}
    1.53 +
    1.54 +exports["test base64.decode with wrong charset"] = function (assert) {
    1.55 +
    1.56 +  assert.throws(function() {
    1.57 +    base64.decode(utf8text, "utf-16");
    1.58 +  }, "The charset argument can be only 'utf-8'");
    1.59 +
    1.60 +  assert.throws(function() {
    1.61 +    base64.decode(utf8text, "");
    1.62 +  }, "The charset argument can be only 'utf-8'");
    1.63 +
    1.64 +  assert.throws(function() {
    1.65 +    base64.decode(utf8text, 8);
    1.66 +  }, "The charset argument can be only 'utf-8'");
    1.67 +
    1.68 +}
    1.69 +
    1.70 +exports["test encode/decode Unicode without utf-8 as charset"] = function (assert) {
    1.71 +
    1.72 +  assert.notEqual(base64.decode(base64.encode(utf8text)), utf8text,
    1.73 +    "Unicode strings needs 'utf-8' charset"
    1.74 +  );
    1.75 +
    1.76 +}
    1.77 +
    1.78 +require("test").run(exports);

mercurial