Repository: google/python-fire
File: fire/fire_test.py (Python)

Code before:
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import fire
from fire import test_components as tc
from fire import trace
from fire.core_test import BaseTest

import six
import unittest


class FireTest(BaseTest):
  def testFire(self):
    fire.Fire(tc.Empty, '')
    fire.Fire(tc.OldStyleEmpty, '')
    fire.Fire(tc.WithInit, '')
    self.assertEqual(fire.Fire(tc.NoDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'triple 4'), 12)

  def testFireNoArgs(self):
    self.assertEqual(fire.Fire(tc.MixedDefaults, 'ten'), 10)

  def testFireExceptions(self):
    # Exceptions of Fire are printed to stderr and a FireExit is raised.
    with self.assertRaises(fire.FireExit):
      fire.Fire(tc.Empty, 'nomethod')  # Member doesn't exist.
    with self.assertRaises(fire.FireExit):
      fire.Fire(tc.NoDefaults, 'double')  # Missing argument.
    with self.assertRaises(fire.FireExit):
      fire.Fire(tc.TypedProperties, 'delta x')  # Missing key.

    # Exceptions of the target components are still raised.
    with self.assertRaises(ZeroDivisionError):
      fire.Fire(tc.NumberDefaults, 'reciprocal 0.0')


Review comment (suggestion):
otherwise these error about `--ignore-files` from the nosetests command

Code after:
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import fire
from fire import test_components as tc
from fire import testutils
from fire import trace

import mock
import six
import sys
import unittest


class FireTest(testutils.BaseTestCase):
  def testFire(self):
    with mock.patch.object(sys, 'argv', ['progname']):
      fire.Fire(tc.Empty)
      fire.Fire(tc.OldStyleEmpty)
      fire.Fire(tc.WithInit)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'triple 4'), 12)

  def testFireNoArgs(self):
    self.assertEqual(fire.Fire(tc.MixedDefaults, 'ten'), 10)

  def testFireExceptions(self):
    # Exceptions of Fire are printed to stderr and a FireExit is raised.
    with self.assertRaises(fire.FireExit):
      fire.Fire(tc.Empty, 'nomethod')  # Member doesn't exist.
    with self.assertRaises(fire.FireExit):
      fire.Fire(tc.NoDefaults, 'double')  # Missing argument.
    with self.assertRaises(fire.FireExit):
      fire.Fire(tc.TypedProperties, 'delta x')  # Missing key.

    # Exceptions of the target components are still raised.