<commit_before>import 'package:petitparser/debug.dart';
import 'package:petitparser/petitparser.dart';
import 'package:test/test.dart';

final identifier = letter() & word().star();

void main() {
  group('trace', () {
    test('success', () {
      final lines = <Object?>[];
      expect(trace(identifier, output: lines.add).parse('a').isSuccess, isTrue);
      expect(lines, [
        "Instance of 'SequenceParser<dynamic>'",
        "  Instance of 'CharacterParser'[letter expected]",
        "  Success[1:2]: a",
        "  Instance of 'PossessiveRepeatingParser<String>'[0..*]",
        "    Instance of 'CharacterParser'[letter or digit expected]",
        "    Failure[1:2]: letter or digit expected",
        "  Success[1:2]: []",
        "Success[1:2]: [a, []]",
      ]);
    });
    test('failure', () {
      final lines = <Object?>[];
      expect(trace(identifier, output: lines.add).parse('1').isFailure, isTrue);
      expect(lines, [
        "Instance of 'SequenceParser<dynamic>'",
        "  Instance of 'CharacterParser'[letter expected]",
        "  Failure[1:1]: letter expected",
        "Failure[1:1]: letter expected",
      ]);
    });
  });
  group('profile', () {
    Matcher isProfileFrame({required String parser, int count = 0}) =>
        isA<ProfileFrame>()
            .having(
                (frame) => frame.parser.toString(), 'parser', contains(parser))
            .having((frame) => frame.count, 'count', count)
            .having((frame) => frame.elapsed, 'elapsed',
                greaterThanOrEqualTo(Duration.zero))
            .having((frame) => frame.toString(), 'toString',
                allOf(startsWith(count.toString()), contains(parser)));

    test('success', () {
      final frames = <ProfileFrame>[];
      final parser = profile(identifier, output: frames.add);
      expect(parser.parse('ab123').isSuccess, isTrue);
      expect(frames, [
        isProfileFrame(parser: 'SequenceParser', count: 1),
        isProfileFrame(parser: '[0..*]', count: 1),
        isProfileFrame(parser: 'letter or digit expected', count: 5),
        isProfileFrame(parser: 'letter expected', count: 1),
      ]);
    });
    test('failure', () {
      final frames = <ProfileFrame>[];
      final parser = profile(identifier, output: frames.add);
      expect(parser.parse('1').isFailure, isTrue);
      expect(frames, [
        isProfileFrame(parser: 'SequenceParser', count: 1),
        isProfileFrame(parser: '[0..*]'),
        isProfileFrame(parser: 'letter or digit expected'),
        isProfileFrame(parser: 'letter expected', count: 1),
      ]);
    });
  });
  group('progress', () {
    Matcher isProgressFrame({required String parser, required int position}) =>
        isA<ProgressFrame>()
            .having(
                (frame) => frame.parser.toString(), 'parser', contains(parser))
            .having((frame) => frame.context.position, 'position', position)
            .having((frame) => frame.toString(), 'toString',
                allOf(startsWith('*' * (position + 1)), contains(parser)));

    test('success', () {
      final frames = <ProgressFrame>[];
      final parser = progress(identifier, output: frames.add);
      expect(parser.parse('ab123').isSuccess, isTrue);
      expect(frames, [
        isProgressFrame(parser: 'SequenceParser', position: 0),
        isProgressFrame(parser: 'letter expected', position: 0),
        isProgressFrame(parser: '[0..*]', position: 1),
        isProgressFrame(parser: 'letter or digit expected', position: 1),
        isProgressFrame(parser: 'letter or digit expected', position: 2),
        isProgressFrame(parser: 'letter or digit expected', position: 3),
        isProgressFrame(parser: 'letter or digit expected', position: 4),
        isProgressFrame(parser: 'letter or digit expected', position: 5),
      ]);
    });
    test('failure', () {
      final frames = <ProgressFrame>[];
      final parser = progress(identifier, output: frames.add);
      expect(parser.parse('1').isFailure, isTrue);
      expect(frames, [
        isProgressFrame(parser: 'SequenceParser', position: 0),
        isProgressFrame(parser: 'letter expected', position: 0),
      ]);
    });
  });
}<commit_msg>Cleanup the test code for the debug tools.<commit_after>import 'package:petitparser/debug.dart';
import 'package:petitparser/petitparser.dart';
import 'package:test/test.dart';

final identifier = letter() & word().star();

Matcher isProfileFrame({required String parser, int count = 0}) =>
    isA<ProfileFrame>()
        .having((frame) => frame.parser.toString(), 'parser', contains(parser))
        .having((frame) => frame.count, 'count', count)
        .having((frame) => frame.elapsed, 'elapsed',
            greaterThanOrEqualTo(Duration.zero))
        .having((frame) => frame.toString(), 'toString',
            allOf(startsWith(count.toString()), contains(parser)));

Matcher isProgressFrame({required String parser, required int position}) =>
    isA<ProgressFrame>()
        .having((frame) => frame.parser.toString(), 'parser', contains(parser))
        .having((frame) => frame.context.position, 'position', position)
        .having((frame) => frame.toString(), 'toString',
            allOf(startsWith('*' * (position + 1)), contains(parser)));

void main() {
  group('trace', () {
    test('success', () {
      final lines = <Object?>[];
      expect(trace(identifier, output: lines.add).parse('a').isSuccess, isTrue);
      expect(lines, [
        "Instance of 'SequenceParser<dynamic>'",
        "  Instance of 'CharacterParser'[letter expected]",
        "  Success[1:2]: a",
        "  Instance of 'PossessiveRepeatingParser<String>'[0..*]",
        "    Instance of 'CharacterParser'[letter or digit expected]",
        "    Failure[1:2]: letter or digit expected",
        "  Success[1:2]: []",
        "Success[1:2]: [a, []]",
      ]);
    });
    test('failure', () {
      final lines = <Object?>[];
      expect(trace(identifier, output: lines.add).parse('1').isFailure, isTrue);
      expect(lines, [
        "Instance of 'SequenceParser<dynamic>'",
        "  Instance of 'CharacterParser'[letter expected]",
        "  Failure[1:1]: letter expected",
        "Failure[1:1]: letter expected",
      ]);
    });
  });
  group('profile', () {
    test('success', () {
      final frames = <ProfileFrame>[];
      final parser = profile(identifier, output: frames.add);
      expect(parser.parse('ab123').isSuccess, isTrue);
      expect(frames, [
        isProfileFrame(parser: 'SequenceParser', count: 1),
        isProfileFrame(parser: '[0..*]', count: 1),
        isProfileFrame(parser: 'letter or digit expected', count: 5),
        isProfileFrame(parser: 'letter expected', count: 1),
      ]);
    });
    test('failure', () {
      final frames = <ProfileFrame>[];
      final parser = profile(identifier, output: frames.add);
      expect(parser.parse('1').isFailure, isTrue);
      expect(frames, [
        isProfileFrame(parser: 'SequenceParser', count: 1),
        isProfileFrame(parser: '[0..*]'),
        isProfileFrame(parser: 'letter or digit expected'),
        isProfileFrame(parser: 'letter expected', count: 1),
      ]);
    });
  });
  group('progress', () {
    test('success', () {
      final frames = <ProgressFrame>[];
      final parser = progress(identifier, output: frames.add);
      expect(parser.parse('ab123').isSuccess, isTrue);
      expect(frames, [
        isProgressFrame(parser: 'SequenceParser', position: 0),
        isProgressFrame(parser: 'letter expected', position: 0),
        isProgressFrame(parser: '[0..*]', position: 1),
        isProgressFrame(parser: 'letter or digit expected', position: 1),
        isProgressFrame(parser: 'letter or digit expected', position: 2),
        isProgressFrame(parser: 'letter or digit expected', position: 3),
        isProgressFrame(parser: 'letter or digit expected', position: 4),
        isProgressFrame(parser: 'letter or digit expected', position: 5),
      ]);
    });
    test('failure', () {
      final frames = <ProgressFrame>[];
      final parser = progress(identifier, output: frames.add);
      expect(parser.parse('1').isFailure, isTrue);
      expect(frames, [
        isProgressFrame(parser: 'SequenceParser', position: 0),
        isProgressFrame(parser: 'letter expected', position: 0),
      ]);
    });
  });
}
{'fraction_non_alphanumeric': 0.16752484732367381, 'fraction_numerical': 0.010298167884085739, 'mean_word_length': 3.092111709946105, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5809629', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2321, 'n_words': 531}