Added a few simple node tests, and the sequence one doesn't pass (let's work now)

This commit is contained in:
Jesse Beder
2011-09-10 23:31:12 -05:00
parent 432268917b
commit b4963ab0fa
2 changed files with 37 additions and 1 deletions

View File

@@ -105,14 +105,26 @@ namespace YAML
// sequence // sequence
void node_data::append(node& node, shared_memory_holder /* pMemory */) void node_data::append(node& node, shared_memory_holder /* pMemory */)
{ {
if(m_type == NodeType::Undefined || m_type == NodeType::Null) {
m_type = NodeType::Sequence;
m_sequence.clear();
}
if(m_type != NodeType::Sequence) if(m_type != NodeType::Sequence)
throw std::runtime_error("Can't append to a non-sequence node"); throw std::runtime_error("Can't append to a non-sequence node");
m_sequence.push_back(&node); m_sequence.push_back(&node);
} }
void node_data::insert(node& key, node& value, shared_memory_holder /* pMemory */) void node_data::insert(node& key, node& value, shared_memory_holder pMemory)
{ {
if(m_type == NodeType::Undefined || m_type == NodeType::Null) {
m_type = NodeType::Map;
m_map.clear();
} else if(m_type == NodeType::Sequence) {
convert_sequence_to_map(pMemory);
}
if(m_type != NodeType::Map) if(m_type != NodeType::Map)
throw std::runtime_error("Can't insert into a non-map node"); throw std::runtime_error("Can't insert into a non-map node");

View File

@@ -25,6 +25,28 @@ namespace Test
YAML_ASSERT(node.as<std::string>() == "Hello, World!"); YAML_ASSERT(node.as<std::string>() == "Hello, World!");
return true; return true;
} }
TEST IntScalar()
{
YAML::Node node = YAML::Node(15);
YAML_ASSERT(node.Type() == YAML::NodeType::Scalar);
YAML_ASSERT(node.as<int>() == 15);
return true;
}
TEST SimpleSequence()
{
YAML::Node node;
node.append(10);
node.append("foo");
node.append("monkey");
YAML_ASSERT(node.Type() == YAML::NodeType::Sequence);
YAML_ASSERT(node.size() == 3);
YAML_ASSERT(node[0].as<int>() == 10);
YAML_ASSERT(node[1].as<std::string>() == "foo");
YAML_ASSERT(node[1].as<std::string>() == "monkey");
return true;
}
} }
void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) { void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) {
@@ -50,6 +72,8 @@ namespace Test
int total = 0; int total = 0;
RunNodeTest(&Node::SimpleScalar, "simple scalar", passed, total); RunNodeTest(&Node::SimpleScalar, "simple scalar", passed, total);
RunNodeTest(&Node::IntScalar, "int scalar", passed, total);
RunNodeTest(&Node::SimpleSequence, "simple sequence", passed, total);
std::cout << "Node tests: " << passed << "/" << total << " passed\n"; std::cout << "Node tests: " << passed << "/" << total << " passed\n";
return passed == total; return passed == total;