-
Notifications
You must be signed in to change notification settings - Fork 4
/
TransformerTests.cs
108 lines (94 loc) · 3.25 KB
/
TransformerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections.Generic;
using System.Threading.Tasks;
using Codez;
using Codez.Alphabets;
using Codez.Generators;
using Codez.Transformers;
using Xunit;
using Xunit.Abstractions;
namespace Tests
{
public class TransformerTests
{
private readonly ITestOutputHelper output;
public TransformerTests(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public async Task Can_generate_and_transform_to_container_names()
{
var generator = new CodeGenerator(
alphabet: new StringAlphabet("0123456789"),
transformer: new ContainerNamesTransformer()
);
var result = await generator.GenerateAsync(2);
output.WriteLine(result);
Assert.NotNull(result);
Assert.Contains("_", result);
}
[Fact]
public async Task Can_fail_to_transform()
{
var generator = new CodeGenerator(
alphabet: new StringAlphabet("0123456789"),
transformer: new ContainerNamesTransformer()
);
var result = await generator.TryGenerateAsync(1);
Assert.False(result.Success);
Assert.Equal(FailureReasonType.Transform, result.Reason);
}
public class ContainerNamesTransformer : ITransformer
{
/// <summary>
/// Space dudes
/// </summary>
private readonly Dictionary<char, string> names = new Dictionary<char, string>
{
{'0', "Surfer"},
{'1', "Armstrong" },
{'2', "Aldrin"},
{'3', "Bowie"},
{'4', "Baker"},
{'5', "Starlord"},
{'6', "Conrad"},
{'7', "Duke"},
{'8', "Doctor"},
{'9', "Galactus" }
};
private readonly Dictionary<char, string> adjectives = new Dictionary<char, string>
{
{ '0', "Funny"},
{ '1', "Sleepy"},
{ '2', "Happy"},
{ '3', "Boring"},
{ '4', "Manic"},
{ '5', "Excited"},
{ '6', "Gassy" },
{ '7', "Cuddly" },
{ '8', "Smelly" },
{ '9', "Scratchy" }
};
public ValueTask<CodeGeneratorResult> Transform(CodeGeneratorResult result)
{
if (result.Value.Length != 2)
return new ValueTask<CodeGeneratorResult>(new CodeGeneratorResult
{
Value = null,
Reason = FailureReasonType.Transform,
Retries = result.Retries,
Success = false
});
var adjective = adjectives[result.Value[0]];
var name = names[result.Value[1]];
return new ValueTask<CodeGeneratorResult>(new CodeGeneratorResult
{
Value = $"{adjective}_{name}",
Reason = result.Reason,
Retries = result.Retries,
Success = result.Success
});
}
}
}
}