mathmaker  0.4(alpha)
mathmaker_dev/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 xrange(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, superverbose_opening_token + add_space \
00098                              + n + verbose_space + OK \
00099                              + superverbose_closing_token \
00100                              + next_line)
00101             last_was_OK = True
00102         else:
00103             if verbose:
00104                 os.write(output,
00105                      "\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             else:
00112                 os.write(output, FAILED)
00113 
00114             failed_counter += 1
00115             last_was_OK = False
00116 
00117     else:
00118         for i in xrange(len(machines)):
00119             if len(given_string) >= i + 1:
00120                 if superverbose:
00121                     n = str(counter)
00122                 else:
00123                     n = ""
00124                 if superverbose_counter == 10 and verbose:
00125                     next_line = "\n"
00126                     superverbose_counter = 0
00127                 else:
00128                     next_line = ""
00129                 if counter < 10 and superverbose:
00130                     add_space = "  "
00131                 elif counter < 100 and superverbose:
00132                     add_space = " "
00133                 else:
00134                     add_space = ""
00135 
00136                 if computed_string[i].replace("\n", "") == \
00137                                              given_string[i].replace("\n", ""):
00138 
00139                     os.write(output, superverbose_opening_token + add_space \
00140                                      + n + verbose_space + OK \
00141                                      + superverbose_closing_token \
00142                                      + next_line)
00143                     last_was_OK = True
00144                 else:
00145                     if verbose:
00146                         os.write(output,
00147                                  "\n" + superverbose_opening_token \
00148                                  + add_space + str(counter) + " "            \
00149                                  + FAILED                                     \
00150                                  + '"' \
00151                                  + computed_string[i].replace("\n", "") \
00152                                  + '"'             \
00153                                  + "\nshould have been\n"                     \
00154                                  + '"' + given_string[i].replace("\n", "") \
00155                                  + '"' + "\n")
00156                     else:
00157                         os.write(output, FAILED)
00158 
00159                     failed_counter += 1
00160                     last_was_OK = False
00161 
00162             else:
00163                 if verbose:
00164                     os.write(output,
00165                                  "\n" + "[# " + str(counter) + " "            \
00166                                  + MISSING                                    \
00167                                  + "more machines than refs strings."
00168                                  + "\n")
00169                 else:
00170                     os.write(output, MISSING)
00171 
00172                 missing_counter += 1
00173                 last_was_OK = False
00174 
00175         if len(given_string) > len(machines):
00176             if verbose:
00177                 os.write(output,
00178                          "\n" + "# " + str(counter) + " "                     \
00179                          + MISSING                                            \
00180                          + "more refs strings than machines."
00181                          + "\n")
00182             else:
00183                 os.write(output, MISSING)
00184 
00185             missing_counter += 1
00186             last_was_OK = False
00187 
00188 def short_test_run(language):
00189     M = machine.LaTeX(language)
00190 
00191     for sheet_key in sheet.AVAILABLE:
00192         M.write(str(sheet_key[0](M)))
00193 
00194 
00195 def long_test_run(n, language):
00196     for i in xrange(n):
00197         short_test_run(language)
00198 
00199 
00200 def language_test(language):
00201     global tested_language
00202     try:
00203         gettext.translation(software.NAME,
00204                             localdir,
00205                             [language]).install()
00206     except IOError, msg:
00207         tested_language = 'en'
00208         gettext.translation(software.NAME,
00209                             localdir,
00210                             ['en']).install()
00211 
00212         print "error to check in source code of autotest/__init__()"