-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class ResultData { | ||
/// id | ||
final int id; | ||
|
||
/// 半角空白を区切り文字とした式 | ||
final String formula; | ||
|
||
/// 計算結果 | ||
final double result; | ||
|
||
/// 作成された日時 | ||
final DateTime createdDate; | ||
|
||
ResultData(this.id, this.formula, this.result, this.createdDate); | ||
|
||
/// 式の配列から`ResultDataを生成します。 | ||
ResultData.fromList( | ||
this.id, List<String> formulaList, this.result, this.createdDate) | ||
: formula = formulaList.join(" "); | ||
|
||
/// JSONをデコードして出てきた`Map<String, dynamic>`から[ResultData]を生成します。 | ||
ResultData.fromJson(Map<String, dynamic> json) | ||
: id = json["id"], | ||
formula = json["formula"], | ||
result = json["result"], | ||
createdDate = json["created_date"]; | ||
|
||
/// JSONへエンコードするためにデータを変換します。 | ||
Map<String, dynamic> toJson() => { | ||
"id": id, | ||
"formula": formula, | ||
"result": result, | ||
"created_date": createdDate, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:calculator2022/model/result_data.dart'; | ||
|
||
void main() { | ||
test("fromJson_test", () { | ||
final json = <String, dynamic>{ | ||
"id": 0, | ||
"formula": "fo-myura", | ||
"result": 0.3232, | ||
"created_date": DateTime(2001, 05, 24, 12, 34) | ||
}; | ||
var jsd = ResultData.fromJson(json); | ||
|
||
expect(jsd.formula, "fo-myura"); | ||
}); | ||
|
||
test("toJson_test", () { | ||
final jsd = ResultData(1, "fo-myura", 3, DateTime(2001, 05, 24, 12, 34)); | ||
final json = <String, dynamic>{ | ||
"id": 1, | ||
"formula": "fo-myura", | ||
"result": 3, | ||
"created_date": DateTime(2001, 05, 24, 12, 34) | ||
}; | ||
final jsdJson = jsd.toJson(); | ||
|
||
expect(jsdJson, json); | ||
}); | ||
|
||
test("toJsonList_test", () { | ||
final formulaList = ["1", "2", "+"]; | ||
final jsd = | ||
ResultData.fromList(1, formulaList, 3, DateTime(2001, 05, 24, 12, 34)); | ||
final json = <String, dynamic>{ | ||
"id": 1, | ||
"formula": "1 2 +", | ||
"result": 3, | ||
"created_date": DateTime(2001, 05, 24, 12, 34) | ||
}; | ||
var jsdJson = jsd.toJson(); | ||
|
||
expect(jsdJson, json); | ||
}); | ||
} |