Skip to content

Instantly share code, notes, and snippets.

@emilybache
Last active October 20, 2024 09:35
Show Gist options
  • Save emilybache/b7aef33a8fe449d47c0a194a9451bd3c to your computer and use it in GitHub Desktop.
Save emilybache/b7aef33a8fe449d47c0a194a9451bd3c to your computer and use it in GitHub Desktop.
GildedRoseApprovalTest in C#
using GildedRoseKata;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;
namespace GildedRoseTests;
public class ApprovalTest
{
[Fact]
public Task UpdateQuality()
{
var names = new[] { "foo", "Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros" };
var sellIns = new[] { -1, 0, 5, 6, 10, 11 };
var qualitys = new[] { 0, 1, 2, 49, 50 };
var allCombinations = AllCombinations(DoUpdateQuality,
names, sellIns, qualitys
);
return Verifier.Verify(allCombinations);
}
private List<string> AllCombinations(Func<string, int, int, string> theFunction,
string[] args1, int[] args2, int[] args3)
{
var cartesianProduct =
from arg1 in args1
from arg2 in args2
from arg3 in args3
select new { arg1, arg2, arg3 };
var result = new List<string>();
foreach (var combination in cartesianProduct)
{
string combinationResult;
try
{
combinationResult = theFunction(combination.arg1, combination.arg2, combination.arg3);
}
catch (Exception e)
{
combinationResult = e.ToString();
}
result.Add($"{combination.arg1}, {combination.arg2}, {combination.arg3} => {combinationResult}");
}
return result;
}
private string DoUpdateQuality(string name, int sellIn, int quality)
{
Item[] items = { new() { Name = name, SellIn = sellIn, Quality = quality } };
GildedRose app = new GildedRose(items);
app.UpdateQuality();
return PrintItem(items[0]);
}
public string PrintItem(Item item)
{
return $"{item.Name} sellIn: {item.SellIn} quality: {item.Quality}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment