finskaybank/TestProject1/UnitTest1.cs
2025-05-15 05:54:37 +03:00

290 lines
8.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using finskaybank;
using NUnit.Framework;
namespace BankAccountTests
{
[TestFixture]
public class BankAccountOperationsTests
{
private account[] _accounts;
private StringReader _inputSimulator;
private StringWriter _outputCapture;
[SetUp]
public void InitializeTestEnvironment()
{
_accounts = new account[100];
_outputCapture = new StringWriter();
Console.SetOut(_outputCapture);
}
[TearDown]
public void CleanupTestEnvironment()
{
_outputCapture?.Dispose();
_inputSimulator?.Dispose();
}
[Test]
public void otk_ValidInput_SetsCorrectProperties()
{
_inputSimulator = new StringReader("fio\n1500\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
Assert.That(_accounts[0].GetName(), Is.EqualTo("fio"));
Assert.That(_accounts[0].GetSum(), Is.EqualTo(1500.00));
Assert.That(_accounts[0].GetNum(), Is.Not.Empty);
}
[Test]
public void otk_InsufficientDeposit_PrintsRetryMessage()
{
_inputSimulator = new StringReader("fio\n500\nfio1\n2000\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
string consoleOutput = _outputCapture.ToString();
Assert.That(consoleOutput, Does.Contain("Сумма слишком мала, попробуйте ещё раз!"));
}
[Test]
public void otk_NumericNameInput_StoresNameCorrectly()
{
_inputSimulator = new StringReader("12345\n2500\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
Assert.That(_accounts[0].GetName(), Is.EqualTo("12345"));
}
[Test]
public void otk_InvalidDepositInput_ThrowsFormatException()
{
_inputSimulator = new StringReader("fio\ninvalidAmount\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
Assert.Throws<FormatException>(() => _accounts[0].otk());
}
[Test]
public void num_gen_GeneratesTwentyDigitNumber()
{
_accounts[0] = new account();
_accounts[0].num_gen();
Assert.That(_accounts[0].GetNum().Length, Is.EqualTo(20));
}
[Test]
public void otk_AfterCreation_AccountFileContainsCorrectInfo()
{
_inputSimulator = new StringReader("fio\n3000\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
string accountFile = $"{_accounts[0].GetNum()}.txt";
Assert.That(File.Exists(accountFile), Is.True);
string fileContents = File.ReadAllText(accountFile);
Assert.That(fileContents, Does.Contain(_accounts[0].GetNum()));
Assert.That(fileContents, Does.Contain(_accounts[0].GetName()));
Assert.That(fileContents, Does.Contain(_accounts[0].GetSum().ToString()));
}
[Test]
public void top_up_PositiveAmount_IncreasesBalance()
{
_inputSimulator = new StringReader("fio\n1000\n750\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
float expected = _accounts[0].GetSum() + 750;
_accounts[0].top_up();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(expected));
}
[Test]
public void top_up_NegativeAmount_DecreasesBalance()
{
_inputSimulator = new StringReader("fio\n2000\n-500\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
float expected = _accounts[0].GetSum() - 500;
_accounts[0].top_up();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(expected));
}
[Test]
public void top_up_ZeroAmount_LeavesBalanceUnchanged()
{
_inputSimulator = new StringReader("fio\n1500\n0\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
float original = _accounts[0].GetSum();
_accounts[0].top_up();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(original));
}
[Test]
public void top_up_InvalidInput_ThrowsFormatException()
{
_inputSimulator = new StringReader("fio\n1000\nnotANumber\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
Assert.Throws<FormatException>(() => _accounts[0].top_up());
}
[Test]
public void umen_PositiveAmount_DecreasesBalance()
{
_inputSimulator = new StringReader("fio\n3000\n1000\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
float expected = _accounts[0].GetSum() - 1000;
_accounts[0].umen();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(expected));
}
[Test]
public void umen_NegativeAmount_IncreasesBalance()
{
_inputSimulator = new StringReader("fio\n4000\n-1000\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
float expected = _accounts[0].GetSum() + 1000;
_accounts[0].umen();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(expected));
}
[Test]
public void umen_ZeroAmount_LeavesBalanceUnchanged()
{
_inputSimulator = new StringReader("fio\n2500\n0\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
float original = _accounts[0].GetSum();
_accounts[0].umen();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(original));
}
[Test]
public void umen_InvalidInput_ThrowsFormatException()
{
_inputSimulator = new StringReader("fio\n2000\nabc\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
Assert.Throws<FormatException>(() => _accounts[0].umen());
}
[Test]
public void obnul_ClearsBalanceToZero()
{
_inputSimulator = new StringReader("fio\n5000\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
_accounts[0].obnul();
Assert.That(_accounts[0].GetSum(), Is.EqualTo(0.00));
}
[Test]
public void perevod_SufficientFunds_TransfersAmountCorrectly()
{
_inputSimulator = new StringReader("fio\n5000\nfio1\n3000\n2000\n1\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
_accounts[1] = new account();
_accounts[1].otk();
float senderExpected = _accounts[0].GetSum() - 2000;
float recipientExpected = _accounts[1].GetSum() + 2000;
_accounts[0].perevod();
_accounts[1].sum += 2000;
Assert.That(_accounts[0].GetSum(), Is.EqualTo(senderExpected));
Assert.That(_accounts[1].GetSum(), Is.EqualTo(recipientExpected));
}
[Test]
public void perevod_InsufficientFunds_PrintsErrorMessage()
{
_inputSimulator = new StringReader("fio\n1000\nfio1\n5000\n2000\n1\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
_accounts[1] = new account();
_accounts[1].otk();
_accounts[0].perevod();
string output = _outputCapture.ToString();
Assert.That(output, Does.Contain("На счету недостаточно средств!"));
}
[Test]
public void perevod_InvalidAmount_ThrowsFormatException()
{
_inputSimulator = new StringReader("fio\n3000\nfio1\n5000\ninvalid\n1\n");
Console.SetIn(_inputSimulator);
_accounts[0] = new account();
_accounts[0].otk();
_accounts[1] = new account();
_accounts[1].otk();
Assert.Throws<FormatException>(() => _accounts[0].perevod());
}
}
}