Menu

[r1677]: / framework / trunk / tests / tc_test.cpp  Maximize  Restore  History

Download this file

145 lines (132 with data), 3.0 kB

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "tc_test_content.h"
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/json.h>
#include <cppcms/url_mapper.h>
#include "dummy_api.h"
#include "test.h"
class test_app : public cppcms::application {
public:
test_app(cppcms::service &srv) :
cppcms::application(srv),
srv_(srv)
{
mapper().assign("foo","/foo");
mapper().assign("/");
mapper().assign("/{1}");
mapper().assign("/{1}/{2}");
}
~test_app()
{
release_context();
}
void set_context()
{
std::map<std::string,std::string> env;
env["HTTP_HOST"]="www.example.com";
env["SCRIPT_NAME"]="/foo";
env["PATH_INFO"]="/bar";
env["REQUEST_METHOD"]="GET";
booster::shared_ptr<dummy_api> api(new dummy_api(srv_,env,output_));
booster::shared_ptr<cppcms::http::context> cnt(new cppcms::http::context(api));
assign_context(cnt);
response().out();
output_.clear();
}
std::string str()
{
response().out() << std::flush;
std::string result = output_;
output_.clear();
return result;
}
void test_skins()
{
std::cout << "- Testing different skins" << std::endl;
data::master m;
render("tc_skin_a","master",m);
TEST(str()=="a");
render("tc_skin_b","master",m);
TEST(str()=="b");
render("test_default_master",m);
TEST(str()=="c");
}
void test_views()
{
std::cout << "- Testing different views" << std::endl;
data::master m;
render("view_x",m);
TEST(str()=="view x");
render("view_y",m);
TEST(str()=="view y");
}
void test_templates()
{
std::cout << "- Testing different template calls" << std::endl;
data::master m;
m.integer = 1;
m.text = "str";
m.integers.push_back(21);
m.integers.push_back(22);
render("master_tmpl",m);
TEST(str()=="\nA\nx=10\nx=1\nx=10 y=test\nx=1 y=str\nx=21,x=22\n");
}
void test_if()
{
std::cout << "- Testing conditions" << std::endl;
data::master m;
m.integer = 1;
m.text = "x";
render("master_if",m);
TEST(str()=="\ninteger\n!!integer\ntext not empty\n!text empty\ntrue\n\ntrue\n");
m.integer = 0;
m.text = "";
render("master_if",m);
TEST(str()=="\n!integer\n!integer\ntext empty\n!!text not empty\ntrue\n\ntrue\n");
}
void test_url()
{
std::cout << "- Testing url" << std::endl;
data::master m;
m.integer = 1;
m.text = "/";
render("master_url",m);
TEST(str()=="\n"
"/\n"
"/1\n"
"/1/%2f\n"
"/1\n"
"/1/%2f\n"
"/1//\n"
"/foo\n");
}
private:
std::string output_;
cppcms::service &srv_;
};
int main()
{
try {
cppcms::json::value cfg;
cfg["views"]["paths"][0]="./";
cfg["views"]["skins"][0]="tc_skin_a";
cfg["views"]["skins"][1]="tc_skin_b";
cfg["views"]["skins"][2]="tc_skin";
cfg["views"]["default_skin"]="tc_skin";
cppcms::service srv(cfg);
test_app app(srv);
app.set_context();
app.test_skins();
app.test_views();
app.test_templates();
app.test_if();
app.test_url();
}
catch(std::exception const &e)
{
std::cerr << "Fail " << e.what() << std::endl;
return 1;
}
std::cout << "Ok" << std::endl;
return 0;
}
MongoDB Logo MongoDB