Last active
January 14, 2022 07:50
-
-
Save letyletylety/85bd9b5f85e3b19c2b87cafb240763ef to your computer and use it in GitHub Desktop.
dart dart equality test1
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
import 'package:equatable/equatable.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:logger/logger.dart'; | |
/// Object | |
/// The base class for all Dart objects except `null`. | |
/// | |
/// Because `Object` is a root of the non-nullable Dart class hierarchy, | |
/// every other non-`Null` Dart class is a subclass of `Object`. | |
class Point extends Object { | |
Point(this.y, this.x); | |
int y; | |
int x; | |
void change(int newY, int newX) { | |
y = newY; | |
x = newX; | |
} | |
@override | |
String toString() { | |
return 'Point {y: $y x: $x}'; | |
} | |
} | |
@immutable | |
class ImmutablePoint { | |
// const constructor | |
const ImmutablePoint(this.y, this.x); | |
final int y; | |
final int x; | |
// void change(int newY, int newX) { | |
// y = newY; | |
// x = newX; | |
// } | |
} | |
@immutable | |
class ImmutablePoint2 { | |
// const constructor | |
const ImmutablePoint2(this.y, this.x); | |
final int y; | |
final int x; | |
@override | |
bool operator ==(Object other) { | |
return (other is ImmutablePoint2) && other.x == x && other.y == y; | |
} | |
// Map 과 Set 에서 key 역할을 한다. | |
@override | |
int get hashCode => x.hashCode & y.hashCode; | |
} | |
@immutable | |
class ImmutablePoint3 extends Equatable { | |
// const constructor | |
const ImmutablePoint3(this.y, this.x); | |
final int y; | |
final int x; | |
@override | |
List<Object?> get props => [y, x]; | |
} | |
void main() { | |
final log = Logger(printer: PrettyPrinter(methodCount: 0)).i; | |
group('equality test', () { | |
test('test 1', () { | |
// 1과 2는 다르다 | |
expect(1 == 2, false); | |
// 1과 1은 같다. | |
expect(1 == 1, true); | |
// 1.0 과 1은 같다. (num 타입) | |
expect(1.0 == 1, true); | |
expect('Hello' == 'Hi', false); | |
expect('Hello' == 'hello', false); | |
// 대소문자까지 같아야 같다. | |
expect('Hello' == 'Hello', true); | |
// 다트에서 1은 true 가 아니다. | |
expect(1 == true, false); | |
// 0도 false가 아니다. | |
expect(0 == false, false); | |
}); | |
test('object test', () { | |
// (1,2) 와 (2,3)은 다르다. | |
final point1 = Point(1, 2); | |
expect(point1 == Point(2, 3), false); | |
// point2는 그대로 복사했으니 같다 | |
final point2 = point1; | |
expect(point1 == point2, true); | |
/// 포인트 1의 값을 이용해 새로운 Point3을 만듬 | |
// 들어있는 y와 x의 값은 같지만 다르다. | |
final newX = point1.x; | |
final newY = point1.y; | |
final point3 = Point(newY, newX); | |
expect(point1 == point3, false); | |
expect(identical(point1, point3), false); | |
// 값을 바꾼다. | |
// 값을 바꿨지만 point2는 point1과 같다. | |
point2.change(2, 3); | |
log(point2); | |
log(point1); | |
expect(point1 == point2, true); | |
expect(point1 == Point(2, 3), false); | |
expect(identical(Point(2, 3), point1), false); | |
expect(Point(1, 2) == Point(1, 2), false); | |
}); | |
test('object test 2', () { | |
final point1 = Point(1, 2); | |
final point2 = Point(2, 3); | |
expect(point1.runtimeType == point2.runtimeType, true); | |
}); | |
test('immutable object test', () { | |
const point1 = ImmutablePoint(1, 2); | |
// 값이 다른 const object | |
const point2 = ImmutablePoint(2, 3); | |
// 당연히 다름 | |
expect(point1 == point2, false); | |
// 값이 같은 const object | |
const point3 = ImmutablePoint(1, 2); | |
expect(point1 == point3, true); | |
// const를 쓰지 않으면 | |
// non constant object 가 만들어 진다. | |
var point4 = ImmutablePoint(1, 2); | |
expect(point1 == point4, false); | |
expect(const ImmutablePoint(1, 2) == const ImmutablePoint(1, 2), true); | |
expect(ImmutablePoint(1, 2) == ImmutablePoint(1, 2), false); | |
}); | |
test('immutable 2', () { | |
expect(ImmutablePoint2(1, 2) == ImmutablePoint2(1, 2), true); | |
}); | |
test('immutable 3', () { | |
expect(ImmutablePoint3(1, 2) == ImmutablePoint3(1, 2), true); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment