import 'package:flutter_test/flutter_test.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; String marked(String text, String kw) { final ranges = SearchHighlightTool.matchRanges(text, kw); final buf = StringBuffer(); int cursor = 0; final runes = text.runes.toList(); for (final r in ranges) { if (r.start < cursor) continue; if (r.start > cursor) { buf.write(String.fromCharCodes(runes.sublist(cursor, r.start))); } buf.write('[${String.fromCharCodes(runes.sublist(r.start, r.end))}]'); cursor = r.end; } if (cursor < runes.length) buf.write(String.fromCharCodes(runes.sublist(cursor))); return buf.toString(); } void main() { test('商品名直接命中', () { expect(marked('康师傅红烧方便面', '方便'), '康师傅红烧[方便]面'); }); test('多次出现全部高亮', () { expect(marked('可口可乐可乐瓶装', '可乐'), '可口[可乐][可乐]瓶装'); }); test('大小写不敏感', () { expect(marked('CocaCola 500ml', 'cola'), 'Coca[Cola] 500ml'); }); test('条码直接命中', () { expect(marked('6901234567892', '1234'), '690[1234]567892'); }); test('拼音首字母头部命中', () { expect(marked('康师傅红烧方便面', 'ksf'), '[康师傅]红烧方便面'); }); test('拼音首字母中部命中', () { expect(marked('康师傅红烧方便面', 'fhs'), '康师[傅红烧]方便面'); }); test('拼音首字母尾部命中', () { expect(marked('康师傅红烧方便面', 'fbm'), '康师傅红烧[方便面]'); }); test('汉字与字母混合拼音命中', () { expect(marked('农夫山泉550ml', 'nfsq'), '[农夫山泉]550ml'); }); test('无命中不高亮', () { expect(marked('康师傅红烧方便面', 'zzz'), '康师傅红烧方便面'); }); test('空关键字不高亮', () { expect(marked('康师傅红烧方便面', ''), '康师傅红烧方便面'); expect(marked('康师傅红烧方便面', ' '), '康师傅红烧方便面'); }); }