-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_xml_test.cpp
57 lines (47 loc) · 1.32 KB
/
auto_xml_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <gtest/gtest.h>
#include "auto_curl.h"
#include "auto_xml.h"
using namespace std;
class auto_xml_test: public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
// ----------------------------------------------------------------------
struct MyGetTitleVisitor {
string title;
void operator()(string tagName, XmlDto xmlDto) {
if (tagName=="channel")
title = xmlDto.get("title");
}
};
// get <rss> <channel> <title> value
TEST_F(auto_xml_test, TestGetFeed) {
string title(XmlDto(curl_get("http://feeds.nytimes.com/nyt/rss/HomePage")).accept(MyGetTitleVisitor()).title);
printf("holy crap! title is %s\n", title.c_str());
}
// ----------------------------------------------------------------------
struct ItemVisitor {
void operator()(string tagName, XmlDto xmlDto) {
if (tagName=="item") {
string title;
if (xmlDto.safe_get("title", &title))
printf("holy crap! item is %s\n", title.c_str());
}
}
};
struct ChannelVisitor {
void operator()(string tagName, XmlDto xmlDto) {
if (tagName=="channel")
xmlDto.accept(ItemVisitor());
}
};
// get all <rss> <channel> <item> <title> values
TEST_F(auto_xml_test, TestGetItems) {
XmlDto(curl_get("http://feeds.nytimes.com/nyt/rss/HomePage")).accept(ChannelVisitor());
}