forked from google/built_value.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher_test.dart
142 lines (117 loc) · 4.65 KB
/
matcher_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2017, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import 'package:built_value/built_value.dart';
import 'package:built_value_test/matcher.dart';
import 'package:test/test.dart';
import 'values.dart';
void main() {
group('built_value matcher', () {
test('matches if same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..string = 'str');
expect(value, equalsBuilt(value));
});
test('reports if not same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..string = 'str');
final otherValue = value.rebuild((b) => b..simpleValue.anInt = 5);
_expectMismatch(value, otherValue,
"at location ['simpleValue']['anInt'] is <3> instead of <5>");
});
test('reports deep match on lists if not same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..simpleValue.list.add('foo')
..string = 'str');
final otherValue = value.rebuild((b) => b..simpleValue.list.add('bar'));
_expectMismatch(value, otherValue, 'shorter than expected');
_expectMismatch(value, otherValue, "['simpleValue']['list'][1]");
});
test('reports deep match on list multimaps if not same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..simpleValue.multimap.add(42, true)
..string = 'str');
final otherValue =
value.rebuild((b) => b..simpleValue.multimap.add(42, false));
_expectMismatch(
value, otherValue, " ['simpleValue']['multimap']['42'][1]");
_expectMismatch(value, otherValue, 'shorter than expected');
});
test('reports deep match on maps if not same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..simpleValue.map['foo'] = 3
..simpleValue.map['bar'] = 4
..string = 'str');
final otherValue = value.rebuild((b) => b..simpleValue.map['bar'] = 5);
_expectMismatch(value, otherValue,
"at location ['simpleValue']['map']['bar'] is <4> instead of <5>");
});
test('reports deep match on sets if not same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..simpleValue.aSet.add(42)
..string = 'str');
final otherValue = value.rebuild((b) => b..simpleValue.aSet.remove(42));
_expectMismatch(value, otherValue, "['simpleValue']['aSet']");
_expectMismatch(value, otherValue, 'larger than expected');
});
test('reports deep match on set multimaps if not same', () {
final value = CompoundValue((b) => b
..simpleValue.anInt = 3
..simpleValue.setMultimap.add(42, true)
..string = 'str');
final otherValue =
value.rebuild((b) => b..simpleValue.setMultimap.add(42, false));
_expectMismatch(
value, otherValue, "['simpleValue']['setMultimap']['42'][1]");
_expectMismatch(value, otherValue, 'shorter than expected');
});
test('reports if the wrong type', () {
final value = 42;
final otherValue = CompoundValue((b) => b..simpleValue.anInt = 5);
_expectMismatch(value, otherValue, 'is the wrong type');
});
test('compared value matcher', () {
final value = ComparedValue((b) => b
..name = 'foo'
..onChanged = () => 'Change happened!');
final otherValue = ComparedValue((b) => b
..name = 'foo'
..onChanged = () => 'Change happened!');
expect(value, otherValue);
});
test('compared value matcher with different onChanged outcomes', () {
final value = ComparedValue((b) => b
..name = 'foo'
..onChanged = () => 'Change happened!');
final otherValue = ComparedValue((b) => b
..name = 'foo'
..onChanged = () => 'Change did not happen!');
expect(value, otherValue);
});
test('compared value matcher with different names', () {
final value = ComparedValue((b) => b
..name = 'foo'
..onChanged = () => 'Change happened!');
final otherValue = ComparedValue((b) => b
..name = 'bar'
..onChanged = () => 'Change did not happen!');
_expectMismatch(value, otherValue, 'is \'foo\' instead of \'bar\'');
});
});
}
void _expectMismatch(
Object value, Built otherValue, String expectedMismatchMessage) {
try {
expect(value, equalsBuilt(otherValue));
} catch (exception) {
expect(exception.toString(), contains(expectedMismatchMessage));
return;
}
throw StateError('Expected mismatch.');
}