Skip to content

Commit 271834e

Browse files
authored
Add example examples (#111276)
1 parent 42fe03d commit 271834e

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

examples/api/README.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,28 @@ wanted to add a new example to the `Curve2D` class:
7474
The "See code in" line needs to be formatted exactly as above, with no wrapping
7575
or newlines, one space after the "`**`" at the beginning, and one space before
7676
the "`**`" at the end, and the words "See code in" at the beginning of the line.
77-
This is what the snippets tool and the IDE use when finding the example source
78-
code that you are creating.
77+
This is what the snippets tool use when finding the example source code that you
78+
are creating.
7979

8080
Use `{@tool dartpad}` for Dartpad examples, and use `{@tool sample}` for
8181
examples that shouldn't be run/shown in Dartpad.
8282

8383
Once that comment block is inserted in the source code, create a new file at the
84-
appropriate path under [`examples/api`](.) that matches the location of the
85-
source file they are linked from, and are named for the symbol they are attached
86-
to, in lower_snake_case, with an index relating to their order within the doc
87-
comment. So, for the `Curve2D` case, since it's in the `animation` package, in
88-
a file called `curves.dart`, and it's the first example, it goes in
84+
appropriate path under [`examples/api`](.). See the
85+
[sample_templates](./lib/sample_templates/) directory for examples of different
86+
types of samples with some best practices applied.
87+
88+
The filename should match the location of the source file it is linked from, and
89+
is named for the symbol it is attached to, in lower_snake_case, with an index
90+
relating to their order within the doc comment. So, for the `Curve2D` example
91+
above, since it's in the `animation` library, in a file called `curves.dart`,
92+
and it's the first example, it should have the name
8993
`examples/api/lib/animation/curves/curve2_d.0.dart`.
9094

91-
You should also add tests for your sample code under [`examples/api/test`](./test).
95+
You should also add tests for your sample code under
96+
[`examples/api/test`](./test), that matches their location under [lib](./lib),
97+
ending in `_test.dart`. See the section on [writing tests](#writing-tests) for
98+
more information on what kinds of tests to write.
9299

93100
The entire example should be in a single file, so that Dartpad can load it.
94101

@@ -123,18 +130,20 @@ context that the analyzer uses.
123130

124131
## Writing Tests
125132

126-
Examples are required to have tests. There is already a "smoke test" that runs
127-
all the API examples, just to make sure that they start up without crashing. In
128-
addition, we also require writing tests of functionality in the examples, and
129-
generally just do what we normally do for writing tests. The one thing that
130-
makes it more challenging for the examples is that they can't really be written
131-
for testability in any obvious way, since that would probably complicate the
132-
example and make it harder to explain.
133+
Examples are required to have tests. There is already a "smoke test" that simply
134+
builds and runs all the API examples, just to make sure that they start up
135+
without crashing. Functionality tests are required the examples, and generally
136+
just do what is normally done for writing tests. The one thing that makes it
137+
more challenging to do for examples is that they can't really be written for
138+
testability in any obvious way, since that would complicate the examples and
139+
make them harder to explain.
133140

134141
As an example, in regular framework code, you might include a parameter for a
135142
`Platform` object that can be overridden by a test to supply a dummy platform,
136-
but in the example, this would be unnecessary complexity for the example. In all
137-
other ways, these are just normal tests.
143+
but in the example. This would be unnecessarily complex for the example. In all
144+
other ways, these are just normal tests. You don't need to re-test the
145+
functionality of the widget being used in the example, but you should test the
146+
functionality and integrity of the example itself.
138147

139148
Tests go into a directory under [test](./test) that matches their location under
140149
[lib](./lib). They are named the same as the example they are testing, with

0 commit comments

Comments
 (0)