# Path: scripnix/pybin/common_netmask.py
# COMMAND_NAME = "common-netmask"
# 
# def find_common_netmask_and_length(ip_addresses):
#     """ Return a tuple of the 4-tuple netmask and mask length that most completely matches the given list of dotted-quad IPv4 address
#         strings.
#     """
#     quad_addresses = [quads_from_dotted_ip(ia) for ia in ip_addresses]
#     bit_addresses = [bits_from_quads(qa) for qa in quad_addresses]
#     netmask_bits = []
# 
#     for bit_posn in range(IP_ADDRESS_BIT_WIDTH):
#         bit_set = {ba[bit_posn] for ba in bit_addresses}
# 
#         if len(bit_set) == 1:
#             # All of the bits at position bit_posn are identical, so add that bit to the netmask.
#             netmask_bits.append(bit_set.pop())
#         else:
#             break
# 
#     netmask = "".join(netmask_bits).ljust(IP_ADDRESS_BIT_WIDTH, "0")
#     return quads_from_bits(netmask), len(netmask_bits)
# 
# def format_netmask_and_length(netmask, length):
#     """ Return a string of the form a.b.c.d/e for the given 4-tuple netmask (a,b,c,d) and mask length (e).
#     """
#     return "{}/{}".format(".".join(str(n) for n in netmask), length)
# 
# @common_command_and_options(command_name=COMMAND_NAME)
# @click.argument("ipaddr", nargs=-1)
# def main(ipaddr):
#     """ Given any number of full or partial dotted-quad IPv4 addresses, display the netmask (and bit length) that matches all of the
#         addresses.
# 
#         The common-netmask command is part of Scripnix.
#     """
#     if ipaddr:
#         click.echo(format_netmask_and_length(*find_common_netmask_and_length(ip_addresses=ipaddr)))
# 
# Path: tests/command.py
# def common_help_option(command_entry, command_name):
#     """ Test help message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = HELP_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
# 
#     version_option_switch("-h")
#     version_option_switch("--help")
# 
# def common_version_option(command_entry, command_name):
#     """ Test version message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = VERSION_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
#         assert match.group(2) == __version__
# 
#     version_option_switch("-V")
#     version_option_switch("--version")


import click
import pytest
from click.testing import CliRunner
from scripnix.pybin.common_netmask import COMMAND_NAME, find_common_netmask_and_length, format_netmask_and_length, main
from .command import common_help_option, common_version_option
    with pytest.raises(click.ClickException) as excinfo:
        find_common_netmask_and_length(["10.10.257.10"])

    assert "Invalid quad value '257' in IP address" in str(excinfo)


def test_invalid_quad_length_common_netmask():
    with pytest.raises(click.ClickException) as excinfo:
        find_common_netmask_and_length(["10.10.10.10.10"])

    assert "More than four quads in IP address" in str(excinfo)


@pytest.mark.parametrize('arguments,expected', [
    ([], ""),
    (["128", "0"], "0.0.0.0/0"),
    (["255", "128"], "128.0.0.0/1"),
    (["192.168.20.0", "192.168.21.0"], "192.168.20.0/23"),
    (["76.9.32.0", "76.9.63.255"], "76.9.32.0/19"),
])
def test_main(arguments, expected):
    runner = CliRunner()
    arguments_copy = arguments[:]
    result = runner.invoke(main, arguments)
    assert arguments_copy == arguments
    assert result.exit_code == 0
    assert result.output.strip() == expected


def test_version_option():

# Path: scripnix/pybin/common_netmask.py
# COMMAND_NAME = "common-netmask"
# 
# def find_common_netmask_and_length(ip_addresses):
#     """ Return a tuple of the 4-tuple netmask and mask length that most completely matches the given list of dotted-quad IPv4 address
#         strings.
#     """
#     quad_addresses = [quads_from_dotted_ip(ia) for ia in ip_addresses]
#     bit_addresses = [bits_from_quads(qa) for qa in quad_addresses]
#     netmask_bits = []
# 
#     for bit_posn in range(IP_ADDRESS_BIT_WIDTH):
#         bit_set = {ba[bit_posn] for ba in bit_addresses}
# 
#         if len(bit_set) == 1:
#             # All of the bits at position bit_posn are identical, so add that bit to the netmask.
#             netmask_bits.append(bit_set.pop())
#         else:
#             break
# 
#     netmask = "".join(netmask_bits).ljust(IP_ADDRESS_BIT_WIDTH, "0")
#     return quads_from_bits(netmask), len(netmask_bits)
# 
# def format_netmask_and_length(netmask, length):
#     """ Return a string of the form a.b.c.d/e for the given 4-tuple netmask (a,b,c,d) and mask length (e).
#     """
#     return "{}/{}".format(".".join(str(n) for n in netmask), length)
# 
# @common_command_and_options(command_name=COMMAND_NAME)
# @click.argument("ipaddr", nargs=-1)
# def main(ipaddr):
#     """ Given any number of full or partial dotted-quad IPv4 addresses, display the netmask (and bit length) that matches all of the
#         addresses.
# 
#         The common-netmask command is part of Scripnix.
#     """
#     if ipaddr:
#         click.echo(format_netmask_and_length(*find_common_netmask_and_length(ip_addresses=ipaddr)))
# 
# Path: tests/command.py
# def common_help_option(command_entry, command_name):
#     """ Test help message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = HELP_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
# 
#     version_option_switch("-h")
#     version_option_switch("--help")
# 
# def common_version_option(command_entry, command_name):
#     """ Test version message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = VERSION_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
#         assert match.group(2) == __version__
# 
#     version_option_switch("-V")
#     version_option_switch("--version")


# Path: tests/test_common_netmask.py
import click
import pytest
from click.testing import CliRunner
from scripnix.pybin.common_netmask import COMMAND_NAME, find_common_netmask_and_length, format_netmask_and_length, main
from .command import common_help_option, common_version_option

    with pytest.raises(click.ClickException) as excinfo:
        find_common_netmask_and_length(["10.10.257.10"])

    assert "Invalid quad value '257' in IP address" in str(excinfo)


def test_invalid_quad_length_common_netmask():
    with pytest.raises(click.ClickException) as excinfo:
        find_common_netmask_and_length(["10.10.10.10.10"])

    assert "More than four quads in IP address" in str(excinfo)


@pytest.mark.parametrize('arguments,expected', [
    ([], ""),
    (["128", "0"], "0.0.0.0/0"),
    (["255", "128"], "128.0.0.0/1"),
    (["192.168.20.0", "192.168.21.0"], "192.168.20.0/23"),
    (["76.9.32.0", "76.9.63.255"], "76.9.32.0/19"),
])
def test_main(arguments, expected):
    runner = CliRunner()
    arguments_copy = arguments[:]
    result = runner.invoke(main, arguments)
    assert arguments_copy == arguments
    assert result.exit_code == 0
    assert result.output.strip() == expected


def test_version_option():

    common_version_option(command_entry=main, command_name=COMMAND_NAME)