-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiso_4217_test.gleam
77 lines (66 loc) · 1.75 KB
/
iso_4217_test.gleam
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
import dime.{type Currency}
import gleam/dynamic.{field, int, list, string}
import gleam/io
import gleam/json
import gleam/list
import gleam/result
import gleeunit/should
import simplifile
type TestCurrency {
TestCurrency(
country: String,
display_name: String,
alpha_code: String,
numeric_code: String,
minor_units: Int,
)
}
fn test_currency_list_decoder() {
dynamic.decode5(
TestCurrency,
field("country", of: string),
field("display_name", of: string),
field("alpha_code", of: string),
field("numeric_code", of: string),
field("minor_units", of: int),
)
|> list
}
pub fn iso_4217__test() {
let assert Ok(data) = simplifile.read("test/data/currencies.json")
let assert Ok(test_currencies) =
json.decode(from: data, using: test_currency_list_decoder())
test_currencies
|> list.each(test_from_alpha_code)
test_currencies
|> list.each(test_from_numeric_code)
}
fn test_from_alpha_code(test_currency: TestCurrency) {
dime.from_alpha_code(test_currency.alpha_code)
|> result.map_error(fn(error) {
io.println("Failed to parse \"" <> test_currency.alpha_code <> "\"")
error
})
|> should.be_ok
|> common_assertions(test_currency)
}
fn test_from_numeric_code(test_currency: TestCurrency) {
dime.from_numeric_code(test_currency.numeric_code)
|> result.map_error(fn(error) {
io.println("Failed to parse \"" <> test_currency.numeric_code <> "\"")
error
})
|> should.be_ok
|> common_assertions(test_currency)
}
fn common_assertions(actual: Currency, expected: TestCurrency) {
actual
|> dime.alpha_code
|> should.equal(expected.alpha_code)
actual
|> dime.numeric_code
|> should.equal(expected.numeric_code)
actual
|> dime.minor_units
|> should.equal(expected.minor_units)
}