From 08f0ab0dc46dabfaf6f7a12a61ea3d40090e1766 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Tue, 1 Mar 2022 07:39:16 +1100 Subject: [PATCH] Format numbers in output --- .../IO/TextIOTests/NumberFormatTests.cs | 79 +++++++++++++++++++ .../dotnet/Games.Common/IO/IReadWrite.cs | 15 ++++ 00_Common/dotnet/Games.Common/IO/TextIO.cs | 14 +++- 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 00_Common/dotnet/Games.Common.Test/IO/TextIOTests/NumberFormatTests.cs diff --git a/00_Common/dotnet/Games.Common.Test/IO/TextIOTests/NumberFormatTests.cs b/00_Common/dotnet/Games.Common.Test/IO/TextIOTests/NumberFormatTests.cs new file mode 100644 index 00000000..670a7366 --- /dev/null +++ b/00_Common/dotnet/Games.Common.Test/IO/TextIOTests/NumberFormatTests.cs @@ -0,0 +1,79 @@ +using System; +using System.IO; +using FluentAssertions; +using Xunit; + +namespace Games.Common.IO.TextIOTests; + +public class NumberFormatTests +{ + [Theory] + [MemberData(nameof(WriteFloatTestCases))] + public void Write_Float_FormatsNumberSameAsBasic(float value, string basicString) + { + var outputWriter = new StringWriter(); + var io = new TextIO(new StringReader(""), outputWriter); + + io.Write(value); + + outputWriter.ToString().Should().BeEquivalentTo(basicString); + } + + [Theory] + [MemberData(nameof(WriteFloatTestCases))] + public void WriteLine_Float_FormatsNumberSameAsBasic(float value, string basicString) + { + var outputWriter = new StringWriter(); + var io = new TextIO(new StringReader(""), outputWriter); + + io.WriteLine(value); + + outputWriter.ToString().Should().BeEquivalentTo(basicString + Environment.NewLine); + } + + public static TheoryData WriteFloatTestCases() + => new() + { + { 1000F, " 1000 " }, + { 3.1415927F, " 3.1415927 " }, + { 1F, " 1 " }, + { 0F, " 0 " }, + { -1F, "-1 " }, + { -3.1415927F, "-3.1415927 " }, + { -1000F, "-1000 " }, + }; + + [Theory] + [MemberData(nameof(WriteIntTestCases))] + public void Write_Int_FormatsNumberSameAsBasic(int value, string basicString) + { + var outputWriter = new StringWriter(); + var io = new TextIO(new StringReader(""), outputWriter); + + io.Write(value); + + outputWriter.ToString().Should().BeEquivalentTo(basicString); + } + + [Theory] + [MemberData(nameof(WriteIntTestCases))] + public void WriteLine_Int_FormatsNumberSameAsBasic(int value, string basicString) + { + var outputWriter = new StringWriter(); + var io = new TextIO(new StringReader(""), outputWriter); + + io.WriteLine(value); + + outputWriter.ToString().Should().BeEquivalentTo(basicString + Environment.NewLine); + } + + public static TheoryData WriteIntTestCases() + => new() + { + { 1000, " 1000 " }, + { 1, " 1 " }, + { 0, " 0 " }, + { -1, "-1 " }, + { -1000, "-1000 " }, + }; +} diff --git a/00_Common/dotnet/Games.Common/IO/IReadWrite.cs b/00_Common/dotnet/Games.Common/IO/IReadWrite.cs index 629b22aa..2a6433e1 100644 --- a/00_Common/dotnet/Games.Common/IO/IReadWrite.cs +++ b/00_Common/dotnet/Games.Common/IO/IReadWrite.cs @@ -1,3 +1,5 @@ +using System; + namespace Games.Common.IO; /// @@ -65,4 +67,17 @@ public interface IReadWrite /// /// The to be written. void WriteLine(string message); + + /// + /// Writes a to output, formatted per the BASIC interpreter, with leading and trailing spaces. + /// + /// The to be written. + void Write(float value); + + /// + /// Writes a to output, formatted per the BASIC interpreter, with leading and trailing spaces, + /// followed by a new-line. + /// + /// The to be written. + void WriteLine(float value); } diff --git a/00_Common/dotnet/Games.Common/IO/TextIO.cs b/00_Common/dotnet/Games.Common/IO/TextIO.cs index 5a7cf753..9c3d6ba9 100644 --- a/00_Common/dotnet/Games.Common/IO/TextIO.cs +++ b/00_Common/dotnet/Games.Common/IO/TextIO.cs @@ -67,10 +67,6 @@ public class TextIO : IReadWrite nameof(quantity), $"'{nameof(quantity)}' must be greater than zero."); - public void Write(string value) => _output.Write(value); - - public void WriteLine(string value) => _output.WriteLine(value); - public string ReadString(string prompt) { return ReadStrings(prompt, 1)[0]; @@ -90,4 +86,14 @@ public class TextIO : IReadWrite Write(prompt + "? "); return _input.ReadLine(); } + + public void Write(string value) => _output.Write(value); + + public void WriteLine(string value) => _output.WriteLine(value); + + public void Write(float value) => _output.Write(GetString(value)); + + public void WriteLine(float value) => _output.WriteLine(GetString(value)); + + private string GetString(float value) => value < 0 ? $"{value} " : $" {value} "; }