Skip to content

Commit

Permalink
Merge branch 'main' into feature/#1
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Jun 12, 2022
2 parents d1a5a19 + 8f94295 commit eeaebaf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/model/operators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final Map<String, Operator> _strOprMap = {
")": 0x002,
"+": 0x100,
"-": 0x101,
"//": 0x102,
"*": 0x110,
"/": 0x111,
"^": 0x120,
Expand All @@ -28,6 +29,7 @@ final Map<String, Operator> _strOprMap = {
final Map<Operator, BinaryOperator> _binaryOprFuncMap = {
0x100: (left, right) => left + right,
0x101: (left, right) => left - right,
0x102: (left, right) => left * right / (left + right),
0x110: (left, right) => left * right,
0x111: (left, right) => left / right,
0x120: (left, right) => pow(left, right).toDouble()
Expand Down Expand Up @@ -142,6 +144,9 @@ class Operators {
/// `-` 引き算
static const sub = 0x101;

/// `//` 並列接続の抵抗の足し算演算子
static const para = 0x102;

/// `*` 掛け算
static const multi = 0x110;

Expand Down
35 changes: 35 additions & 0 deletions lib/model/result_data.dart
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,
};
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
shared_preferences: ^2.0.15

dev_dependencies:
flutter_test:
Expand Down
44 changes: 44 additions & 0 deletions test/model/result_data_test.dart
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);
});
}

0 comments on commit eeaebaf

Please sign in to comment.