#!/usr/bin/env python

"""Unit test for our escape and unescape functions."""

# This was working fine on 2.x but not 3.x, when we were using base64

import sys

import escape_mod
import helpers


def main():
    """Run the tests."""
    all_good = True
    for string in [
            '/abc',
            '/def/ghi',
            '/jkl/mno/',
            'abc',
            helpers.string_to_binary('abc'),
            'abc\ndef\nghi',
            'abc def ghi',
            'abc def\nghi',
            'abc%def%ghi',
            'abc%20 %%',
    ]:

        encoded = escape_mod.escape(string)
        decoded = escape_mod.unescape(encoded)

        stb_string = helpers.string_to_binary(string)
        if stb_string == decoded:
            sys.stdout.write('good\n')
        else:
            sys.stdout.write('bad: %s, %s\n' % (stb_string, decoded))
            all_good = False

    if not all_good:
        sys.stderr.write('%s: One or more tests failed\n' % sys.argv[0])
        sys.exit(1)


main()