File: test_term_html.pl

package info (click to toggle)
swi-prolog 8.0.2%2Bdfsg-3%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (147 lines) | stat: -rw-r--r-- 5,299 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
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2014, VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(test_term_html,
          [ test_term_html/0,
            term_html_string/3                  % @Term, -String, +Options
          ]).
:- include(test_local).

:- use_module(library(plunit)).
:- use_module(library(debug)).
:- use_module(library(option)).
:- use_module(library(memfile)).
:- use_module(library(sgml)).
:- use_module(library(xpath)).
:- use_module(library(http/html_write)).

:- use_module(library(http/term_html)).

test_term_html :-
    run_tests([ term_html
              ]).


term_html_string(Term, HTMLString, Options) :-
    phrase(term(Term, Options), Tokens),
    with_output_to(string(HTMLString), print_html(Tokens)).

string_dom(String, DOM) :-
    setup_call_cleanup(
        new_memory_file(MF),
        ( setup_call_cleanup(
              open_memory_file(MF, write, Out),
              write(Out, String),
              close(Out)),
          setup_call_cleanup(
              open_memory_file(MF, read, In),
              load_html(stream(In), DOM, []),
              close(In))
        ),
        free_memory_file(MF)).

string_plain(HTMLString, Plain) :-
    string_dom(HTMLString, [DOM]),
    xpath(DOM, /(*(text)), PlainAtom),
    atom_string(PlainAtom, Plain).

trip(TermIn, StringOut, TermOut, Options) :-
    (   option(numbervars(true), Options)
    ->  copy_term(TermIn, Term1),
        numbervars(Term1, 1, _, [singletons(true)])
    ;   Term1 = TermIn
    ),
    term_html_string(Term1, HTMLString, Options),
    string_plain(HTMLString, StringOut),
    debug(term_html(plain), 'Plain: ~p', [StringOut]),
    (   option(quoted(true), Options),
        \+ option(max_depth(_), Options)
    ->  term_string(TermOut, StringOut)
    ;   true
    ).

test_term_html(String) :-
    test_term_html(String, String,
                   [ quoted(true),
                     numbervars(true)
                   ]).

test_term_html(In, Out, Options) :-
    term_string(TermIn, In),
    trip(TermIn, StringOut, TermOut, Options),
    assertion(Out == StringOut),
    (   option(quoted(true), Options),
        \+ option(max_depth(_), Options)
    ->  assertion(TermIn =@= TermOut)
    ;   true
    ).

:- begin_tests(term_html).

test(op) :- test_term_html("_ is 1/9").
test(op) :- test_term_html("_ is (*)/9").
test(op) :- test_term_html("_ is a mod b").
test(op) :- test_term_html("_ is ... mod b").

test(compound) :- test_term_html("a(b)").
test(compound) :- test_term_html("a(1/c)").
test(compound) :- test_term_html("a((a,b))").
test(compound) :- test_term_html("a((h:-b))").

test(dict) :- test_term_html("_{}").
test(dict) :- test_term_html("_{a:1, b:2}").
%test(dict) :- test_term_html("_{* :1}").       % FIXME: quoted keys do not work
test(dict) :- test_term_html("_{a: *}").

test(list) :- test_term_html("[]").
test(list) :- test_term_html("[a]").
test(list) :- test_term_html("[a|b]").

test(curl) :- test_term_html("{}").
test(curl) :- test_term_html("{a}").
test(curl) :- test_term_html("{a,b}").
test(curl) :- test_term_html("{a->b;c}").

test(ellipsis) :- test_term_html("a(b(c))",    "a(...)",       [max_depth(1)]).
test(ellipsis) :- test_term_html("[a,b,c]",    "[...|...]",    [max_depth(1)]).
test(ellipsis) :- test_term_html("[a,b,c]",    "[a|...]",      [max_depth(2)]).
test(ellipsis) :- test_term_html("[a,b,c]",    "[a, b|...]",   [max_depth(3)]).
test(ellipsis) :- test_term_html("[a,b,c]",    "[a, b, c]",    [max_depth(4)]).
test(ellipsis) :- test_term_html("[a(b),c]",   "[a(...)|...]", [max_depth(2)]).
test(ellipsis) :- test_term_html("a{b:1,c:2}", "a{...}",       [max_depth(1)]).
test(ellipsis) :- test_term_html("a{b:1,c:2}", "a{b:..., c:...}",
    [max_depth(2)]).
test(ellipsis) :- test_term_html("+a",         "+ ...",        [max_depth(1)]).

:- end_tests(term_html).