OLD | NEW |
(Empty) | |
| 1 library petitparser.test.debug_test; |
| 2 |
| 3 import 'package:test/test.dart'; |
| 4 |
| 5 import 'package:petitparser/petitparser.dart'; |
| 6 import 'package:petitparser/debug.dart'; |
| 7 |
| 8 final identifier = letter() & word().star(); |
| 9 |
| 10 main() { |
| 11 group('continuation', () { |
| 12 test('delegation', () { |
| 13 var parser = new ContinuationParser(digit(), (continuation, context) { |
| 14 return continuation(context); |
| 15 }); |
| 16 expect(parser.parse('1').isSuccess, isTrue); |
| 17 expect(parser.parse('a').isSuccess, isFalse); |
| 18 }); |
| 19 test('divertion', () { |
| 20 var parser = new ContinuationParser(digit(), (continuation, context) { |
| 21 return letter().parseOn(context); |
| 22 }); |
| 23 expect(parser.parse('1').isSuccess, isFalse); |
| 24 expect(parser.parse('a').isSuccess, isTrue); |
| 25 }); |
| 26 test('resume', () { |
| 27 var capture = new List(); |
| 28 var parser = new ContinuationParser(digit(), (continuation, Context contex
t) { |
| 29 capture.add([continuation, context]); |
| 30 // we have to return something for now |
| 31 return context.failure('Abort'); |
| 32 }); |
| 33 // execute the parser twice to collect the continuations |
| 34 expect(parser.parse('1').isSuccess, isFalse); |
| 35 expect(parser.parse('a').isSuccess, isFalse); |
| 36 // later we can execute the captured continuations |
| 37 expect(capture[0][0](capture[0][1]).isSuccess, isTrue); |
| 38 expect(capture[1][0](capture[1][1]).isSuccess, isFalse); |
| 39 // of course the continuations can be resumed multiple times |
| 40 expect(capture[0][0](capture[0][1]).isSuccess, isTrue); |
| 41 expect(capture[1][0](capture[1][1]).isSuccess, isFalse); |
| 42 }); |
| 43 test('success', () { |
| 44 var parser = new ContinuationParser(digit(), (continuation, Context contex
t) { |
| 45 return context.success('Always succeed'); |
| 46 }); |
| 47 expect(parser.parse('1').isSuccess, isTrue); |
| 48 expect(parser.parse('a').isSuccess, isTrue); |
| 49 }); |
| 50 test('failure', () { |
| 51 var parser = new ContinuationParser(digit(), (continuation, Context contex
t) { |
| 52 return context.failure('Always fail'); |
| 53 }); |
| 54 expect(parser.parse('1').isSuccess, isFalse); |
| 55 expect(parser.parse('a').isSuccess, isFalse); |
| 56 }); |
| 57 test('copy', () { |
| 58 var parser = new ContinuationParser(digit(), (continuation, context) { |
| 59 return continuation(context); |
| 60 }); |
| 61 var copy = parser.copy(); |
| 62 expect(copy.parse('1').isSuccess, isTrue); |
| 63 expect(copy.parse('a').isSuccess, isFalse); |
| 64 }); |
| 65 }); |
| 66 group('trace', () { |
| 67 test('success', () { |
| 68 var lines = new List(); |
| 69 expect(trace(identifier, (line) => lines.add(line)).parse('a').isSuccess,
isTrue); |
| 70 expect(lines, [ |
| 71 'Instance of \'SequenceParser\'', |
| 72 ' Instance of \'CharacterParser\'[letter expected]', |
| 73 ' Success[1:2]: a', |
| 74 ' Instance of \'PossessiveRepeatingParser\'[0..*]', |
| 75 ' Instance of \'CharacterParser\'[letter or digit expected]', |
| 76 ' Failure[1:2]: letter or digit expected', |
| 77 ' Success[1:2]: []', |
| 78 'Success[1:2]: [a, []]' |
| 79 ]); |
| 80 }); |
| 81 test('failure', () { |
| 82 var lines = new List(); |
| 83 expect(trace(identifier, (line) => lines.add(line)).parse('1').isFailure,
isTrue); |
| 84 expect(lines, [ |
| 85 'Instance of \'SequenceParser\'', |
| 86 ' Instance of \'CharacterParser\'[letter expected]', |
| 87 ' Failure[1:1]: letter expected', |
| 88 'Failure[1:1]: letter expected' |
| 89 ]); |
| 90 }); |
| 91 }); |
| 92 group('profile', () { |
| 93 test('success', () { |
| 94 var lines = new List(); |
| 95 expect(profile(identifier, (line) => lines.add(line)) |
| 96 .parse('ab123').isSuccess, isTrue); |
| 97 lines = lines |
| 98 .map((row) => row.split('\t')) |
| 99 .map((row) => [int.parse(row[0]), int.parse(row[1]), row[2]]); |
| 100 expect(lines, hasLength(4)); |
| 101 expect(lines.every((row) => row[1] >= 0), isTrue); |
| 102 expect(lines.firstWhere((row) => row[2].indexOf('SequenceParser') > 0)[0],
1); |
| 103 expect(lines.firstWhere((row) => row[2].indexOf('letter expected') > 0)[0]
, 1); |
| 104 expect(lines.firstWhere((row) => row[2].indexOf('PossessiveRepeatingParser
') > 0)[0], 1); |
| 105 expect(lines.firstWhere((row) => row[2].indexOf('letter or digit expected'
) > 0)[0], 5); |
| 106 }); |
| 107 test('failure', () { |
| 108 var lines = new List(); |
| 109 expect(profile(identifier, (line) => lines.add(line)).parse('1').isFailure
, isTrue); |
| 110 lines = lines |
| 111 .map((row) => row.split('\t')) |
| 112 .map((row) => [int.parse(row[0]), int.parse(row[1]), row[2]]); |
| 113 expect(lines, hasLength(4)); |
| 114 expect(lines.every((row) => row[1] >= 0), isTrue); |
| 115 expect(lines.firstWhere((row) => row[2].indexOf('SequenceParser') > 0)[0],
1); |
| 116 expect(lines.firstWhere((row) => row[2].indexOf('letter expected') > 0)[0]
, 1); |
| 117 expect(lines.firstWhere((row) => row[2].indexOf('PossessiveRepeatingParser
') > 0)[0], 0); |
| 118 expect(lines.firstWhere((row) => row[2].indexOf('letter or digit expected'
) > 0)[0], 0); |
| 119 }); |
| 120 }); |
| 121 group('progress', () { |
| 122 test('success', () { |
| 123 var lines = new List(); |
| 124 expect(progress(identifier, (line) => lines.add(line)) |
| 125 .parse('ab123').isSuccess, isTrue); |
| 126 expect(lines, [ |
| 127 '* Instance of \'SequenceParser\'', |
| 128 '* Instance of \'CharacterParser\'[letter expected]', |
| 129 '** Instance of \'PossessiveRepeatingParser\'[0..*]', |
| 130 '** Instance of \'CharacterParser\'[letter or digit expected]', |
| 131 '*** Instance of \'CharacterParser\'[letter or digit expected]', |
| 132 '**** Instance of \'CharacterParser\'[letter or digit expected]', |
| 133 '***** Instance of \'CharacterParser\'[letter or digit expected]', |
| 134 '****** Instance of \'CharacterParser\'[letter or digit expected]' |
| 135 ]); |
| 136 }); |
| 137 test('failure', () { |
| 138 var lines = new List(); |
| 139 expect(progress(identifier, (line) => lines.add(line)).parse('1').isFailur
e, isTrue); |
| 140 expect(lines, [ |
| 141 '* Instance of \'SequenceParser\'', |
| 142 '* Instance of \'CharacterParser\'[letter expected]' |
| 143 ]); |
| 144 }); |
| 145 }); |
| 146 } |
OLD | NEW |