How to use the cmd2.utils.cast function in cmd2

To help you get started, we’ve selected a few cmd2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast():
    # Boolean
    assert utils.cast(True, True) == True
    assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast():
    # Boolean
    assert utils.cast(True, True) == True
    assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast():
    # Boolean
    assert utils.cast(True, True) == True
    assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
# Boolean
    assert utils.cast(True, True) == True
    assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast_problems(capsys):
    expected = 'Problem setting parameter (now {}) to {}; incorrect type?\n'

    # Boolean current, with new value not convertible to bool
    current = True
    new = [True, True]
    assert utils.cast(current, new) == current
    out, err = capsys.readouterr()
    assert out == expected.format(current, new)

    # Non-boolean current, with new value not convertible to current type
    current = 1
    new = 'octopus'
    assert utils.cast(current, new) == current
    out, err = capsys.readouterr()
    assert out == expected.format(current, new)
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast():
    # Boolean
    assert utils.cast(True, True) == True
    assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast():
    # Boolean
    assert utils.cast(True, True) == True
    assert utils.cast(True, False) == False
    assert utils.cast(True, 0) == False
    assert utils.cast(True, 1) == True
    assert utils.cast(True, 'on') == True
    assert utils.cast(True, 'off') == False
    assert utils.cast(True, 'ON') == True
    assert utils.cast(True, 'OFF') == False
    assert utils.cast(True, 'y') == True
    assert utils.cast(True, 'n') == False
    assert utils.cast(True, 't') == True
    assert utils.cast(True, 'f') == False

    # Non-boolean same type
    assert utils.cast(1, 5) == 5
    assert utils.cast(3.4, 2.7) == 2.7
    assert utils.cast('foo', 'bar') == 'bar'
    assert utils.cast([1,2], [3,4]) == [3,4]
github python-cmd2 / cmd2 / tests / test_cmd2.py View on Github external
def test_cast_problems(capsys):
    expected = 'Problem setting parameter (now {}) to {}; incorrect type?\n'

    # Boolean current, with new value not convertible to bool
    current = True
    new = [True, True]
    assert utils.cast(current, new) == current
    out, err = capsys.readouterr()
    assert out == expected.format(current, new)

    # Non-boolean current, with new value not convertible to current type
    current = 1
    new = 'octopus'
    assert utils.cast(current, new) == current
    out, err = capsys.readouterr()
    assert out == expected.format(current, new)