Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refatoração - Gabriel Antonio da Silva #83

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Bb]in/
[Oo]bj/

*.received.txt
*.received.txt
/.vs
156 changes: 156 additions & 0 deletions TheatricalPlayersRefactoringKata.Tests/StatementFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using TheatricalPlayersRefactoringKata.Formatters;
using TheatricalPlayersRefactoringKata.Interfaces;
using TheatricalPlayersRefactoringKata.Strategies;
using Xunit;

namespace TheatricalPlayersRefactoringKata.Tests
{
public class StatementFormatterTests
{
private readonly Dictionary<string, Play> _plays;
private readonly Dictionary<string, IStatementStrategy> _strategies;

public StatementFormatterTests()
{
// Initialize
_plays = new Dictionary<string, Play>
{
{ "hamlet", new Play("Hamlet", 1000, "tragedy") },
{ "as-you-like-it", new Play("As You Like It", 2000, "comedy") }
};

_strategies = new Dictionary<string, IStatementStrategy>
{
{ "tragedy", new TragedyStrategy() },
{ "comedy", new ComedyStrategy() }
};
}

[Fact]
public void TestZeroAudience_TextStatementFormatter()
{
// Arrange
var invoice = new Invoice("BigCo", new List<Performance>
{
new Performance("hamlet", 0)
});

var formatter = new TextStatementFormatter();

// Act
var result = formatter.Format(invoice, _plays, _strategies);

// Assert
var expected = "Statement for BigCo\n Hamlet: $0.00 (0 seats)\nAmount owed is $0.00\nYou earned 0 credits\n";
Assert.Equal(expected, result);
}

[Fact]
public void TestUnknownPlayType_TextStatementFormatter()
{
// Arrange
var invoice = new Invoice("BigCo", new List<Performance>
{
new Performance("unknownPlayId", 5)
});

var formatter = new TextStatementFormatter();

// Act & Assert
var ex = Assert.Throws<KeyNotFoundException>(() => formatter.Format(invoice, _plays, _strategies));
Assert.Equal("The given key 'unknownPlayId' was not present in the dictionary.", ex.Message);
}

[Fact]
public void TestExtremeValues_TextStatementFormatter()
{
// Arrange
var invoice = new Invoice("BigCo", new List<Performance>
{
new Performance("hamlet", 10000),
new Performance("as-you-like-it", 5000)
});

var formatter = new TextStatementFormatter();

// Act
var result = formatter.Format(invoice, _plays, _strategies);

// Assert
var expected = "Statement for BigCo\n" +
" Hamlet: $99,800.00 (10000 seats)\n" +
" As You Like It: $40,200.00 (5000 seats)\n" +
"Amount owed is $140,000.00\n" +
"You earned 15940 credits\n";
Assert.Equal(expected, result);
}

[Fact]
public void TestZeroAudience_XMLStatementFormatter()
{
// Arrange
var invoice = new Invoice("BigCo", new List<Performance>
{
new Performance("hamlet", 0)
});

var formatter = new XmlStatementFormatter();

// Act
var result = formatter.Format(invoice, _plays, _strategies);

// Expected
var expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<Statement xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
" <Customer>BigCo</Customer>\n" +
" <Items>\n" +
" <Item>\n" +
" <AmountOwed>0</AmountOwed>\n" +
" <EarnedCredits>0</EarnedCredits>\n" +
" <Seats>0</Seats>\n" +
" </Item>\n" +
" </Items>\n" +
" <AmountOwed>0</AmountOwed>\n" +
" <EarnedCredits>0</EarnedCredits>\n" +
"</Statement>\n";

// Normalizar XML
result = NormalizeXml(result);
expected = NormalizeXml(expected);

Assert.Equal(expected, result);
}

[Fact]
public void TestUnknownPlayType_XMLStatementFormatter()
{
// Arrange
var invoice = new Invoice("BigCo", new List<Performance>
{
new Performance("unknownPlayId", 5)
});

var formatter = new XmlStatementFormatter();

// Act & Assert
var ex = Assert.Throws<KeyNotFoundException>(() => formatter.Format(invoice, _plays, _strategies));
Assert.Equal("The given key 'unknownPlayId' was not present in the dictionary.", ex.Message);
}

private string RemoveXmlDeclaration(string xml)
{
// Remove declaração do XML se existente
const string xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
return xml.StartsWith(xmlDeclaration) ? xml.Substring(xmlDeclaration.Length).TrimStart() : xml;
}

private string NormalizeXml(string xml)
{
// Remove declaração do XML se existente XML e normaliza
xml = RemoveXmlDeclaration(xml);
return xml.Replace("\r\n", "\n").Trim();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Statement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customer>BigCo</Customer>
<Items>
Expand Down
156 changes: 113 additions & 43 deletions TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,136 @@
using System;
using System.Collections.Generic;
using ApprovalTests;
using ApprovalTests.Reporters;
using ApprovalTests;
using System.Collections.Generic;
using TheatricalPlayersRefactoringKata;
using Xunit;
using TheatricalPlayersRefactoringKata.Interfaces;
using TheatricalPlayersRefactoringKata.Strategies;

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"));
private readonly Dictionary<string, Play> _plays;
private readonly Invoice _invoice;
private readonly IStatementFormatterFactory _formatterFactory;

public StatementPrinterTests()
{
_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>
{
_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("richard-iii", 20)
}
);
}

StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);
[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestStatementExampleLegacy()
{
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") }
};

Approvals.Verify(result);
}
Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
}
);

[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"));
StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);

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>
{
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);
StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays, true);

Approvals.Verify(result);
Approvals.Verify(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TheatricalPlayersRefactoringKata.Interfaces;

namespace TheatricalPlayersRefactoringKata.Formatters
{
public class StatementFormatterFactory : IStatementFormatterFactory
{
public IStatementFormatter CreateFormatter(bool asXml)
{
return asXml ? new XmlStatementFormatter() : new TextStatementFormatter();
}
}
}
Loading