parsertests.cpp 1.98 KB
Newer Older
1
2
3
4
5
6
7
8
#include "parsertests.h"
#include "handlermacros.h"
#include "yaml-cpp/yaml.h"
#include <iostream>

namespace Test
{
    namespace Parser {
9
		TEST NoEndOfMapFlow()
10
11
12
13
        {
            try {
                HANDLE("---{header: {id: 1");
            } catch(const YAML::ParserException& e) {
14
                YAML_ASSERT(e.msg == std::string(YAML::ErrorMsg::END_OF_MAP_FLOW));
15
16
17
18
                return true;
            }
            return "  no exception caught";
        }
19
20
21
22
23
24
25
26
27
28
29
30
        
        TEST PlainScalarStartingWithQuestionMark()
        {
            HANDLE("foo: ?bar");
            EXPECT_DOC_START();
            EXPECT_MAP_START("?", 0);
            EXPECT_SCALAR("?", 0, "foo");
            EXPECT_SCALAR("?", 0, "?bar");
            EXPECT_MAP_END();
            EXPECT_DOC_END();
            DONE();
        }
31
32
33
34
35
36
37
38
39
40
41
42

        TEST NullStringScalar()
        {
            HANDLE("foo: null");
            EXPECT_DOC_START();
            EXPECT_MAP_START("?", 0);
            EXPECT_SCALAR("?", 0, "foo");
            EXPECT_NULL(0);
            EXPECT_MAP_END();
            EXPECT_DOC_END();
            DONE();
        }
43
44
45
46
47
48
49
50
51
52
53
54
55
    }
    
	namespace {
		void RunParserTest(TEST (*test)(), const std::string& name, int& passed, int& total) {
			TEST ret;
			try {
				ret = test();
			} catch(const YAML::Exception& e) {
				ret.ok = false;
				ret.error = std::string("  Exception caught: ") + e.what();
			}
			
			if(!ret.ok) {
56
				std::cout << "Parser test failed: " << name << "\n";
57
58
59
60
61
62
63
64
65
66
67
68
69
				std::cout << ret.error << "\n";
			}
			
			if(ret.ok)
				passed++;
			total++;
		}
	}
	
	bool RunParserTests()
	{
		int passed = 0;
		int total = 0;
70
		RunParserTest(&Parser::NoEndOfMapFlow, "No end of map flow", passed, total);
71
		RunParserTest(&Parser::PlainScalarStartingWithQuestionMark, "Plain scalar starting with question mark", passed, total);
72
		RunParserTest(&Parser::NullStringScalar, "Null string scalar", passed, total);
73
74
75
76
77
		
		std::cout << "Parser tests: " << passed << "/" << total << " passed\n";
		return passed == total;
	}
}