/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
*/
-#include <iostream>
-#include <stdlib.h>
#include <dali-toolkit-test-suite-utils.h>
#include <dali-toolkit/dali-toolkit.h>
-#include <dali-toolkit/public-api/builder/json-parser.h>
+#include <dali-toolkit/devel-api/builder/json-parser.h>
+#include <stdlib.h>
+#include <iostream>
using namespace Dali;
using namespace Dali::Toolkit;
namespace
{
-
-std::string ReplaceQuotes(const std::string &in_s)
+std::string ReplaceQuotes(const std::string& in_s)
{
std::string s(in_s);
// wrong as no embedded quote but had regex link problems
void CompareTrees(const TreeNode& a, const TreeNode& b)
{
- DALI_TEST_CHECK( a.GetType() == b.GetType() );
+ DALI_TEST_CHECK(a.GetType() == b.GetType());
- DALI_TEST_CHECK( a.Size() == b.Size() );
+ DALI_TEST_CHECK(a.Size() == b.Size());
- if( a.GetName() )
+ if(a.GetName())
{
- DALI_TEST_CHECK( std::string( a.GetName() ) == std::string( b.GetName() ) );
+ DALI_TEST_CHECK(std::string(a.GetName()) == std::string(b.GetName()));
}
- DALI_TEST_CHECK( a.HasSubstitution() == b.HasSubstitution() );
+ DALI_TEST_CHECK(a.HasSubstitution() == b.HasSubstitution());
- switch( a.GetType() )
+ switch(a.GetType())
{
case TreeNode::OBJECT:
case TreeNode::ARRAY:
{
- for( TreeNode::ConstIterator aiter = a.CBegin(), biter = b.CBegin();
- aiter != a.CEnd() && biter != b.CEnd(); ++aiter, ++biter )
+ for(TreeNode::ConstIterator aiter = a.CBegin(), biter = b.CBegin();
+ aiter != a.CEnd() && biter != b.CEnd();
+ ++aiter, ++biter)
{
- CompareTrees( (*aiter).second, (*biter).second );
+ CompareTrees((*aiter).second, (*biter).second);
}
break;
}
case TreeNode::STRING:
{
- DALI_TEST_CHECK( std::string( a.GetString() ) == std::string( b.GetString() ) );
+ DALI_TEST_CHECK(std::string(a.GetString()) == std::string(b.GetString()));
break;
}
case TreeNode::FLOAT:
{
- DALI_TEST_CHECK( a.GetFloat() == b.GetFloat() );
+ DALI_TEST_CHECK(a.GetFloat() == b.GetFloat());
break;
}
case TreeNode::INTEGER:
{
- DALI_TEST_CHECK( a.GetInteger() == b.GetInteger());
+ DALI_TEST_CHECK(a.GetInteger() == b.GetInteger());
break;
}
case TreeNode::BOOLEAN:
{
- DALI_TEST_CHECK( a.GetBoolean() == b.GetBoolean() );
+ DALI_TEST_CHECK(a.GetBoolean() == b.GetBoolean());
break;
}
default:
}
}
-
-}
-
+} // namespace
int UtcDaliJsonParserMethod01(void)
{
tet_infoline("JSON basic test");
- std::string s1( ReplaceQuotes(
-"{ \
+ std::string s1(ReplaceQuotes(
+ "{ \
'string':'value2', \
'integer':2, \
'float':2.0, \
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
if(parser.ParseError())
{
TreeNode::ConstIterator iterArray = node->CBegin();
DALI_TEST_CHECK(iterArray != node->CEnd());
- DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
- DALI_TEST_CHECK( (*iterArray).first == NULL );
- DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 1);
+ DALI_TEST_CHECK(((*iterArray).second).GetType() == TreeNode::INTEGER);
+ DALI_TEST_CHECK((*iterArray).first == NULL);
+ DALI_TEST_CHECK(((*iterArray).second).GetInteger() == 1);
++iterArray;
DALI_TEST_CHECK(iterArray != node->CEnd());
- DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
- DALI_TEST_CHECK( (*iterArray).first == NULL );
- DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 2);
+ DALI_TEST_CHECK(((*iterArray).second).GetType() == TreeNode::INTEGER);
+ DALI_TEST_CHECK((*iterArray).first == NULL);
+ DALI_TEST_CHECK(((*iterArray).second).GetInteger() == 2);
++iterArray;
DALI_TEST_CHECK(iterArray != node->CEnd());
- DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
- DALI_TEST_CHECK( (*iterArray).first == NULL );
- DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 3);
+ DALI_TEST_CHECK(((*iterArray).second).GetType() == TreeNode::INTEGER);
+ DALI_TEST_CHECK((*iterArray).first == NULL);
+ DALI_TEST_CHECK(((*iterArray).second).GetInteger() == 3);
++iter;
DALI_TEST_CHECK(iter != root->CEnd());
TreeNode::ConstIterator iterObject = node->CBegin();
DALI_TEST_CHECK(iterObject != node->CEnd());
- DALI_TEST_CHECK( ((*iterObject).second).GetType() == TreeNode::STRING);
- DALI_TEST_CHECK( std::string((*iterObject).first) == std::string("key" ));
- DALI_TEST_CHECK( std::string(((*iterObject).second).GetString()) == std::string("value"));
+ DALI_TEST_CHECK(((*iterObject).second).GetType() == TreeNode::STRING);
+ DALI_TEST_CHECK(std::string((*iterObject).first) == std::string("key"));
+ DALI_TEST_CHECK(std::string(((*iterObject).second).GetString()) == std::string("value"));
+
+ ++iter;
+ DALI_TEST_CHECK(!(iter != root->CEnd()));
+
+ ++iter; // Go past the end
+
+ iter++; // Use the other operator using the post increment operator
tet_result(TET_PASS);
END_TEST;
tet_infoline("JSON Comments");
- std::string s1( ReplaceQuotes(" \
+ std::string s1(ReplaceQuotes(
+ " \
// some comments with empty line above \n\
{ \
// inline comments \n\
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
if(parser.ParseError())
{
END_TEST;
}
-
int UtcDaliJsonParserMethod03(void)
{
ToolkitTestApplication application;
tet_infoline("JSON Empty line comment");
- std::string s1( ReplaceQuotes(
-"/*\n" \
-"c comment\n" \
-"*/"\
-"// next empty line comment\n"\
-"//\n"\
-"{\n"\
-" 'key':'value'\n"\
-"}\n"\
-));
+ std::string s1(ReplaceQuotes(
+ "/*\n"
+ "c comment\n"
+ "*/"
+ "// next empty line comment\n"
+ "//\n"
+ "{\n"
+ " 'key':'value'\n"
+ "}\n"));
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
if(parser.ParseError())
{
tet_infoline("JSON Merge");
- std::string s1( ReplaceQuotes(" \
+ std::string s1(ReplaceQuotes(
+ " \
{ \
'animations': \
{ \
'actor':'bump-image', \
'property':'uLightPosition', \
'value':[0.8, 0.0, -1.5], \
- 'alpha-function': 'BOUNCE', \
- 'time-period': { 'duration': 2.5 } \
+ 'alphaFunction': 'BOUNCE', \
+ 'timePeriod': { 'duration': 2.5 } \
} \
] \
} \
} \
"));
- std::string s2( ReplaceQuotes(" \
+ std::string s2(ReplaceQuotes(
+ " \
{ \
'animations': \
{ \
{ \
'duration': 5.0, \
'loop': true, \
- 'end-action':'DISCARD' \
+ 'endAction':'DISCARD' \
} \
} \
} \
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
if(parser.ParseError())
{
}
DALI_TEST_CHECK(!parser.ParseError());
- parser.Parse( s2 );
+ parser.Parse(s2);
if(parser.ParseError())
{
const TreeNode* root = parser.GetRoot();
DALI_TEST_CHECK(root);
- const TreeNode *node = root->Find("bump");
+ const TreeNode* node = root->Find("bump");
DALI_TEST_CHECK(node);
DALI_TEST_CHECK(static_cast<int>(node->Size()) == 4);
- DALI_TEST_CHECK( node->GetChild("duration") );
- DALI_TEST_CHECK( node->GetChild("loop") );
- DALI_TEST_CHECK( node->GetChild("properties") );
-
+ DALI_TEST_CHECK(node->GetChild("duration"));
+ DALI_TEST_CHECK(node->GetChild("loop"));
+ DALI_TEST_CHECK(node->GetChild("properties"));
tet_result(TET_PASS);
END_TEST;
tet_infoline("JSON Pack & Write");
- std::string s1( ReplaceQuotes(" \
+ std::string s1(ReplaceQuotes(
+ " \
{ \
'animations': \
{ \
'actor':'bump-image', \
'property':'uLightPosition', \
'value':[0.8, 0.0, -1.5], \
- 'alpha-function': 'BOUNCE', \
- 'time-period': { 'duration': 2.5 } \
+ 'alphaFunction': 'BOUNCE', \
+ 'timePeriod': { 'duration': 2.5 } \
} \
] \
} \
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
if(parser.ParseError())
{
std::stringstream b;
parser.Write(b, 2);
- DALI_TEST_CHECK( a.str() == b.str() );
+ DALI_TEST_CHECK(a.str() == b.str());
tet_result(TET_PASS);
END_TEST;
namespace
{
-
-static const int NUMBER_OK_TESTS = 36;
-const char *TEST_OK[NUMBER_OK_TESTS] = {
+static const int NUMBER_OK_TESTS = 36;
+const char* TEST_OK[NUMBER_OK_TESTS] = {
"{ 'hex': '\u0123\u4567\u89AB\uCDEF\uabcd\uef4A' }",
"{ 'special': '`1~!@#$%^&*()_+-={:[,]}|;.</>?' }",
"{ 'slash': '/ & \' }",
"{'JSON Test Pattern pass3': { 'The outermost value': 'must be an object or array.', 'In this test': 'It is an object.' } }",
"[[[[[[[[[[[[[[[[[[['Not too deep']]]]]]]]]]]]]]]]]]]",
};
-}
-
+} // namespace
int UtcDaliJsonParserMethod06(void)
{
{
parser = JsonParser::New();
- parser.Parse( ReplaceQuotes(TEST_OK[i]) );
+ parser.Parse(ReplaceQuotes(TEST_OK[i]));
if(parser.ParseError())
{
namespace
{
-
static const int NUMBER_FAIL_TESTS = 34;
-const char *TEST_FAIL[] = {
+const char* TEST_FAIL[] = {
"[' tab\t character \t in\t string ']",
"['Extra close']]",
"['Colon instead of comma': false]",
"[{'no comma':1} {'b:2}]",
"{'extra comma':1,}",
};
-}
+} // namespace
int UtcDaliJsonParserMethod07(void)
{
{
parser = JsonParser::New();
- parser.Parse( ReplaceQuotes(TEST_FAIL[i]) );
+ parser.Parse(ReplaceQuotes(TEST_FAIL[i]));
if(!parser.ParseError())
{
tet_printf("Invalid JSON parse test %d Failed", i);
tet_printf("%s", ReplaceQuotes(TEST_FAIL[i]).c_str());
- tet_printf("JSON Error %d:%d %s (%s)", parser.GetErrorLineNumber(), parser.GetErrorColumn(),
- parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
+ tet_printf("JSON Error %d:%d %s (%s)", parser.GetErrorLineNumber(), parser.GetErrorColumn(), parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
}
DALI_TEST_CHECK(parser.ParseError());
}
-
parser = JsonParser::New();
- parser.Parse( "['single quote']" );
+ parser.Parse("['single quote']");
if(!parser.ParseError())
{
tet_infoline("JSON error reporting");
- std::string s1( ReplaceQuotes("\
+ std::string s1(ReplaceQuotes(
+ "\
{ \n\
'float':,], \n\
} \n\
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
DALI_TEST_CHECK(parser.ParseError());
- DALI_TEST_CHECK(1 == parser.GetErrorLineNumber());
+ DALI_TEST_CHECK(1 == parser.GetErrorLineNumber());
DALI_TEST_CHECK(53 == parser.GetErrorPosition());
DALI_TEST_CHECK(11 == parser.GetErrorColumn());
+ DALI_TEST_CHECK("Missing Value" == parser.GetErrorDescription());
tet_result(TET_PASS);
END_TEST;
tet_infoline("JSON Pack()");
- std::string s1( ReplaceQuotes("\
+ std::string s1(ReplaceQuotes(
+ "\
{ \
'string':'value2', \
'integer':2, \
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
std::stringstream ss1;
parser.Write(ss1, 2);
tet_infoline("JSON empty data");
- std::string s1( "" );
+ std::string s1("");
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
DALI_TEST_CHECK(parser.ParseError());
ToolkitTestApplication application;
tet_infoline("JSON tree copy");
- std::string s1( ReplaceQuotes(" \
+ std::string s1(ReplaceQuotes(
+ " \
{ \
'animations': \
{ \
'actor':'bump-image', \
'property':'uLightPosition', \
'value':[0.8, 0.0, -1.5], \
- 'alpha-function': 'BOUNCE', \
- 'time-period': { 'duration': 2.5 } \
+ 'alphaFunction': 'BOUNCE', \
+ 'timePeriod': { 'duration': 2.5 } \
} \
] \
} \
JsonParser parser = JsonParser::New();
- parser.Parse( s1 );
+ parser.Parse(s1);
JsonParser parser2 = JsonParser::New(*parser.GetRoot());
DALI_TEST_CHECK(parser.GetRoot());
DALI_TEST_CHECK(parser2.GetRoot());
- CompareTrees( *parser.GetRoot(), *parser2.GetRoot() );
+ CompareTrees(*parser.GetRoot(), *parser2.GetRoot());
tet_result(TET_PASS);
END_TEST;
}
+
+int UtcDaliJsonParserMerge1(void)
+{
+ ToolkitTestApplication application;
+ tet_infoline("JSON tree merge");
+
+ std::string s1(ReplaceQuotes(
+ " \
+{ \
+ 'styles': \
+ { \
+ 'button': \
+ { \
+ 'backgroundColor':[0.8, 0.0, 1.0, 1.0], \
+ 'foregroundColor':[1, 1, 1, 1] \
+ } \
+ } \
+} \
+"));
+
+ JsonParser parser = JsonParser::New();
+ JsonParser testParser = JsonParser::New();
+
+ testParser.Parse(s1);
+
+ parser.Parse(s1);
+ parser.Parse(s1); // Merge the tree into itself. The value array should not grow.
+
+ DALI_TEST_CHECK(parser.GetRoot());
+
+ CompareTrees(*parser.GetRoot(), *testParser.GetRoot());
+
+ END_TEST;
+}
+
+int UtcDaliJsonParserDownCast(void)
+{
+ BaseHandle handle = JsonParser::New();
+ JsonParser parser = JsonParser::DownCast(handle);
+ DALI_TEST_CHECK(parser);
+ END_TEST;
+}
+
+int UtcDaliJsonParserTreeNodeCount(void)
+{
+ std::string s1(ReplaceQuotes(
+ " \
+{ \
+ 'styles': \
+ { \
+ 'button': \
+ { \
+ 'backgroundColor':[0.8, 0.0, 1.0, 1.0], \
+ 'foregroundColor':[1, 1, 1, 1] \
+ } \
+ } \
+} \
+"));
+
+ JsonParser parser = JsonParser::New();
+ parser.Parse(s1);
+
+ const TreeNode* treeNode = parser.GetRoot();
+ DALI_TEST_EQUALS(1, treeNode->Count("styles"), TEST_LOCATION);
+ DALI_TEST_EQUALS(0, treeNode->Count("random"), TEST_LOCATION);
+
+ END_TEST;
+}
+
+int UtcDaliJsonParserTreeNodeFind(void)
+{
+ std::string s1(ReplaceQuotes(
+ "\
+{ \
+ 'string':'value2', \
+ 'integer':2, \
+ 'float':2.3, \
+ 'boolean':true, \
+ 'nil':null, \
+ 'array':[1,2,3], \
+ 'object':{'key':'value'} \
+} \
+"));
+
+ JsonParser parser = JsonParser::New();
+ parser.Parse(s1);
+
+ const TreeNode* treeNode = parser.GetRoot();
+ const TreeNode* childNode = treeNode->Find("string");
+ DALI_TEST_CHECK(childNode);
+ const TreeNode* sameNode = childNode->Find("string");
+ DALI_TEST_EQUALS(sameNode, childNode, TEST_LOCATION);
+
+ END_TEST;
+}