File: test_xsd.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (197 lines) | stat: -rw-r--r-- 7,047 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
:- module(test_xsd, [test_xsd/0]).

user:file_search_path(foreign, '.').
user:file_search_path(library, '.').
user:file_search_path(library, '../plunit').

:- use_module(library(sgml)).
:- use_module(library(apply)).
:- use_module(library(debug)).
:- use_module(library(plunit)).

test_xsd :-
    run_tests([ xsd_number_string,
                xsd_time_string
              ]).

expand_xsd(XSD:Local, Out) :-
    XSD == xsd,
    !,
    atom_concat('http://www.w3.org/2001/XMLSchema#', Local, Out).
expand_xsd(In, Out) :-
    compound(In),
    !,
    compound_name_arguments(In, Name, Args0),
    maplist(expand_xsd, Args0, Args),
    compound_name_arguments(Out, Name, Args).
expand_xsd(T, T).

term_expansion(In, Out) :-
    expand_xsd(In, Out).

:- begin_tests(xsd_number_string).

test(string_0, N == 0.0) :-
    xsd_number_string(N, "0.0E0").
test(string_0, N == -0.0) :-
    xsd_number_string(N, "-0.0E0").
test(string_nan, N == NaN) :-
    xsd_number_string(N, "NaN"),
    NaN is nan.
test(string_inf, N =:= inf) :-
    xsd_number_string(N, "INF").
test(string_neginf, N =:= -inf) :-
    xsd_number_string(N, "-INF").
test(nan_string, S == "NaN") :-
    NaN is nan,
    xsd_number_string(NaN, S).
test(nan_inf, S == "INF") :-
    Inf is inf,
    xsd_number_string(Inf, S).
test(nan_inf, S == "-INF") :-
    Inf is -inf,
    xsd_number_string(Inf, S).
test(float, F == 110.0) :-
    xsd_number_string(F, "1.1e2").

:- end_tests(xsd_number_string).

:- begin_tests(xsd_time_string).

% Term --> String

test(time_string, S == "2016-01-30") :-
    xsd_time_string(date(2016,01,30), Y, S),
    assertion(Y == xsd:date).
test(time_string, S == "-0400-01-30") :-
    xsd_time_string(date(-0400,01,30), Y, S),
    assertion(Y == xsd:date).
test(time_string, S == "2016-01-30T20:29:15") :-
    xsd_time_string(date_time(2016,01,30,20,29,15), Y, S),
    assertion(Y == xsd:dateTime).
test(time_string, S == "2016-01-30T20:29:15.25") :-
    xsd_time_string(date_time(2016,01,30,20,29,15.25), Y, S),
    assertion(Y == xsd:dateTime).
test(time_string, S == "2016-01-30T20:29:15Z") :-
    xsd_time_string(date_time(2016,01,30,20,29,15,0), Y, S),
    assertion(Y == xsd:dateTime).
test(time_string, S == "2016-01-30T20:29:05.456Z") :-
    xsd_time_string(date_time(2016,01,30,20,29,5.456,0), Y, S),
    assertion(Y == xsd:dateTime).
test(time_string, S == "2016-01-30T20:29:15+01:00") :-
    xsd_time_string(date_time(2016,01,30,20,29,15,3600), Y, S),
    assertion(Y == xsd:dateTime).
test(time_string, S == "2016-01-30T20:29:15.56+01:00") :-
    xsd_time_string(date_time(2016,01,30,20,29,15.56,3600), Y, S),
    assertion(Y == xsd:dateTime).
test(time_string, S == "20:29:15") :-
    xsd_time_string(time(20,29,15), Y, S),
    assertion(Y == xsd:time).
test(time_string, S == "20:29:15.75") :-
    xsd_time_string(time(20,29,15.75), Y, S),
    assertion(Y == xsd:time).
test(time_string, S == "2016-01") :-
    xsd_time_string(year_month(2016,01), Y, S),
    assertion(Y == xsd:gYearMonth).
test(time_string, S == "01-30") :-
    xsd_time_string(month_day(01,30), Y, S),
    assertion(Y == xsd:gMonthDay).
test(time_string, S == "2016") :-
    xsd_time_string(2016, xsd:gYear, S).
test(time_string, S == "-0400") :-
    xsd_time_string(-0400, xsd:gYear, S).
test(time_string, S == "01") :-
    xsd_time_string(01, xsd:gMonth, S).
test(time_string, S == "30") :-
    xsd_time_string(30, xsd:gDay, S).

% Term <- String

test(string_time, T = date(2016,01,30)) :-
    xsd_time_string(T, Y, "2016-01-30"),
    assertion(Y == xsd:date).
test(string_time, T = date(-2016,01,30)) :-
    xsd_time_string(T, Y, "-2016-01-30"),
    assertion(Y == xsd:date).
test(string_time, T = date_time(2016,01,30,20,29,15)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:15"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,20,29,15.25)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:15.25"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,24,0,0)) :-
    xsd_time_string(T, Y, "2016-01-30T24:00:00"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,20,29,15,0)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:15Z"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,20,29,15.10,0)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:15.10Z"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,20,29,15,3600)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:15+01:00"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,20,29,5.6,3600)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:05.6+01:00"),
    assertion(Y == xsd:dateTime).
test(string_time, T = date_time(2016,01,30,20,29,5.6,50400)) :-
    xsd_time_string(T, Y, "2016-01-30T20:29:05.6+14:00"),
    assertion(Y == xsd:dateTime).
test(string_time, T = time(20,29,15)) :-
    xsd_time_string(T, Y, "20:29:15"),
    assertion(Y == xsd:time).
test(string_time, T = time(20,29,15.75)) :-
    xsd_time_string(T, Y, "20:29:15.75"),
    assertion(Y == xsd:time).
test(string_time, T = year_month(2016,01)) :-
    xsd_time_string(T, Y, "2016-01"),
    assertion(Y == xsd:gYearMonth).
test(string_time, T = month_day(01,30)) :-
    xsd_time_string(T, Y, "01-30"),
    assertion(Y == xsd:gMonthDay).
test(string_time, T = 2016) :-
    xsd_time_string(T, Y, "2016"),
    assertion(Y == xsd:gYear).
test(string_time, T = -2016) :-
    xsd_time_string(T, Y, "-2016"),
    assertion(Y == xsd:gYear).
test(string_time, T = 01) :-
    xsd_time_string(T, xsd:gMonth, "01").
test(string_time, T = 30) :-
    xsd_time_string(T, xsd:gDay, "30").

% Parsing errors

test(string_error, error(syntax_error(xsd_time))) :-
    xsd_time_string(_, _, "").
test(string_error, error(domain_error(day,35))) :-
    xsd_time_string(_, _, "2016-01-35").
test(string_error, error(domain_error(second,65))) :-
    xsd_time_string(_, _, "20:29:65").
test(string_time, error(domain_error(day,35))) :-
    xsd_time_string(_, xsd:gDay, "35").
test(string_time, error(domain_error(month,13))) :-
    xsd_time_string(_, xsd:gMonth, "13").
test(string_time, error(instantiation_error)) :-
    xsd_time_string(_, _, "13").

% Generate errors

test(term_error, error(domain_error(day,35))) :-
    xsd_time_string(date(2016,01,35), _, _).
test(term_error, error(domain_error(day,35))) :-
    xsd_time_string(date_time(2016,01,35,20,29,15), _, _).
test(term_error, error(domain_error(hour,25))) :-
    xsd_time_string(date_time(2016,01,30,25,29,15), _, _).
test(term_error, error(domain_error(hour,25))) :-
    xsd_time_string(date_time(2016,01,30,25,29,15,0), _, _).
test(term_error, error(domain_error(tz_offset,100 000))) :-
    xsd_time_string(date_time(2016,01,30,20,29,15,100 000), _, _).
test(term_error, error(domain_error(month,20))) :-
    xsd_time_string(20, xsd:gMonth, _).
test(term_error, error(domain_error(day,35))) :-
    xsd_time_string(35, xsd:gDay, _).
test(term_error, error(domain_error(day,-01))) :-
    xsd_time_string(-01, xsd:gDay, _).

:- end_tests(xsd_time_string).