mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 14:40:30 +03:00
convert docstring examples to unittests
This commit is contained in:
parent
35a2a564dc
commit
d81030ab8a
4 changed files with 133 additions and 135 deletions
|
@ -4,27 +4,6 @@ Helper functions for working with $(I C strings).
|
||||||
This module is intended to provide fast, safe and garbage free
|
This module is intended to provide fast, safe and garbage free
|
||||||
way to work with $(I C strings).
|
way to work with $(I C strings).
|
||||||
|
|
||||||
Example:
|
|
||||||
---
|
|
||||||
version(Posix):
|
|
||||||
|
|
||||||
import core.stdc.stdlib: free;
|
|
||||||
import core.sys.posix.stdlib: setenv;
|
|
||||||
import std.exception: enforce;
|
|
||||||
|
|
||||||
void setEnvironment(in char[] name, in char[] value)
|
|
||||||
{ enforce(setenv(name.tempCString(), value.tempCString(), 1) != -1); }
|
|
||||||
---
|
|
||||||
---
|
|
||||||
version(Windows):
|
|
||||||
|
|
||||||
import core.sys.windows.windows: SetEnvironmentVariableW;
|
|
||||||
import std.exception: enforce;
|
|
||||||
|
|
||||||
void setEnvironment(in char[] name, in char[] value)
|
|
||||||
{ enforce(SetEnvironmentVariableW(name.tempCStringW(), value.tempCStringW())); }
|
|
||||||
---
|
|
||||||
|
|
||||||
Copyright: Denis Shelomovskij 2013-2014
|
Copyright: Denis Shelomovskij 2013-2014
|
||||||
|
|
||||||
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
|
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
|
||||||
|
@ -36,6 +15,28 @@ COREREF = $(HTTP dlang.org/phobos/core_$1.html#$2, $(D core.$1.$2))
|
||||||
*/
|
*/
|
||||||
module std.internal.cstring;
|
module std.internal.cstring;
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
version(Posix)
|
||||||
|
{
|
||||||
|
import core.stdc.stdlib: free;
|
||||||
|
import core.sys.posix.stdlib: setenv;
|
||||||
|
import std.exception: enforce;
|
||||||
|
|
||||||
|
void setEnvironment(in char[] name, in char[] value)
|
||||||
|
{ enforce(setenv(name.tempCString(), value.tempCString(), 1) != -1); }
|
||||||
|
}
|
||||||
|
|
||||||
|
version(Windows)
|
||||||
|
{
|
||||||
|
import core.sys.windows.windows: SetEnvironmentVariableW;
|
||||||
|
import std.exception: enforce;
|
||||||
|
|
||||||
|
void setEnvironment(in char[] name, in char[] value)
|
||||||
|
{ enforce(SetEnvironmentVariableW(name.tempCStringW(), value.tempCStringW())); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
import std.traits;
|
import std.traits;
|
||||||
import std.range;
|
import std.range;
|
||||||
|
|
121
std/signals.d
121
std/signals.d
|
@ -80,63 +80,6 @@ extern (C) void rt_detachDisposeEvent( Object obj, DisposeEvt evt );
|
||||||
* Mixin to create a signal within a class object.
|
* Mixin to create a signal within a class object.
|
||||||
*
|
*
|
||||||
* Different signals can be added to a class by naming the mixins.
|
* Different signals can be added to a class by naming the mixins.
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
---
|
|
||||||
import std.signals;
|
|
||||||
import std.stdio;
|
|
||||||
|
|
||||||
class Observer
|
|
||||||
{ // our slot
|
|
||||||
void watch(string msg, int i)
|
|
||||||
{
|
|
||||||
writefln("Observed msg '%s' and value %s", msg, i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
{
|
|
||||||
int value() { return _value; }
|
|
||||||
|
|
||||||
int value(int v)
|
|
||||||
{
|
|
||||||
if (v != _value)
|
|
||||||
{ _value = v;
|
|
||||||
// call all the connected slots with the two parameters
|
|
||||||
emit("setting new value", v);
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mix in all the code we need to make Foo into a signal
|
|
||||||
mixin Signal!(string, int);
|
|
||||||
|
|
||||||
private :
|
|
||||||
int _value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
Foo a = new Foo;
|
|
||||||
Observer o = new Observer;
|
|
||||||
|
|
||||||
a.value = 3; // should not call o.watch()
|
|
||||||
a.connect(&o.watch); // o.watch is the slot
|
|
||||||
a.value = 4; // should call o.watch()
|
|
||||||
a.disconnect(&o.watch); // o.watch is no longer a slot
|
|
||||||
a.value = 5; // so should not call o.watch()
|
|
||||||
a.connect(&o.watch); // connect again
|
|
||||||
a.value = 6; // should call o.watch()
|
|
||||||
destroy(o); // destroying o should automatically disconnect it
|
|
||||||
a.value = 7; // should not call o.watch()
|
|
||||||
}
|
|
||||||
---
|
|
||||||
* which should print:
|
|
||||||
* <pre>
|
|
||||||
* Observed msg 'setting new value' and value 4
|
|
||||||
* Observed msg 'setting new value' and value 6
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mixin template Signal(T1...)
|
mixin template Signal(T1...)
|
||||||
|
@ -271,6 +214,70 @@ mixin template Signal(T1...)
|
||||||
size_t slots_idx; // used length of slots[]
|
size_t slots_idx; // used length of slots[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.signals;
|
||||||
|
|
||||||
|
int observedMessageCounter = 0;
|
||||||
|
|
||||||
|
class Observer
|
||||||
|
{ // our slot
|
||||||
|
void watch(string msg, int value)
|
||||||
|
{
|
||||||
|
switch(observedMessageCounter++)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
assert(msg == "setting new value");
|
||||||
|
assert(value == 4);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
assert(msg == "setting new value");
|
||||||
|
assert(value == 6);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(0, "Unknown observation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
int value() { return _value; }
|
||||||
|
|
||||||
|
int value(int v)
|
||||||
|
{
|
||||||
|
if (v != _value)
|
||||||
|
{ _value = v;
|
||||||
|
// call all the connected slots with the two parameters
|
||||||
|
emit("setting new value", v);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mix in all the code we need to make Foo into a signal
|
||||||
|
mixin Signal!(string, int);
|
||||||
|
|
||||||
|
private :
|
||||||
|
int _value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Foo a = new Foo;
|
||||||
|
Observer o = new Observer;
|
||||||
|
|
||||||
|
a.value = 3; // should not call o.watch()
|
||||||
|
a.connect(&o.watch); // o.watch is the slot
|
||||||
|
a.value = 4; // should call o.watch()
|
||||||
|
a.disconnect(&o.watch); // o.watch is no longer a slot
|
||||||
|
a.value = 5; // so should not call o.watch()
|
||||||
|
a.connect(&o.watch); // connect again
|
||||||
|
a.value = 6; // should call o.watch()
|
||||||
|
destroy(o); // destroying o should automatically disconnect it
|
||||||
|
a.value = 7; // should not call o.watch()
|
||||||
|
|
||||||
|
assert(observedMessageCounter == 2);
|
||||||
|
}
|
||||||
|
|
||||||
// A function whose sole purpose is to get this module linked in
|
// A function whose sole purpose is to get this module linked in
|
||||||
// so the unittest will run.
|
// so the unittest will run.
|
||||||
void linkin() { }
|
void linkin() { }
|
||||||
|
|
36
std/string.d
36
std/string.d
|
@ -6228,30 +6228,6 @@ unittest
|
||||||
* in one of a known set of strings, and the program will helpfully
|
* in one of a known set of strings, and the program will helpfully
|
||||||
* autocomplete the string once sufficient characters have been
|
* autocomplete the string once sufficient characters have been
|
||||||
* entered that uniquely identify it.
|
* entered that uniquely identify it.
|
||||||
* Example:
|
|
||||||
* ---
|
|
||||||
* import std.stdio;
|
|
||||||
* import std.string;
|
|
||||||
*
|
|
||||||
* void main()
|
|
||||||
* {
|
|
||||||
* static string[] list = [ "food", "foxy" ];
|
|
||||||
*
|
|
||||||
* auto abbrevs = std.string.abbrev(list);
|
|
||||||
*
|
|
||||||
* foreach (key, value; abbrevs)
|
|
||||||
* {
|
|
||||||
* writefln("%s => %s", key, value);
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* ---
|
|
||||||
* produces the output:
|
|
||||||
* <pre>
|
|
||||||
* fox => foxy
|
|
||||||
* food => food
|
|
||||||
* foxy => foxy
|
|
||||||
* foo => food
|
|
||||||
* </pre>
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
string[string] abbrev(string[] values) @safe pure
|
string[string] abbrev(string[] values) @safe pure
|
||||||
|
@ -6303,6 +6279,18 @@ string[string] abbrev(string[] values) @safe pure
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.string;
|
||||||
|
|
||||||
|
static string[] list = [ "food", "foxy" ];
|
||||||
|
auto abbrevs = std.string.abbrev(list);
|
||||||
|
assert(abbrevs == ["fox": "foxy", "food": "food",
|
||||||
|
"foxy": "foxy", "foo": "food"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@trusted pure unittest
|
@trusted pure unittest
|
||||||
{
|
{
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
|
68
std/uuid.d
68
std/uuid.d
|
@ -75,27 +75,6 @@ $(TR $(TDNW UUID namespaces)
|
||||||
* boost._uuid) from the Boost project with some minor additions and API
|
* boost._uuid) from the Boost project with some minor additions and API
|
||||||
* changes for a more D-like API.
|
* changes for a more D-like API.
|
||||||
*
|
*
|
||||||
* Example:
|
|
||||||
* ------------------------
|
|
||||||
* UUID[] ids;
|
|
||||||
* ids ~= randomUUID();
|
|
||||||
* ids ~= md5UUID("test.name.123");
|
|
||||||
* ids ~= sha1UUID("test.name.123");
|
|
||||||
*
|
|
||||||
* foreach(entry; ids)
|
|
||||||
* {
|
|
||||||
* assert(entry.variant == UUID.Variant.rfc4122);
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* assert(ids[0].uuidVersion == UUID.Version.randomNumberBased);
|
|
||||||
* assert(ids[1].toString() == "22390768-cced-325f-8f0f-cfeaa19d0ccd");
|
|
||||||
* assert(ids[1].data == [34, 57, 7, 104, 204, 237, 50, 95, 143, 15, 207,
|
|
||||||
* 234, 161, 157, 12, 205]);
|
|
||||||
*
|
|
||||||
* UUID id;
|
|
||||||
* assert(id.empty);
|
|
||||||
*
|
|
||||||
* ------------------------
|
|
||||||
* Standards:
|
* Standards:
|
||||||
* $(LINK2 http://www.ietf.org/rfc/rfc4122.txt, RFC 4122)
|
* $(LINK2 http://www.ietf.org/rfc/rfc4122.txt, RFC 4122)
|
||||||
*
|
*
|
||||||
|
@ -118,6 +97,28 @@ $(TR $(TDNW UUID namespaces)
|
||||||
*/
|
*/
|
||||||
module std.uuid;
|
module std.uuid;
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.uuid;
|
||||||
|
|
||||||
|
UUID[] ids;
|
||||||
|
ids ~= randomUUID();
|
||||||
|
ids ~= md5UUID("test.name.123");
|
||||||
|
ids ~= sha1UUID("test.name.123");
|
||||||
|
|
||||||
|
foreach(entry; ids)
|
||||||
|
{
|
||||||
|
assert(entry.variant == UUID.Variant.rfc4122);
|
||||||
|
}
|
||||||
|
assert(ids[0].uuidVersion == UUID.Version.randomNumberBased);
|
||||||
|
assert(ids[1].toString() == "22390768-cced-325f-8f0f-cfeaa19d0ccd");
|
||||||
|
assert(ids[1].data == [34, 57, 7, 104, 204, 237, 50, 95, 143, 15, 207,
|
||||||
|
234, 161, 157, 12, 205]);
|
||||||
|
UUID id;
|
||||||
|
assert(id.empty);
|
||||||
|
}
|
||||||
|
|
||||||
import std.range.primitives;
|
import std.range.primitives;
|
||||||
import std.traits;
|
import std.traits;
|
||||||
|
|
||||||
|
@ -329,18 +330,6 @@ public struct UUID
|
||||||
* hyphens exactly like above.
|
* hyphens exactly like above.
|
||||||
*
|
*
|
||||||
* For a less strict parser, see $(LREF parseUUID)
|
* For a less strict parser, see $(LREF parseUUID)
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* -------------------------
|
|
||||||
* id = UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
|
|
||||||
* assert(id.data == [138, 179, 6, 14, 44, 186, 79, 35, 183, 76,
|
|
||||||
* 181, 45, 179, 189, 251, 70]);
|
|
||||||
* assert(id.toString() == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
|
|
||||||
*
|
|
||||||
* //Can also be used in CTFE, for example as UUID literals:
|
|
||||||
* enum ctfeID = UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
|
|
||||||
* //here parsing is done at compile time, no runtime overhead!
|
|
||||||
* -------------------------
|
|
||||||
*/
|
*/
|
||||||
this(T)(in T[] uuid) if(isSomeChar!(Unqual!T))
|
this(T)(in T[] uuid) if(isSomeChar!(Unqual!T))
|
||||||
{
|
{
|
||||||
|
@ -398,6 +387,19 @@ public struct UUID
|
||||||
UUIDParsingException.Reason.invalidChar, "Couldn't parse ubyte");
|
UUIDParsingException.Reason.invalidChar, "Couldn't parse ubyte");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe pure unittest
|
||||||
|
{
|
||||||
|
auto id = UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
|
||||||
|
assert(id.data == [138, 179, 6, 14, 44, 186, 79, 35, 183, 76,
|
||||||
|
181, 45, 179, 189, 251, 70]);
|
||||||
|
assert(id.toString() == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
|
||||||
|
|
||||||
|
//Can also be used in CTFE, for example as UUID literals:
|
||||||
|
enum ctfeID = UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
|
||||||
|
//here parsing is done at compile time, no runtime overhead!
|
||||||
|
}
|
||||||
|
|
||||||
@safe pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
import std.exception;
|
import std.exception;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue