Skip to content

Commit 2038c09

Browse files
committed
Renamed CryptoStatsSourceTest.cs and added a CryptoNameResolver, that resolves cryptocurrency names based on their symbols
1 parent 711ff70 commit 2038c09

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace CryptoStatsSource
6+
{
7+
public interface ICryptoNameResolver
8+
{
9+
public Task<string> Resolve(string symbol);
10+
11+
public Task Refresh();
12+
}
13+
14+
public class CryptoNameResolverImpl : ICryptoNameResolver
15+
{
16+
private ICryptoStatsSource _cryptoStatsSource;
17+
private Dictionary<String, String> _symbolToNameMap;
18+
19+
public CryptoNameResolverImpl(ICryptoStatsSource cryptoStatsSource)
20+
{
21+
_cryptoStatsSource = cryptoStatsSource;
22+
}
23+
24+
public async Task Refresh()
25+
{
26+
_symbolToNameMap = new();
27+
(await _cryptoStatsSource.GetAvailableCryptocurrencies()).ForEach(c =>
28+
_symbolToNameMap.TryAdd(c.Symbol, c.Name));
29+
}
30+
31+
32+
public async Task<string> Resolve(string symbol)
33+
{
34+
if (_symbolToNameMap?.GetValueOrDefault(symbol) == null) await Refresh();
35+
36+
return _symbolToNameMap.GetValueOrDefault(symbol, null);
37+
}
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using CryptoStatsSource;
2+
using Xunit;
3+
4+
namespace Tests.Integration.CryptoStatsSource
5+
{
6+
public class ResolveNameTest
7+
{
8+
private readonly CoingeckoSource _source;
9+
private readonly CryptoNameResolverImpl _resolver;
10+
11+
public ResolveNameTest()
12+
{
13+
_source = new();
14+
_resolver = new(_source);
15+
}
16+
17+
[Fact]
18+
public async void SimpleThreeEntries()
19+
{
20+
Assert.Equal("Bitcoin", await _resolver.Resolve("btc"));
21+
Assert.Equal("Cardano", await _resolver.Resolve("ada"));
22+
Assert.Equal("Litecoin", await _resolver.Resolve("ltc"));
23+
Assert.Equal("Ethereum", await _resolver.Resolve("eth"));
24+
Assert.Null(await _resolver.Resolve("abcefghbzbzrfoo"));
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)