mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Format numbers in output
This commit is contained in:
@@ -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<float, string> 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<int, string> WriteIntTestCases()
|
||||||
|
=> new()
|
||||||
|
{
|
||||||
|
{ 1000, " 1000 " },
|
||||||
|
{ 1, " 1 " },
|
||||||
|
{ 0, " 0 " },
|
||||||
|
{ -1, "-1 " },
|
||||||
|
{ -1000, "-1000 " },
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
namespace Games.Common.IO;
|
namespace Games.Common.IO;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -65,4 +67,17 @@ public interface IReadWrite
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The <see cref="string" /> to be written.</param>
|
/// <param name="message">The <see cref="string" /> to be written.</param>
|
||||||
void WriteLine(string message);
|
void WriteLine(string message);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The <see cref="float" /> to be written.</param>
|
||||||
|
void Write(float value);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces,
|
||||||
|
/// followed by a new-line.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The <see cref="float" /> to be written.</param>
|
||||||
|
void WriteLine(float value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,10 +67,6 @@ public class TextIO : IReadWrite
|
|||||||
nameof(quantity),
|
nameof(quantity),
|
||||||
$"'{nameof(quantity)}' must be greater than zero.");
|
$"'{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)
|
public string ReadString(string prompt)
|
||||||
{
|
{
|
||||||
return ReadStrings(prompt, 1)[0];
|
return ReadStrings(prompt, 1)[0];
|
||||||
@@ -90,4 +86,14 @@ public class TextIO : IReadWrite
|
|||||||
Write(prompt + "? ");
|
Write(prompt + "? ");
|
||||||
return _input.ReadLine();
|
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} ";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user