Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 47. tests for scala foldable blocks #75

Merged
merged 7 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/folding/foldable_block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ extension FoldableBlockList on List<FoldableBlock> {
sort((a, b) => a.startLine - b.startLine);
}

/// Joins intersecting blocks in list.
/// Expected that list is sorted by start line.
void joinIntersecting() {
if (length < 2) {
return;
Expand Down
127 changes: 50 additions & 77 deletions test/src/folding/foldable_block_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,68 @@ import 'package:flutter_test/flutter_test.dart';
import 'parsers/test_executor.dart';

void main() {
group('Foldable block test', () {
test('Join intersected blocks in empty list will not throw exception', () {
group('FoldableBlockList.joinIntersecting', () {
test('Empty -> Empty', () {
const blocks = <FoldableBlock>[];

final actual = blocks.deepCopy()..joinIntersecting();
final actual = [...blocks]..joinIntersecting();

expect(actual, blocks);
});

test('Join blocks in list with one block will not throw exception', () {
test('Single -> Single', () {
const blocks = [
FoldableBlock(startLine: 0, endLine: 1, type: FBT.braces),
];

final actual = blocks.deepCopy()..joinIntersecting();
final actual = [...blocks]..joinIntersecting();

expect(actual, blocks);
});

test('Join intersecting blocks in list with two intersecting blocks', () {
const blocks = [
FoldableBlock(startLine: 0, endLine: 1, type: FBT.braces),
FoldableBlock(startLine: 1, endLine: 2, type: FBT.braces),
];
const matcher = [
FoldableBlock(startLine: 0, endLine: 2, type: FBT.union),
];

final actual = blocks.deepCopy()..joinIntersecting();

expect(actual, matcher);
});

test('Join intersecting blocks in list with two not intersecting blocks',
() {
const blocks = [
FoldableBlock(startLine: 0, endLine: 1, type: FBT.braces),
FoldableBlock(startLine: 2, endLine: 3, type: FBT.braces),
];

final actual = blocks.deepCopy()..joinIntersecting();

expect(actual, blocks);
});

test('Multiple not intersected blocks will not join', () {
test('Not intersected blocks do not join', () {
const blocks = [
FB(startLine: 0, endLine: 3, type: FBT.braces),
FB(startLine: 4, endLine: 5, type: FBT.imports),
FB(startLine: 8, endLine: 10, type: FBT.parentheses),
];

final actual = blocks.deepCopy()..joinIntersecting();
final actual = [...blocks]..joinIntersecting();

expect(actual, blocks);
});

test('Multiple intersected blocks will join', () {

test('Mixed blocks join correctly', () {
const blocks = [
FB(startLine: 0, endLine: 3, type: FBT.braces),
FB(startLine: 2, endLine: 5, type: FBT.imports),
FB(startLine: 4, endLine: 10, type: FBT.parentheses),
FB(startLine: 6, endLine: 10, type: FBT.parentheses),
];
const matcher = [
FB(startLine: 0, endLine: 10, type: FBT.union),
const expected = [
FB(startLine: 0, endLine: 5, type: FBT.union),
FB(startLine: 6, endLine: 10, type: FBT.parentheses),
];

final actual = blocks.deepCopy()..joinIntersecting();
final actual = [...blocks]..joinIntersecting();

expect(actual, matcher);
expect(actual, expected);
});

test('Mixed blocks will join correctly', () {
test('Multiple intersected blocks will join in single', () {
const blocks = [
FB(startLine: 0, endLine: 3, type: FBT.braces),
FB(startLine: 2, endLine: 5, type: FBT.imports),
FB(startLine: 6, endLine: 10, type: FBT.parentheses),
FB(startLine: 4, endLine: 10, type: FBT.parentheses),
];
const matcher = [
FB(startLine: 0, endLine: 5, type: FBT.union),
FB(startLine: 6, endLine: 10, type: FBT.parentheses),
const expected = [
FB(startLine: 0, endLine: 10, type: FBT.union),
];

final actual = blocks.deepCopy()..joinIntersecting();
final actual = [...blocks]..joinIntersecting();

expect(actual, matcher);
expect(actual, expected);
});

test('Nested intersected blocks will join', () {
Expand All @@ -98,51 +74,48 @@ void main() {
FB(startLine: 1, endLine: 3, type: FBT.imports),
FB(startLine: 3, endLine: 5, type: FBT.parentheses),
];
const matcher = [
const expected = [
FB(startLine: 0, endLine: 5, type: FBT.braces),
FB(startLine: 1, endLine: 5, type: FBT.union),
];

final actual = blocks.deepCopy()..joinIntersecting();

expect(actual, matcher);
});

test('Unsorted intersected blocks will not join', () {
const blocks = [
FB(startLine: 3, endLine: 5, type: FBT.braces),
FB(startLine: 8, endLine: 10, type: FBT.imports),
FB(startLine: 1, endLine: 3, type: FBT.imports),
];

final actual = blocks.deepCopy()..joinIntersecting();
final actual = [...blocks]..joinIntersecting();

expect(actual, blocks);
expect(actual, expected);
});

// TODO(Malarg): fix this. It's not desired behavior
// This test represents situation, shown on diagram below:
// 0 1 2 3 4 5
// 0 |
// 1 | |
// 2 | |
// 3 | |
// 4 | |
// 5 | |
// 6 | |
// 7 | |
// 8 | |
// 9 | |
// 10 | |
// 11 | |
// 12 |
//
// blocks[0] and blocks[3] should be joined, but they aren't.
test('Containing children blocks will not join', () {
alexeyinkin marked this conversation as resolved.
Show resolved Hide resolved
const blocks = [
FB(startLine: 0, endLine: 6, type: FBT.braces),
FB(startLine: 1, endLine: 3, type: FBT.parentheses),
FB(startLine: 4, endLine: 5, type: FBT.parentheses),
FB(startLine: 6, endLine: 12, type: FBT.braces),
FB(startLine: 7, endLine: 9, type: FBT.parentheses),
FB(startLine: 10, endLine: 11, type: FBT.parentheses),
FB(startLine: 0, endLine: 6, type: FBT.braces), // 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the digits please.

FB(startLine: 1, endLine: 3, type: FBT.parentheses), // 1
FB(startLine: 4, endLine: 5, type: FBT.parentheses), // 2
FB(startLine: 6, endLine: 12, type: FBT.braces), // 3
FB(startLine: 7, endLine: 9, type: FBT.parentheses), // 4
FB(startLine: 10, endLine: 11, type: FBT.parentheses), // 5
];

final actual = blocks.deepCopy()..joinIntersecting();
// 0 intersects with 3, not detected.
final actual = [...blocks]..joinIntersecting();

expect(actual, blocks);
});
});
}

extension _FoldableBlockList on List<FoldableBlock> {
List<FoldableBlock> deepCopy() {
final result = List<FoldableBlock>.from(this);
for (int i = 0; i < length; i++) {
result[i] = this[i].copy();
}
return result;
}
}