-
Notifications
You must be signed in to change notification settings - Fork 0
/
outer_source_test.dart
43 lines (36 loc) · 1.05 KB
/
outer_source_test.dart
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
import 'dart:convert';
import 'dart:io';
import 'package:test_api/test_api.dart';
// JSONをこのクラスにパースする
class MenuItem {
final int id;
final String name;
final List<Topping> toppings;
const MenuItem(this.id, this.name, this.toppings);
}
class Topping {
final int id;
final String type;
const Topping(this.id, this.type);
}
// パース本体
MenuItem parseJsonString(String jsonString) {
final rawMenu = jsonDecode(jsonString);
final List<dynamic> rawToppings = rawMenu["topping"] ?? [];
return MenuItem(
int.parse(rawMenu["id"]),
rawMenu["name"],
rawToppings
.map((rawTopping) =>
Topping(int.parse(rawTopping["id"]), rawTopping["type"]))
.toList());
}
void main() {
test("JSONを期待通りにパースできるか", () {
// 実行パスはプロジェクトルートになるので注意
final sourceFile = File("./test/stub_data/sample.json");
final item = parseJsonString(
sourceFile.readAsStringSync());
expect(item.name, "Cake");
});
}