mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/maintenance/autotest/common.py
00001 # -*- coding: utf-8 -*-
00002 
00003 # Mathmaker creates automatically maths exercises sheets
00004 # with their answers
00005 # Copyright 2006-2014 Nicolas Hainaux <nico_h@users.sourceforge.net>
00006 
00007 # This file is part of Mathmaker.
00008 
00009 # Mathmaker is free software; you can redistribute it and/or modify
00010 # it under the terms of the GNU General Public License as published by
00011 # the Free Software Foundation; either version 3 of the License, or
00012 # any later version.
00013 
00014 # Mathmaker is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 
00019 # You should have received a copy of the GNU General Public License
00020 # along with Mathmaker; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 import os
00024 import sys
00025 import gettext
00026 import string
00027 from optparse import OptionParser
00028 from lib import *
00029 from core.base_calculus import *
00030 from core.calculus import *
00031 import sheet
00032 import machine
00033 
00034 
00035 
00036 pathname = os.path.dirname(sys.argv[0])
00037 localdir = os.path.abspath(pathname) + "/locale"
00038 
00039 tested_language = 'en'
00040 
00041 machines = [machine.LaTeX(tested_language)]
00042 
00043 output = sys.stderr.fileno()
00044 err_output = sys.stderr.fileno()
00045 
00046 OK = "."
00047 FAILED = "×"
00048 MISSING = "!"
00049 verbose = False
00050 verbose_space = ""
00051 superverbose = False
00052 last_was_OK = False
00053 counter = 0
00054 global_counter = 0
00055 failed_counter = 0
00056 missing_counter = 0
00057 section = ""
00058 superverbose_opening_token = ""
00059 superverbose_closing_token = ""
00060 superverbose_counter = 0
00061 
00062 def check(t, given_string):
00063     global global_counter, counter, last_was_OK, superverbose_counter,\
00064            failed_counter, missing_counter
00065     counter += 1
00066     superverbose_counter += 1
00067     global_counter += 1
00068 
00069     computed_string = []
00070 
00071     if isinstance(t, Printable):
00072         for i in range(len(machines)):
00073             computed_string.append(machines[i].type_string(t))
00074     else:
00075         computed_string.append(str(t))
00076 
00077     if not isinstance(t, Printable):
00078         if superverbose:
00079             n = str(counter)
00080         else:
00081             n = ""
00082         if superverbose_counter == 10 and verbose:
00083             next_line = "\n"
00084             superverbose_counter = 0
00085         else:
00086             next_line = ""
00087         if counter < 10 and superverbose:
00088             add_space = "  "
00089         elif counter < 100 and superverbose:
00090             add_space = " "
00091         else:
00092             add_space = ""
00093 
00094         if computed_string[0].replace("\n", "") == \
00095                                              given_string[0].replace("\n", ""):
00096 
00097             os.write(output, bytes(superverbose_opening_token + add_space \
00098                              + n + verbose_space + OK \
00099                              + superverbose_closing_token \
00100                              + next_line, 'utf-8'))
00101             last_was_OK = True
00102         else:
00103             if verbose:
00104                 os.write(output,
00105                      bytes("\n" + superverbose_opening_token + add_space \
00106                      + str(counter) + " "                         \
00107                      + FAILED                                                 \
00108                      + '"' + computed_string[0].replace("\n", "") + '"'                         \
00109                      + "\nshould have been\n"                                 \
00110                      + '"' + given_string[0].replace("\n", "") + '"' + "\n",
00111                      'utf-8'))
00112             else:
00113                 os.write(output, bytes(FAILED, 'utf-8'))
00114 
00115             failed_counter += 1
00116             last_was_OK = False
00117 
00118     else:
00119         for i in range(len(machines)):
00120             if len(given_string) >= i + 1:
00121                 if superverbose:
00122                     n = str(counter)
00123                 else:
00124                     n = ""
00125                 if superverbose_counter == 10 and verbose:
00126                     next_line = "\n"
00127                     superverbose_counter = 0
00128                 else:
00129                     next_line = ""
00130                 if counter < 10 and superverbose:
00131                     add_space = "  "
00132                 elif counter < 100 and superverbose:
00133                     add_space = " "
00134                 else:
00135                     add_space = ""
00136 
00137                 if computed_string[i].replace("\n", "") == \
00138                                              given_string[i].replace("\n", ""):
00139 
00140                     os.write(output, bytes(superverbose_opening_token \
00141                                      + add_space \
00142                                      + n + verbose_space + OK \
00143                                      + superverbose_closing_token \
00144                                      + next_line, 'utf-8'))
00145                     last_was_OK = True
00146                 else:
00147                     if verbose:
00148                         os.write(output,
00149                                  bytes("\n" + superverbose_opening_token \
00150                                  + add_space + str(counter) + " "            \
00151                                  + FAILED                                     \
00152                                  + '"' \
00153                                  + computed_string[i].replace("\n", "") \
00154                                  + '"'             \
00155                                  + "\nshould have been\n"                     \
00156                                  + '"' + given_string[i].replace("\n", "") \
00157                                  + '"' + "\n", 'utf-8'))
00158                     else:
00159                         os.write(output, bytes(FAILED, 'utf-8'))
00160 
00161                     failed_counter += 1
00162                     last_was_OK = False
00163 
00164             else:
00165                 if verbose:
00166                     os.write(output,
00167                                  bytes("\n" + "[# " + str(counter) + " "      \
00168                                  + MISSING                                    \
00169                                  + "more machines than refs strings."
00170                                  + "\n", 'utf-8'))
00171                 else:
00172                     os.write(output, bytes(MISSING, 'utf-8'))
00173 
00174                 missing_counter += 1
00175                 last_was_OK = False
00176 
00177         if len(given_string) > len(machines):
00178             if verbose:
00179                 os.write(output,
00180                          bytes("\n" + "# " + str(counter) + " "                     \
00181                          + MISSING                                            \
00182                          + "more refs strings than machines."
00183                          + "\n", 'utf-8'))
00184             else:
00185                 os.write(output, bytes(MISSING, 'utf-8'))
00186 
00187             missing_counter += 1
00188             last_was_OK = False
00189 
00190 def short_test_run(language):
00191     M = machine.LaTeX(language)
00192 
00193     for sheet_key in sheet.AVAILABLE:
00194         M.write(str(sheet_key[0](M)))
00195 
00196 
00197 def long_test_run(n, language):
00198     for i in range(n):
00199         short_test_run(language)
00200 
00201 
00202 def language_test(language):
00203     global tested_language
00204     try:
00205         gettext.translation(software.NAME,
00206                             localdir,
00207                             [language]).install()
00208     except IOError as msg:
00209         tested_language = 'en'
00210         gettext.translation(software.NAME,
00211                             localdir,
00212                             ['en']).install()
00213 
00214         print("error to check in source code of autotest/__init__()")