summaryrefslogtreecommitdiffstats
path: root/tests/auto/qscriptvalue/testgen/gen.py
blob: fddb85388e59e8541b1cdb826d70c73cf621b9d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)

#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Library General Public
#License as published by the Free Software Foundation; either
#version 2 of the License, or (at your option) any later version.

#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Library General Public License for more details.

#You should have received a copy of the GNU Library General Public License
#along with this library; see the file COPYING.LIB.  If not, write to
#the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#Boston, MA 02110-1301, USA.

from __future__ import with_statement
from string import Template

class Options():
  """Option manager. It parse and check all paramteres, set internal variables."""
  def __init__(self, args):
    import logging as log
    log.basicConfig()
    #comand line options parser
    from optparse import OptionParser
    #load some directory searching stuff
    import os.path, sys
      
    opt = OptionParser("%prog [options] path_to_input_file path_to_output_file.")
    
    self._o, self._a = opt.parse_args(args)

    try:
      if not (os.path.exists(self._a[0])):
        raise Exception("Path doesn't exist")
      if len(self._a) != 2:
        raise IndexError("Only two files!")
      self._o.ipath = self._a[0]
      self._o.opath = self._a[1]
    except IndexError:
      log.error("Bad usage. Please try -h or --help")
      sys.exit(1)
    except Exception:
      log.error("Path '" + self._a[0] + " or " + self._a[1] + "' don't exist")
      sys.exit(2)

  def __getattr__(self, attr):
    """map all options properties into this object (remove one level of indirection)"""
    return getattr(self._o, attr)


mainTempl = Template("""/*
  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

////////////////////////////////////////////////////////////////
// THIS FILE IS AUTOGENERATED, ALL MODIFICATIONS WILL BE LAST //
////////////////////////////////////////////////////////////////

#include "testgenerator.h"

#include <QtCore/qdatastream.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qnumeric.h>
#include <QtCore/qvariant.h>
#include <QtCore/qvector.h>
#include <QtScript/qscriptvalue.h>
#include <QtScript/qscriptengine.h>



typedef bool (QScriptValue::*ComparisionType) (const QScriptValue&) const;
static QVector<bool> compare(ComparisionType compare, QScriptValue value, const QScriptValueList& values) {
    QVector<bool> result;
    result.reserve(${count});

    QScriptValueList::const_iterator i = values.constBegin();
    for (; i != values.constEnd(); ++i) {
        result << (value.*compare)(*i);
    }
    return result;
}

static void dump(QDataStream& out, QScriptValue& value, const QString& expression, const QScriptValueList& allValues)
{
        out << QString(expression);
        
        out << value.isValid();
        out << value.isBool();
        out << value.isBoolean();
        out << value.isNumber();
        out << value.isFunction();
        out << value.isNull();
        out << value.isString();
        out << value.isUndefined();
        out << value.isVariant();
        out << value.isQObject();
        out << value.isQMetaObject();
        out << value.isObject();
        out << value.isDate();
        out << value.isRegExp();
        out << value.isArray();
        out << value.isError();

        out << value.toString();
        out << value.toNumber();
        out << value.toBool();
        out << value.toBoolean();
        out << value.toInteger();
        out << value.toInt32();
        out << value.toUInt32();
        out << value.toUInt16();

        out << compare(&QScriptValue::equals, value, allValues);
        out << compare(&QScriptValue::strictlyEquals, value, allValues);
        out << compare(&QScriptValue::lessThan, value, allValues);
        out << compare(&QScriptValue::instanceOf, value, allValues);

        out << qscriptvalue_cast<QString>(value);
        out << qscriptvalue_cast<qsreal>(value);
        out << qscriptvalue_cast<bool>(value);
        out << qscriptvalue_cast<qint32>(value);
        out << qscriptvalue_cast<quint32>(value);
        out << qscriptvalue_cast<quint16>(value);
}

void TestGenerator::prepareData()
{
    QScriptEngine* engine = new QScriptEngine;

    QScriptValueList allValues;
    allValues << ${values};
    QVector<QString> allDataTags;
    allDataTags.reserve(${count});
    allDataTags << ${dataTags};
    QDataStream out(&m_tempFile);
    out << allDataTags;

    for(unsigned i = 0; i < ${count}; ++i)
      dump(out, allValues[i], allDataTags[i], allValues);

    delete engine;
}
""")
qsvTempl = Template("""
    {
        QScriptValue value = ${expr};
        dump(out, value, "${expr_esc}", allValues);
    }""")



if __name__ == '__main__':
  import sys
  o = Options(sys.argv[1:])
  out = []
  qsv = []
  # load input file
  with open(o.ipath) as f:
    for row in f.readlines():
      qsv.append(row)

  #skip comments and empty lines
  qsv = filter(lambda w: len(w.strip()) and not w.startswith('#'), qsv)
  
  escape = lambda w: w.replace('\\','\\\\').replace('"','\\"')
  
  for row in qsv:
    row = row.replace('\n','')
    row_esc = escape(row)
    out.append(qsvTempl.substitute(expr = row, expr_esc = row_esc))

  result = mainTempl.substitute(dump= "".join(out) \
                              , values = (11 * ' ' + '<< ').join(qsv) \
                              , count = len(qsv) \
                              , dataTags = (11 * ' ' + '<< ').join(map(lambda w: '"' + escape(w.replace('\n','')) + '"\n', qsv)))

  with open(o.opath, 'w') as f:
    f.write(result)