shamblett / xml2json

XML to JSON conversion for Dart
MIT License
54 stars 19 forks source link

Parker unit tests #54

Closed jakobleck closed 1 year ago

jakobleck commented 1 year ago

In the readme it says that the rules in https://github.com/doekman/xml2json-xslt would be followed for Parker conversion, with one exception.

I wrote a test file for this and included the above git repo as a submodule to run the unit tests, and found that more of the unit tests do not pass than what the exception mentioned in the readme would explain. I personally don't mind or have any concrete issue resulting from this, just stumbled upon it while debugging an issue in a test (not one of this repo) and report it here for reference.

@shamblett If you like, play around with the below test or simply close this issue.

@TestOn('vm')
import 'dart:convert';
import 'dart:io';

import 'package:test/test.dart';
import 'package:xml2json/xml2json.dart';

void main() async {
  Directory parkerAssetsDirectory = Directory("test/parker/unittests");
  Iterable<File> xmlFiles = (await parkerAssetsDirectory
          .list()
          .where((event) => event is File)
          .toList())
      .cast<File>()
      .where((element) => element.path.endsWith("xml"));

  await Future.forEach(xmlFiles, testXmlFile);
}

Future<void> testXmlFile(File xmlFile) async {
  String xmlFilePath = xmlFile.path;
  String jsonFilePath =
      "${xmlFilePath.substring(0, xmlFilePath.length - 3)}json.expected";
  File jsonFile = File(jsonFilePath);
  String jsonFileContent = await jsonFile.readAsString();
  String xmlFileContent = await xmlFile.readAsString();

  test("correct parker conversion of $xmlFilePath", () {
    final xmlParser = Xml2Json();
    xmlParser.parse(xmlFileContent);
    var jsonResponse = xmlParser.toParker();
    //print(jsonResponse);
    expect(jsonDecode(jsonResponse), jsonDecode(jsonFileContent));
  });
}

This yields

/Users/user/fvm/versions/3.0.5/bin/cache/dart-sdk/bin/dart run test -r json /Users/user/git_root/xml2json/test/parker_test.dart
Testing started at 11:59 ...
package:test_api            expect
test/parker_test.dart 33:5  testXmlFile.<fn>

Expected: {'dates': {'birthdate': '2006-11-14', 'appointment': '2006-11-14T12:30:00'}}
  Actual: {
            'dates': '  This should be ignored, as usual.\\\\n  ',
            'birthdate': '2006-11-14',
            'appointment': ['2006-11-14T12:30', '2006-11-14T12:30:00']
          }
   Which: has different length and has extra map key 'birthdate'

package:test_api            expect
test/parker_test.dart 33:5  testXmlFile.<fn>

Expected: {
            'root': {
              'zero': 0,
              'fixed': 10.25,
              'octal': '0123',
              'hex': '0x123',
              'padded_zero': '01',
              'fixed-neg': -10.25,
              'int': 123,
              'int-neg': -321,
              'float': -12.123,
              'exp': -123400.0
            }
          }
  Actual: {
            'root': {
              'zero': '0',
              'fixed': '10.25',
              'octal': '0123',
              'hex': '0x123',
              'padded_zero': '01',
              'fixed-neg': '-10.25',
              'int': '123',
              'int-neg': '-321',
              'float': '-12.123',
              'exp': '-1.234e5'
            }
          }
   Which: at location ['root']['zero'] is '0' instead of <0>

package:test_api            expect
test/parker_test.dart 33:5  testXmlFile.<fn>

Expected: {'array': ['one']}
  Actual: {'array': {'item': 'one'}}
   Which: at location ['array'] is {'item': 'one'} which is not Iterable

package:test_api            expect
test/parker_test.dart 33:5  testXmlFile.<fn>

Expected: {
            'strings': {
              'script': '<script>alert("YES")</script>',
              'string': '"Begin/end quote"',
              'escape': 'Line one (één in Dutch)\n'
                'Line two:\ttab\n'
                'He said: "Much fun with a €"\n'
                'More unicode†',
              'poetry': 'JABBERWOCKY\n'
                '\n'
                'Lewis Carroll\n'
                '\n'
                '(from Through the Looking-Glass and What Alice Found There, 1872)\n'
                '`Twas brillig, and the slithy toves\n'
                '  Did gyre and gimble in the wabe:\n'
                'All mimsy were the borogoves,\n'
                '  And the mome raths outgrabe.\n'
                '\n'
                ' \n'
                '\n'
                '"Beware the Jabberwock, my son!\n'
                '  The jaws that bite, the claws that catch!\n'
                'Beware the Jubjub bird, and shun\n'
                '  The frumious Bandersnatch!"\n'
                'He took his vorpal sword in hand:\n'
                '  Long time the manxome foe he sought --\n'
                'So rested he by the Tumtum tree,\n'
                '  And stood awhile in thought.\n'
                'And, as in uffish thought he stood,\n'
                '  The Jabberwock, with eyes of flame,\n'
                'Came whiffling through the tulgey wood,\n'
                '  And burbled as it came!\n'
                'One, two! One, two! And through and through\n'
                '  The vorpal blade went snicker-snack!\n'
                'He left it dead, and with its head\n'
                '  He went galumphing back.\n'
                '"And, has thou slain the Jabberwock?\n'
                '  Come to my arms, my beamish boy!\n'
                'O frabjous day! Callooh! Callay!\'\n'
                '  He chortled in his joy.\n'
                '\n'
                '\n'
                '\n'
                '`Twas brillig, and the slithy toves\n'
                '  Did gyre and gimble in the wabe;\n'
                'All mimsy were the borogoves,\n'
                '  And the mome raths outgrabe.'
            }
          }
  Actual: {
            'strings': '  This text should be ignored\\\\n  ',
            'script': '<script>alert("YES")</script>',
            'string': [
              'Zooi',
              'Tab\\tText',
              'Quote "test"',
              'Backslash \\ test',
              'Quote " en \\ test',
              '"Forsooth! It \'tis as I thought!"',
              '"Begin/end quote"'
            ],
            'escape': 'Line one (één in Dutch)\\\\nLine two:\\ttab\\\\nHe said: "Much fun with a €"\\\\nMore unicode†',
            'poetry': 'JABBERWOCKY\\\\n\\\\nLewis Carroll\\\\n\\\\n(from Through the Looking-Glass and What Alice Found There, 1872)\\\\n`Twas brillig, and the slithy toves\\\\n  Did gyre and gimble in the wabe:\\\\nAll mimsy were the borogoves,\\\\n  And the mome raths outgrabe.\\\\n\\\\n \\\\n\\\\n"Beware the Jabberwock, my son!\\\\n  The jaws that bite, the claws that catch!\\\\nBeware the Jubjub bird, and shun\\\\n  The frumious Bandersnatch!"\\\\nHe took his vorpal sword in hand:\\\\n  Long time the manxome foe he sought --\\\\nSo rested he by the Tumtum tree,\\\\n  And stood awhile in thought.\\\\nAnd, as in uffish thought he stood,\\\\n  The Jabberwock, with eyes of flame,\\\\nCame whiffling through the tulgey wood,\\\\n  And burbled as it came!\\\\nOne, two! One, two! And through and through\\\\n  The vorpal blade went snicker-snack!\\\\nHe left it dead, and with its head\\\\n  He went galumphing back.\\\\n"And, has thou slain the Jabberwock?\\\\n  Come to my arms, my beamish boy!\\\\nO frabjous day! Callooh! Callay!\'\\\\n  He chortled in his joy.\\\\n\\\\n\\\\n\\\\n`Twas brillig, and the slithy toves\\\\n  Did gyre and gimble in the wabe;\\\\nAll mimsy were the borogoves,\\\\n  And the mome raths outgrabe.'
          }
   Which: has different length and has extra map key 'script'

package:test_api            expect
test/parker_test.dart 33:5  testXmlFile.<fn>

Expected: {
            'types': {
              'true-boolean': true,
              'false-boolean': false,
              'object': {'zooi': 'jaja', 'zut': 'frot'},
              'array': [null, true, 2, 3, 4, 5, 'six', {'key': 'item', 'value': 7}]
            }
          }
  Actual: {
            'types': {
              'true-boolean': 'true',
              'false-boolean': 'false',
              'object': {'zooi': 'jaja', 'zut': 'frot'},
              'array': {'item': ['true', '2', '3', '4', '5', 'six', {'key': 'item', 'value': '7'}]}
            }
          }
   Which: at location ['types']['true-boolean'] is 'true' instead of <true>

package:test_api            expect
test/parker_test.dart 33:5  testXmlFile.<fn>

Expected: {
            'text': '\n'
              '  \n'
              '<div id="test">\n'
              '<span>This is "text", &lt;dude&gt;.</span>\n'
              '<span>c:\\windows</span>\n'
              '</div>\n'
              '\n'
              ''
          }
  Actual: {
            'text': '\\\\n<div id="test"><span>This is "text", &lt;dude&gt;.</span><span>c:\\windows</span></div>'
          }
   Which: at location ['text'] is '\\\\n<div id="test"><span>This is "text", &lt;dude&gt;.</span><span>c:\\windows</span></div>' instead of '\n'
            '  \n'
            '<div id="test">\n'
            '<span>This is "text", &lt;dude&gt;.</span>\n'
            '<span>c:\\windows</span>\n'
            '</div>\n'
            '\n'
            ''

Process finished with exit code 1
shamblett commented 1 year ago

Hi thanks for this, I'll look at this, maybe I can be clearer in the readme if nothing else.

shamblett commented 1 year ago

Closing, no further action taken.