d separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works.  But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

  3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

    a) Accompany it with the complete corresponding machine-readable
    source code, which must be distributed under the terms of Sections
    1 and 2 above on a medium customarily used for software interchange; or,

    b) Accompany it with a written offer, valid for at least three
    years, to give any third party, for a charge no more than your
    cost of physically performing source distribution, a complete
    machine-readable copy of the corresponding source code, to be
    distributed under the terms of Sections 1 and 2 above on a medium
    customarily used for software interchange; or,

    c) Accompany it with the information you received as to the offer
    to distribute corresponding source code.  (This alternative is
    allowed only for noncommercial distribution and only if you
    received the program in object code or executable form with such
    an"""Tests for the coalescestreams preprocessor"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from nbformat import v4 as nbformat

from .base import PreprocessorTestsBase
from ..coalescestreams import coalesce_streams


class TestCoalesceStreams(PreprocessorTestsBase):
    """Contains test functions for coalescestreams.py"""

    def test_coalesce_streams(self):
        """coalesce_streams preprocessor output test"""
        nb = self.build_notebook()
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, "a")
        self.assertEqual(outputs[1].output_type, "display_data")
        self.assertEqual(outputs[2].text, "cd")
        self.assertEqual(outputs[3].text, "ef")

    def test_coalesce_sequenced_streams(self):
        """Can the coalesce streams preprocessor merge a sequence of streams?"""
        outputs = [nbformat.new_output(output_type="stream", name="stdout", text="0"),
                   nbformat.new_output(output_type="stream", name="stdout", text="1"),
                   nbformat.new_output(output_type="stream", name="stdout", text="2"),
                   nbformat.new_output(output_type="stream", name="stdout", text="3"),
                   nbformat.new_output(output_type="stream", name="stdout", text="4"),
                   nbformat.new_output(output_type="stream", name="stdout", text="5"),
                   nbformat.new_output(output_type="stream", name="stdout", text="6"),
                   nbformat.new_output(output_type="stream", name="stdout", text="7")]
        cells=[nbformat.new_code_cell(source="# None", execution_count=1,outputs=outputs)]

        nb = nbformat.new_notebook(cells=cells)
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, u'01234567')

    def test_coalesce_replace_streams(self):
        """Are \\r characters handled?"""
        outputs = [nbformat.new_output(output_type="stream", name="stdout", text="z"),
                   nbformat.new_output(output_type="stream", name="stdout", text="\ra"),
                   nbformat.new_output(output_type="stream", name="stdout", text="\nz\rb"),
                   nbformat.new_output(output_type="stream", name="stdout", text="\nz"),
                   nbformat.new_output(output_type="stream", name="stdout", text="\rc\n"),
                   nbformat.new_output(output_type="stream", name="stdout", text="z\rz\rd")]
        cells=[nbformat.new_code_cell(source="# None", execution_count=1,outputs=outputs)]

        nb = nbformat.new_notebook(cells=cells)
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
