This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
forked from Southclaws/pawn-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.pwn
125 lines (103 loc) · 3.85 KB
/
test.pwn
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
#define RUN_TESTS
#include <a_samp>
#include <YSI\y_utils>
#include <YSI\y_testing>
#include "templates.inc"
main() {
//
}
Test:Simple() {
new Template:t = Template_Create("Hello, {{ name }}! Today is {{ date }}");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered,
PAIR_STR("name", "Southclaws"),
PAIR_STR("date", "Monday")
);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "Hello, Southclaws! Today is Monday") == 0);
}
Test:Types() {
new Template:t = Template_Create("String: {{ string }} Int: {{ int }} Float: {{ float }}");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered,
PAIR_STR("string", "hello"),
PAIR_INT("int", 42),
PAIR_FLOAT("float", 5.5)
);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "String: hello Int: 42 Float: 5.5") == 0);
}
Test:Conditionals() {
new Template:t = Template_Create("Hello {% if name %}{{ name }}{% else %}Anonymous{% endif %}.");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered,
PAIR_STR("name", "Southclaws")
);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "Hello Southclaws.") == 0);
// no variables passed here
ret = Template_Render(t, rendered, sizeof rendered);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "Hello Anonymous.") == 0);
}
Test:Filters() {
new Template:t = Template_Create("{{ name | upcase }}");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered,
PAIR_STR("name", "Southclaws")
);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "SOUTHCLAWS") == 0);
}
Test:Assignment() {
new Template:t = Template_Create("\
{% assign fruits = \"apples, oranges, peaches\" %}\
{% if fruits %}\
{{ fruits }}\
{% endif %}");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered,
PAIR_STR("name", "Southclaws")
);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "apples, oranges, peaches") == 0);
}
Test:GlobalVariables() {
Template_SetGlobalString("player", "name", "Southclaws");
Template_SetGlobalInt("player", "id", 3720);
Template_SetGlobalFloat("player", "pos_x", 5.5);
new Template:t = Template_Create("Name: {{ player.name }}, ID: {{ player.id }}, Pos X: {{ player.pos_x }}");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "Name: Southclaws, ID: 3720, Pos X: 5.5") == 0);
}
Test:TemplateVariabes() {
new Template:t = Template_Create("Location: {{ location }}, Geo Id: {{ geoid }}, Lat: {{ lat }}");
Template_SetString(t, "location", "England");
Template_SetInt(t, "geoid", 37);
Template_SetFloat(t, "lat", 9893.2);
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "Location: England, Geo Id: 37, Lat: 9893.2001953125") == 0);
}
Test:LoadFromFile() {
Template_SetGlobalString("system", "name", "Machine");
Template_SetGlobalInt("system", "id", 7780);
Template_SetGlobalFloat("system", "coord_x", 9.5);
new Template:t = Template_LoadFromFile("scriptfiles/file.txt");
new rendered[64];
new ret = Template_Render(t, rendered, sizeof rendered);
printf("ret: %d rendered: '%s'", ret, rendered);
ASSERT(ret == 0);
ASSERT(strcmp(rendered, "Name: Machine, ID: 7780, Pos X: 9.5") == 0);
}