Esempio n. 1
0
        public void Row_height_definition_size_calculation_according_priority_fixed_then_size_to_content_then_star(OutputMode outputMode)
        {
            var grid = new GridView();

            grid.SetRows(RowDefinition.Star(1), RowDefinition.SizeToContent(), RowDefinition.Fixed(4));
            grid.SetChild(new ContentView("The"), 0, 0);
            grid.SetChild(new ContentView("quick brown fox"), 0, 1);
            grid.SetChild(new ContentView("jumped over the sleepy"), 0, 2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal, outputMode);

            grid.Render(renderer, new Region(0, 0, 8, 6));

            terminal.Events.Should().BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("quick   "),
                new TestTerminal.CursorPositionChanged(new Point(0, 1)),
                new TestTerminal.ContentWritten("brown   "),
                new TestTerminal.CursorPositionChanged(new Point(0, 2)),
                new TestTerminal.ContentWritten("jumped  "),
                new TestTerminal.CursorPositionChanged(new Point(0, 3)),
                new TestTerminal.ContentWritten("over the"),
                new TestTerminal.CursorPositionChanged(new Point(0, 4)),
                new TestTerminal.ContentWritten("sleepy  "),
                new TestTerminal.CursorPositionChanged(new Point(0, 5)),
                new TestTerminal.ContentWritten("        "));
        }
        public void Views_can_be_registered_for_specific_types()
        {
            ParseResult parseResult = null;

            var command = new RootCommand
            {
                Handler = CommandHandler.Create <ParseResult, IConsole>(
                    (r, c) =>
                {
                    parseResult = r;
                    c.Append(new ParseResultView(r));
                }
                    )
            };

            var parser = new CommandLineBuilder(command)
                         .UseMiddleware(
                c =>
            {
                c.BindingContext.AddService(
                    s => new ParseResultView(s.GetService <ParseResult>())
                    );
            }
                )
                         .Build();

            var terminal = new TestTerminal {
                IsAnsiTerminal = false
            };

            parser.Invoke("", terminal);

            terminal.Out.ToString().Should().Contain(parseResult.Diagram());
        }
Esempio n. 3
0
        public void Column_width_definition_is_preserved_even_defintion_is_mixed_for_subsequent_columns(OutputMode outputMode)
        {
            var grid = new GridView();

            grid.SetColumns(ColumnDefinition.Fixed(10), ColumnDefinition.Star(1), ColumnDefinition.Fixed(10), ColumnDefinition.SizeToContent());
            grid.SetChild(new ContentView("The quick"), 0, 0);
            grid.SetChild(new ContentView("brown fox"), 1, 0);
            grid.SetChild(new ContentView("jumped"), 2, 0);
            grid.SetChild(new ContentView("over the sleepy"), 3, 0);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal, outputMode);

            grid.Render(renderer, new Region(0, 0, 121, 1));

            terminal.Events.Should().BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("The quick "),
                new TestTerminal.CursorPositionChanged(new Point(10, 0)),
                new TestTerminal.ContentWritten("  "),
                new TestTerminal.CursorPositionChanged(new Point(12, 0)),
                new TestTerminal.ContentWritten("brown fox" + new string(' ', 71)),
                new TestTerminal.CursorPositionChanged(new Point(92, 0)),
                new TestTerminal.ContentWritten("  "),
                new TestTerminal.CursorPositionChanged(new Point(94, 0)),
                new TestTerminal.ContentWritten("jumped    "),
                new TestTerminal.CursorPositionChanged(new Point(104, 0)),
                new TestTerminal.ContentWritten("  "),
                new TestTerminal.CursorPositionChanged(new Point(106, 0)),
                new TestTerminal.ContentWritten("over the sleepy"));
        }
Esempio n. 4
0
        public void Size_to_content_grid_with_narrow_region_increases_row_height(OutputMode outputMode)
        {
            var grid = new GridView();

            grid.SetColumns(ColumnDefinition.SizeToContent(), ColumnDefinition.SizeToContent());
            grid.SetRows(RowDefinition.SizeToContent(), RowDefinition.SizeToContent());
            grid.SetChild(new ContentView("The quick"), 0, 0);
            grid.SetChild(new ContentView("brown fox"), 1, 0);
            grid.SetChild(new ContentView("jumped over"), 0, 1);
            grid.SetChild(new ContentView("the sleepy"), 1, 1);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal, outputMode);

            grid.Render(renderer, new Region(0, 0, 18, 3));

            terminal.Events.Should().BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("The quick    "),
                new TestTerminal.CursorPositionChanged(new Point(0, 1)),
                new TestTerminal.ContentWritten("             "),
                new TestTerminal.CursorPositionChanged(new Point(13, 0)),
                new TestTerminal.ContentWritten("brown"),
                new TestTerminal.CursorPositionChanged(new Point(13, 1)),
                new TestTerminal.ContentWritten("fox  "),
                new TestTerminal.CursorPositionChanged(new Point(0, 2)),
                new TestTerminal.ContentWritten("jumped over  "),
                new TestTerminal.CursorPositionChanged(new Point(13, 2)),
                new TestTerminal.ContentWritten("the s"));
        }
        public void Vertical_stack_wraps_content_when_region_is_not_wide_enough()
        {
            var stackLayout = new StackLayoutView();
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            stackLayout.Render(renderer, new Region(0, 0, 5, 4));

            terminal.Events
            .Should()
            .BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("The  "),
                new TestTerminal.CursorPositionChanged(new Point(0, 1)),
                new TestTerminal.ContentWritten("quick"),
                new TestTerminal.CursorPositionChanged(new Point(0, 2)),
                new TestTerminal.ContentWritten("brown"),
                new TestTerminal.CursorPositionChanged(new Point(0, 3)),
                new TestTerminal.ContentWritten("fox  ")
                );
        }
Esempio n. 6
0
        public void Fixed_grid_lays_out_fixed_rows_and_columns(OutputMode outputMode)
        {
            var grid = new GridView();

            grid.SetColumns(ColumnDefinition.Fixed(6), ColumnDefinition.Fixed(4));
            grid.SetRows(RowDefinition.Fixed(1), RowDefinition.Fixed(2));
            grid.SetChild(new ContentView("The quick"), 0, 0);
            grid.SetChild(new ContentView("brown fox"), 1, 0);
            grid.SetChild(new ContentView("jumped over"), 0, 1);
            grid.SetChild(new ContentView("the sleepy"), 1, 1);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal, outputMode);

            grid.Render(renderer, new Region(0, 0, 10, 4));

            terminal.Events
            .Should()
            .BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("The   "),
                new TestTerminal.CursorPositionChanged(new Point(6, 0)),
                new TestTerminal.ContentWritten("brow"),
                new TestTerminal.CursorPositionChanged(new Point(0, 1)),
                new TestTerminal.ContentWritten("jumped"),
                new TestTerminal.CursorPositionChanged(new Point(0, 2)),
                new TestTerminal.ContentWritten("over  "),
                new TestTerminal.CursorPositionChanged(new Point(6, 1)),
                new TestTerminal.ContentWritten("the "),
                new TestTerminal.CursorPositionChanged(new Point(6, 2)),
                new TestTerminal.ContentWritten("slee"));
        }
        public void Same_input_with_or_without_ansi_codes_produce_the_same_wrap(
            int left,
            int top,
            int width,
            int height
            )
        {
            var terminalWithoutAnsiCodes = new TestTerminal();
            var rendererWithoutAnsiCodes = new ConsoleRenderer(
                terminalWithoutAnsiCodes,
                OutputMode.NonAnsi
                );

            var terminalWithAnsiCodes = new TestTerminal();
            var rendererWithAnsiCodes = new ConsoleRenderer(
                terminalWithAnsiCodes,
                OutputMode.NonAnsi
                );

            FormattableString formattableString =
                $"Call me {StyleSpan.BoldOn()}{StyleSpan.UnderlinedOn()}Ishmael{StyleSpan.UnderlinedOff()}{StyleSpan.BoldOff()}. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and {ForegroundColorSpan.Rgb(60, 0, 0)}methodically{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(90, 0, 0)}knocking{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(120, 0, 0)}people's{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(160, 0, 0)}hats{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(220, 0, 0)}off{ForegroundColorSpan.Reset()} then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.";

            var stringWithoutCodes = formattableString.ToString();

            var region = new Region(left, top, width, height);

            rendererWithAnsiCodes.RenderToRegion(formattableString, region);

            rendererWithoutAnsiCodes.RenderToRegion(stringWithoutCodes, region);

            var outputFromInputWithoutAnsiCodes = terminalWithoutAnsiCodes.Out.ToString();
            var outputFromInputWithAnsiCodes    = terminalWithAnsiCodes.Out.ToString();

            outputFromInputWithAnsiCodes.Should().Be(outputFromInputWithoutAnsiCodes);
        }
Esempio n. 8
0
        public void Star_grid_lays_out_in_even_grid(OutputMode outputMode)
        {
            var grid = new GridView();

            grid.SetColumns(ColumnDefinition.Star(1), ColumnDefinition.Star(1));
            grid.SetRows(RowDefinition.Star(1), RowDefinition.Star(1));
            grid.SetChild(new ContentView("The quick"), 0, 0);
            grid.SetChild(new ContentView("brown fox"), 1, 0);
            grid.SetChild(new ContentView("jumped"), 0, 1);
            grid.SetChild(new ContentView("over"), 1, 1);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal, outputMode);

            grid.Render(renderer, new Region(0, 0, 10, 4));

            terminal.Events.Should().BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("The  "),
                new TestTerminal.CursorPositionChanged(new Point(0, 1)),
                new TestTerminal.ContentWritten("quick"),
                new TestTerminal.CursorPositionChanged(new Point(5, 0)),
                new TestTerminal.ContentWritten("brown"),
                new TestTerminal.CursorPositionChanged(new Point(5, 1)),
                new TestTerminal.ContentWritten("fox  "),
                new TestTerminal.CursorPositionChanged(new Point(0, 2)),
                new TestTerminal.ContentWritten("jumpe"),
                new TestTerminal.CursorPositionChanged(new Point(0, 3)),
                new TestTerminal.ContentWritten("     "),
                new TestTerminal.CursorPositionChanged(new Point(5, 2)),
                new TestTerminal.ContentWritten("over "),
                new TestTerminal.CursorPositionChanged(new Point(5, 3)),
                new TestTerminal.ContentWritten("     "));
        }
        public void Sets_outputMode_to_ansi_when_windows_and_virtual_terminal()
        {
            var terminal = new TestTerminal();

            var outputMode = terminal.DetectOutputMode();

            outputMode.Should().Be(OutputMode.Ansi);
        }
        public TableRenderingTests(ITestOutputHelper output)
        {
            _output = output;

            _terminal = new TestTerminal {
                Width = 150
            };
        }
Esempio n. 11
0
        public ViewWrappingTests()
        {
            _terminal = new TestTerminal {
                Width = 150
            };

            consoleRenderer = new ConsoleRenderer(_terminal);
        }
Esempio n. 12
0
        public void OutputStyle()
        {
            ITerminal terminal = new TestTerminal();

            terminal.OutputEmphasize("");
            terminal.OutputBlink("");
            terminal.OutputBold("");
            terminal.OutputStandout("");
            terminal.OutputUnderline("");
        }
Esempio n. 13
0
        public void OutputColor()
        {
            ITerminal terminal = new TestTerminal();

            terminal.OutputErrorLine("");
            terminal.OutputWarningLine("");
            terminal.OutputInformationLine("");
            terminal.OutputFatalLine("");
            terminal.OutputDebugLine("");
        }
        public void Sets_outputMode_to_file_when_output_is_redirected()
        {
            var terminal = new TestTerminal();

            terminal.IsOutputRedirected = true;

            var outputMode = terminal.DetectOutputMode();

            outputMode.Should().Be(OutputMode.File);
        }
Esempio n. 15
0
        public void Sets_outputMode_to_file_when_output_is_redirected()
        {
            var terminal = new TestTerminal();

            terminal.SetOut(new StringWriter());

            var outputMode = terminal.DetectOutputMode();

            outputMode.Should().Be(OutputMode.File);
        }
Esempio n. 16
0
        public void When_CursorTop_is_set_then_a_cursor_position_is_recorded()
        {
            var terminal = new TestTerminal();

            terminal.CursorTop = 12;

            terminal.Events
            .OfType <TestTerminal.CursorPositionChanged>()
            .Select(e => e.Position)
            .Should()
            .BeEquivalentSequenceTo(new Point(0, 12));
        }
Esempio n. 17
0
        public void When_ANSI_sequences_are_used_to_set_cursor_positions_then_CursorPositionChanged_events_are_recorded()
        {
            var terminal = new TestTerminal();

            terminal.Out.Write($"before move{Ansi.Cursor.Move.ToLocation(3, 5).EscapeSequence}after move");

            terminal.Events
            .Should()
            .BeEquivalentSequenceTo(
                new TestTerminal.ContentWritten("before move"),
                new TestTerminal.CursorPositionChanged(new Point(2, 4)),
                new TestTerminal.ContentWritten("after move"));
        }
Esempio n. 18
0
        public void ContentWritten_events_do_not_include_escape_sequences()
        {
            var terminal = new TestTerminal();

            var renderer = new ConsoleRenderer(terminal, OutputMode.Ansi);

            var region = new Region(0, 0, 4, 1);

            renderer.RenderToRegion($"{ForegroundColorSpan.Red()}text{ForegroundColorSpan.Reset()}", region);

            terminal.Events
            .Should()
            .Contain(e => e is TestTerminal.ContentWritten &&
                     ((TestTerminal.ContentWritten)e).Content == "text");
        }
        public void Measuring_a_vertical_stack_with_word_wrap_it_sums_max_height_for_each_row()
        {
            var stackLayout = new StackLayoutView(Orientation.Vertical);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(7, 10));

            size.Should().BeEquivalentTo(new Size("brown ".Length, 4));
        }
        public void Measuring_a_vertical_stack_sums_content_height()
        {
            var stackLayout = new StackLayoutView(Orientation.Vertical);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(10, 10));

            size.Should().BeEquivalentTo(new Size(9, 2));
        }
        public void Measuring_a_horizontal_stack_sums_content_width()
        {
            var stackLayout = new StackLayoutView(Orientation.Horizontal);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(20, 20));

            size.Should().BeEquivalentTo(new Size("The quickbrown fox".Length, 1));
        }
        public void Measuring_a_horizontal_stack_with_truncated_height_measures_max_for_each_child()
        {
            var stackLayout = new StackLayoutView(Orientation.Horizontal);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(7, 1));

            size.Should().BeEquivalentTo(new Size(7, 1));
        }
        public void Measuring_a_horizontal_stack_with_word_wrap_it_sums_max_width_for_each_child()
        {
            var stackLayout = new StackLayoutView(Orientation.Horizontal);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(10, 10));

            size.Should().BeEquivalentTo(new Size(10, 2));
        }
        public void Measuring_a_horizontal_stack_with_wide_children_wraps_last_child()
        {
            var stackLayout = new StackLayoutView(Orientation.Horizontal);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(12, 5));

            size.Should().BeEquivalentTo(new Size(12, 2));
        }
        public void Measuring_a_vertical_stack_with_tall_children_trims_last_child()
        {
            var stackLayout = new StackLayoutView(Orientation.Vertical);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(5, 3));

            size.Should().BeEquivalentTo(new Size(5, 3));
        }
        public void Measuring_a_vertical_stack_with_row_truncation_the_top_row_is_measured_first()
        {
            var stackLayout = new StackLayoutView(Orientation.Vertical);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            var size = stackLayout.Measure(renderer, new Size(7, 1));

            var firstViewTopRow = "The ".Length;

            size.Should().BeEquivalentTo(new Size(firstViewTopRow, 1));
        }
Esempio n. 27
0
        public void Timeline_allows_replay_of_content_rendering_and_cursor_positions(OutputMode outputMode)
        {
            var terminal = new TestTerminal();

            var renderer = new ConsoleRenderer(terminal, outputMode);

            var region = new Region(1, 3, 11, 2);

            renderer.RenderToRegion("first line\nsecond line", region);

            terminal.Events
            .Where(e => !(e is TestTerminal.AnsiControlCodeWritten))
            .Should()
            .BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(1, 3)),
                new TestTerminal.ContentWritten("first line "),
                new TestTerminal.CursorPositionChanged(new Point(1, 4)),
                new TestTerminal.ContentWritten("second line"));
        }
Esempio n. 28
0
        public void When_a_newline_is_written_by_a_ConsoleRenderer_then_a_cursor_position_is_recorded(
            OutputMode outputMode,
            string threeLinesOfText)
        {
            var console = new TestTerminal();

            var renderer = new ConsoleRenderer(console, outputMode);

            renderer.RenderToRegion(threeLinesOfText, new Region(2, 5, 13, 3));

            console.Events
            .OfType <TestTerminal.CursorPositionChanged>()
            .Select(e => e.Position)
            .Should()
            .BeEquivalentSequenceTo(
                new Point(2, 5),
                new Point(2, 6),
                new Point(2, 7));
        }
Esempio n. 29
0
        public void Horizontal_stack_displays_content_stacked_on_next_to_each_other()
        {
            var stackLayout = new StackLayoutView(Orientation.Horizontal);
            var child1      = new ContentView("The quick");
            var child2      = new ContentView("brown fox");

            stackLayout.Add(child1);
            stackLayout.Add(child2);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal);

            stackLayout.Render(renderer, new Region(0, 0, 18, 1));

            terminal.Events.Should().BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("The quick         "),
                new TestTerminal.CursorPositionChanged(new Point(9, 0)),
                new TestTerminal.ContentWritten("brown fox"));
        }
Esempio n. 30
0
        public void Column_width_definition_size_calculation_according_priority_fixed_then_size_to_content_then_star(OutputMode outputMode)
        {
            var grid = new GridView();

            grid.SetColumns(ColumnDefinition.Star(1), ColumnDefinition.SizeToContent(), ColumnDefinition.Fixed(10));
            grid.SetChild(new ContentView("The quick"), 0, 0);
            grid.SetChild(new ContentView("brown fox"), 1, 0);
            grid.SetChild(new ContentView("jumped"), 2, 0);

            var terminal = new TestTerminal();
            var renderer = new ConsoleRenderer(terminal, outputMode);

            grid.Render(renderer, new Region(0, 0, 22, 1));

            terminal.Events.Should().BeEquivalentSequenceTo(
                new TestTerminal.CursorPositionChanged(new Point(0, 0)),
                new TestTerminal.ContentWritten("brown fox"),
                new TestTerminal.CursorPositionChanged(new Point(9, 0)),
                new TestTerminal.ContentWritten("  "),
                new TestTerminal.CursorPositionChanged(new Point(11, 0)),
                new TestTerminal.ContentWritten("jumped    "));
        }
Esempio n. 31
0
        private static void ConnectionDropedTest()
        {
            // INIT TEST

            var a = new TestTerminal("A", new PhoneNumber("101"));
            var b = new TestTerminal("B", new PhoneNumber("102"));
            var c = new TestTerminal("C", new PhoneNumber("103"));

            var aa = new TestPort("AA");
            var bb = new TestPort("BB");
            var cc = new TestPort("CC");

            var station = new TestStation("AAA", new List<ITerminal> { a, b, c },
                                          new List<IPort> { aa, bb, cc });

            // START TEST

            var t1 = station.GetPreparedTerminal() as TestTerminal;
            var t2 = station.GetPreparedTerminal() as TestTerminal;
            var t3 = station.GetPreparedTerminal() as TestTerminal;

            if (t1 == null || t2 == null || t3 == null) return;

            t1.Plug();
            t2.Plug();
            t3.Plug();

            Console.WriteLine("\n");
            t1.Connect(t2.PhoneNumber);
            t1.SendMessage("Hi!");
            t2.SendMessage("Hallo!");
            t3.Connect(t2.PhoneNumber); // port not bind because target port busy
            t1.SendMessage("How are you?!");
            aa.Close(); // close port of t1 close
            t2.SendMessage("I am ok"); // droped because aa closed
            Console.WriteLine("\n");
            aa.Open();
            t3.Connect(t1.PhoneNumber);
            t1.Unplug();
            t3.SendMessage("Hi"); // droped because t1 unpluged
        }
Esempio n. 32
0
        private static void BillingSystemTest()
        {
            // INIT TEST

            var a = new TestTerminal("A", new PhoneNumber("101"));
            var b = new TestTerminal("B", new PhoneNumber("102"));

            var aa = new TestPort("AA");
            var bb = new TestPort("BB");

            var station = new TestStation("AAA", new List<ITerminal> { a, b },
                                          new List<IPort> { aa, bb });

            var billing = new TestBillingSystem();

            ITariff tariffFirst = new TestTariff(connectCost: 1000, minCost: 300);
            ITariff tariffSecond = new TestTariff(connectCost: 0, minCost: 500);

            var provider = new TestProvider(station, billing);

            // START TEST

            var t1 = provider.SignContract(tariffFirst) as TestTerminal;
            var t2 = provider.SignContract(tariffSecond) as TestTerminal;

            if (t1 == null || t2 == null) return;

            t1.Plug();
            t2.Plug();

            Console.WriteLine("------ FIRST MONTH ------");

            Console.WriteLine("--- First call ---");
            t1.Connect(t2.PhoneNumber);
            t2.SendMessage("Ololololol");
            t2.Disconnect();

            Console.WriteLine(" --- Second call ---");
            t2.Unplug();
            t1.Connect(t2.PhoneNumber);
            t2.Plug();

            Console.WriteLine(" --- third call ---");
            t2.Connect(t1.PhoneNumber);
            t2.SendMessage("Ololol");
            t1.Disconnect();

            Console.WriteLine("--- statistic ---");
            var calls = provider.GetStatisticForPeriod(t1.PhoneNumber);
            foreach (var call in calls)
                Console.WriteLine(call);
            Console.WriteLine($"Debt of {t1.PhoneNumber} - {provider.GetDebt(t1.PhoneNumber)}");
            Console.WriteLine($"Debt of {t2.PhoneNumber} - {provider.GetDebt(t2.PhoneNumber)}");

            billing.StartNewPeriod();
            Console.WriteLine("------ SECOND MONTH ------");

            Console.WriteLine("--- statistic ---");
            calls = provider.GetStatisticForPeriod(t1.PhoneNumber);
            foreach (var call in calls)
                Console.WriteLine(call);
            Console.WriteLine($"Debt of {t1.PhoneNumber} - {provider.GetDebt(t1.PhoneNumber)}");
            Console.WriteLine($"Debt of {t2.PhoneNumber} - {provider.GetDebt(t2.PhoneNumber)}");

            Console.WriteLine($"--- {t1.PhoneNumber} changing tariff ---");
            if (provider.TryChangeTariff(t1.PhoneNumber, tariffSecond))
                Console.WriteLine("tariff changed successfully");
            else
                Console.WriteLine("tariff not changed");

            Console.WriteLine($"--- {t1.PhoneNumber} changing tariff for the second time ---");
            if (provider.TryChangeTariff(t1.PhoneNumber, tariffFirst))
                Console.WriteLine("tariff changed successfully");
            else
                Console.WriteLine("tariff not changed");
        }