| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.class_equality_test; | |
| 6 | |
| 7 @MirrorsUsed(targets: "test.class_equality_test") | |
| 8 import 'dart:mirrors'; | |
| 9 | |
| 10 import 'package:expect/expect.dart'; | |
| 11 | |
| 12 class A<T> {} | |
| 13 | |
| 14 class B extends A<int> {} | |
| 15 | |
| 16 class BadEqualityHash { | |
| 17 int count = 0; | |
| 18 bool operator ==(other) => true; | |
| 19 int get hashCode => count++; | |
| 20 } | |
| 21 | |
| 22 typedef bool Predicate(Object o); | |
| 23 Predicate somePredicate; | |
| 24 | |
| 25 checkEquality(List<Map> equivalenceClasses) { | |
| 26 for (var equivalenceClass in equivalenceClasses) { | |
| 27 equivalenceClass.forEach((name, member) { | |
| 28 equivalenceClass.forEach((otherName, otherMember) { | |
| 29 // Reflexivity, symmetry and transitivity. | |
| 30 Expect.equals(member, otherMember, "$name == $otherName"); | |
| 31 Expect.equals(member.hashCode, otherMember.hashCode, | |
| 32 "$name.hashCode == $otherName.hashCode"); | |
| 33 }); | |
| 34 for (var otherEquivalenceClass in equivalenceClasses) { | |
| 35 if (otherEquivalenceClass == equivalenceClass) continue; | |
| 36 otherEquivalenceClass.forEach((otherName, otherMember) { | |
| 37 Expect.notEquals( | |
| 38 member, otherMember, "$name != $otherName"); // Exclusion. | |
| 39 // Hash codes may or may not be equal. | |
| 40 }); | |
| 41 } | |
| 42 }); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 void subroutine() {} | |
| 47 | |
| 48 main() { | |
| 49 LibraryMirror thisLibrary = currentMirrorSystem() | |
| 50 .findLibrary(const Symbol('test.class_equality_test')); | |
| 51 | |
| 52 var o1 = new Object(); | |
| 53 var o2 = new Object(); | |
| 54 | |
| 55 var badEqualityHash1 = new BadEqualityHash(); | |
| 56 var badEqualityHash2 = new BadEqualityHash(); | |
| 57 | |
| 58 checkEquality([ | |
| 59 {'reflect(o1)': reflect(o1), 'reflect(o1), again': reflect(o1)}, | |
| 60 {'reflect(o2)': reflect(o2), 'reflect(o2), again': reflect(o2)}, | |
| 61 { | |
| 62 'reflect(badEqualityHash1)': reflect(badEqualityHash1), | |
| 63 'reflect(badEqualityHash1), again': reflect(badEqualityHash1) | |
| 64 }, | |
| 65 { | |
| 66 'reflect(badEqualityHash2)': reflect(badEqualityHash2), | |
| 67 'reflect(badEqualityHash2), again': reflect(badEqualityHash2) | |
| 68 }, | |
| 69 {'reflect(true)': reflect(true), 'reflect(true), again': reflect(true)}, | |
| 70 {'reflect(false)': reflect(false), 'reflect(false), again': reflect(false)}, | |
| 71 {'reflect(null)': reflect(null), 'reflect(null), again': reflect(null)}, | |
| 72 { | |
| 73 'reflect(3.5+4.5)': reflect(3.5 + 4.5), | |
| 74 'reflect(6.5+1.5)': reflect(6.5 + 1.5) | |
| 75 }, | |
| 76 {'reflect(3+4)': reflect(3 + 4), 'reflect(6+1)': reflect(6 + 1)}, | |
| 77 {'reflect("foo")': reflect("foo"), 'reflect("foo"), again': reflect("foo")}, | |
| 78 { | |
| 79 'currentMirrorSystem().voidType': currentMirrorSystem().voidType, | |
| 80 'thisLibrary.declarations[#subroutine].returnType': | |
| 81 (thisLibrary.declarations[#subroutine] as MethodMirror).returnType | |
| 82 }, | |
| 83 { | |
| 84 'currentMirrorSystem().dynamicType': currentMirrorSystem().dynamicType, | |
| 85 'thisLibrary.declarations[#main].returnType': | |
| 86 (thisLibrary.declarations[#main] as MethodMirror).returnType | |
| 87 }, | |
| 88 { | |
| 89 'reflectClass(A)': reflectClass(A), | |
| 90 'thisLibrary.declarations[#A]': thisLibrary.declarations[#A], | |
| 91 'reflect(new A<int>()).type.originalDeclaration': | |
| 92 reflect(new A<int>()).type.originalDeclaration | |
| 93 }, | |
| 94 { | |
| 95 'reflectClass(B).superclass': reflectClass(B).superclass, | |
| 96 'reflect(new A<int>()).type': reflect(new A<int>()).type | |
| 97 }, | |
| 98 { | |
| 99 'reflectClass(B)': reflectClass(B), | |
| 100 'thisLibrary.declarations[#B]': thisLibrary.declarations[#B], | |
| 101 'reflect(new B()).type': reflect(new B()).type | |
| 102 }, | |
| 103 { | |
| 104 'reflectClass(BadEqualityHash).declarations[#==]': | |
| 105 reflectClass(BadEqualityHash).declarations[#==], | |
| 106 'reflect(new BadEqualityHash()).type.declarations[#==]': | |
| 107 reflect(new BadEqualityHash()).type.declarations[#==] | |
| 108 }, | |
| 109 { | |
| 110 'reflectClass(BadEqualityHash).declarations[#==].parameters[0]': | |
| 111 (reflectClass(BadEqualityHash).declarations[#==] as MethodMirror) | |
| 112 .parameters[0], | |
| 113 'reflect(new BadEqualityHash()).type.declarations[#==].parameters[0]': | |
| 114 (reflect(new BadEqualityHash()).type.declarations[#==] | |
| 115 as MethodMirror) | |
| 116 .parameters[0] | |
| 117 }, | |
| 118 { | |
| 119 'reflectClass(BadEqualityHash).declarations[#count]': | |
| 120 reflectClass(BadEqualityHash).declarations[#count], | |
| 121 'reflect(new BadEqualityHash()).type.declarations[#count]': | |
| 122 reflect(new BadEqualityHash()).type.declarations[#count] | |
| 123 }, | |
| 124 { | |
| 125 'reflectType(Predicate)': reflectType(Predicate), | |
| 126 'thisLibrary.declarations[#somePredicate].type': | |
| 127 (thisLibrary.declarations[#somePredicate] as VariableMirror).type | |
| 128 }, | |
| 129 { | |
| 130 'reflectType(Predicate).referent': | |
| 131 (reflectType(Predicate) as TypedefMirror).referent, | |
| 132 'thisLibrary.declarations[#somePredicate].type.referent': | |
| 133 ((thisLibrary.declarations[#somePredicate] as VariableMirror).type | |
| 134 as TypedefMirror) | |
| 135 .referent | |
| 136 }, | |
| 137 { | |
| 138 'reflectClass(A).typeVariables.single': | |
| 139 reflectClass(A).typeVariables.single, | |
| 140 'reflect(new A<int>()).type.originalDeclaration.typeVariables.single': | |
| 141 reflect(new A<int>()).type.originalDeclaration.typeVariables.single | |
| 142 }, | |
| 143 {'currentMirrorSystem()': currentMirrorSystem()}, | |
| 144 {'currentMirrorSystem().isolate': currentMirrorSystem().isolate}, | |
| 145 { | |
| 146 'thisLibrary': thisLibrary, | |
| 147 'reflectClass(A).owner': reflectClass(A).owner, | |
| 148 'reflectClass(B).owner': reflectClass(B).owner, | |
| 149 'reflect(new A()).type.owner': reflect(new A()).type.owner, | |
| 150 'reflect(new B()).type.owner': reflect(new B()).type.owner | |
| 151 }, | |
| 152 ]); | |
| 153 } | |
| OLD | NEW |