Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Add tests for RefractElementFactory
Browse files Browse the repository at this point in the history
Cleanup forgoted `#ifdef`
  • Loading branch information
Jiri Kratochvil committed Mar 9, 2016
1 parent 50a7a26 commit d6e34c6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
1 change: 1 addition & 0 deletions drafter.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"test/test-CircularReferenceTest.cc",
"test/test-ApplyVisitorTest.cc",
"test/test-ExtendElementTest.cc",
"test/test-ElementFactoryTest.cc",
],
'dependencies': [
"libdrafter",
Expand Down
2 changes: 0 additions & 2 deletions src/NamedTypesRegistry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ namespace drafter {
std::cout << "==BASE TYPE ORDER==" << std::endl;
#endif /* DEBUG_DEPENDENCIES */

#if 1
// first level registration - we will create empty elements with correct type info
for (DataStructures::const_iterator i = found.begin(); i != found.end(); ++i) {
const std::string& name = i->node->name.symbol.literal;
Expand Down Expand Up @@ -392,7 +391,6 @@ namespace drafter {
GetNamedTypesRegistry().add(element);
}
}
#endif

#ifdef DEBUG_DEPENDENCIES
std::cout << "==DEPENDENCIES INFO END==" << std::endl;
Expand Down
28 changes: 26 additions & 2 deletions src/RefractElementFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,42 @@
#define DRAFTER_REFRACTELEMENTFACTORY_H

#include <string>
#include <MSON.h>
#include "MSON.h"

namespace refract {
struct IElement;
}

namespace drafter {

/**
* This interface is used while conversion from legacy AST
* into refract tree.
*
* For usage see test/test-ElementFactoryTest.cc
*
* WARNING:
* There is little bit different behavior for primitives and for complex elements.
*
* For any case:
* - if `literal` is empty - return __empty__ element of required type
*
* For primitives (string|number|bool):
* - `sample` is false - return typed element with __value__ set via `ToLiteral<>` conversion
* - `sample` is true - return typed element with __attributes.samples__ set via `ToLiteral<>` conversion
*
* For complex elements (object|array|enum):
* - `sample` is false - return typed element with set element name to `literal`
* - `sample` is true - return StringElement with name __generic__ with value set to `literal`
*
* This inconsistent behavior is determined by rules of converting legacy AST to Refract.
*
* Code will be deprecated (refactoring or remove) after remove legacy AST.
*/
struct RefractElementFactory
{
virtual ~RefractElementFactory() {}
virtual refract::IElement* Create(const std::string& literal, bool) = 0;
virtual refract::IElement* Create(const std::string& literal, bool sample) = 0;
};

RefractElementFactory& FactoryFromType(const mson::BaseTypeName typeName);
Expand Down
5 changes: 0 additions & 5 deletions test/fixtures/extend/circular.apib
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@
## B (enum)
- (A)
- (array[A])





73 changes: 73 additions & 0 deletions test/test-ElementFactoryTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "catch.hpp"

#include "refract/Visitors.h"
#include "refract/Element.h"

#include "RefractElementFactory.h"


using namespace refract;
using namespace drafter;

TEST_CASE("Create empty primitive element","[ElementFactory]") {
RefractElementFactory& factory = FactoryFromType(mson::StringTypeName);
IElement* e = factory.Create(std::string(), false);

StringElement* str = TypeQueryVisitor::as<StringElement>(e);
REQUIRE(str != NULL);
REQUIRE(str->empty());
REQUIRE(str->meta.empty());
REQUIRE(str->attributes.empty());
REQUIRE(str->element() == StringElement::TraitType::element());
}

TEST_CASE("Create primitive element w/ value","[ElementFactory]") {
RefractElementFactory& factory = FactoryFromType(mson::NumberTypeName);
IElement* e = factory.Create("42", false);

NumberElement* number = TypeQueryVisitor::as<NumberElement>(e);
REQUIRE(number != NULL);
REQUIRE(!number->empty());
REQUIRE(number->meta.empty());
REQUIRE(number->attributes.empty());
REQUIRE(number->value == 42);
}

TEST_CASE("Create primitive element w/ sample","[ElementFactory]") {
RefractElementFactory& factory = FactoryFromType(mson::NumberTypeName);
IElement* e = factory.Create("42", true);

NumberElement* number = TypeQueryVisitor::as<NumberElement>(e);
REQUIRE(number != NULL);
REQUIRE(number->empty());
REQUIRE(number->meta.empty());

REQUIRE(number->attributes.size() == 1); // sample attr
IElement::MemberElementCollection::const_iterator it = number->attributes.find("samples");
REQUIRE((it != number->attributes.end()));
}

TEST_CASE("Create empty complex element","[ElementFactory]") {
RefractElementFactory& factory = FactoryFromType(mson::EnumTypeName);
IElement* e = factory.Create(std::string(), false);

EnumElement* enm = TypeQueryVisitor::as<EnumElement>(e);
REQUIRE(enm != NULL);
REQUIRE(enm->empty());
REQUIRE(enm->meta.empty());
REQUIRE(enm->attributes.empty());
REQUIRE(enm->element() == EnumElement::TraitType::element());
}

TEST_CASE("Create element as generic","[ElementFactory]") {
RefractElementFactory& factory = FactoryFromType(mson::EnumTypeName);
IElement* e = factory.Create("Enumerator", true);

StringElement* generic = TypeQueryVisitor::as<StringElement>(e);
REQUIRE(generic != NULL);
REQUIRE(!generic->empty());
REQUIRE(generic->meta.empty());
REQUIRE(generic->attributes.empty());
REQUIRE(generic->element() == "generic");
REQUIRE(generic->value == "Enumerator");
}

0 comments on commit d6e34c6

Please sign in to comment.