Skip to content

Commit

Permalink
More tests being ported over
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Dec 7, 2024
1 parent 2dc1cee commit 25ff60a
Show file tree
Hide file tree
Showing 12 changed files with 1,040 additions and 1,085 deletions.
155 changes: 0 additions & 155 deletions modules/juce_core/javascript/juce_JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,159 +644,4 @@ Result JSON::parseQuotedString (String::CharPointerType& t, var& result)
return Result::ok();
}

//==============================================================================
//==============================================================================
#if JUCE_UNIT_TESTS

class JSONTests final : public UnitTest
{
public:
JSONTests()
: UnitTest ("JSON", UnitTestCategories::json)
{
}

static String createRandomWideCharString (Random& r)
{
juce_wchar buffer[40] = { 0 };

for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
{
if (r.nextBool())
{
do
{
buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
} while (! CharPointer_UTF16::canRepresent (buffer[i]));
}
else
buffer[i] = (juce_wchar) (1 + r.nextInt (0xff));
}

return CharPointer_UTF32 (buffer);
}

static String createRandomIdentifier (Random& r)
{
char buffer[30] = { 0 };

for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
{
static const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:";
buffer[i] = chars[r.nextInt (sizeof (chars) - 1)];
}

return CharPointer_ASCII (buffer);
}

// Creates a random double that can be easily stringified, to avoid
// false failures when decimal places are rounded or truncated slightly
static var createRandomDouble (Random& r)
{
return var ((r.nextDouble() * 1000.0) + 0.1);
}

static var createRandomVar (Random& r, int depth)
{
switch (r.nextInt (depth > 3 ? 6 : 8))
{
case 0:
return {};
case 1:
return r.nextInt();
case 2:
return r.nextInt64();
case 3:
return r.nextBool();
case 4:
return createRandomDouble (r);
case 5:
return createRandomWideCharString (r);

case 6:
{
var v (createRandomVar (r, depth + 1));

for (int i = 1 + r.nextInt (30); --i >= 0;)
v.append (createRandomVar (r, depth + 1));

return v;
}

case 7:
{
auto o = new DynamicObject();

for (int i = r.nextInt (30); --i >= 0;)
o->setProperty (createRandomIdentifier (r), createRandomVar (r, depth + 1));

return o;
}

default:
return {};
}
}

void runTest() override
{
{
beginTest ("JSON");

auto r = getRandom();

expect (JSON::parse (String()) == var());
expect (JSON::parse ("{}").isObject());
expect (JSON::parse ("[]").isArray());
expect (JSON::parse ("[ 1234 ]")[0].isInt());
expect (JSON::parse ("[ 12345678901234 ]")[0].isInt64());
expect (JSON::parse ("[ 1.123e3 ]")[0].isDouble());
expect (JSON::parse ("[ -1234]")[0].isInt());
expect (JSON::parse ("[-12345678901234]")[0].isInt64());
expect (JSON::parse ("[-1.123e3]")[0].isDouble());

for (int i = 100; --i >= 0;)
{
var v;

if (i > 0)
v = createRandomVar (r, 0);

const auto oneLine = r.nextBool();
const auto asString = JSON::toString (v, oneLine);
const auto parsed = JSON::parse ("[" + asString + "]")[0];
const auto parsedString = JSON::toString (parsed, oneLine);
expect (asString.isNotEmpty() && parsedString == asString);
}
}

{
beginTest ("Float formatting");

std::map<double, String> tests;
tests[1] = "1.0";
tests[1.1] = "1.1";
tests[1.01] = "1.01";
tests[0.76378] = "0.76378";
tests[-10] = "-10.0";
tests[10.01] = "10.01";
tests[0.0123] = "0.0123";
tests[-3.7e-27] = "-3.7e-27";
tests[1e+40] = "1.0e40";
tests[-12345678901234567.0] = "-1.234567890123457e16";
tests[192000] = "192000.0";
tests[1234567] = "1.234567e6";
tests[0.00006] = "0.00006";
tests[0.000006] = "6.0e-6";

for (auto& test : tests)
expectEquals (JSON::toString (test.first), test.second);
}
}
};

static JSONTests JSONUnitTests;

#endif

} // namespace juce
11 changes: 4 additions & 7 deletions modules/juce_core/javascript/juce_JSONSerialisation.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,9 @@ class ToVar

if (obj == nullptr)
{
// Serialisation failure! This may be caused by archiving a primitive or
// SerialisationSize, and then attempting to archive a named pair to the same
// archive instance.
// When using named pairs, *all* items serialised with a particular archiver must be
// named pairs.
jassertfalse;
// Serialisation failure! This may be caused by archiving a primitive or SerialisationSize, and
// then attempting to archive a named pair to the same archive instance.
// When using named pairs, *all* items serialised with a particular archiver must be named pairs.

value.reset();
return;
Expand Down Expand Up @@ -246,7 +243,7 @@ class ToVar
{
if (const auto converted = convert (n.value))
{
obj.setProperty (Identifier (std::string (n.name)), *converted);
obj.setProperty (Identifier (String (n.name)), *converted);
return true;
}

Expand Down
Loading

0 comments on commit 25ff60a

Please sign in to comment.