mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
Add samples index (#359)
* add samples_index to web/ directory Co-authored-by: Thea Flowers <theaflowers@google.com> * add pub_get.dart script * build sample index in peanut post build * re-generate sample index with web demos * print more details in peanut_post_build.dart * add images for demos * run generator * update README * add animations and provider shopper as symlinks * add links to symlinked web demos * use relative paths * update cookbook images, urls, and description CSS * use relative URL for navbar link * unstage HTML files * .gitignore generated HTML files * add margin to toolbar * rename escape functions * add and update copyright headers Co-authored-by: Thea Flowers <theaflowers@google.com>
This commit is contained in:
165
web/samples_index/test/samples_index_test.dart
Normal file
165
web/samples_index/test/samples_index_test.dart
Normal file
@@ -0,0 +1,165 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:samples_index/samples_index.dart';
|
||||
import 'package:samples_index/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:checked_yaml/checked_yaml.dart';
|
||||
|
||||
void main() {
|
||||
group('YAML', () {
|
||||
test('parsing', () async {
|
||||
var file = File('test/yaml/single.yaml');
|
||||
var contents = await file.readAsString();
|
||||
expect(contents, isNotEmpty);
|
||||
|
||||
var index = checkedYamlDecode(contents, (m) => Index.fromJson(m),
|
||||
sourceUrl: file.uri);
|
||||
expect(index.samples, isNotEmpty);
|
||||
|
||||
var sample = index.samples.first;
|
||||
expect(sample, isNotNull);
|
||||
expect(sample.name, 'Kittens');
|
||||
expect(sample.screenshots, hasLength(2));
|
||||
expect(sample.source, 'http://github.com/johnpryan/kittens');
|
||||
expect(sample.description, 'A sample kitten app');
|
||||
expect(sample.difficulty, 'beginner');
|
||||
expect(sample.widgets, hasLength(2));
|
||||
expect(sample.widgets.first, 'AnimatedBuilder');
|
||||
expect(sample.packages, hasLength(2));
|
||||
expect(sample.packages.first, 'json_serializable');
|
||||
expect(sample.tags, hasLength(3));
|
||||
expect(sample.tags[1], 'kittens');
|
||||
expect(sample.platforms, hasLength(3));
|
||||
expect(sample.links, hasLength(2));
|
||||
expect(sample.links[1].text, 'author');
|
||||
expect(sample.links[1].href, 'http://jpryan.me');
|
||||
expect(sample.type, 'sample');
|
||||
expect(sample.date, DateTime.parse('2019-12-15T02:59:43.1Z'));
|
||||
expect(sample.channel, 'stable');
|
||||
});
|
||||
|
||||
test('bad yaml', () async {
|
||||
var file = File('test/yaml/bad.yaml');
|
||||
var contents = await file.readAsString();
|
||||
expect(contents, isNotEmpty);
|
||||
|
||||
expect(
|
||||
() => checkedYamlDecode(contents, (m) => Index.fromJson(m),
|
||||
sourceUrl: file.uri),
|
||||
throwsA(predicate((e) =>
|
||||
e is ParsedYamlException &&
|
||||
e.message.endsWith('Unsupported value for "name".'))));
|
||||
});
|
||||
});
|
||||
|
||||
group('searching', () {
|
||||
test('search attributes', () async {
|
||||
var file = File('test/yaml/single.yaml');
|
||||
var contents = await file.readAsString();
|
||||
expect(contents, isNotEmpty);
|
||||
|
||||
var index = checkedYamlDecode(contents, (m) => Index.fromJson(m),
|
||||
sourceUrl: file.uri);
|
||||
var sample = index.samples.first;
|
||||
expect(
|
||||
sample.searchAttributes.split(' '),
|
||||
containsAll([
|
||||
'kittens',
|
||||
'tag:beginner',
|
||||
'tag:kittens',
|
||||
'tag:cats',
|
||||
|
||||
// Verify tags are searchable without the prefix
|
||||
'beginner',
|
||||
'kittens',
|
||||
'cats',
|
||||
|
||||
'platform:web',
|
||||
'platform:ios',
|
||||
'platform:android',
|
||||
|
||||
// Verify platforms are searchable without the prefix
|
||||
'web',
|
||||
'ios',
|
||||
'android',
|
||||
|
||||
'widget:AnimatedBuilder',
|
||||
'widget:FutureBuilder',
|
||||
'package:json_serializable',
|
||||
'package:path',
|
||||
|
||||
'type:sample',
|
||||
]));
|
||||
});
|
||||
|
||||
test('matchesQuery', () {
|
||||
var attributes = 'kittens '
|
||||
'tag:beginner '
|
||||
'tag:kittens '
|
||||
'tag:cats '
|
||||
'platform:web '
|
||||
'platform:ios '
|
||||
'platform:android '
|
||||
'widget:AnimatedBuilder '
|
||||
'widget:FutureBuilder '
|
||||
'package:json_serializable '
|
||||
'package:path';
|
||||
|
||||
// Test if various queries match these attributes
|
||||
expect(matchesQuery('foo', attributes), false);
|
||||
expect(matchesQuery('kittens', attributes), true);
|
||||
expect(matchesQuery('tag:cats', attributes), true);
|
||||
expect(matchesQuery('tag:dogs', attributes), false);
|
||||
expect(matchesQuery('package:path', attributes), true);
|
||||
|
||||
// Test if partial queries match these attributes
|
||||
expect(matchesQuery('kitten', attributes), true);
|
||||
|
||||
// Test if multiple keywords match
|
||||
expect(matchesQuery('kittens tag:cats', attributes), true);
|
||||
expect(matchesQuery('kitten tag:cats', attributes), true);
|
||||
expect(matchesQuery('tag:beginner dogs', attributes), false);
|
||||
expect(matchesQuery('asdf ', attributes), false);
|
||||
});
|
||||
});
|
||||
|
||||
group('Hash parameters', () {
|
||||
test('can be parsed', () {
|
||||
expect(parseHash('#?search=kittens&platform=web'),
|
||||
containsPair('search', 'kittens'));
|
||||
expect(parseHash('#?search=kittens&platform=web'),
|
||||
containsPair('platform', 'web'));
|
||||
expect(parseHash('#?type=sample'), containsPair('type', 'sample'));
|
||||
expect(parseHash('#?type=cookbook'), containsPair('type', 'cookbook'));
|
||||
});
|
||||
|
||||
test('can be set', () {
|
||||
expect(
|
||||
formatHash({
|
||||
'search': 'kittens',
|
||||
'platform': 'web',
|
||||
}),
|
||||
equals('?search=kittens&platform=web'));
|
||||
});
|
||||
|
||||
test('creates search attributes', () {
|
||||
expect(
|
||||
searchQueryFromParams({
|
||||
'search': 'kittens',
|
||||
'platform': 'web',
|
||||
'type': 'sample',
|
||||
}),
|
||||
equals('kittens type:sample platform:web'));
|
||||
expect(
|
||||
searchQueryFromParams({
|
||||
'search': 'kittens',
|
||||
}),
|
||||
equals('kittens'));
|
||||
expect(searchQueryFromParams({}), equals(''));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user