Skip to content

Commit ca148a7

Browse files
Add book-store exercise (#308)
Add book-store exercise test generator
1 parent f1c637d commit ca148a7

23 files changed

Lines changed: 218 additions & 131 deletions

exercises/book-store/BookStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
public static class BookStore
55
{
6-
public static double CalculateTotalCost(List<int> books)
6+
public static double Total(IEnumerable<int> books)
77
{
88
throw new NotImplementedException("You need to implement this function.");
99
}
Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,97 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
// This file was auto-generated based on version 1.0.1 of the canonical data.
2+
33
using Xunit;
44

55
public class BookStoreTest
66
{
77
[Fact]
8-
public void Basket_with_single_book()
8+
public void Only_a_single_book()
99
{
10-
Assert.Equal(8, BookStore.CalculateTotalCost(MakeList(1)));
10+
var input = new[] { 1 };
11+
Assert.Equal(8, BookStore.Total(input));
1112
}
1213

1314
[Fact(Skip = "Remove to run test")]
14-
public void Basket_with_two_of_same_book()
15+
public void Two_of_the_same_book()
1516
{
16-
Assert.Equal(16, BookStore.CalculateTotalCost(MakeList(2, 2)));
17+
var input = new[] { 2, 2 };
18+
Assert.Equal(16, BookStore.Total(input));
1719
}
1820

1921
[Fact(Skip = "Remove to run test")]
2022
public void Empty_basket()
2123
{
22-
Assert.Equal(0, BookStore.CalculateTotalCost(MakeList()));
24+
var input = new int[0];
25+
Assert.Equal(0, BookStore.Total(input));
2326
}
2427

2528
[Fact(Skip = "Remove to run test")]
26-
public void Basket_with_two_different_books()
29+
public void Two_different_books()
2730
{
28-
Assert.Equal(15.2, BookStore.CalculateTotalCost(MakeList(1, 2)));
31+
var input = new[] { 1, 2 };
32+
Assert.Equal(15.2, BookStore.Total(input));
2933
}
3034

3135
[Fact(Skip = "Remove to run test")]
32-
public void Basket_with_three_different_books()
36+
public void Three_different_books()
3337
{
34-
Assert.Equal(21.6, BookStore.CalculateTotalCost(MakeList(1, 2, 3)));
38+
var input = new[] { 1, 2, 3 };
39+
Assert.Equal(21.6, BookStore.Total(input));
3540
}
3641

3742
[Fact(Skip = "Remove to run test")]
38-
public void Basket_with_four_different_books()
43+
public void Four_different_books()
3944
{
40-
Assert.Equal(25.6, BookStore.CalculateTotalCost(MakeList(1, 2, 3, 4)));
45+
var input = new[] { 1, 2, 3, 4 };
46+
Assert.Equal(25.6, BookStore.Total(input));
4147
}
4248

4349
[Fact(Skip = "Remove to run test")]
44-
public void Basket_with_five_different_books()
50+
public void Five_different_books()
4551
{
46-
Assert.Equal(30, BookStore.CalculateTotalCost(MakeList(1, 2, 3, 4, 5)));
52+
var input = new[] { 1, 2, 3, 4, 5 };
53+
Assert.Equal(30, BookStore.Total(input));
4754
}
4855

4956
[Fact(Skip = "Remove to run test")]
50-
public void Basket_with_eight_books()
57+
public void Two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three()
5158
{
52-
Assert.Equal(51.20, BookStore.CalculateTotalCost(MakeList(1, 1, 2, 2, 3, 3, 4, 5)));
59+
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 5 };
60+
Assert.Equal(51.2, BookStore.Total(input));
5361
}
5462

5563
[Fact(Skip = "Remove to run test")]
56-
public void Basket_with_nine_books()
64+
public void Group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three()
5765
{
58-
Assert.Equal(55.60, BookStore.CalculateTotalCost(MakeList(1, 1, 2, 2, 3, 3, 4, 4, 5)));
66+
var input = new[] { 1, 1, 2, 2, 3, 4 };
67+
Assert.Equal(40.8, BookStore.Total(input));
5968
}
6069

6170
[Fact(Skip = "Remove to run test")]
62-
public void Basket_with_ten_books()
71+
public void Two_each_of_first_4_books_and_1_copy_each_of_rest()
6372
{
64-
Assert.Equal(60, BookStore.CalculateTotalCost(MakeList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)));
73+
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5 };
74+
Assert.Equal(55.6, BookStore.Total(input));
6575
}
6676

6777
[Fact(Skip = "Remove to run test")]
68-
public void Basket_with_eleven_books()
78+
public void Two_copies_of_each_book()
6979
{
70-
Assert.Equal(68, BookStore.CalculateTotalCost(MakeList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1)));
80+
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
81+
Assert.Equal(60, BookStore.Total(input));
7182
}
7283

7384
[Fact(Skip = "Remove to run test")]
74-
public void Basket_with_twelve_books()
85+
public void Three_copies_of_first_book_and_2_each_of_remaining()
7586
{
76-
Assert.Equal(75.20, BookStore.CalculateTotalCost(MakeList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2)));
87+
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1 };
88+
Assert.Equal(68, BookStore.Total(input));
7789
}
7890

79-
private static List<int> MakeList(params int[] values)
91+
[Fact(Skip = "Remove to run test")]
92+
public void Three_each_of_first_2_books_and_2_each_of_remaining_books()
8093
{
81-
return values.ToList();
94+
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2 };
95+
Assert.Equal(75.2, BookStore.Total(input));
8296
}
83-
}
97+
}

exercises/book-store/Example.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
public static class BookStore
66
{
7-
public static double CalculateTotalCost(List<int> books)
7+
public static double Total(IEnumerable<int> books)
88
{
9-
return CalculateTotalCost(books, 0);
9+
return Total(books, 0);
1010
}
1111

12-
private static double CalculateTotalCost(List<int> books, double priceSoFar)
12+
private static double Total(IEnumerable<int> books, double priceSoFar)
1313
{
14-
if (books.Count == 0)
14+
if (!books.Any())
1515
{
1616
return priceSoFar;
1717
}
@@ -33,7 +33,7 @@ private static double CalculateTotalCost(List<int> books, double priceSoFar)
3333
remaining.Remove(item);
3434
}
3535

36-
var price = CalculateTotalCost(remaining.ToList(), priceSoFar + CostPerGroup(i));
36+
var price = Total(remaining.ToList(), priceSoFar + CostPerGroup(i));
3737
minPrice = Math.Min(minPrice, price);
3838
}
3939

exercises/transpose/TransposeTest.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public void Empty_string()
1515
[Fact(Skip = "Remove to run test")]
1616
public void Two_characters_in_a_row()
1717
{
18-
var input =
19-
"A1";
18+
var input = "A1";
2019
var expected =
2120
"A\n" +
2221
"1";
@@ -29,8 +28,7 @@ public void Two_characters_in_a_column()
2928
var input =
3029
"A\n" +
3130
"1";
32-
var expected =
33-
"A1";
31+
var expected = "A1";
3432
Assert.Equal(expected, Transpose.String(input));
3533
}
3634

@@ -50,8 +48,7 @@ public void Simple()
5048
[Fact(Skip = "Remove to run test")]
5149
public void Single_line()
5250
{
53-
var input =
54-
"Single line.";
51+
var input = "Single line.";
5552
var expected =
5653
"S\n" +
5754
"i\n" +

generators/Exercise.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ protected Exercise()
99
{
1010
Name = GetType().ToExerciseName();
1111
CanonicalData = CanonicalDataParser.Parse(Name);
12-
Configuration = new ExerciseConfiguration();
1312
}
1413

1514
public string Name { get; }
1615
public CanonicalData CanonicalData { get; }
17-
public ExerciseConfiguration Configuration { get; }
1816

1917
public void Generate() => TestClassFile.Write(this, Render());
2018

generators/ExerciseConfiguration.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

generators/Exercises/BeerSong.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using Generators.Output;
2-
3-
namespace Generators.Exercises
1+
namespace Generators.Exercises
42
{
53
public class BeerSong : Exercise
64
{
75
public BeerSong()
86
{
97
foreach (var canonicalDataCase in CanonicalData.Cases)
10-
canonicalDataCase.Expected = new MultiLineString(canonicalDataCase.Expected.ToString());
8+
canonicalDataCase.UseExpectedParameter = true;
119
}
1210
}
1311
}

generators/Exercises/BookStore.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace Generators.Exercises
4+
{
5+
public class BookStore : Exercise
6+
{
7+
public BookStore()
8+
{
9+
foreach (var canonicalDataCase in CanonicalData.Cases)
10+
{
11+
canonicalDataCase.Input = ((JArray)canonicalDataCase.Properties["basket"]).Values<int>();
12+
canonicalDataCase.UseInputParameters = true;
13+
}
14+
}
15+
}
16+
}

generators/Exercises/FoodChain.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Generators.Output;
2-
using Newtonsoft.Json.Linq;
1+
using Newtonsoft.Json.Linq;
32

43
namespace Generators.Exercises
54
{
@@ -8,7 +7,10 @@ public class FoodChain : Exercise
87
public FoodChain()
98
{
109
foreach (var canonicalDataCase in CanonicalData.Cases)
11-
canonicalDataCase.Expected = new MultiLineString((JArray)canonicalDataCase.Expected);
10+
{
11+
canonicalDataCase.Expected = string.Join("\n", (JArray)canonicalDataCase.Expected);
12+
canonicalDataCase.UseExpectedParameter = true;
13+
}
1214
}
1315
}
1416
}

generators/Exercises/NthPrime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public class NthPrime : Exercise
66
{
77
public NthPrime()
88
{
9-
Configuration.ExceptionType = typeof(ArgumentOutOfRangeException);
10-
Configuration.ThrowExceptionWhenExpectedValueEquals = x => x is bool;
9+
foreach (var canonicalDataCase in CanonicalData.Cases)
10+
canonicalDataCase.ExceptionThrown = canonicalDataCase.Expected is bool ? typeof(ArgumentOutOfRangeException) : null;
1111
}
1212
}
1313
}

0 commit comments

Comments
 (0)