SOURCE CODE: Uize.Test.Uize.Loc.FileFormats.AndroidStrings (view docs)

/*______________
|       ______  |   U I Z E    J A V A S C R I P T    F R A M E W O R K
|     /      /  |   ---------------------------------------------------
|    /    O /   |    MODULE : Uize.Test.Uize.Loc.FileFormats.AndroidStrings Class
|   /    / /    |
|  /    / /  /| |    ONLINE : http://uize.com
| /____/ /__/_| | COPYRIGHT : (c)2014-2016 UIZE
|          /___ |   LICENSE : Available under MIT License or GNU General Public License
|_______________|             http://uize.com/license.html
*/

/* Module Meta Data
  type: Test
  importance: 1
  codeCompleteness: 100
  docCompleteness: 100
*/

/*?
  Introduction
    The =Uize.Test.Uize.Loc.FileFormats.AndroidStrings= module defines a suite of unit tests for the =Uize.Loc.FileFormats.AndroidStrings= module.

    *DEVELOPERS:* `Chris van Rensburg`
*/

Uize.module ({
  name:'Uize.Test.Uize.Loc.FileFormats.AndroidStrings',
  builder:function () {
    'use strict';

    var
      /*** no strings ***/
        _stringsNoStrings = {},
        _fileNoStrings =
          '\n' +
          '\n' +
          '\n',

      /*** one string ***/
        _stringsOneString = {
          foo:'bar'
        },
        _fileOneString =
          '\n' +
          '\n' +
          '  bar\n' +
          '\n',

      /*** multiple strings ***/
        _stringsMultipleStrings = {
          foo:'bar',
          baz:'qux',
          hello:'world'
        },
        _fileMultipleStrings =
          '\n' +
          '\n' +
          '  bar\n' +
          '  qux\n' +
          '  world\n' +
          '\n',

      /*** special characters ***/
        _stringsSpecialCharacters = {
          foo:'This string contains a \' (single quote), a " (double quote), a \r (carriage return), a \n (new line), and a \\ (backslash).'
        },
        _fileSpecialCharacters =
          '\n' +
          '\n' +
          '  This string contains a \\\' (single quote), a \\" (double quote), a \\r (carriage return), a \\n (new line), and a \\\\ (backslash).\n' +
          '\n',

      /*** string arrays ***/
        _stringsStringArrays = {
          foo:[
            'foo string 1',
            'foo string 2',
            'foo string 3'
          ],
          bar:[
            'bar string 1',
            'bar string 2'
          ]
        },
        _fileStringArrays =
          '\n' +
          '\n' +
          '  \n' +
          '    foo string 1\n' +
          '    foo string 2\n' +
          '    foo string 3\n' +
          '  \n' +
          '  \n' +
          '    bar string 1\n' +
          '    bar string 2\n' +
          '  \n' +
          '\n',

      /*** plural forms ***/
        _stringsPluralForms = {
          foo:{
            one:'Delete the selected file?',
            other:'Delete the selected {count} files?'
          }
        },
        _filePluralForms =
          '\n' +
          '\n' +
          '  \n' +
          '    Delete the selected file?\n' +
          '    Delete the selected {count} files?\n' +
          '  \n' +
          '\n'
    ;

    return Uize.Test.resolve ({
      title:'Uize.Loc.FileFormats.AndroidStrings Module Test',
      test:[
        Uize.Test.requiredModulesTest ('Uize.Loc.FileFormats.AndroidStrings'),
        Uize.Test.staticMethodsTest ([
          ['Uize.Loc.FileFormats.AndroidStrings.stringHasNonStylingHtml',[
            ['An empty string is not considered to contain non-styling HTML',
              '',
              false
            ],
            ['When a string does not contain HTML, it is not considered to contain non-styling HTML',
              'foo bar',
              false
            ],
            ['When a string contains only the supported styling HTML tags, it is not considered to contain non-styling HTML',
              'foo &&& bar baz',
              false
            ],
            ['When a string contains HTML tags besides the supported styling HTML tags, it is considered to contain non-styling HTML',
              '
foo &&& bar baz
', true ] ]], ['Uize.Loc.FileFormats.AndroidStrings.encodeStringUsingEncodingMode',[ /*** test when the encoding mode is "htmlEncodeAlways" ***/ ['When the string contains no HTML and the encoding mode is "htmlEncodeAlways", then it will be HTML encoded', ['foo & bar','htmlEncodeAlways'], 'foo & bar' ], ['When the string contains only the supported styling HTML tags and the encoding mode is "htmlEncodeAlways", then it will be HTML encoded', ['foo & bar','htmlEncodeAlways'], '<i><u><b>foo &amp; bar</b></u></i>' ], ['When the string contains HTML tags beyond the supported styling tags and the encoding mode is "htmlEncodeAlways", then it will be HTML encoded', ['
foo & bar
','htmlEncodeAlways'], '<div><i><u><b>foo &amp; bar</b></u></i></div>' ], /*** test when the encoding mode is "cdataWrapAlways" ***/ ['When the string contains no HTML and the encoding mode is "cdataWrapAlways", then it will be CDATA wrapped', ['foo & bar','cdataWrapAlways'], '' ], ['When the string contains only the supported styling HTML tags and the encoding mode is "cdataWrapAlways", then it will be CDATA wrapped', ['foo & bar','cdataWrapAlways'], 'foo & bar]]>' ], ['When the string contains HTML tags beyond the supported styling tags and the encoding mode is "cdataWrapAlways", then it will be CDATA wrapped', ['
foo & bar
','cdataWrapAlways'], 'foo & bar]]>' ], /*** test when the encoding mode is "cdataWrapIfAnyHtml" ***/ ['When the string contains no HTML and the encoding mode is "cdataWrapIfAnyHtml", then it will be HTML encoded', ['foo & bar','cdataWrapIfAnyHtml'], 'foo & bar' ], ['When the string contains only the supported styling HTML tags and the encoding mode is "cdataWrapIfAnyHtml", then it will be CDATA wrapped', ['foo & bar','cdataWrapIfAnyHtml'], 'foo & bar]]>' ], ['When the string contains HTML tags beyond the supported styling tags and the encoding mode is "cdataWrapIfAnyHtml", then it will be CDATA wrapped', ['
foo & bar
','cdataWrapIfAnyHtml'], 'foo & bar]]>' ], /*** test when the encoding mode is "cdataWrapIfNonStylingHtml" ***/ ['When the string contains no HTML and the encoding mode is "cdataWrapIfNonStylingHtml", then it will be HTML encoded', ['foo & bar','cdataWrapIfNonStylingHtml'], 'foo & bar' ], ['When the string contains only the supported styling HTML tags and the encoding mode is "cdataWrapIfNonStylingHtml", then it will be left as is', ['foo & bar','cdataWrapIfNonStylingHtml'], 'foo & bar' ], ['When the string contains HTML tags beyond the supported styling tags and the encoding mode is "cdataWrapIfNonStylingHtml", then it will be CDATA wrapped', ['
foo & bar
','cdataWrapIfNonStylingHtml'], 'foo & bar]]>' ] ]], ['Uize.Loc.FileFormats.AndroidStrings.from',[ ['Parsing an empty resource strings file produces an empty strings object', _fileNoStrings, Uize.copy (_stringsNoStrings) ], ['Parsing a resource strings file containing just a single string tag produces a strings object with a single string entry', _fileOneString, Uize.clone (_stringsOneString) ], ['Parsing a resource strings file containing multiple string tags produces a strings object with entries for all the strings', _fileMultipleStrings, Uize.clone (_stringsMultipleStrings) ], ['Backslash-escaped double quote, single quote, "r", "n", and backslash characters inside strings are unescaped', _fileSpecialCharacters, Uize.clone (_stringsSpecialCharacters) ], ['A resource strings file may contain string arrays represented with the tag, and such string array tags are parsed to string array values in the returned strings object', _fileStringArrays, Uize.clone (_stringsStringArrays) ], ['A resource strings file may contain strings with plural forms represented with the tag, and such plural forms tags are parsed to object values in the returned strings object', _filePluralForms, Uize.clone (_stringsPluralForms) ], /*** test support for enclosing values in quotes ***/ ['String values inside string tags can be enclosed in single or double quotes, and these quotes are not considered to be part of the value', '\n' + '\n' + ' \'This string is enclosed in single quotes.\'\n' + ' "This string is enclosed in double quotes."\n' + ' \n' + ' \'This string is enclosed in single quotes.\'\n' + ' "This string is enclosed in double quotes."\n' + ' \n' + ' \n' + ' \'Delete the selected file?\'\n' + ' "Delete the selected {count} files?"\n' + ' \n' + '\n', { foo:'This string is enclosed in single quotes.', bar:'This string is enclosed in double quotes.', baz:[ 'This string is enclosed in single quotes.', 'This string is enclosed in double quotes.', ], qux:{ one:'Delete the selected file?', other:'Delete the selected {count} files?' } } ], /*** test support for CDATA sections inside string values ***/ ['String values inside string tags may contain CDATA sections for wrapping content that has XML-style tags that should be treated literally and not parsed as XML', '\n' + '\n' + ' This string]]> contains HTML]]> tags.\n' + ' string also contains HTML tags.]]>\n' + ' \n' + ' This string]]> contains HTML]]> tags.\n' + ' string also contains HTML tags.]]>\n' + ' \n' + ' \n' + ' Delete the]]> selected file]]>?\n' + ' the selected {count} files?]]>\n' + ' \n' + '\n', { foo:'This string contains HTML tags.', bar:'This string also contains HTML tags.', baz:[ 'This string contains HTML tags.', 'This string also contains HTML tags.', ], qux:{ one:'Delete the selected file?', other:'Delete the selected {count} files?' } } ], ['When a string value inside a string tag contains CDATA sections, and when the contents of those CDATA sections contains double quotes, those double quotes are first escaped so they are not confused with terminating quotes of the string', '\n' + '\n' + ' This string]]> contains HTML]]> tags.\n' + '\n', { foo:'This string contains HTML tags.' } ], /*** test special handling of limited HTML formatting tags and xliff:g tags ***/ ['A string tag may contain bold, underline, and italics HTML tags for inline formatting.', '\n' + '\n' + ' This string contains a bolded word, an underlined word, and an italicized sord.\n' + ' \n' + ' This string contains a bolded word, an underlined word, and an italicized sord.\n' + ' \n' + ' \n' + ' Delete the selected file?\n' + ' Delete the selected {count} files?\n' + ' \n' + '\n', { foo:'This string contains a bolded word, an underlined word, and an italicized sord.', bar:[ 'This string contains a bolded word, an underlined word, and an italicized sord.' ], baz:{ one:'Delete the selected file?', other:'Delete the selected {count} files?' } } ], ['Bold, underline, and italics HTML tags inside a string tag may be nested inside one another.', '\n' + '\n' + ' This string contains a section that is bolded, underlined, and italicized.\n' + ' \n' + ' This string contains a section that is bolded, underlined, and italicized.\n' + ' \n' + ' \n' + ' Delete the selected file?\n' + ' Delete the selected {count} files?\n' + ' \n' + '\n', { foo:'This string contains a section that is bolded, underlined, and italicized.', bar:[ 'This string contains a section that is bolded, underlined, and italicized.' ], baz:{ one:'Delete the selected file?', other:'Delete the selected {count} files?' } } ], ['A string tag may contain xliff:g tags to denote native code sequences, and the contents of these tags are treated as literal text.', '\n' + '\n' + ' This string contains an xliff:g tag for a {{nativeCodeSequence}}.\n' + ' This string two xliff:g tags: {{nativeCodeSequence1}} and [#nativeCodeSequence2#].\n' + ' \n' + ' This string two xliff:g tags: {{nativeCodeSequence1}} and [#nativeCodeSequence2#].\n' + ' \n' + ' \n' + ' Delete the selected file?\n' + ' Delete the selected {count} files?\n' + ' \n' + '\n', { foo:'This string contains an xliff:g tag for a {{nativeCodeSequence}}.', bar:'This string two xliff:g tags: {{nativeCodeSequence1}} and [#nativeCodeSequence2#].', baz:[ 'This string two xliff:g tags: {{nativeCodeSequence1}} and [#nativeCodeSequence2#].' ], qux:{ one:'Delete the selected file?', other:'Delete the selected {count} files?' } } ], ['A string tag may contain xliff:g tags that are wrapped in bold, underline, and italics HTML tags', '\n' + '\n' + ' This string contains an xliff:g tag for a {{nativeCodeSequence}} that is wrapped in the supported HTML formatting tags.\n' + ' \n' + ' This string contains an xliff:g tag for a {{nativeCodeSequence}} that is wrapped in the supported HTML formatting tags.\n' + ' \n' + ' \n' + ' Delete the selected file?\n' + ' Delete the selected {count} files?\n' + ' \n' + '\n', { foo:'This string contains an xliff:g tag for a {{nativeCodeSequence}} that is wrapped in the supported HTML formatting tags.', bar:[ 'This string contains an xliff:g tag for a {{nativeCodeSequence}} that is wrapped in the supported HTML formatting tags.' ], qux:{ one:'Delete the selected file?', other:'Delete the selected {count} files?' } } ] ]], ['Uize.Loc.FileFormats.AndroidStrings.to',[ ['Serializing an empty strings object produces a valid resource strings file containing no string nodes', Uize.copy (_stringsNoStrings), _fileNoStrings ], ['Serializing a strings object with a single string entry produces a resource strings file containing just a single string tag', Uize.clone (_stringsOneString), _fileOneString ], ['Serializing a strings object with multiple string entries produces a resource strings file containing string tags for all the strings', Uize.clone (_stringsMultipleStrings), _fileMultipleStrings ], ['Double quote, single quote, carriage return, new line, and backslash characters inside strings are escaped with backslashes, to satisfy the idiosyncratic requirements of the Android resource file format', Uize.clone (_stringsSpecialCharacters), _fileSpecialCharacters ], ['Serializing a strings object with string array values produces a resource strings file containing tags representing the string array values', Uize.clone (_stringsStringArrays), _fileStringArrays ], ['Serializing a strings object with object values representing strings with plural forms produces a resource strings file containing tags representing the strings with plural forms', Uize.clone (_stringsPluralForms), _filePluralForms ], ['When serializing a strings object, strings are encoded by default with the "cdataWrapIfNonStylingHtml" encoding mode', { stringWithNoHtml:'foo & bar', stringWithSupportedStylingHtmlTags:'foo & bar', stringWithUnsupportedHtmlTags:'
foo & bar
' }, '\n' + '\n' + ' foo & bar\n' + ' foo & bar\n' + ' foo & bar]]>\n' + '\n', ] ]] ]) ] }); } });