|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# The MIT License (MIT) |
| 3 | +# |
| 4 | +# Copyright © 2014 Tim Bielawa <[email protected]> |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person |
| 7 | +# obtaining a copy of this software and associated documentation files |
| 8 | +# (the "Software"), to deal in the Software without restriction, |
| 9 | +# including without limitation the rights to use, copy, modify, merge, |
| 10 | +# publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | +# and to permit persons to whom the Software is furnished to do so, |
| 12 | +# subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be |
| 15 | +# included in all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +# SOFTWARE. |
| 25 | + |
| 26 | + |
| 27 | +""" |
| 28 | +Test the click 'Bitmath' type integration |
| 29 | +""" |
| 30 | + |
| 31 | +from . import TestCase |
| 32 | +import bitmath |
| 33 | +from bitmath.integrations.bmclick import BitmathType, BITMATH |
| 34 | +import click |
| 35 | +from click.testing import CliRunner |
| 36 | + |
| 37 | + |
| 38 | +class TestClickType(TestCase): |
| 39 | + def setUp(self): |
| 40 | + self.runner = CliRunner() |
| 41 | + |
| 42 | + def test_click_BitmathType_good_one_arg(self): |
| 43 | + @click.command() |
| 44 | + @click.argument('arg', type=BitmathType()) |
| 45 | + def func(arg): |
| 46 | + click.echo(arg) |
| 47 | + |
| 48 | + result = self.runner.invoke(func, ['1000EB']) |
| 49 | + self.assertFalse(result.exception) |
| 50 | + self.assertEqual(result.output.splitlines(), [str(bitmath.EB(1000))]) |
| 51 | + |
| 52 | + def test_click_BitmathType_good_one_opt(self): |
| 53 | + @click.command() |
| 54 | + @click.option('--opt', type=BitmathType()) |
| 55 | + def func(opt): |
| 56 | + click.echo(opt) |
| 57 | + |
| 58 | + result = self.runner.invoke(func, ['--opt', '1007TB']) |
| 59 | + self.assertFalse(result.exception) |
| 60 | + self.assertEqual(result.output.splitlines(), [str(bitmath.TB(1007))]) |
| 61 | + |
| 62 | + def test_click_BitmathType_good_two_args(self): |
| 63 | + @click.command() |
| 64 | + @click.argument('arg1', type=BitmathType()) |
| 65 | + @click.argument('arg2', type=BitmathType()) |
| 66 | + def func(arg1, arg2): |
| 67 | + click.echo(arg1) |
| 68 | + click.echo(arg2) |
| 69 | + |
| 70 | + result = self.runner.invoke(func, ['1337B', '0.001GiB']) |
| 71 | + self.assertFalse(result.exception) |
| 72 | + self.assertEqual(result.output.splitlines(), [str(bitmath.Byte(1337)), |
| 73 | + str(bitmath.GiB(0.001))]) |
| 74 | + |
| 75 | + def test_click_BitmathType_bad_wtfareyoudoing(self): |
| 76 | + @click.command() |
| 77 | + @click.argument('arg', type=BitmathType()) |
| 78 | + def func(arg): |
| 79 | + click.echo(arg) |
| 80 | + |
| 81 | + result = self.runner.invoke(func, ['2098329324kdsjflksdjf']) |
| 82 | + self.assertTrue(result.exception) |
| 83 | + |
| 84 | + def test_click_BitmathType_good_spaces_in_value(self): |
| 85 | + @click.command() |
| 86 | + @click.argument('arg1', type=BitmathType()) |
| 87 | + @click.argument('arg2', type=BitmathType()) |
| 88 | + def func(arg1, arg2): |
| 89 | + click.echo(arg1) |
| 90 | + click.echo(arg2) |
| 91 | + |
| 92 | + result = self.runner.invoke(func, ['100 MiB', '200 KiB']) |
| 93 | + self.assertFalse(result.exception) |
| 94 | + self.assertEqual(result.output.splitlines(), [str(bitmath.MiB(100)), |
| 95 | + str(bitmath.KiB(200))]) |
| 96 | + |
| 97 | + def test_click_BitmathType_bad_spaces_in_value(self): |
| 98 | + @click.command() |
| 99 | + @click.argument('arg', type=BitmathType()) |
| 100 | + def func(arg): |
| 101 | + click.echo(arg) |
| 102 | + |
| 103 | + result = self.runner.invoke(func, ['1000', 'EB']) |
| 104 | + self.assertTrue(result.exception) |
| 105 | + |
| 106 | + def test_click_BITMATH_good_one_arg(self): |
| 107 | + @click.command() |
| 108 | + @click.argument('arg', type=BITMATH) |
| 109 | + def func(arg): |
| 110 | + click.echo(arg) |
| 111 | + |
| 112 | + result = self.runner.invoke(func, ['1234.5 TiB']) |
| 113 | + self.assertFalse(result.exception) |
| 114 | + self.assertEqual(result.output.splitlines(), [str(bitmath.TiB(1234.5))]) |
0 commit comments