<commit_before>from __future__ import with_statement
import StringIO
import time
from robot.result.jsondatamodel import DataModel

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        json = None
import unittest
import xml.sax as sax

from robot.result.jsparser import _RobotOutputHandler
from robot.result.json import json_dump
from robot.result.elementhandlers import Context
from robot.utils.asserts import assert_equals, assert_true, assert_not_equals, assert_false


# TODO: Split this monster test suite.
# At least json_dump tests should be moved to their own module.

class TestJsSerializer(unittest.TestCase):

    SUITE_XML = """
<suite source="/tmp/verysimple.txt" name="Verysimple">
  <doc>*html* &lt;esc&gt; http://x.y http://x.y/z.jpg</doc>
  <metadata>
    <item name="key">val</item>
    <item name="esc">&lt;</item>
    <item name="html">http://x.y.x.jpg</item>
  </metadata>
  <test name="Test" timeout="">
    <doc>*html* &lt;esc&gt; http://x.y http://x.y/z.jpg</doc>
    <kw type="kw" name="Keyword.Example" timeout="1 second">
      <doc>*html* &lt;esc&gt; http://x.y http://x.y/z.jpg</doc>
      <arguments><arg>a1</arg><arg>a2</arg></arguments>
      <msg timestamp="20110601 12:01:51.353" level="WARN">simple</msg>
      <status status="PASS" endtime="20110601 12:01:51.353" starttime="20110601 12:01:51.376"></status>
    </kw>
    <tags><tag>t1</tag><tag>t2</tag></tags>
    <status status="PASS" endtime="20110601 12:01:51.354" critical="yes" starttime="20110601 12:01:51.353"></status>
  </test>
  <status status="PASS" endtime="20110601 12:01:51.354" starttime="20110601 12:01:51.329"></status>
</suite>"""

    FOR_LOOP_XML = """
        <kw type="for" name="${i} IN RANGE [ 2 ]" timeout="">
            <doc></doc>
            <arguments></arguments>
            <kw type="foritem" name="${i} = 0" timeout="">
                <doc></doc>
                <arguments></arguments>
                <kw type="kw" name="babba" timeout="">
                    <doc>Foo bar.</doc>
                    <arguments>
                        <arg>${i}</arg>
                    </arguments>
                    <msg timestamp="20110617 09:56:04.365" level="INFO">0</msg>
                    <status status="PASS" endtime="20110617 09:56:04.365" starttime="20110617 09:56:04.365"></status>
                </kw>
                <status status="PASS" endtime="20110617 09:56:04.365" starttime="20110617 09:56:04.365"></status>
            </kw>
            <kw type="foritem" name="${i} = 1" timeout="">
                <doc></doc>
                <arguments></arguments>
                <kw type="kw" name="babba" timeout="">
                    <doc>Foo bar.</doc>
                    <arguments>
                        <arg>${i}</arg>
                    </arguments>
                    <msg timestamp="20110617 09:56:04.366" level="INFO">1</msg>
                    <status status="PASS" endtime="20110617 09:56:04.366" starttime="20110617 09:56:04.366"></status>
                </kw>
                <status status="PASS" endtime="20110617 09:56:04.367" starttime="20110617 09:56:04.366"></status>
            </kw>
            <kw type="foritem" name="${i} = 2" timeout="">
                <doc></doc>
                <arguments></arguments>
                <kw type="kw" name="babba" timeout="">
                    <doc>Foo bar.</doc>
                    <arguments>
                        <arg>${i}</arg>
                    </arguments>
                    <msg timestamp="20110617 09:56:04.367" level="INFO">2</msg>
                    <status status="PASS" endtime="20110617 09:56:04.367" starttime="20110617 09:56:04.367"></status>
                </kw>
                <status status="PASS" endtime="20110617 09:56:04.368" starttime="20110617 09:56:04.367"></status>
            </kw>
            <status status="PASS" endtime="20110617 09:56:04.368" starttime="20110617 09:56:04.364"></status>
        </kw>
        """

    def setUp(self):
        self._context = Context()
        self._handler = _RobotOutputHandler(self._context)

    def test_message_xml_parsing(self):
        data_model = self._get_data_model('<msg timestamp="20110531 12:48:09.088" level="FAIL">AssertionError</msg>')
        self.assert_model(data_model,
                          1306835289088,
                          [-1, 1, 2],
                          ['*', '*F', '*AssertionError'],
                          [0])

    def test_plain_message_xml_parsing(self):
        data_model = self._get_data_model('<msg timestamp="20110531 12:48:09.088" level="FAIL">AssertionError</msg>')
        self.assert_model(data_model, basemillis=1306835289088, plain_suite=[0, '*F', '*AssertionError'])

    def assert_model(self, data_model, basemillis=None, suite=None, strings=None, integers=None, plain_suite=None):
        if basemillis is not None:
            assert_equals(data_model._robot_data['baseMillis'], basemillis)
        if suite is not None:
            assert_equals(data_model._robot_data['suite'], suite)
        if strings is not None:
            assert_equals(data_model._robot_data['strings'], strings)
        if integers is not None:
            assert_equals(data_model._robot_data['integers'], integers)
        if plain_suite is not None:
            self._assert_plain_suite(data_model, plain_suite)

    def assert_model_does_not_contain(self, data_model, items):
        suite = self._reverse_from_ids(data_model,
                                       data_model._robot_data['suite'],
                                       data_model._index_remap)
        self._check_does_not_contain(suite, ['*'+i for i in items])

    def _check_does_not_contain(self, suite, items):
        for item in suite:
            assert_true(item not in items)
            if isinstance(item, list):
                self._check_does_not_contain(item, items)

    def _assert_plain_suite(self, data_model, plain_suite):
        reversed = self._reverse_from_ids(data_model, data_model._robot_data['suite'])
        assert_equals(reversed, plain_suite)

    def _reverse_from_ids(self, data, item, remap={}):
        recurse = self._reverse_from_ids
        if isinstance(item, (int, long)):
            if item in remap:
                item = remap[item]
            return self._reverse_id(data, item)
        if isinstance(item, list):
            return [recurse(data, i, remap) for i in item]
        if isinstance(item, dict):
            return dict((recurse(data, k, remap),
                         recurse(data, item[k], remap)) for k in item)
        raise AssertionError('Unexpected item %r' % item)

    def _reverse_id(self, data_model, id):
        if id is None:
            return None
        elif id < 0:
            return data_model._robot_data['integers'][abs(id)-1]<commit_msg>oops, should run unit tests between the last modification and commit<commit_after>from __future__ import with_statement
import StringIO
import time
from robot.result.jsondatamodel import DataModel

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        json = None
import unittest
import xml.sax as sax

from robot.result.jsparser import _RobotOutputHandler
from robot.result.json import json_dump
from robot.result.elementhandlers import Context
from robot.utils.asserts import assert_equals, assert_true, assert_not_equals, assert_false


# TODO: Split this monster test suite.
# At least json_dump tests should be moved to their own module.

class TestJsSerializer(unittest.TestCase):

    SUITE_XML = """
<suite source="/tmp/verysimple.txt" name="Verysimple">
  <doc>*html* &lt;esc&gt; http://x.y http://x.y/z.jpg</doc>
  <metadata>
    <item name="key">val</item>
    <item name="esc">&lt;</item>
    <item name="html">http://x.y.x.jpg</item>
  </metadata>
  <test name="Test" timeout="">
    <doc>*html* &lt;esc&gt; http://x.y http://x.y/z.jpg</doc>
    <kw type="kw" name="Keyword.Example" timeout="1 second">
      <doc>*html* &lt;esc&gt; http://x.y http://x.y/z.jpg</doc>
      <arguments><arg>a1</arg><arg>a2</arg></arguments>
      <msg timestamp="20110601 12:01:51.353" level="WARN">simple</msg>
      <status status="PASS" endtime="20110601 12:01:51.353" starttime="20110601 12:01:51.376"></status>
    </kw>
    <tags><tag>t1</tag><tag>t2</tag></tags>
    <status status="PASS" endtime="20110601 12:01:51.354" critical="yes" starttime="20110601 12:01:51.353"></status>
  </test>
  <status status="PASS" endtime="20110601 12:01:51.354" starttime="20110601 12:01:51.329"></status>
</suite>"""

    FOR_LOOP_XML = """
        <kw type="for" name="${i} IN RANGE [ 2 ]" timeout="">
            <doc></doc>
            <arguments></arguments>
            <kw type="foritem" name="${i} = 0" timeout="">
                <doc></doc>
                <arguments></arguments>
                <kw type="kw" name="babba" timeout="">
                    <doc>Foo bar.</doc>
                    <arguments>
                        <arg>${i}</arg>
                    </arguments>
                    <msg timestamp="20110617 09:56:04.365" level="INFO">0</msg>
                    <status status="PASS" endtime="20110617 09:56:04.365" starttime="20110617 09:56:04.365"></status>
                </kw>
                <status status="PASS" endtime="20110617 09:56:04.365" starttime="20110617 09:56:04.365"></status>
            </kw>
            <kw type="foritem" name="${i} = 1" timeout="">
                <doc></doc>
                <arguments></arguments>
                <kw type="kw" name="babba" timeout="">
                    <doc>Foo bar.</doc>
                    <arguments>
                        <arg>${i}</arg>
                    </arguments>
                    <msg timestamp="20110617 09:56:04.366" level="INFO">1</msg>
                    <status status="PASS" endtime="20110617 09:56:04.366" starttime="20110617 09:56:04.366"></status>
                </kw>
                <status status="PASS" endtime="20110617 09:56:04.367" starttime="20110617 09:56:04.366"></status>
            </kw>
            <kw type="foritem" name="${i} = 2" timeout="">
                <doc></doc>
                <arguments></arguments>
                <kw type="kw" name="babba" timeout="">
                    <doc>Foo bar.</doc>
                    <arguments>
                        <arg>${i}</arg>
                    </arguments>
                    <msg timestamp="20110617 09:56:04.367" level="INFO">2</msg>
                    <status status="PASS" endtime="20110617 09:56:04.367" starttime="20110617 09:56:04.367"></status>
                </kw>
                <status status="PASS" endtime="20110617 09:56:04.368" starttime="20110617 09:56:04.367"></status>
            </kw>
            <status status="PASS" endtime="20110617 09:56:04.368" starttime="20110617 09:56:04.364"></status>
        </kw>
        """

    def setUp(self):
        self._context = Context()
        self._handler = _RobotOutputHandler(self._context)

    def test_message_xml_parsing(self):
        data_model = self._get_data_model('<msg timestamp="20110531 12:48:09.088" level="FAIL">AssertionError</msg>')
        self.assert_model(data_model,
                          1306835289088,
                          [-1, 1, 2],
                          ['*', '*F', '*AssertionError'],
                          [0])

    def test_plain_message_xml_parsing(self):
        data_model = self._get_data_model('<msg timestamp="20110531 12:48:09.088" level="FAIL">AssertionError</msg>')
        self.assert_model(data_model, basemillis=1306835289088, plain_suite=[0, '*F', '*AssertionError'])

    def assert_model(self, data_model, basemillis=None, suite=None, strings=None, integers=None, plain_suite=None):
        if basemillis is not None:
            assert_equals(data_model._robot_data['baseMillis'], basemillis)
        if suite is not None:
            assert_equals(data_model._robot_data['suite'], suite)
        if strings is not None:
            assert_equals(data_model._robot_data['strings'], strings)
        if integers is not None:
            assert_equals(data_model._robot_data['integers'], integers)
        if plain_suite is not None:
            self._assert_plain_suite(data_model, plain_suite)

    def assert_model_does_not_contain(self, data_model, items):
        suite = self._reverse_from_ids(data_model,
                                       data_model._robot_data['suite'],
                                       data_model._index_remap)
        self._check_does_not_contain(suite, ['*'+i for i in items])

    def _check_does_not_contain(self, suite, items):
        for item in suite:
            assert_true(item not in items)
            if isinstance(item, list):
                self._check_does_not_contain(item, items)

    def _assert_plain_suite(self, data_model, plain_suite):
        reversed = self._reverse_from_ids(data_model, data_model._robot_data['suite'])
        assert_equals(reversed, plain_suite)

    def _reverse_from_ids(self, data, item, remap={}):
        if item is None:
            return None
        if isinstance(item, (int, long)):
            if item in remap:
                item = remap[item]
            return self._reverse_id(data, item)
        recurse = self._reverse_from_ids
        if isinstance(item, list):
            return [recurse(data, i, remap) for i in item]
        if isinstance(item, dict):
            return dict((recurse(data, k, remap),
                         recurse(data, item[k], remap)) for k in item)
        raise AssertionError('Unexpected item %r' % item)

    def _reverse_id(self, data_model, id):
        if id is None:
            return None
        elif id < 0:
            return data_model._robot_data['integers'][abs(id)-1]
{'fraction_non_alphanumeric': 0.14195037507212926, 'fraction_numerical': 0.0702538949798038, 'mean_word_length': 2.2910040351293617, 'pattern_counts': {'":': 0, '<': 229, '<?xml version=': 0, '>': 227, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6419748', 'n_tokens_mistral': 4969, 'n_tokens_neox': 4204, 'n_words': 875}