ddoc'd unittest

This commit is contained in:
Andy Colson 2015-01-22 20:40:08 -06:00
parent 234915ee09
commit f5b01e66ea

View file

@ -3,16 +3,17 @@
/** /**
JavaScript Object Notation JavaScript Object Notation
Synopsis:
Synopsis:
---- ----
//parse a file or string of json into a usable structure //parse a file or string of json into a usable structure
string s = "{ \"language\": \"D\", \"rating\": 3.14, \"code\": \"42\" }"; string s = "{ \"language\": \"D\", \"rating\": 3.14, \"code\": \"42\" }";
JSONValue j = parseJSON(s) JSONValue j = parseJSON(s);
writeln("Language: ", j["language"].str(), " Rating: ", j["rating"].floating()); writeln("Language: ", j["language"].str(),
" Rating: ", j["rating"].floating()
);
// j and j["language"] return JSONValue, j["language"].str() returns a string // j and j["language"] return JSONValue,
// j["language"].str returns a string
//check a type //check a type
long x; long x;
@ -22,7 +23,7 @@ JavaScript Object Notation
} }
else else
{ {
x = to!int(j["code"].str()); x = to!int(j["code"].str);
} }
// create a json struct // create a json struct
@ -32,7 +33,7 @@ JavaScript Object Notation
// create an array to assign to list // create an array to assign to list
jj.object["list"] = JSONValue( ["a", "b", "c"] ); jj.object["list"] = JSONValue( ["a", "b", "c"] );
// list already exists, so .object optional // list already exists, so .object optional
j["list"].array ~= JSONValue("D"); jj["list"].array ~= JSONValue("D");
s = j.toString(); s = j.toString();
writeln(s); writeln(s);
@ -95,18 +96,19 @@ struct JSONValue
/** /**
Returns the JSON_TYPE of the value stored in this structure. Returns the JSON_TYPE of the value stored in this structure.
Example:
---
string j = "{ \"language\": \"D\" }";
JSONValue j = parseJSON(s);
// j.type() == JSON_TYPE.OBJECT
// j["language"].type() == JSON_TYPE.STRING
---
*/ */
@property JSON_TYPE type() const @property JSON_TYPE type() const
{ {
return type_tag; return type_tag;
} }
///
unittest
{
string s = "{ \"language\": \"D\" }";
JSONValue j = parseJSON(s);
assert(j.type() == JSON_TYPE.OBJECT);
assert(j["language"].type() == JSON_TYPE.STRING);
}
/** /**
$(RED Deprecated. Instead, please assign the value with the adequate $(RED Deprecated. Instead, please assign the value with the adequate
@ -151,19 +153,8 @@ struct JSONValue
return type_tag = newType; return type_tag = newType;
} }
/** Value getter/setter for $(D JSON_TYPE.STRING). /// Value getter/setter for $(D JSON_TYPE.STRING).
Throws $(D JSONException) for read access if $(D type) is not $(D JSON_TYPE.STRING). /// Throws $(D JSONException) for read access if $(D type) is not $(D JSON_TYPE.STRING).
Example:
---
JSONValue j = [ "language": "D" ];
// get value
string s = j["language"].str();
// change existing key to new string
j["language"].str("Perl");
---
*/
@property inout(string) str() inout @property inout(string) str() inout
{ {
enforce!JSONException(type == JSON_TYPE.STRING, enforce!JSONException(type == JSON_TYPE.STRING,
@ -176,6 +167,20 @@ struct JSONValue
assign(v); assign(v);
return store.str; return store.str;
} }
///
unittest
{
JSONValue j = [ "language": "D" ];
// get value
assert(j["language"].str() == "D");
// str() or str is ok
assert(j["language"].str == "D");
// change existing key to new string
j["language"].str("Perl");
assert(j["language"].str == "Perl");
}
/// Value getter/setter for $(D JSON_TYPE.INTEGER). /// Value getter/setter for $(D JSON_TYPE.INTEGER).
/// Throws $(D JSONException) for read access if $(D type) is not $(D JSON_TYPE.INTEGER). /// Throws $(D JSONException) for read access if $(D type) is not $(D JSON_TYPE.INTEGER).
@ -372,6 +377,18 @@ struct JSONValue
store = arg.store; store = arg.store;
type_tag = arg.type; type_tag = arg.type;
} }
///
unittest
{
JSONValue j = JSONValue( "a string" );
j = JSONValue(42);
j = JSONValue( [1, 2, 3] );
assert(j.type() == JSON_TYPE.ARRAY);
j = JSONValue( ["language": "D"] );
assert(j.type() == JSON_TYPE.OBJECT);
}
void opAssign(T)(T arg) if(!isStaticArray!T && !is(T : JSONValue)) void opAssign(T)(T arg) if(!isStaticArray!T && !is(T : JSONValue))
{ {
@ -393,6 +410,13 @@ struct JSONValue
"JSONValue array index is out of range"); "JSONValue array index is out of range");
return store.array[i]; return store.array[i];
} }
///
unittest
{
JSONValue j = JSONValue( [42, 43, 44] );
assert( j[0].integer == 42 );
assert( j[1].integer == 43 );
}
/// Hash syntax for json objects. /// Hash syntax for json objects.
/// Throws $(D JSONException) if $(D type) is not $(D JSON_TYPE.OBJECT). /// Throws $(D JSONException) if $(D type) is not $(D JSON_TYPE.OBJECT).
@ -403,6 +427,12 @@ struct JSONValue
return *enforce!JSONException(k in store.object, return *enforce!JSONException(k in store.object,
"Key not found: " ~ k); "Key not found: " ~ k);
} }
///
unittest
{
JSONValue j = JSONValue( ["language": "D"] );
assert( j["language"].str() == "D" );
}
/// Operator sets $(D value) for element of JSON object by $(D key) /// Operator sets $(D value) for element of JSON object by $(D key)
/// If JSON value is null, then operator initializes it with object and then /// If JSON value is null, then operator initializes it with object and then
@ -419,6 +449,13 @@ struct JSONValue
store.object[key] = value; store.object[key] = value;
} }
///
unittest
{
JSONValue j = JSONValue( ["language": "D"] );
j["language"].str = "Perl";
assert( j["language"].str == "Perl" );
}
void opIndexAssign(T)(T arg, size_t i) void opIndexAssign(T)(T arg, size_t i)
{ {
@ -428,6 +465,13 @@ struct JSONValue
"JSONValue array index is out of range"); "JSONValue array index is out of range");
store.array[i] = arg; store.array[i] = arg;
} }
///
unittest
{
JSONValue j = JSONValue( ["Perl", "C"] );
j[1].str = "D";
assert( j[1].str == "D" );
}
JSONValue opBinary(string op : "~", T)(T arg) JSONValue opBinary(string op : "~", T)(T arg)
{ {
@ -479,6 +523,12 @@ struct JSONValue
"JSONValue is not an object"); "JSONValue is not an object");
return k in store.object; return k in store.object;
} }
///
unittest
{
JSONValue j = [ "language": "D", "author": "walter" ];
string a = ("author" in j).str;
}
/// Implements the foreach $(D opApply) interface for json arrays. /// Implements the foreach $(D opApply) interface for json arrays.
int opApply(int delegate(size_t index, ref JSONValue) dg) int opApply(int delegate(size_t index, ref JSONValue) dg)