File: TemplateTest.cpp

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (118 lines) | stat: -rw-r--r-- 3,036 bytes parent folder | download
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
#include <TestSupport.h>
#include <Utils/Template.h>
#include <cstdarg>

using namespace Passenger;

namespace tut {
	struct TemplateTest {
		string apply(const char *templateContent, ...) {
			va_list ap;
			const char *arg;
			StringMap<StaticString> params;

			va_start(ap, templateContent);
			while ((arg = va_arg(ap, const char *)) != NULL) {
				params.set(arg, va_arg(ap, const char *));
			}
			string result = Template::apply(templateContent, params);
			va_end(ap);
			return result;
		}
	};
	
	DEFINE_TEST_GROUP(TemplateTest);
	
	TEST_METHOD(1) {
		// Test 1 substitution.
		string result = apply("hello {{name}}",
			"name", "world",
			NULL);
		ensure_equals(result, "hello world");
	}

	TEST_METHOD(2) {
		// Test multiple substitutions.
		string result = apply("hello {{name}} and {{name2}}",
			"name", "joe",
			"name2", "jane",
			NULL);
		ensure_equals(result, "hello joe and jane");
	}

	TEST_METHOD(3) {
		// Test unspecified substitutions.
		string result = apply("hello {{name}} and {{name2}}!",
			"name", "joe",
			NULL);
		ensure_equals(result, "hello joe and !");
	}

	TEST_METHOD(4) {
		// Test default values.
		string result = apply("hello {{name|default=joe}} and {{name2|default=jane}}",
			NULL);
		ensure_equals(result, "hello joe and jane");
	}

	TEST_METHOD(5) {
		// Substitutions are HTML-escaped by default.
		string result = apply("hello {{name}}",
			"name", "<joe>",
			NULL);
		ensure_equals(result, "hello &lt;joe&gt;");
		result = apply("hello {{name|default=<joe>}}",
			NULL);
		ensure_equals(result, "hello &lt;joe&gt;");
	}

	TEST_METHOD(6) {
		// HTML escaping can be disabled with the 'raw' option.
		string result = apply("hello {{name|raw}}",
			"name", "<joe>",
			NULL);
		ensure_equals(result, "hello <joe>");
	}

	TEST_METHOD(7) {
		// Test combining default values and the 'raw' option.
		string result = apply("hello {{name|raw,default=<joe>}}",
			NULL);
		ensure_equals(result, "hello <joe>");
	}

	TEST_METHOD(8) {
		// Test 'if' statements.
		string result = apply("hello. {{if morning}}good morning. {{/if}}{{if evening}}good evening. {{/if}}",
			"morning", "true",
			NULL);
		ensure_equals(result, "hello. good morning. ");
	}

	TEST_METHOD(9) {
		// An 'if' condition is considered true when it is neither the empty string nor "false".
		string result = apply("hello. {{if morning}}good morning. {{/if}}"
			"{{if afternoon}}good afternoon. {{/if}"
			"{{if evening}}good evening. {{/if}}",
			"morning", "of course",
			"afternoon", "false",
			NULL);
		ensure_equals(result, "hello. good morning. ");
	}

	TEST_METHOD(10) {
		// Test nesting substitutions within 'if's.
		string result = apply("hello. {{if name}}good morning {{name}}.{{/if}}",
			"name", "joe",
			NULL);
		ensure_equals(result, "hello. good morning joe.");
	}

	TEST_METHOD(11) {
		// Test auto-breaking on certain characters.
		string result = apply("{{content}}",
			"content", "Hello, world: a=b;c=d",
			NULL);
		ensure_equals(result, "Hello,<wbr> world:<wbr> a=<wbr>b;<wbr>c=<wbr>d");
	}
}