tests/auto/qscriptvalue/testgen/gen.py
branchRCL_3
changeset 5 d3bac044e0f0
child 8 3f74d0d4af4c
equal deleted inserted replaced
4:3b1da2848fc7 5:d3bac044e0f0
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
       
     5 
       
     6 ## $QT_BEGIN_LICENSE:LGPL$
       
     7 ## No Commercial Usage
       
     8 ## This file contains pre-release code and may not be distributed.
       
     9 ## You may use this file in accordance with the terms and conditions
       
    10 ## contained in the Technology Preview License Agreement accompanying
       
    11 ## this package.
       
    12 ##
       
    13 ## GNU Lesser General Public License Usage
       
    14 ## Alternatively, this file may be used under the terms of the GNU Lesser
       
    15 ## General Public License version 2.1 as published by the Free Software
       
    16 ## Foundation and appearing in the file LICENSE.LGPL included in the
       
    17 ## packaging of this file.  Please review the following information to
       
    18 ## ensure the GNU Lesser General Public License version 2.1 requirements
       
    19 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    20 ##
       
    21 ## In addition, as a special exception, Nokia gives you certain additional
       
    22 ## rights.  These rights are described in the Nokia Qt LGPL Exception
       
    23 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    24 ##
       
    25 ## If you have questions regarding the use of this file, please contact
       
    26 ## Nokia at qt-info@nokia.com.
       
    27 ##
       
    28 ##
       
    29 ##
       
    30 ##
       
    31 ##
       
    32 ##
       
    33 ##
       
    34 ##
       
    35 ## $QT_END_LICENSE$
       
    36 
       
    37 from __future__ import with_statement
       
    38 from string import Template
       
    39 
       
    40 class Options():
       
    41   """Option manager. It parse and check all paramteres, set internal variables."""
       
    42   def __init__(self, args):
       
    43     import logging as log
       
    44     log.basicConfig()
       
    45     #comand line options parser
       
    46     from optparse import OptionParser
       
    47     #load some directory searching stuff
       
    48     import os.path, sys
       
    49       
       
    50     opt = OptionParser("%prog [options] path_to_input_file path_to_output_file.")
       
    51     
       
    52     self._o, self._a = opt.parse_args(args)
       
    53 
       
    54     try:
       
    55       if not (os.path.exists(self._a[0])):
       
    56         raise Exception("Path doesn't exist")
       
    57       if len(self._a) != 2:
       
    58         raise IndexError("Only two files!")
       
    59       self._o.ipath = self._a[0]
       
    60       self._o.opath = self._a[1]
       
    61     except IndexError:
       
    62       log.error("Bad usage. Please try -h or --help")
       
    63       sys.exit(1)
       
    64     except Exception:
       
    65       log.error("Path '" + self._a[0] + " or " + self._a[1] + "' don't exist")
       
    66       sys.exit(2)
       
    67 
       
    68   def __getattr__(self, attr):
       
    69     """map all options properties into this object (remove one level of indirection)"""
       
    70     return getattr(self._o, attr)
       
    71 
       
    72 
       
    73 mainTempl = Template("""/*
       
    74 /****************************************************************************
       
    75 **
       
    76 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
    77 ** All rights reserved.
       
    78 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
    79 **
       
    80 ** This file is part of the test suite of the Qt Toolkit.
       
    81 **
       
    82 ** $QT_BEGIN_LICENSE:LGPL$
       
    83 ** No Commercial Usage
       
    84 ** This file contains pre-release code and may not be distributed.
       
    85 ** You may use this file in accordance with the terms and conditions
       
    86 ** contained in the Technology Preview License Agreement accompanying
       
    87 ** this package.
       
    88 **
       
    89 ** GNU Lesser General Public License Usage
       
    90 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    91 ** General Public License version 2.1 as published by the Free Software
       
    92 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    93 ** packaging of this file.  Please review the following information to
       
    94 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    95 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    96 **
       
    97 ** In addition, as a special exception, Nokia gives you certain additional
       
    98 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    99 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
   100 **
       
   101 ** If you have questions regarding the use of this file, please contact
       
   102 ** Nokia at qt-info@nokia.com.
       
   103 **
       
   104 **
       
   105 **
       
   106 **
       
   107 **
       
   108 **
       
   109 **
       
   110 **
       
   111 ** $QT_END_LICENSE$
       
   112 **
       
   113 ****************************************************************************/
       
   114 
       
   115 ////////////////////////////////////////////////////////////////
       
   116 // THIS FILE IS AUTOGENERATED, ALL MODIFICATIONS WILL BE LAST //
       
   117 ////////////////////////////////////////////////////////////////
       
   118 
       
   119 #include "testgenerator.h"
       
   120 
       
   121 #include <QtCore/qdatastream.h>
       
   122 #include <QtCore/qdatetime.h>
       
   123 #include <QtCore/qdebug.h>
       
   124 #include <QtCore/qfile.h>
       
   125 #include <QtCore/qnumeric.h>
       
   126 #include <QtCore/qvariant.h>
       
   127 #include <QtCore/qvector.h>
       
   128 #include <QtScript/qscriptvalue.h>
       
   129 #include <QtScript/qscriptengine.h>
       
   130 
       
   131 
       
   132 
       
   133 typedef bool (QScriptValue::*ComparisionType) (const QScriptValue&) const;
       
   134 static QVector<bool> compare(ComparisionType compare, QScriptValue value, const QScriptValueList& values) {
       
   135     QVector<bool> result;
       
   136     result.reserve(${count});
       
   137 
       
   138     QScriptValueList::const_iterator i = values.constBegin();
       
   139     for (; i != values.constEnd(); ++i) {
       
   140         result << (value.*compare)(*i);
       
   141     }
       
   142     return result;
       
   143 }
       
   144 
       
   145 static void dump(QDataStream& out, QScriptValue& value, const QString& expression, const QScriptValueList& allValues)
       
   146 {
       
   147         out << QString(expression);
       
   148         
       
   149         out << value.isValid();
       
   150         out << value.isBool();
       
   151         out << value.isBoolean();
       
   152         out << value.isNumber();
       
   153         out << value.isFunction();
       
   154         out << value.isNull();
       
   155         out << value.isString();
       
   156         out << value.isUndefined();
       
   157         out << value.isVariant();
       
   158         out << value.isQObject();
       
   159         out << value.isQMetaObject();
       
   160         out << value.isObject();
       
   161         out << value.isDate();
       
   162         out << value.isRegExp();
       
   163         out << value.isArray();
       
   164         out << value.isError();
       
   165 
       
   166         out << value.toString();
       
   167         out << value.toNumber();
       
   168         out << value.toBool();
       
   169         out << value.toBoolean();
       
   170         out << value.toInteger();
       
   171         out << value.toInt32();
       
   172         out << value.toUInt32();
       
   173         out << value.toUInt16();
       
   174 
       
   175         out << compare(&QScriptValue::equals, value, allValues);
       
   176         out << compare(&QScriptValue::strictlyEquals, value, allValues);
       
   177         out << compare(&QScriptValue::lessThan, value, allValues);
       
   178         out << compare(&QScriptValue::instanceOf, value, allValues);
       
   179 
       
   180         out << qscriptvalue_cast<QString>(value);
       
   181         out << qscriptvalue_cast<qsreal>(value);
       
   182         out << qscriptvalue_cast<bool>(value);
       
   183         out << qscriptvalue_cast<qint32>(value);
       
   184         out << qscriptvalue_cast<quint32>(value);
       
   185         out << qscriptvalue_cast<quint16>(value);
       
   186 }
       
   187 
       
   188 void TestGenerator::prepareData()
       
   189 {
       
   190     QScriptEngine* engine = new QScriptEngine;
       
   191 
       
   192     QScriptValueList allValues;
       
   193     allValues << ${values};
       
   194     QVector<QString> allDataTags;
       
   195     allDataTags.reserve(${count});
       
   196     allDataTags << ${dataTags};
       
   197     QDataStream out(&m_tempFile);
       
   198     out << allDataTags;
       
   199 
       
   200     for(unsigned i = 0; i < ${count}; ++i)
       
   201       dump(out, allValues[i], allDataTags[i], allValues);
       
   202 
       
   203     delete engine;
       
   204 }
       
   205 """)
       
   206 qsvTempl = Template("""
       
   207     {
       
   208         QScriptValue value = ${expr};
       
   209         dump(out, value, "${expr_esc}", allValues);
       
   210     }""")
       
   211 
       
   212 
       
   213 
       
   214 if __name__ == '__main__':
       
   215   import sys
       
   216   o = Options(sys.argv[1:])
       
   217   out = []
       
   218   qsv = []
       
   219   # load input file
       
   220   with open(o.ipath) as f:
       
   221     for row in f.readlines():
       
   222       qsv.append(row)
       
   223 
       
   224   #skip comments and empty lines
       
   225   qsv = filter(lambda w: len(w.strip()) and not w.startswith('#'), qsv)
       
   226   
       
   227   escape = lambda w: w.replace('\\','\\\\').replace('"','\\"')
       
   228   
       
   229   for row in qsv:
       
   230     row = row.replace('\n','')
       
   231     row_esc = escape(row)
       
   232     out.append(qsvTempl.substitute(expr = row, expr_esc = row_esc))
       
   233 
       
   234   result = mainTempl.substitute(dump= "".join(out) \
       
   235                               , values = (11 * ' ' + '<< ').join(qsv) \
       
   236                               , count = len(qsv) \
       
   237                               , dataTags = (11 * ' ' + '<< ').join(map(lambda w: '"' + escape(w.replace('\n','')) + '"\n', qsv)))
       
   238 
       
   239   with open(o.opath, 'w') as f:
       
   240     f.write(result)
       
   241 
       
   242