All Downloads are FREE. Search and download functionalities are using the official Maven repository.

goog.format.emailaddress_test.js Maven / Gradle / Ivy

Go to download

The Google Closure Library is a collection of JavaScript code designed for use with the Google Closure JavaScript Compiler. This non-official distribution was prepared by the ClojureScript team at http://clojure.org/

There is a newer version: 0.0-20230227-c7c0a541
Show newest version
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.format.EmailAddressTest');
goog.setTestOnly('goog.format.EmailAddressTest');

goog.require('goog.array');
goog.require('goog.format.EmailAddress');
goog.require('goog.testing.jsunit');

function testparseList() {
  assertParsedList('', [], 'Failed to parse empty stringy');
  assertParsedList(',,', [], 'Failed to parse string with commas only');

  assertParsedList('', ['[email protected]']);

  assertParsedList(
      ',', ['[email protected]'],
      'Failed to parse 1 address with trailing comma');

  assertParsedList(
      ', ', ['[email protected]'],
      'Failed to parse 1 address with trailing whitespace and comma');

  assertParsedList(
      ',', ['[email protected]'],
      'Failed to parse 1 address with leading comma');

  assertParsedList(
      ' ,', ['[email protected]'],
      'Failed to parse 1 address with leading whitespace and comma');

  assertParsedList(
      ', ', ['[email protected]', '[email protected]'],
      'Failed to parse 2 email addresses');

  assertParsedList(
      ', ,', ['[email protected]', '[email protected]'],
      'Failed to parse 2 email addresses and trailing comma');

  assertParsedList(
      ', , ', ['[email protected]', '[email protected]'],
      'Failed to parse 2 email addresses, trailing comma and whitespace');

  assertParsedList(
      'John Doe ; Jane Doe , ' +
          '',
      ['[email protected]', '[email protected]', '[email protected]'],
      'Failed to parse addresses with semicolon separator');
}

function testparseListOpenersAndClosers() {
  assertParsedList(
      '[email protected], "[email protected]", , ' +
          '([email protected]), [[email protected]]',
      [
        '[email protected]', '"[email protected]"', '[email protected]', '([email protected])',
        '[[email protected]]'
      ],
      'Failed to handle all 5 opener/closer characters');
}

function testparseListIdn() {
  var idnaddr = 'mailtest@\u4F8B\u3048.\u30C6\u30B9\u30C8';
  assertParsedList(idnaddr, [idnaddr]);
}

function testparseListWithQuotedSpecialChars() {
  var res = assertParsedList(
      'a\\"b\\"c ,"g\\"h\\"i\\\\" ', ['[email protected]', '[email protected]']);
  assertEquals('Wrong name 0', 'a"b"c', res[0].getName());
  assertEquals('Wrong name 1', 'g"h"i\\', res[1].getName());
}

function testparseListWithCommaInLocalPart() {
  var res = assertParsedList(
      '"Doe, John" , ',
      ['[email protected]', '[email protected]']);

  assertEquals('Doe, John', res[0].getName());
  assertEquals('', res[1].getName());
}

function testparseListWithWhitespaceSeparatedEmails() {
  var res = assertParsedList(
      '[email protected]  [email protected] "G H"  [email protected]',
      ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']);
  assertEquals('G H', res[3].getName());
}

function testparseListSystemNewlines() {
  // These Windows newlines can be inserted in IE8, or copied-and-pasted from
  // bad data on a Mac, as seen in bug 11081852.
  assertParsedList(
      '[email protected]\r\[email protected]', ['[email protected]', '[email protected]'],
      'Failed to parse Windows newlines');
  assertParsedList(
      '[email protected]\[email protected]', ['[email protected]', '[email protected]'],
      'Failed to parse *nix newlines');
  assertParsedList(
      '[email protected]\n\[email protected]', ['[email protected]', '[email protected]'],
      'Failed to parse obsolete newlines');
  assertParsedList(
      '[email protected]\[email protected]', ['[email protected]', '[email protected]'],
      'Failed to parse pre-OS X Mac newlines');
}

function testToString() {
  var f = function(str) {
    return goog.format.EmailAddress.parse(str).toString();
  };

  // No modification.
  assertEquals('JOHN Doe ', f('JOHN Doe '));

  // Extra spaces.
  assertEquals('JOHN Doe ', f(' JOHN  Doe   '));

  // No name.
  assertEquals('[email protected]', f(''));
  assertEquals('[email protected]', f('[email protected]'));

  // No address.
  assertEquals('JOHN Doe', f('JOHN Doe <>'));

  // Special chars in the name.
  assertEquals('"JOHN, Doe" ', f('JOHN, Doe '));
  assertEquals(
      '"JOHN(Johnny) Doe" ',
      f('JOHN(Johnny) Doe '));
  assertEquals(
      '"JOHN[Johnny] Doe" ',
      f('JOHN[Johnny] Doe '));
  assertEquals(
      '"JOHN@work Doe" ', f('JOHN@work Doe '));
  assertEquals(
      '"JOHN:theking Doe" ',
      f('JOHN:theking Doe '));
  assertEquals(
      '"JOHN\\\\ Doe" ', f('JOHN\\ Doe '));
  assertEquals(
      '"JOHN.com Doe" ', f('JOHN.com Doe '));

  // Already quoted.
  assertEquals(
      '"JOHN, Doe" ', f('"JOHN, Doe" '));

  // Needless quotes.
  assertEquals('JOHN Doe ', f('"JOHN Doe" '));
  // Not quoted-string, but has double quotes.
  assertEquals(
      '"JOHN, Doe" ', f('JOHN, "Doe" '));

  // No special characters other than quotes.
  assertEquals('JOHN Doe ', f('JOHN "Doe" '));

  // Escaped quotes are also removed.
  assertEquals(
      '"JOHN, Doe" ', f('JOHN, \\"Doe\\" '));
}

function doIsValidTest(testFunc, valid, invalid) {
  goog.array.forEach(valid, function(str) {
    assertTrue('"' + str + '" should be valid.', testFunc(str));
  });
  goog.array.forEach(invalid, function(str) {
    assertFalse('"' + str + '" should be invalid.', testFunc(str));
  });
}

function testIsValid() {
  var valid = [
    '[email protected]', '', 'eric ', '"e" ',
    '[email protected]', '[email protected]', '[email protected]', 'o\'[email protected]',
    '[email protected]', '!/#$%&\'*+-=~|`{}?^[email protected]',
    '[email protected]'
  ];
  var invalid = [
    'e', '', 'e @c.com', 'a@b', 'foo.com', '[email protected]', 'test@gma=il.com',
    'aaa@gmail', 'has some [email protected]', 'has@three@[email protected]',
    '@no-local-part.com', 'み.ん-あ@みんあ.みんあ', 'みんあ@test.com',
    'test@test.みんあ', 'test@みんあ.com', 'fullwidthfullstop@sld' +
        '\uff0e' +
        'tld',
    'ideographicfullstop@sld' +
        '\u3002' +
        'tld',
    'halfwidthideographicfullstop@sld' +
        '\uff61' +
        'tld'
  ];
  doIsValidTest(goog.format.EmailAddress.isValidAddress, valid, invalid);
}

function testIsValidLocalPart() {
  var valid = [
    'e', 'a.b+foo', 'o\'hara', 'user+someone', '!/#$%&\'*+-=~|`{}?^_',
    'confirm-bhk=modulo.org'
  ];
  var invalid = [
    'A@b@c', 'a"b(c)d,e:f;gi[j\\k]l', 'just"not"right',
    'this is"not\\allowed', 'this\\ still\"not\\\\allowed', 'has some spaces'
  ];
  doIsValidTest(goog.format.EmailAddress.isValidLocalPartSpec, valid, invalid);
}

function testIsValidDomainPart() {
  var valid =
      ['example.com', 'dept.example.org', 'long.domain.with.lots.of.dots'];
  var invalid = [
    '', '@has.an.at.sign', '..has.leading.dots', 'gma=il.com',
    'DoesNotHaveADot', 'sld' +
        '\uff0e' +
        'tld',
    'sld' +
        '\u3002' +
        'tld',
    'sld' +
        '\uff61' +
        'tld'
  ];
  doIsValidTest(goog.format.EmailAddress.isValidDomainPartSpec, valid, invalid);
}


/**
 * Asserts that parsing the inputString produces a list of email addresses
 * containing the specified address strings, irrespective of their order.
 * @param {string} inputString A raw address list.
 * @param {Array} expectedList The expected results.
 * @param {string=} opt_message An assertion message.
 * @return {string} the resulting email address objects.
 */
function assertParsedList(inputString, expectedList, opt_message) {
  var message = opt_message || 'Should parse address correctly';
  var result = goog.format.EmailAddress.parseList(inputString);
  assertEquals(
      'Should have correct # of addresses', expectedList.length, result.length);
  for (var i = 0; i < expectedList.length; ++i) {
    assertEquals(message, expectedList[i], result[i].getAddress());
  }
  return result;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy