Skip to content

Commit

Permalink
reorganização e refatoração do código, inclusão de peças históricas e…
Browse files Browse the repository at this point in the history
… correção nos testes
  • Loading branch information
kaiquedu committed Dec 18, 2024
1 parent cfc7331 commit 952340f
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 146 deletions.
183 changes: 131 additions & 52 deletions TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,145 @@
using System;
using System.Collections.Generic;
using ApprovalTests;
using ApprovalTests.Reporters;
using Moq;
using Xunit;
using TheatricalPlayersRefactoringKata.Services;
using TheatricalPlayersRefactoringKata.Models;
using TheatricalPlayersRefactoringKata.Services.Formatters;
using TheatricalPlayersRefactoringKata.Services.PlayType;

namespace TheatricalPlayersRefactoringKata.Tests;

public class StatementPrinterTests
namespace TheatricalPlayersRefactoringKata.Tests
{
[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestStatementExampleLegacy()
public class StatementPrinterTests
{
var plays = new Dictionary<string, Play>();
plays.Add("hamlet", new Play("Hamlet", 4024, "tragedy"));
plays.Add("as-like", new Play("As You Like It", 2670, "comedy"));
plays.Add("othello", new Play("Othello", 3560, "tragedy"));

Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestStatementExampleLegacy()
{
var plays = new Dictionary<string, Play>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
}
);
{ "hamlet", new Play("Hamlet", 4024, "tragedy") },
{ "as-like", new Play("As You Like It", 2670, "comedy") },
{ "othello", new Play("Othello", 3560, "tragedy") }
};

StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);
Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40)
}
);

Approvals.Verify(result);
}
StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);

[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestTextStatementExample()
{
var plays = new Dictionary<string, Play>();
plays.Add("hamlet", new Play("Hamlet", 4024, "tragedy"));
plays.Add("as-like", new Play("As You Like It", 2670, "comedy"));
plays.Add("othello", new Play("Othello", 3560, "tragedy"));
plays.Add("henry-v", new Play("Henry V", 3227, "history"));
plays.Add("john", new Play("King John", 2648, "history"));
plays.Add("richard-iii", new Play("Richard III", 3718, "history"));

Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
Approvals.Verify(result);
}

[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestTextStatementExample()
{
var plays = new Dictionary<string, Play>
{
{ "hamlet", new Play("Hamlet", 4024, "tragedy") },
{ "as-like", new Play("As You Like It", 2670, "comedy") },
{ "othello", new Play("Othello", 3560, "tragedy") },
{ "henry-v", new Play("Henry V", 3227, "history") },
{ "john", new Play("King John", 2648, "history") },
{ "richard-iii", new Play("Richard III", 3718, "history") }
};

Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
new Performance("henry-v", 20),
new Performance("john", 39),
new Performance("henry-v", 20),
}
);

StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);

Approvals.Verify(result);
}

[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestXmlStatementExample()
{
var plays = new Dictionary<string, Play>
{
{ "hamlet", new Play("Hamlet", 4024, "tragedy") },
{ "as-like", new Play("As You Like It", 2670, "comedy") },
{ "othello", new Play("Othello", 3560, "tragedy") },
{ "henry-v", new Play("Henry V", 3227, "history") },
{ "john", new Play("King John", 2648, "history") },
{ "richard-iii", new Play("Richard III", 3718, "history") }
};

Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
new Performance("henry-v", 20),
new Performance("john", 39),
new Performance("henry-v", 20),
}
);

StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.PrintXml(invoice, plays);

Approvals.Verify(result);
}

[Fact]
public void TestGenerateStatement()
{
var plays = new Dictionary<string, Play>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
new Performance("henry-v", 20),
new Performance("john", 39),
new Performance("henry-v", 20)
}
);

StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);

Approvals.Verify(result);
{ "hamlet", new Play("Hamlet", 4024, "tragedy") }
};

var invoice = new Invoice(
"BigCo",
new List<Performance>
{
new Performance("hamlet", 30)
}
);

var statementPrinter = new StatementPrinter();
var statement = statementPrinter.GenerateStatement(invoice, plays);

Assert.NotNull(statement);
Assert.Single(statement.Items);
Assert.Equal(30, statement.Items[0].Seats);
Assert.Equal("Hamlet", statement.Items[0].PlayName);
}

[Fact]
public void TestFormatterLogic()
{
var formatterMock = new Mock<IStatementFormatter>();
formatterMock.Setup(x => x.Format(It.IsAny<Statement>())).Returns("Mock result");

var statement = new Statement { Customer = "BigCo" };
var result = formatterMock.Object.Format(statement);

Assert.Equal("Mock result", result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<ItemGroup>
<PackageReference Include="ApprovalTests" Version="5.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

Expand Down
19 changes: 0 additions & 19 deletions TheatricalPlayersRefactoringKata/Invoice.cs

This file was deleted.

15 changes: 15 additions & 0 deletions TheatricalPlayersRefactoringKata/Models/Invoice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace TheatricalPlayersRefactoringKata;

public class Invoice
{
public string Customer { get; set; }
public List<Performance> Performances { get; set; }

public Invoice(string customer, List<Performance> performances)
{
Customer = customer;
Performances = performances;
}
}
13 changes: 13 additions & 0 deletions TheatricalPlayersRefactoringKata/Models/Performance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TheatricalPlayersRefactoringKata;

public class Performance
{
public string PlayId { get; set; }
public int Audience { get; set; }

public Performance(string playId, int audience)
{
PlayId = playId;
Audience = audience;
}
}
16 changes: 16 additions & 0 deletions TheatricalPlayersRefactoringKata/Models/Play.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace TheatricalPlayersRefactoringKata;

public class Play
{
public string Name { get; set; }
public int Lines { get; set; }
public string Type { get; set; }

public Play(string name, int lines, string type)
{
Name = name;
Lines = lines;
Type = type;
}
}

19 changes: 19 additions & 0 deletions TheatricalPlayersRefactoringKata/Models/Statement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;

namespace TheatricalPlayersRefactoringKata.Models;

public class Statement
{
public string Customer { get; set; }
public List<StatementItem> Items { get; set; } = new();
public decimal TotalAmount { get; set; }
public int TotalCredits { get; set; }
}

public class StatementItem
{
public string PlayName { get; set; }
public decimal AmountOwed { get; set; }
public int EarnedCredits { get; set; }
public int Seats { get; set; }
}
17 changes: 0 additions & 17 deletions TheatricalPlayersRefactoringKata/Performance.cs

This file was deleted.

18 changes: 0 additions & 18 deletions TheatricalPlayersRefactoringKata/Play.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TheatricalPlayersRefactoringKata.Models;

namespace TheatricalPlayersRefactoringKata.Services.Formatters;

public interface IStatementFormatter
{
string Format(Statement statement);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Globalization;
using TheatricalPlayersRefactoringKata.Models;

namespace TheatricalPlayersRefactoringKata.Services.Formatters;

public class TextStatementFormatter : IStatementFormatter
{
public string Format(Statement statement)
{
var cultureInfo = new CultureInfo("en-US");
var result = $"Statement for {statement.Customer}\n";

foreach (var item in statement.Items)
{
result += $" {item.PlayName}: {item.AmountOwed.ToString("C", cultureInfo)} ({item.Seats} seats)\n";
}

result += $"Amount owed is {statement.TotalAmount.ToString("C", cultureInfo)}\n";
result += $"You earned {statement.TotalCredits} credits\n";

return result;
}
}
Loading

0 comments on commit 952340f

Please sign in to comment.