Skip to content

Commit

Permalink
Library tour example tests from dart:html onwards
Browse files Browse the repository at this point in the history
Contributes to dart-lang#407 and dart-lang#416.

Continuation of dart-lang#440
  • Loading branch information
chalin committed Nov 22, 2017
1 parent b604e88 commit 8fc1d7a
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 112 deletions.
140 changes: 140 additions & 0 deletions examples/lib/library_tour/html.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// ignore_for_file: unused_element, unused_local_variable
import 'dart:async';
// #docregion import
import 'dart:html';
// #enddocregion import

void miscDeclAnalyzedButNotTested() {
{
// #docregion querySelector
// Find an element by id (an-id).
Element elem1 = querySelector('#an-id');

// Find an element by class (a-class).
Element elem2 = querySelector('.a-class');

// Find all elements by tag (<div>).
List<Element> elems1 = querySelectorAll('div');

// Find all text inputs.
List<Element> elems2 =
querySelectorAll('input[type="text"]');

// Find all elements with the CSS class 'class'
// inside of a <p> that is inside an element with
// the ID 'id'.
List<Element> elems3 = querySelectorAll('#id p.class');
// #enddocregion querySelector
}

{
Element elem;
// #docregion attributes
elem.attributes['someAttribute'] = 'someValue';
// #enddocregion attributes
}

{
// #docregion creating-elements
var elem = new ParagraphElement();
elem.text = 'Creating is easy!';
// #enddocregion creating-elements

// #docregion creating-from-html
var elem2 =
new Element.html('<p>Creating <em>is</em> easy!</p>');
// #enddocregion creating-from-html

// #docregion body-children-add
document.body.children.add(elem2);
// #enddocregion body-children-add

// #docregion nodes-add
querySelector('#inputs').nodes.add(elem);
// #enddocregion nodes-add

// #docregion replaceWith
querySelector('#status').replaceWith(elem);
// #enddocregion replaceWith

// #docregion remove
// Find a node by ID, and remove it from the DOM.
querySelector('#expendable').remove();
// #enddocregion remove
}

{
// #docregion classes-add
var elem = querySelector('#message');
elem.classes.add('warning');
// #enddocregion classes-add

// #docregion set-id
var message = new DivElement();
message.id = 'message2';
message.text = 'Please subscribe to the Dart mailing list.';
// #enddocregion set-id
}

{
// #docregion elem-set-cascade
var message = new DivElement()
..id = 'message2'
..text = 'Please subscribe to the Dart mailing list.';
// #enddocregion elem-set-cascade

// #docregion set-style
message.style
..fontWeight = 'bold'
..fontSize = '3em';
// #enddocregion set-style

void submitData() {}
// #docregion onClick
// Find a button by ID and add an event handler.
querySelector('#submitInfo').onClick.listen((e) {
// When the button is clicked, it runs this code.
submitData();
});
// #enddocregion onClick

// #docregion target
document.body.onClick.listen((e) {
final clickedElem = e.target;
// ...
});
// #enddocregion target
}

Future tryGetString() async {
String jsonUri = 'data.json';
// #docregion try-getString
try {
var data = await HttpRequest.getString(jsonUri);
// Process data...
} catch (e) {
// Handle exception...
}
// #enddocregion try-getString
}

{
var url, encodedData;
void requestComplete(HttpRequest req) {}
// #docregion new-HttpRequest
var request = new HttpRequest();
request
..open('POST', url)
..onLoadEnd.listen((e) => requestComplete(request))
..send(encodedData);
// #enddocregion new-HttpRequest

// #docregion xxx
11111;
// #enddocregion xxx

// #docregion xxx
11111;
// #enddocregion xxx
}
}
3 changes: 3 additions & 0 deletions examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ version: 0.0.1
environment:
sdk: '>=1.20.1 <2.0.0'

dependencies:
browser: any

dev_dependencies:
test: ^0.12.0
107 changes: 107 additions & 0 deletions examples/test/library_tour/html_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// #docplaster
@Tags(const ['browser'])
@TestOn('browser')
import 'dart:async';
import 'dart:html';
import 'package:test/test.dart';

void main() {
test('querySelector', () {
final html =
// #docregion anchor-html
'<a id="example" href="http://example.com">link text</a>';
// #enddocregion anchor-html
document.body.appendHtml(html);

// #docregion href
var anchor = querySelector('#example') as AnchorElement;
anchor.href = 'http://dartlang.org';
// #enddocregion href
// The code above should throw no exceptions.
});

test('querySelectorAll', () {
// #docregion os-html
final html = '''<!-- In HTML: -->
<p>
<span class="linux">Words for Linux</span>
<span class="macos">Words for Mac</span>
<span class="windows">Words for Windows</span>
</p>''';
// #enddocregion os-html
document.body.appendHtml(html);

String determineUserOs() => 'linux';
// #docregion os
// In Dart:
final osList = ['macos', 'windows', 'linux'];
final userOs = determineUserOs();

// For each possible OS...
for (var os in osList) {
// Matches user OS?
bool shouldShow = (os == userOs);

// Find all elements with class=os. For example, if
// os == 'windows', call querySelectorAll('.windows')
// to find all elements with the class "windows".
// Note that '.$os' uses string interpolation.
for (var elem in querySelectorAll('.$os')) {
elem.hidden = !shouldShow; // Show or hide.
}
}
// #enddocregion os
expect(querySelector('.linux').hidden, isFalse);
expect(querySelector('.macos').hidden, isTrue);
expect(querySelector('.windows').hidden, isTrue);
});

test('getString', () async {
final url = 'http://httpbin.org';
// #docregion getString
Future main() async {
String pageHtml = await HttpRequest.getString(url);
// Do something with pageHtml...
// #enddocregion getString
expect(pageHtml.substring(0, 250),
contains('<title>httpbin'));
// #docregion getString
}
// #enddocregion getString

await main();
});

test('request', () async {
final url = 'http://httpbin.org/post';
// #docregion request
Future main() async {
HttpRequest req =
await HttpRequest.request(url, method: 'POST');
if (req.status == 200) {
// Successful page access...
}
// ...
// #enddocregion request
expect(req.status, 200);
// #docregion request
}
// #enddocregion request

await main();
});

group('xxxx', () {
test('xxxxx', () {
// #docregion xxxx
11111;
// #enddocregion xxxx
});

test('xxxxx', () {
// #docregion xxxx
11111;
// #enddocregion xxxx
});
});
}
18 changes: 15 additions & 3 deletions scripts/analyze-and-test-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ EXAMPLES="$ROOT/examples"

ANALYZE="dartanalyzer --options $EXAMPLES/analysis_options.yaml"

FILTER1="cat -"
FILTER2="cat"
FILTER_ARG="-"
if [[ "$1" == "-q" ]]; then
FILTER1="tr '\r' '\n'"
FILTER2="grep -E"
FILTER_ARG="(Some|All) tests"
shift;
fi

if [[ ! -e $TMP ]]; then mkdir $TMP; fi
LOG_FILE=$TMP/analyzer-output.txt

Expand All @@ -36,17 +46,19 @@ echo Running VM tests ...

TEST="pub run test"

$TEST --exclude-tags=browser | tee $LOG_FILE
$TEST --exclude-tags=browser | tee $LOG_FILE | $FILTER1 | $FILTER2 "$FILTER_ARG"
LOG=$(grep 'All tests passed!' $LOG_FILE)
if [[ -z "$LOG" ]]; then EXIT_STATUS=1; fi
travis_fold end analyzeAndTest.tests.vm

echo
travis_fold start analyzeAndTest.tests.browser
echo Running browser tests ...

# Name the sole browser test file, otherwise all other files get compiled too:
$TEST --tags browser --platform chrome test/language_tour/browser_test.dart | tee $LOG_FILE
$TEST --tags browser --platform chrome \
test/language_tour/browser_test.dart \
test/library_tour/html_test.dart \
| tee $LOG_FILE | $FILTER1 | $FILTER2 "$FILTER_ARG"
LOG=$(grep 'All tests passed!' $LOG_FILE)
if [[ -z "$LOG" ]]; then EXIT_STATUS=1; fi
travis_fold end analyzeAndTest.tests.browser
Expand Down
4 changes: 3 additions & 1 deletion scripts/dartfmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ $DARTFMT -l 65 \
examples/lib/language_tour/exceptions.dart \
examples/test/library_tour/core_test.dart \
examples/lib/library_tour/async/future.dart \
examples/lib/library_tour/async/stream.dart
examples/lib/library_tour/async/stream.dart \
examples/lib/library_tour/html.dart \
examples/test/library_tour/html_test.dart

# If any files were changed, then exit 1:
REFORMATTED_FILES=$(git status --short)
Expand Down
Loading

0 comments on commit 8fc1d7a

Please sign in to comment.