def test_regex(self):
    for regex_instance in (re.compile('a*b', re.IGNORECASE), Regex('a*b', re.IGNORECASE)):
        res = self.round_tripped({'r': regex_instance})['r']
        self.assertEqual('a*b', res.pattern)
        res = self.round_tripped({'r': Regex('a*b', re.IGNORECASE)})['r']
        self.assertEqual('a*b', res.pattern)
        self.assertEqual(re.IGNORECASE, res.flags)
    unicode_options = ((((re.I | re.M) | re.S) | re.U) | re.X)
    regex = re.compile('a*b', unicode_options)
    res = self.round_tripped({'r': regex})['r']
    self.assertEqual(unicode_options, res.flags)
    res = json_util.loads('{"r": {"$regex": "a*b"}}')['r']
    self.assertEqual(0, res.flags)
    self.assertEqual(Regex('.*', 'ilm'), json_util.loads('{"r": {"$regex": ".*", "$options": "ilm"}}')['r'])
    self.assertEqual('{"$regex": ".*", "$options": "mx"}', json_util.dumps(Regex('.*', (re.M | re.X))))
    self.assertEqual('{"$regex": ".*", "$options": "mx"}', json_util.dumps(re.compile(b'.*', (re.M | re.X))))
def test_regex(self):
    for regex_instance in (re.compile('a*b', re.IGNORECASE), Regex('a*b', re.IGNORECASE)):
        res = self.round_tripped({'r': regex_instance})['r']
        self.assertEqual('a*b', res.pattern)
        res = self.round_tripped({'r': Regex('a*b', re.IGNORECASE)})['r']
        self.assertEqual('a*b', res.pattern)
        self.assertEqual(re.IGNORECASE, res.flags)
    unicode_options = ((((re.I | re.M) | re.S) | re.U) | re.X)
    regex = re.compile('a*b', unicode_options)
    res = self.round_tripped({'r': regex})['r']
    self.assertEqual(unicode_options, res.flags)
    res = json_util.loads('{"r": {"$regex": "a*b"}}')['r']
    self.assertEqual(0, res.flags)
    self.assertEqual(Regex('.*', 'ilm'), json_util.loads('{"r": {"$regex": ".*", "$options": "ilm"}}')['r'])
    self.assertEqual('{"$regex": ".*", "$options": "mx"}', json_util.dumps(Regex('.*', (re.M | re.X))))
    self.assertEqual('{"$regex": ".*", "$options": "mx"}', json_util.dumps(re.compile(b'.*', (re.M | re.X))))