def test_programs(self):
        program1 = dsl.Concat(dsl.GetToken(dsl.Type.ALPHANUM, 3),
                              dsl.GetFrom(':'), dsl.GetFirst(dsl.Type.CHAR, 4))
        self.assertEqual(program1('Ud 9:25,JV3 Obb'), '2525,JV3 ObbUd 9')
        self.assertEqual(program1('zLny xmHg 8:43 A44q'), '843 A44qzLny')

        program2 = dsl.Concat(
            dsl.Compose(
                dsl.Replace(' ', ','),
                dsl.GetSpan(dsl.Type.PROP_CASE, 1, dsl.Boundary.START,
                            dsl.Type.PROP_CASE, 4, dsl.Boundary.END)),
            dsl.ConstStr('.'), dsl.GetToken(dsl.Type.PROP_CASE, -1))
        self.assertEqual(program2('Jacob Ethan James Alexander Michael'),
                         'Jacob,Ethan,James,Alexander.Michael')
        self.assertEqual(program2('Earth Fire Wind Water Pluto Sun'),
                         'Earth,Fire,Wind,Water.Sun')
    def test_decode(self):
        id_token_table, token_id_table = tokens.build_token_tables()
        self.assertEqual(len(token_id_table), len(id_token_table))
        program = dsl.Concat(
            dsl.Compose(
                dsl.Replace(' ', ','),
                dsl.GetSpan(dsl.Type.PROP_CASE, 1, dsl.Boundary.START,
                            dsl.Type.PROP_CASE, 4, dsl.Boundary.END)),
            dsl.ConstStr('.'), dsl.GetToken(dsl.Type.PROP_CASE, -1))
        encoding = program.encode(token_id_table)
        self.assertEqual(encoding[-1], token_id_table[dsl.EOS])

        decoded_program = dsl.decode_program(encoding, id_token_table)
        self.assertEqual(
            decoded_program('Jacob Ethan James Alexander Michael'),
            'Jacob,Ethan,James,Alexander.Michael')
        self.assertEqual(decoded_program('Earth Fire Wind Water Pluto Sun'),
                         'Earth,Fire,Wind,Water.Sun')
Exemple #3
0
def random_replace(inputs, delimiter_dict, type_dict):
  del inputs, type_dict
  return dsl.Replace(random_delimiter(delimiter_dict),
                     random.choice(dsl.DELIMITER))