# -*- coding: utf-8 -*-

from __future__ import division, unicode_literals, print_function, absolute_import

from pint.util import (UnitsContainer)
from pint.converters import (ScaleConverter, OffsetConverter)
from pint.definitions import (Definition, PrefixDefinition, UnitDefinition,
                              DimensionDefinition, AliasDefinition)

from pint.testsuite import BaseTestCase

class TestDefinition(BaseTestCase):

    def test_invalid(self):
        self.assertRaises(ValueError, Definition.from_string, 'x = [time] * meter')
        self.assertRaises(ValueError, Definition.from_string, '[x] = [time] * meter')

    def test_prefix_definition(self):
        for definition in ('m- = 1e-3', 'm- = 10**-3', 'm- = 0.001'):
            x = Definition.from_string(definition)
            self.assertIsInstance(x, PrefixDefinition)
            self.assertEqual(x.name, 'm')
            self.assertEqual(x.aliases, ())
            self.assertEqual(x.converter.to_reference(1000), 1)
            self.assertEqual(x.converter.from_reference(0.001), 1)
            self.assertEqual(str(x), 'm')

        x = Definition.from_string('kilo- = 1e-3 = k-')
        self.assertIsInstance(x, PrefixDefinition)
        self.assertEqual(x.name, 'kilo')
        self.assertEqual(x.aliases, ())
        self.assertEqual(x.symbol, 'k')
        self.assertEqual(x.converter.to_reference(1000), 1)
        self.assertEqual(x.converter.from_reference(.001), 1)

        x = Definition.from_string('kilo- = 1e-3 = k- = anotherk-')
        self.assertIsInstance(x, PrefixDefinition)
        self.assertEqual(x.name, 'kilo')
        self.assertEqual(x.aliases, ('anotherk', ))
        self.assertEqual(x.symbol, 'k')
        self.assertEqual(x.converter.to_reference(1000), 1)
        self.assertEqual(x.converter.from_reference(.001), 1)

    def test_baseunit_definition(self):
        x = Definition.from_string('meter = [length]')
        self.assertIsInstance(x, UnitDefinition)
        self.assertTrue(x.is_base)
        self.assertEqual(x.reference, UnitsContainer({'[length]': 1}))

    def test_unit_definition(self):
        x = Definition.from_string('coulomb = ampere * second')
        self.assertIsInstance(x, UnitDefinition)
        self.assertFalse(x.is_base)
        self.assertIsInstance(x.converter, ScaleConverter)
        self.assertEqual(x.converter.scale, 1)
        self.assertEqual(x.reference, UnitsContainer(ampere=1, second=1))

        x = Definition.from_string('faraday =  96485.3399 * coulomb')
        self.assertIsInstance(x, UnitDefinition)
        self.assertFalse(x.is_base)
        self.assertIsInstance(x.converter, ScaleConverter)
        self.assertEqual(x.converter.scale,  96485.3399)
        self.assertEqual(x.reference, UnitsContainer(coulomb=1))

        x = Definition.from_string('degF = 9 / 5 * kelvin; offset: 255.372222')
        self.assertIsInstance(x, UnitDefinition)
        self.assertFalse(x.is_base)
        self.assertIsInstance(x.converter, OffsetConverter)
        self.assertEqual(x.converter.scale, 9/5)
        self.assertEqual(x.converter.offset, 255.372222)
        self.assertEqual(x.reference, UnitsContainer(kelvin=1))

        x = Definition.from_string('turn = 6.28 * radian = _ = revolution = = cycle = _')
        self.assertIsInstance(x, UnitDefinition)
        self.assertEqual(x.name, 'turn')
        self.assertEqual(x.aliases, ('revolution', 'cycle'))
        self.assertEqual(x.symbol, 'turn')
        self.assertFalse(x.is_base)
        self.assertIsInstance(x.converter, ScaleConverter)
        self.assertEqual(x.converter.scale, 6.28)
        self.assertEqual(x.reference, UnitsContainer(radian=1))

    def test_dimension_definition(self):
        x = DimensionDefinition('[time]', '', (), converter='')
        self.assertTrue(x.is_base)
        self.assertEqual(x.name, '[time]')

        x = Definition.from_string('[speed] = [length]/[time]')
        self.assertIsInstance(x, DimensionDefinition)
        self.assertEqual(x.reference, UnitsContainer({'[length]': 1, '[time]': -1}))

    def test_alias_definition(self):
        x = Definition.from_string("@alias meter = metro = metr")
        self.assertIsInstance(x, AliasDefinition)
        self.assertEqual(x.name, "meter")
        self.assertEqual(x.aliases, ("metro", "metr"))

[((1118, 1161), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""kilo- = 1e-3 = k-"""'], {}), "('kilo- = 1e-3 = k-')\n", (1140, 1161), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((1469, 1524), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""kilo- = 1e-3 = k- = anotherk-"""'], {}), "('kilo- = 1e-3 = k- = anotherk-')\n", (1491, 1524), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((1884, 1926), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""meter = [length]"""'], {}), "('meter = [length]')\n", (1906, 1926), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((2131, 2182), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""coulomb = ampere * second"""'], {}), "('coulomb = ampere * second')\n", (2153, 2182), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((2461, 2518), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""faraday =  96485.3399 * coulomb"""'], {}), "('faraday =  96485.3399 * coulomb')\n", (2483, 2518), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((2798, 2865), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""degF = 9 / 5 * kelvin; offset: 255.372222"""'], {}), "('degF = 9 / 5 * kelvin; offset: 255.372222')\n", (2820, 2865), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((3194, 3271), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""turn = 6.28 * radian = _ = revolution = = cycle = _"""'], {}), "('turn = 6.28 * radian = _ = revolution = = cycle = _')\n", (3216, 3271), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((3729, 3780), 'pint.definitions.DimensionDefinition', 'DimensionDefinition', (['"""[time]"""', '""""""', '()'], {'converter': '""""""'}), "('[time]', '', (), converter='')\n", (3748, 3780), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((3872, 3923), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""[speed] = [length]/[time]"""'], {}), "('[speed] = [length]/[time]')\n", (3894, 3923), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((4113, 4166), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['"""@alias meter = metro = metr"""'], {}), "('@alias meter = metro = metr')\n", (4135, 4166), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((756, 790), 'pint.definitions.Definition.from_string', 'Definition.from_string', (['definition'], {}), '(definition)\n', (778, 790), False, 'from pint.definitions import Definition, PrefixDefinition, UnitDefinition, DimensionDefinition, AliasDefinition\n'), ((2049, 2080), 'pint.util.UnitsContainer', 'UnitsContainer', (["{'[length]': 1}"], {}), "({'[length]': 1})\n", (2063, 2080), False, 'from pint.util import UnitsContainer\n'), ((2412, 2446), 'pint.util.UnitsContainer', 'UnitsContainer', ([], {'ampere': '(1)', 'second': '(1)'}), '(ampere=1, second=1)\n', (2426, 2446), False, 'from pint.util import UnitsContainer\n'), ((2758, 2783), 'pint.util.UnitsContainer', 'UnitsContainer', ([], {'coulomb': '(1)'}), '(coulomb=1)\n', (2772, 2783), False, 'from pint.util import UnitsContainer\n'), ((3155, 3179), 'pint.util.UnitsContainer', 'UnitsContainer', ([], {'kelvin': '(1)'}), '(kelvin=1)\n', (3169, 3179), False, 'from pint.util import UnitsContainer\n'), ((3649, 3673), 'pint.util.UnitsContainer', 'UnitsContainer', ([], {'radian': '(1)'}), '(radian=1)\n', (3663, 3673), False, 'from pint.util import UnitsContainer\n'), ((4016, 4061), 'pint.util.UnitsContainer', 'UnitsContainer', (["{'[length]': 1, '[time]': -1}"], {}), "({'[length]': 1, '[time]': -1})\n", (4030, 4061), False, 'from pint.util import UnitsContainer\n')]