"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/Tests/TemplateCmdLineIface.py" (20 Apr 2021, 2483 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "TemplateCmdLineIface.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3-3.2.2_vs_3-3.2.3.

    1 try:
    2     from io import BytesIO, TextIOWrapper
    3 except ImportError:
    4     try:
    5         from cStringIO import StringIO as BytesIO
    6     except ImportError:
    7         from StringIO import StringIO as BytesIO
    8 
    9 from tempfile import NamedTemporaryFile
   10 
   11 import os
   12 import pickle
   13 import sys
   14 import unittest
   15 
   16 from Cheetah.Template import Template
   17 from Cheetah.TemplateCmdLineIface import CmdLineIface
   18 
   19 
   20 class TestHelp(unittest.TestCase):
   21     def setUp(self):
   22         self.NULL = NULL = open(os.devnull, 'w')
   23         sys.stdout = NULL
   24 
   25     def tearDown(self):
   26         self.NULL.close()
   27         sys.stdout = sys.__stdout__
   28 
   29     def test_help(self):
   30         klass = Template.compile(source='$foo')
   31         t = klass()
   32         cmdline = CmdLineIface(t, scriptName='test', cmdLineArgs=['-h'])
   33         self.assertRaises(SystemExit, cmdline._processCmdLineArgs)
   34 
   35 
   36 class TestEnv(unittest.TestCase):
   37     def setUp(self):
   38         os.environ['foo'] = 'test foo'
   39 
   40     def tearDown(self):
   41         del os.environ['foo']
   42 
   43     def test_env(self):
   44         klass = Template.compile(source='$foo')
   45         t = klass()
   46         cmdline = CmdLineIface(t, scriptName='test', cmdLineArgs=['--env'])
   47         cmdline._processCmdLineArgs()
   48         assert str(t) == 'test foo'
   49 
   50 
   51 class TestPickleStdin(unittest.TestCase):
   52     def setUp(self):
   53         pickle_bytes = pickle.dumps({'foo': 'test foo'})
   54         if hasattr(sys.stdin, 'buffer'):
   55             sys.stdin = TextIOWrapper(BytesIO(pickle_bytes))
   56         else:
   57             sys.stdin = BytesIO(pickle_bytes)
   58 
   59     def tearDown(self):
   60         sys.stdin = sys.__stdin__
   61 
   62     def test_pickle_stdin(self):
   63         klass = Template.compile(source='$foo')
   64         t = klass()
   65         cmdline = CmdLineIface(t, scriptName='test',
   66                                cmdLineArgs=['--pickle=-'])
   67         cmdline._processCmdLineArgs()
   68         assert str(t) == 'test foo'
   69 
   70 
   71 class TestPickleFile(unittest.TestCase):
   72     def setUp(self):
   73         self.pickle_file = pickle_file = \
   74             NamedTemporaryFile(mode='wb', delete=False)
   75         pickle.dump({'foo': 'test foo'}, pickle_file)
   76         pickle_file.close()
   77 
   78     def tearDown(self):
   79         os.remove(self.pickle_file.name)
   80 
   81     def test_pickle_file(self):
   82         klass = Template.compile(source='$foo')
   83         t = klass()
   84         cmdline = CmdLineIface(t, scriptName='test',
   85                                cmdLineArgs=['--pickle', self.pickle_file.name])
   86         cmdline._processCmdLineArgs()
   87         assert str(t) == 'test foo'