You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dateKeys-dart/test/open_vectors_vm_test.dart

104 lines
3.3 KiB

// The parts of the vectors that test/vectors/open_vectors.g.dart holds for
// the tests compiled to JavaScript are those of the JSON files that
// tool/open_go_vectors.go writes, and the fixtures it embeds are those of
// testdata/, byte for byte.
@TestOn('vm')
library;
import 'dart:convert';
import 'dart:io';
import 'package:datekeys/datekeys.dart';
import 'package:test/test.dart';
import 'open_support.dart';
import 'vectors/open_vectors.g.dart';
Json readJson(String path) => jsonDecode(File(path).readAsStringSync()) as Json;
/// Whether every case of [part] is a case of [whole], in the same order.
void expectPart(List<Object?> part, List<Object?> whole, String what) {
final all = [for (final c in whole) canonical(c)];
var at = 0;
for (final c in part) {
final k = canonical(c);
while (at < all.length && all[at] != k) {
at++;
}
expect(at, lessThan(all.length), reason: '$what: $k');
at++;
}
expect(part, isNotEmpty, reason: what);
}
void main() {
test('the fixtures are those of testdata/', () {
final fx = jsonDecode(openFixturesJson) as Json;
expect(fx, hasLength(11));
for (final e in fx.entries) {
expect(
e.value,
toHex(File('testdata/fixtures/${e.key}').readAsBytesSync()),
reason: e.key,
);
}
});
test('the cases are those of open_cases.json', () {
final part = jsonDecode(openCasesJson) as Json;
final whole = readJson('test/vectors/open_cases.json');
for (final s in ['fixtures', 'steps']) {
expectPart(part[s]! as List, whole[s]! as List, s);
}
});
test('the heads and the notes are those of their files', () {
final heads = jsonDecode(openHeadsJson) as Json;
final h = readJson('test/vectors/open_heads.json');
for (final s in ['decode', 'encode']) {
expectPart(heads[s]! as List, h[s]! as List, s);
}
final notes = jsonDecode(openNotesJson) as Json;
final n = readJson('test/vectors/open_notes.json');
for (final s in ['check', 'note', 'standard']) {
expectPart(notes[s]! as List, n[s]! as List, s);
}
});
test('the views and the mutations are those of their files', () {
final part = jsonDecode(openInspectJson) as Json;
final whole = readJson('test/vectors/open_inspect.json');
expectPart(part['views']! as List, whole['views']! as List, 'views');
final diff = readJson('testdata/vectors/inspect_differential.json');
final bases = [
for (final b in (diff['bases']! as List).cast<Json>()) b['file'],
];
final mutations = (diff['mutations']! as List).cast<Json>();
final texts = (whole['texts']! as List).cast<String>();
final results = (whole['results']! as List).cast<int>();
final ms = (part['mutations']! as List).cast<Json>();
expect(ms, isNotEmpty);
for (final m in ms) {
final i = m['index']! as int;
final d = mutations[i];
expect(
canonical({
'base': m['base'],
'edits': m['edits'],
'result': m['result'],
'step': m['step'],
'text': m['text'],
}),
canonical({
'base': bases[d['base']! as int],
'edits': d['edits'],
'result': d['result'],
'step': d['step'],
'text': results[i] < 0 ? null : texts[results[i]],
}),
reason: 'mutation $i',
);
}
});
}

Powered by TurnKey Linux.