Skip to content

Commit

Permalink
[tools][builder][tests] Test exceptions in code
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Oct 6, 2024
1 parent fab08ec commit cb76ee8
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 62 deletions.
35 changes: 18 additions & 17 deletions sc-tools/sc-builder/src/gwf_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@

using namespace Constants;

void XmlCharDeleter::operator()(xmlChar * ptr) const
{
if (ptr != nullptr)
xmlFree(ptr);
}

void GWFParser::Parse(std::string const & xmlStr, SCgElements & elements)
{
xmlInitParser();

auto xmlTree = std::unique_ptr<xmlDoc, decltype(&xmlFreeDoc)>(
xmlReadMemory(xmlStr.c_str(), xmlStr.size(), "noname.xml", nullptr, 0), xmlFreeDoc);

if (xmlTree == nullptr)
SC_THROW_EXCEPTION(utils::ExceptionParseError, "Failed to open XML xmlTree.");

Expand All @@ -43,15 +48,7 @@ void GWFParser::Parse(std::string const & xmlStr, SCgElements & elements)
if (staticSector->children == nullptr)
return;

try
{
ProcessStaticSector(staticSector, elements);
}
catch (utils::ScException const & e)
{
SC_LOG_ERROR("GWFParser::ProcessStaticSector: Exception caught in process static sector " << e.Message());
}

ProcessStaticSector(staticSector, elements);
xmlCleanupParser();
}

Expand Down Expand Up @@ -159,9 +156,6 @@ std::shared_ptr<SCgLink> GWFParser::CreateLink(

bool GWFParser::HasContent(xmlNodePtr const node) const
{
if (node == nullptr)
return false;

for (xmlNodePtr child = node->children; child; child = child->next)
{
if (child->type == XML_ELEMENT_NODE && xmlStrcmp(child->name, CONTENT) == 0)
Expand Down Expand Up @@ -253,16 +247,18 @@ void GWFParser::FillContours(SCgContours const & contours, SCgElements const & a
{
for (auto const & [contourId, elements] : contours)
{
auto const & contour = std::dynamic_pointer_cast<SCgContour>(allElements.at(contourId));
auto const & it = allElements.find(contourId);
if (it == allElements.cend())
SC_THROW_EXCEPTION(
utils::ExceptionParseError, "GWFParser::FillContours: Invalid contour id `" << contourId << "`.");

auto const & contour = std::dynamic_pointer_cast<SCgContour>(it->second);
contour->SetElements(elements);
}
}

std::string GWFParser::XmlCharToString(std::unique_ptr<xmlChar, XmlCharDeleter> const & ptr) const
{
if (ptr == nullptr)
return "";

std::size_t length = xmlStrlen(ptr.get());

std::string result(length, '\0');
Expand All @@ -280,6 +276,11 @@ std::unique_ptr<xmlChar, XmlCharDeleter> GWFParser::GetXmlProp(xmlNodePtr node,
std::string GWFParser::GetXmlPropStr(xmlNodePtr node, std::string const & propName) const
{
std::unique_ptr<xmlChar, XmlCharDeleter> const prop = GetXmlProp(node, propName);
if (prop == nullptr)
SC_THROW_EXCEPTION(
utils::ExceptionParseError,
"GWFParser::GetXmlPropStr: Gwf-element doesn't have property name `" << propName << "`.");

std::string const propStr = XmlCharToString(prop);
return propStr;
}
6 changes: 1 addition & 5 deletions sc-tools/sc-builder/src/gwf_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
class XmlCharDeleter
{
public:
void operator()(xmlChar * ptr) const
{
if (ptr == nullptr)
xmlFree(ptr);
}
void operator()(xmlChar * ptr) const;
};

class SCgElement;
Expand Down
10 changes: 5 additions & 5 deletions sc-tools/sc-builder/src/gwf_translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ GWFTranslator::GWFTranslator(ScMemoryContext & context)

std::string GWFTranslator::TranslateGWFToSCs(std::string const & xmlStr, std::string const & filePath)
{
SCgElements elementWithoutParents;
SCgElements elementsWithoutParents;

GWFParser parser;
parser.Parse(xmlStr, elementWithoutParents);
parser.Parse(xmlStr, elementsWithoutParents);

if (elementWithoutParents.empty())
if (elementsWithoutParents.empty())
SC_THROW_EXCEPTION(
utils::ExceptionParseError,
"GWFTranslator::TranslateGWFToSCs: There are no elements in file `" << filePath << "`.");

SCsWriter writer;
Buffer scsBuffer;
std::unordered_set<SCgElementPtr> writtenElements;
writer.Write(elementWithoutParents, filePath, scsBuffer, 0, writtenElements);
writer.Write(elementsWithoutParents, filePath, scsBuffer, 0, writtenElements);

return scsBuffer.GetValue();
}
Expand Down Expand Up @@ -104,7 +104,7 @@ bool GWFTranslator::TranslateImpl(Params const & params)
if (gwfText.empty())
SC_THROW_EXCEPTION(
utils::ExceptionInvalidParams,
"GWFTranslator::TranslateImpl: .gwf file `" << params.m_fileName << "` is empty.");
"GWFTranslator::TranslateImpl: GWF file `" << params.m_fileName << "` is empty.");

std::string const & scsText = TranslateGWFToSCs(gwfText, params.m_fileName);
std::string const & scsSource = WriteStringToFile(scsText, params.m_fileName);
Expand Down
15 changes: 0 additions & 15 deletions sc-tools/sc-builder/src/sc_scg_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ std::string const & SCgElement::GetId() const
return m_id;
}

std::string const & SCgElement::GetParent() const
{
return m_parent;
}

std::string const & SCgElement::GetIdentifier() const
{
return m_identifier;
Expand All @@ -46,11 +41,6 @@ std::string const & SCgElement::GetTag() const
return m_tag;
}

void SCgElement::SetIdentifier(std::string const & newIdentifier)
{
m_identifier = newIdentifier;
}

// SCgNode
SCgNode::SCgNode(
std::string const & id,
Expand Down Expand Up @@ -86,11 +76,6 @@ std::string const & SCgLink::GetContentType() const
return m_contentType;
}

std::string const & SCgLink::GetMimeType() const
{
return m_mimeType;
}

std::string const & SCgLink::GetFileName() const
{
return m_fileName;
Expand Down
4 changes: 0 additions & 4 deletions sc-tools/sc-builder/src/sc_scg_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ class SCgElement
virtual ~SCgElement() = default;

std::string const & GetId() const;
std::string const & GetParent() const;
std::string const & GetIdentifier() const;
std::string const & GetType() const;
std::string const & GetTag() const;

void SetIdentifier(std::string const & newIdentifier);

private:
std::string m_id;
std::string m_parent;
Expand Down Expand Up @@ -64,7 +61,6 @@ class SCgLink : public SCgNode
std::string const & contentData);

std::string const & GetContentType() const;
std::string const & GetMimeType() const;
std::string const & GetFileName() const;
std::string const & GetContentData() const;

Expand Down
5 changes: 1 addition & 4 deletions sc-tools/sc-builder/src/sc_scg_to_scs_types_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ std::string SCgToSCsTypesConverter::GetSCsElementTypeBySCgElementType(
{"BackwardConnectorTypes", &m_backwardConnectorTypes},
{"UnsupportedConnectorTypes", &m_unsupportedConnectorTypes}};

auto it = dictMap.find(dict);
if (it == dictMap.end())
return "";

auto const it = dictMap.find(dict);
return GetSCsElementDesignation(*(it->second), scgElement);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ best!]]></content>
<node type="node/const/perm/general" idtf="3type" shapeColor="0" id="108733542566480" parent="0" left="0" top="0" right="52.8125" bottom="25" textColor="164" text_angle="0" text_font="Times New Roman [Arial]" font_size="10" x="64" y="368.647" haveBus="false" idtf_pos="0">
<content type="3" mime_type="content/term" content_visibility="true" file_name=""><![CDATA[343.344]]></content>
</node>
<node type="node/const/perm/general" idtf="2type" shapeColor="0" id="243323434343434" parent="0" left="0" top="0" right="52.8125" bottom="25" textColor="164" text_angle="0" text_font="Times New Roman [Arial]" font_size="10" x="64" y="368.647" haveBus="false" idtf_pos="0">
<content type="2" mime_type="content/term" content_visibility="true" file_name=""><![CDATA[12]]></content>
</node>
</staticSector>
</GWF>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
@format_arc = (4type => format_png);;
@nrel_format_arc = (nrel_format -> @format_arc);;

2type = ["int64:12"];;

3type = ["float:343.344"];;

1type = [OSTIS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<GWF version="2.0">
<staticSector>
<contour type="contour/const/perm" idtf="" shapeColor="0" id="108108543037984" parent="2342323">
<points>
<point x="104" y="241"/>
<point x="86" y="490"/>
<point x="671" y="537"/>
<point x="627" y="249"/>
</points>
</contour>
</staticSector>
</GWF>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<GWF version="2.0">
<staticSector>
<node type="not/specified" idtf="" id="108108543037984" shapeColor="0" parent="0">
</node>
</staticSector>
</GWF>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<GWF version="2.0">
<staticSector/>
</GWF>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<GWF version="2.0">
<staticSector>
<node type="node/const/perm/general" idtf="" shapeColor="0" parent="0">
</node>
</staticSector>
</GWF>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<GWF version="2.0">
<staticSector>
<node type="node/const/perm/general" idtf="3type" shapeColor="0" id="108733542566480" parent="0" left="0" top="0" right="52.8125" bottom="25" textColor="164" text_angle="0" text_font="Times New Roman [Arial]" font_size="10" x="64" y="368.647" haveBus="false" idtf_pos="0">
<content type="5" mime_type="content/term" content_visibility="true" file_name=""><![CDATA[343.344]]></content>
</node>
</staticSector>
</GWF>
8 changes: 8 additions & 0 deletions sc-tools/sc-builder/tests/kb/tests-gwf-to-scs/unknown_tag.gwf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<GWF version="2.0">
<staticSector>
<unknown type="node/const/perm/general" idtf="3type" shapeColor="0" id="108733542566480" parent="0" left="0" top="0" right="52.8125" bottom="25" textColor="164" text_angle="0" text_font="Times New Roman [Arial]" font_size="10" x="64" y="368.647" haveBus="false" idtf_pos="0">
<content type="3" mime_type="content/term" content_visibility="true" file_name=""><![CDATA[343.344]]></content>
</unknown>
</staticSector>
</GWF>
2 changes: 2 additions & 0 deletions sc-tools/sc-builder/tests/repo.path
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kb
!kb/tests-gwf-to-scs
4 changes: 2 additions & 2 deletions sc-tools/sc-builder/tests/units/builder_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScBuilderTest : public testing::Test
params.dump_memory_statistics = SC_FALSE;

params.clear = SC_FALSE;
params.repo_path = SC_BUILDER_REPO_PATH;
params.repo_path = SC_BUILDER_KB_BIN;

ScMemory::LogMute();
ScMemory::Initialize(params);
Expand All @@ -52,7 +52,7 @@ class ScBuilderTest : public testing::Test
params.dump_memory_statistics = SC_FALSE;

params.clear = SC_FALSE;
params.repo_path = SC_BUILDER_REPO_PATH;
params.repo_path = SC_BUILDER_KB_BIN;

params.user_mode = SC_TRUE;

Expand Down
16 changes: 8 additions & 8 deletions sc-tools/sc-builder/tests/units/test_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ TEST(ScBuilder, RunMain)
"-c",
SC_BUILDER_INI,
"-i",
SC_BUILDER_KB,
"-o",
SC_BUILDER_REPO_PATH,
"-o",
SC_BUILDER_KB_BIN,
"--enabled_ext",
"",
"--clear"};
Expand All @@ -50,14 +50,14 @@ TEST(ScBuilder, RunStopBuilder)
ScOptions options{1, nullptr};

BuilderParams params;
params.m_inputPath = SC_BUILDER_KB;
params.m_outputPath = SC_BUILDER_REPO_PATH;
params.m_inputPath = SC_BUILDER_REPO_PATH;
params.m_outputPath = SC_BUILDER_KB_BIN;
params.m_autoFormatInfo = SC_TRUE;

std::string config = SC_BUILDER_INI;

ScParams memoryParams{options, {}};
memoryParams.Insert({"repo_path", SC_BUILDER_REPO_PATH});
memoryParams.Insert({"repo_path", SC_BUILDER_KB_BIN});

ScConfig configFile{config, {"repo_path"}};
std::string memoryGroupName = "sc-memory";
Expand Down Expand Up @@ -113,15 +113,15 @@ TEST(ScBuilder, BuilderConfig)
ScOptions options{1, nullptr};

BuilderParams builderParams;
builderParams.m_inputPath = SC_BUILDER_KB;
builderParams.m_outputPath = SC_BUILDER_REPO_PATH;
builderParams.m_inputPath = SC_BUILDER_REPO_PATH;
builderParams.m_outputPath = SC_BUILDER_KB_BIN;
builderParams.m_autoFormatInfo = SC_TRUE;

std::string config = SC_BUILDER_INI;
ScConfig configFile{config, {"repo_path"}};

ScParams memoryParams{options, {}};
memoryParams.Insert({"repo_path", SC_BUILDER_REPO_PATH});
memoryParams.Insert({"repo_path", SC_BUILDER_KB_BIN});
memoryParams.Insert({"clear", {}});
ScMemoryConfig memoryConfig{configFile, memoryParams};

Expand Down
3 changes: 2 additions & 1 deletion sc-tools/sc-builder/tests/units/test_defines.hpp.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define SC_BUILDER_REPO_PATH "${SC_BIN_PATH}/sc-builder-test-repo"
#define SC_BUILDER_KB_BIN "${SC_BIN_PATH}/sc-builder-test-repo"
#define SC_BUILDER_REPO_PATH "${CMAKE_CURRENT_LIST_DIR}/repo.path"
#define SC_BUILDER_KB "${CMAKE_CURRENT_LIST_DIR}/kb"
#define SC_BUILDER_INI "${CMAKE_CURRENT_LIST_DIR}/units/sc-builder-test.ini"
#define SC_BUILDER_TEST_REPOS "${CMAKE_CURRENT_LIST_DIR}/repos"
Loading

0 comments on commit cb76ee8

Please sign in to comment.