-
Notifications
You must be signed in to change notification settings - Fork 16
/
source_edit_test.dart
139 lines (115 loc) · 4.48 KB
/
source_edit_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
// Copyright (c) 2020, the Dart project authors. 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:test/test.dart';
import 'package:yaml_edit/yaml_edit.dart';
void main() {
group('SourceEdit', () {
group('fromJson', () {
test('converts from jsonEncode', () {
final sourceEditMap = {
'offset': 1,
'length': 2,
'replacement': 'replacement string'
};
final sourceEdit = SourceEdit.fromJson(sourceEditMap);
expect(sourceEdit.offset, 1);
expect(sourceEdit.length, 2);
expect(sourceEdit.replacement, 'replacement string');
});
test('throws formatException if offset is non-int', () {
final sourceEditJson = {
'offset': '1',
'length': 2,
'replacement': 'replacement string'
};
expect(
() => SourceEdit.fromJson(sourceEditJson), throwsFormatException);
});
test('throws formatException if length is non-int', () {
final sourceEditJson = {
'offset': 1,
'length': '2',
'replacement': 'replacement string'
};
expect(
() => SourceEdit.fromJson(sourceEditJson), throwsFormatException);
});
test('throws formatException if replacement is non-string', () {
final sourceEditJson = {'offset': 1, 'length': 2, 'replacement': 3};
expect(
() => SourceEdit.fromJson(sourceEditJson), throwsFormatException);
});
test('throws formatException if a field is not present', () {
final sourceEditJson = {'offset': 1, 'length': 2};
expect(
() => SourceEdit.fromJson(sourceEditJson), throwsFormatException);
});
});
test('toString returns a nice string representation', () {
final sourceEdit = SourceEdit(1, 2, 'replacement string');
expect(sourceEdit.toString(),
equals('SourceEdit(1, 2, "replacement string")'));
});
group('hashCode', () {
test('returns same value for equal SourceEdits', () {
final sourceEdit1 = SourceEdit(1, 2, 'replacement string');
final sourceEdit2 = SourceEdit(1, 2, 'replacement string');
expect(sourceEdit1.hashCode, equals(sourceEdit2.hashCode));
});
test('returns different value for equal SourceEdits', () {
final sourceEdit1 = SourceEdit(1, 2, 'replacement string');
final sourceEdit2 = SourceEdit(1, 3, 'replacement string');
expect(sourceEdit1.hashCode == sourceEdit2.hashCode, equals(false));
});
});
group('toJson', () {
test('behaves as expected', () {
final sourceEdit = SourceEdit(1, 2, 'replacement string');
final sourceEditJson = sourceEdit.toJson();
expect(
sourceEditJson,
equals({
'offset': 1,
'length': 2,
'replacement': 'replacement string'
}));
});
test('is compatible with fromJson', () {
final sourceEdit = SourceEdit(1, 2, 'replacement string');
final sourceEditJson = sourceEdit.toJson();
final newSourceEdit = SourceEdit.fromJson(sourceEditJson);
expect(newSourceEdit.offset, 1);
expect(newSourceEdit.length, 2);
expect(newSourceEdit.replacement, 'replacement string');
});
});
group('applyAll', () {
test('returns original string when empty list is passed in', () {
const original = 'YAML: YAML';
final result = SourceEdit.applyAll(original, []);
expect(result, original);
});
test('works with list of one SourceEdit', () {
const original = 'YAML: YAML';
final sourceEdits = [SourceEdit(6, 4, 'YAML Ain\'t Markup Language')];
final result = SourceEdit.applyAll(original, sourceEdits);
expect(result, "YAML: YAML Ain't Markup Language");
});
test('works with list of multiple SourceEdits', () {
const original = 'YAML: YAML';
final sourceEdits = [
SourceEdit(6, 4, "YAML Ain't Markup Language"),
SourceEdit(6, 4, "YAML Ain't Markup Language"),
SourceEdit(0, 4, "YAML Ain't Markup Language")
];
final result = SourceEdit.applyAll(original, sourceEdits);
expect(
result,
"YAML Ain't Markup Language: YAML Ain't Markup Language Ain't Markup "
'Language',
);
});
});
});
}