Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cros / init_unittest.py
1 #!/usr/bin/python
2 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """This module tests the command module."""
7
8 from __future__ import print_function
9
10 import argparse
11 import os
12 import sys
13
14 sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
15
16 from chromite.lib import cros_test_lib
17 from chromite import cros
18
19 _COMMAND_NAME = 'superAwesomeCommandOfFunness'
20
21
22 @cros.CommandDecorator(_COMMAND_NAME)
23 class TestCommand(cros.CrosCommand):
24   """A fake command."""
25   def Run(self):
26     print('Just testing')
27
28
29 # pylint: disable=W0212
30 class CommandTest(cros_test_lib.TestCase):
31   """This test class tests that Commands method."""
32
33   def testParserSetsCrosClass(self):
34     """Tests that our parser sets cros_class correctly."""
35     my_parser = argparse.ArgumentParser()
36     cros.CrosCommand.AddParser(my_parser)
37     ns = my_parser.parse_args([])
38     self.assertEqual(ns.cros_class, cros.CrosCommand)
39
40   def testCommandDecorator(self):
41     """Tests that our decorator correctly adds TestCommand to _commands."""
42     # Note this exposes an implementation detail of _commands.
43     self.assertEqual(cros._commands[_COMMAND_NAME], TestCommand)
44
45   def testBadUseOfCommandDecorator(self):
46     """Tests that our decorator correctly rejects bad test commands."""
47     try:
48       # pylint: disable=W0612
49       @cros.CommandDecorator('bad')
50       class BadTestCommand(object):
51         """A command that wasn't implemented correctly."""
52         pass
53
54     except cros.InvalidCommandError:
55       pass
56     else:
57       self.fail('Invalid command was accepted by the CommandDecorator')
58
59
60 if __name__ == '__main__':
61   cros_test_lib.main()