PreviousUpNext

15.4.846  src/lib/prettyprint/big/src/test/prettyprinter-lib-unit-test.pkg

## prettyprinter-lib-unit-test.pkg

# Compiled by:
#     src/lib/test/unit-tests.lib

# Run by:
#     src/lib/test/all-unit-tests.pkg

stipulate
    package pp  =  standard_prettyprinter;                              # standard_prettyprinter        is from   src/lib/prettyprint/big/src/standard-prettyprinter.pkg
    Pp = pp::Pp;
herein

    package prettyprinter_lib_unit_test {
        #
        include package   unit_test;                                            # unit_test                     is from   src/lib/src/unit-test.pkg
        include package   makelib::scripting_globals;

        name =  "src/lib/prettyprint/big/src/test/prettyprinter-lib-unit-test.pkg";

        fun prettyprint_to_string pp_args prettyprint_fn
            = 
            pp::prettyprint_to_string pp_args prettyprint_fn;


        fun assert_streq a b
            =
            {   assert (a == b);
                #
                if (a != b) 
                    printf "\nassert_streq: match FAILED:\n%s' !=\n%s'\n" a b;
                fi;
            };

        fun test_basic_newline_handling  ()
            =
            {
                # This is basically whitebox testing.  Getting
                #     group_tokens_into_lines()
                # &   flatten_lines_back_to_tokens()
                # to handle newlines correctly took some fiddling.
                # Check that it hasn't gotten broken somehow:

                r = pp::prettyprint_to_string [] {.
                    pp = #pp;
                };
                assert_streq r  "";

                s = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                };
                assert_streq s  "abc def";

                t = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                    pp.newline ();
                };
                assert_streq t  "abc def\n";


                u = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                    pp.newline ();
                    pp.lit "ghi"; 
                    pp.lit " "; 
                    pp.lit "jkl"; 
                };
                assert_streq u  "abc def\nghi jkl";

                v = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                    pp.newline ();
                    pp.lit "ghi"; 
                    pp.lit " "; 
                    pp.lit "jkl"; 
                    pp.newline ();
                };
                assert_streq v  "abc def\nghi jkl\n";

                w = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.newline ();
                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                    pp.newline ();
                    pp.lit "ghi"; 
                    pp.lit " "; 
                    pp.lit "jkl"; 
                    pp.newline ();
                };
                assert_streq w  "\nabc def\nghi jkl\n";

                x = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.newline ();
                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                    pp.newline ();
                    pp.lit "ghi"; 
                    pp.lit " "; 
                    pp.lit "jkl"; 
                };
                assert_streq x  "\nabc def\nghi jkl";

                y = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.newline ();
                    pp.lit "abc"; 
                    pp.lit " "; 
                    pp.lit "def"; 
                };
                assert_streq y  "\nabc def";

                z = pp::prettyprint_to_string [] {.
                    pp = #pp;

                    pp.newline ();
                };
                assert_streq z  "\n";
            };

        fun test_basic_box_handling  ()
            =
            {
                r = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.box' 0 -1 {.
                            pp.lit "a123456789";
                            pp.txt' 0 -1 " ";
                            pp.lit "b123456789";
                            pp.txt' 0 -1 " ";
                            pp.lit "c123456789";
                        };
                    };
                assert_streq r  "a123456789 b123456789 c123456789";

                s = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.box' 0 -1 {.
                            pp.lit "a123456789";
                            pp.txt' 0 -1 " ";
                            pp.lit "b123456789";
                            pp.txt' 0 -1 " ";
                            pp.lit "c123456789";
                        };
                    };
                assert_streq s  "a123456789\nb123456789\nc123456789";


                t = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.box' 0 -1 {.
                            pp.lit "a123456789";
                            pp.ind 4;
                            pp.txt " ";
                            pp.lit "b123456789";
                            pp.ind 0;
                            pp.txt " ";
                            pp.lit "c123456789";
                        };
                    };
                assert_streq t  "a123456789 b123456789 c123456789";

                u = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.box' 0 -1 {.
                            pp.lit "a123456789";
                            pp.ind 4;
                            pp.txt " ";
                            pp.lit "b123456789";
                            pp.ind 0;
                            pp.txt " ";
                            pp.lit "c123456789";
                        };
                    };
                assert_streq u  "a123456789\n    b123456789\nc123456789";

                v = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.box {.
                            pp.lit "a123456789";
                            pp.ind 4;
                            pp.txt " ";
                            pp.lit "b123456789";
                            pp.ind 0;
                            pp.txt " ";
                            pp.lit "c123456789";
                        };
                    };
                assert_streq v  "a123456789 b123456789 c123456789";

                w = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.box {.
                            pp.lit "a123456789";
                            pp.ind 4;
                            pp.txt " ";
                            pp.lit "b123456789";
                            pp.ind 0;
                            pp.txt " ";
                            pp.lit "c123456789";
                        };
                    };
                assert_streq w  "    a123456789\n\
                                \        b123456789\n\
                                \    c123456789";

            };

        fun test_basic_wrap_handling  ()
            =
            {
                r = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.txt " ";
                            pp.lit "b1234";             pp.txt " ";
                            pp.lit "c1234";             pp.txt " ";
                            pp.lit "d1234";             pp.txt " ";
                            pp.lit "e1234";             pp.txt " ";
                            pp.lit "f1234";             pp.txt " ";
                            pp.lit "g1234";             pp.txt " ";
                            pp.lit "h1234";             pp.txt " ";
                            pp.lit "i1234";             pp.txt " ";
                            pp.lit "j1234";             pp.txt " ";
                            pp.lit "k1234";             pp.txt " ";
                            pp.lit "l1234";             pp.txt " ";
                            pp.lit "m1234";             pp.txt " ";
                            pp.lit "n1234";             pp.txt " ";
                            pp.lit "o1234";             pp.txt " ";
                            pp.lit "p1234";             pp.txt " ";
                            pp.lit "q1234";             pp.txt " ";
                            pp.lit "r1234";             pp.txt " ";
                            pp.lit "s1234";             pp.txt " ";
                            pp.lit "t1234";             pp.txt " ";
                            pp.lit "u1234";             pp.txt " ";
                            pp.lit "v1234";             pp.txt " ";
                            pp.lit "w1234";             pp.txt " ";
                            pp.lit "x1234";             pp.txt " ";
                            pp.lit "y1234";             pp.txt " ";
                            pp.lit "z1234";             pp.txt ".";
                        };
                    };
                assert_streq r  "a1234 b1234 c1234 d1234 e1234 f1234\n\
                                \g1234 h1234 i1234 j1234 k1234 l1234\n\
                                \m1234 n1234 o1234 p1234 q1234 r1234\n\
                                \s1234 t1234 u1234 v1234 w1234 x1234\n\
                                \y1234 z1234."
                      ; 


                s = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.txt " ";
                            pp.lit "b1234";             pp.txt " ";
                            pp.lit "c1234";             pp.txt " ";
                            pp.lit "d1234";             pp.txt " ";
                            pp.lit "e1234";             pp.txt " ";
                            pp.lit "f1234";             pp.txt " ";
                            pp.lit "g1234";             pp.txt " ";
                            pp.lit "h1234";             pp.txt " ";
                            pp.lit "i1234";             pp.txt " ";
                            pp.lit "j1234";             pp.txt " ";
                            pp.lit "k1234";             pp.txt " ";
                            pp.lit "l1234";             pp.txt " ";
                            pp.lit "m1234";             pp.txt " ";
                            pp.lit "n1234";             pp.txt " ";
                            pp.lit "o1234";             pp.txt " ";
                            pp.lit "p1234";             pp.txt " ";
                            pp.lit "q1234";             pp.txt " ";
                            pp.lit "r1234";             pp.txt " ";
                            pp.lit "s1234";             pp.txt " ";
                            pp.lit "t1234";             pp.txt " ";
                            pp.lit "u1234";             pp.txt " ";
                            pp.lit "v1234";             pp.txt " ";
                            pp.lit "w1234";             pp.txt " ";
                            pp.lit "x1234";             pp.txt " ";
                            pp.lit "y1234";             pp.txt " ";
                            pp.lit "z1234";             pp.txt ".";
                        };
                    };
                assert_streq s  "a1234 b1234 c1234 d1234 e1234\n\
                                \f1234 g1234 h1234 i1234 j1234\n\
                                \k1234 l1234 m1234 n1234 o1234\n\
                                \p1234 q1234 r1234 s1234 t1234\n\
                                \u1234 v1234 w1234 x1234 y1234\n\
                                \z1234."
                      ; 


                t = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.txt' 1 0 " ";
                            pp.lit "b1234";             pp.txt' 1 0 " ";
                            pp.lit "c1234";             pp.txt' 1 0 " ";
                            pp.lit "d1234";             pp.txt' 1 0 " ";
                            pp.lit "e1234";             pp.txt' 1 0 " ";
                            pp.lit "f1234";             pp.txt' 1 0 " ";
                            pp.lit "g1234";             pp.txt' 1 0 " ";
                            pp.lit "h1234";             pp.txt' 1 0 " ";
                            pp.lit "i1234";             pp.txt' 1 0 " ";
                            pp.lit "j1234";             pp.txt' 1 0 " ";
                            pp.lit "k1234";             pp.txt' 1 0 " ";
                            pp.lit "l1234";             pp.txt' 1 0 " ";
                            pp.lit "m1234";             pp.txt' 1 0 " ";
                            pp.lit "n1234";             pp.txt' 1 0 " ";
                            pp.lit "o1234";             pp.txt' 1 0 " ";
                            pp.lit "p1234";             pp.txt' 1 0 " ";
                            pp.lit "q1234";             pp.txt' 1 0 " ";
                            pp.lit "r1234";             pp.txt' 1 0 " ";
                            pp.lit "s1234";             pp.txt' 1 0 " ";
                            pp.lit "t1234";             pp.txt' 1 0 " ";
                            pp.lit "u1234";             pp.txt' 1 0 " ";
                            pp.lit "v1234";             pp.txt' 1 0 " ";
                            pp.lit "w1234";             pp.txt' 1 0 " ";
                            pp.lit "x1234";             pp.txt' 1 0 " ";
                            pp.lit "y1234";             pp.txt' 1 0 " ";
                            pp.lit "z1234";             pp.txt' 1 0 ".";
                        };
                    };
                assert_streq t  "a1234 b1234 c1234 d1234 e1234 f1234\n\
                                \    g1234 h1234 i1234 j1234 k1234 l1234\n\
                                \    m1234 n1234 o1234 p1234 q1234 r1234\n\
                                \    s1234 t1234 u1234 v1234 w1234 x1234\n\
                                \    y1234 z1234."
                      ; 

                u = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.txt' 1 0 " ";
                            pp.lit "b1234";             pp.txt' 1 0 " ";
                            pp.lit "c1234";             pp.txt' 1 0 " ";
                            pp.lit "d1234";             pp.txt' 1 0 " ";
                            pp.lit "e1234";             pp.txt' 1 0 " ";
                            pp.lit "f1234";             pp.txt' 1 0 " ";
                            pp.lit "g1234";             pp.txt' 1 0 " ";
                            pp.lit "h1234";             pp.txt' 1 0 " ";
                            pp.lit "i1234";             pp.txt' 1 0 " ";
                            pp.lit "j1234";             pp.txt' 1 0 " ";
                            pp.lit "k1234";             pp.txt' 1 0 " ";
                            pp.lit "l1234";             pp.txt' 1 0 " ";
                            pp.lit "m1234";             pp.txt' 1 0 " ";
                            pp.lit "n1234";             pp.txt' 1 0 " ";
                            pp.lit "o1234";             pp.txt' 1 0 " ";
                            pp.lit "p1234";             pp.txt' 1 0 " ";
                            pp.lit "q1234";             pp.txt' 1 0 " ";
                            pp.lit "r1234";             pp.txt' 1 0 " ";
                            pp.lit "s1234";             pp.txt' 1 0 " ";
                            pp.lit "t1234";             pp.txt' 1 0 " ";
                            pp.lit "u1234";             pp.txt' 1 0 " ";
                            pp.lit "v1234";             pp.txt' 1 0 " ";
                            pp.lit "w1234";             pp.txt' 1 0 " ";
                            pp.lit "x1234";             pp.txt' 1 0 " ";
                            pp.lit "y1234";             pp.txt' 1 0 " ";
                            pp.lit "z1234";             pp.txt' 1 0 ".";
                        };
                    };
                assert_streq u  "a1234 b1234 c1234 d1234 e1234\n\
                                \    f1234 g1234 h1234 i1234\n\
                                \    j1234 k1234 l1234 m1234\n\
                                \    n1234 o1234 p1234 q1234\n\
                                \    r1234 s1234 t1234 u1234\n\
                                \    v1234 w1234 x1234 y1234\n\
                                \    z1234."
                ;

                v = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.txt' 1 0 " ";
                            pp.lit "b1234";             pp.txt' 1 0 " ";
                            pp.lit "c1234";             pp.txt' 1 0 " ";
                            pp.lit "d1234";             pp.txt' 1 0 " ";
                            pp.lit "e1234";             pp.txt' 1 0 " ";
                            pp.lit "f1234";             pp.txt' 1 0 " ";        pp.ind 4;  
                            pp.lit "g1234";             pp.txt' 1 0 " ";
                            pp.lit "h1234";             pp.txt' 1 0 " ";
                            pp.lit "i1234";             pp.txt' 1 0 " ";
                            pp.lit "j1234";             pp.txt' 1 0 " ";
                            pp.lit "k1234";             pp.txt' 1 0 " ";
                            pp.lit "l1234";             pp.txt' 1 0 " ";
                            pp.lit "m1234";             pp.txt' 1 0 " ";
                            pp.lit "n1234";             pp.txt' 1 0 " ";
                            pp.lit "o1234";             pp.txt' 1 0 " ";
                            pp.lit "p1234";             pp.txt' 1 0 " ";
                            pp.lit "q1234";             pp.txt' 1 0 " ";
                            pp.lit "r1234";             pp.txt' 1 0 " ";
                            pp.lit "s1234";             pp.txt' 1 0 " ";
                            pp.lit "t1234";             pp.txt' 1 0 " ";
                            pp.lit "u1234";             pp.txt' 1 0 " ";
                            pp.lit "v1234";             pp.txt' 1 0 " ";
                            pp.lit "w1234";             pp.txt' 1 0 " ";
                            pp.lit "x1234";             pp.txt' 1 0 " ";
                            pp.lit "y1234";             pp.txt' 1 0 " ";
                            pp.lit "z1234";             pp.txt' 1 0 ".";
                        };
                    };
                assert_streq v  "a1234 b1234 c1234 d1234 e1234 f1234\n\
                                \    g1234 h1234 i1234 j1234 k1234 l1234\n\
                                \        m1234 n1234 o1234 p1234 q1234 r1234\n\
                                \        s1234 t1234 u1234 v1234 w1234 x1234\n\
                                \        y1234 z1234."
                        ;
                w = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  40 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.txt " ";
                            pp.lit "b1234";             pp.txt " ";
                            pp.lit "c1234";             pp.txt " ";
                            pp.lit "d1234";             pp.txt " ";
                            pp.lit "e1234";             pp.txt " ";
                            pp.lit "f1234";             pp.txt " ";     pp.ind 4;  
                            pp.lit "g1234";             pp.txt " ";
                            pp.lit "h1234";             pp.txt " ";
                            pp.lit "i1234";             pp.txt " ";
                            pp.lit "j1234";             pp.txt " ";
                            pp.lit "k1234";             pp.txt " ";
                            pp.lit "l1234";             pp.txt " ";
                            pp.lit "m1234";             pp.txt " ";
                            pp.lit "n1234";             pp.txt " ";
                            pp.lit "o1234";             pp.txt " ";
                            pp.lit "p1234";             pp.txt " ";
                            pp.lit "q1234";  pp.ind 0;  pp.txt " "; # <==
                            pp.lit "r1234";             pp.txt " ";
                            pp.lit "s1234";             pp.txt " ";
                            pp.lit "t1234";             pp.txt " ";
                            pp.lit "u1234";             pp.txt " ";
                            pp.lit "v1234";             pp.txt " ";
                            pp.lit "w1234";             pp.txt " ";
                            pp.lit "x1234";             pp.txt " ";
                            pp.lit "y1234";             pp.txt " ";
                            pp.lit "z1234";             pp.txt ".";
                        };
                    };
                assert_streq w  "a1234 b1234 c1234 d1234 e1234 f1234\n\
                                \    g1234 h1234 i1234 j1234 k1234 l1234\n\
                                \    m1234 n1234 o1234 p1234 q1234 r1234\n\
                                \s1234 t1234 u1234 v1234 w1234 x1234\n\
                                \y1234 z1234."
                        ;
            };

        fun test_basic_cwrap_handling  ()
            =
            {
                r = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.lit " ";
                            pp.lit "b1234";             pp.lit " ";
                            pp.lit "c1234";             pp.lit " ";
                            pp.lit "d1234";             pp.lit " ";
                            pp.cwrap' 0 -1 {.
                                pp.lit "e1234";         pp.txt " ";
                                pp.lit "f1234";         pp.txt " ";
                                pp.lit "g1234";         pp.txt " ";
                                pp.lit "h1234";         pp.txt " ";
                                pp.lit "i1234";         pp.txt " ";
                                pp.lit "j1234";         pp.txt " ";
                                pp.lit "k1234";         pp.txt " ";
                                pp.lit "l1234";         pp.txt " ";
                                pp.lit "m1234";         pp.txt " ";
                                pp.lit "n1234";         pp.txt " ";
                                pp.lit "o1234";         pp.txt " ";
                                pp.lit "p1234";         pp.txt " ";
                                pp.lit "q1234";         pp.txt " ";
                                pp.lit "r1234";         pp.txt " ";
                                pp.lit "s1234";         pp.txt " ";
                                pp.lit "t1234";         pp.txt " ";
                                pp.lit "u1234";         pp.txt " ";
                                pp.lit "v1234";         pp.txt " ";
                                pp.lit "w1234";         pp.txt " ";
                                pp.lit "x1234";         pp.txt " ";
                                pp.lit "y1234";         pp.txt " ";
                                pp.lit "z1234";         pp.txt ".";
                            };
                        };
                    };
                assert_streq r  "a1234 b1234 c1234 d1234 e1234 f1234 g1234 h1234 i1234\n\
                                \                        j1234 k1234 l1234 m1234 n1234\n\
                                \                        o1234 p1234 q1234 r1234 s1234\n\
                                \                        t1234 u1234 v1234 w1234 x1234\n\
                                \                        y1234 z1234."
                ;
            };

        fun test_basic_cbox_handling  ()
            =
            {
                r = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.lit " ";
                            pp.lit "b1234";             pp.lit " ";
                            pp.lit "c1234";             pp.lit " ";
                            pp.lit "d1234";             pp.lit " ";
                            pp.cbox' 0 -1 {.
                                pp.lit "e1234";         pp.txt " ";
                                pp.lit "f1234";         pp.txt " ";
                                pp.lit "g1234";         pp.txt " ";
                                pp.lit "h1234";         pp.txt " ";
                                pp.lit "i1234";         pp.txt " ";
                                pp.lit "j1234";         pp.txt " ";
                                pp.lit "k1234";         pp.txt " ";
                                pp.lit "l1234";         pp.txt " ";
                                pp.lit "m1234";         pp.txt " ";
                                pp.lit "n1234";         pp.txt " ";
                                pp.lit "o1234";         pp.txt " ";
                                pp.lit "p1234";         pp.txt " ";
                                pp.lit "q1234";         pp.txt " ";
                                pp.lit "r1234";         pp.txt " ";
                                pp.lit "s1234";         pp.txt " ";
                                pp.lit "t1234";         pp.txt " ";
                                pp.lit "u1234";         pp.txt " ";
                                pp.lit "v1234";         pp.txt " ";
                                pp.lit "w1234";         pp.txt " ";
                                pp.lit "x1234";         pp.txt " ";
                                pp.lit "y1234";         pp.txt " ";
                                pp.lit "z1234";         pp.txt ".";
                            };
                        };
                    };
                assert_streq r  "a1234 b1234 c1234 d1234 e1234\n\
                                \                        f1234\n\
                                \                        g1234\n\
                                \                        h1234\n\
                                \                        i1234\n\
                                \                        j1234\n\
                                \                        k1234\n\
                                \                        l1234\n\
                                \                        m1234\n\
                                \                        n1234\n\
                                \                        o1234\n\
                                \                        p1234\n\
                                \                        q1234\n\
                                \                        r1234\n\
                                \                        s1234\n\
                                \                        t1234\n\
                                \                        u1234\n\
                                \                        v1234\n\
                                \                        w1234\n\
                                \                        x1234\n\
                                \                        y1234\n\
                                \                        z1234."
                ;

                s = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.lit " ";
                            pp.lit "b1234";             pp.lit " ";
                            pp.lit "c1234";             pp.lit " ";
                            pp.lit "d1234";             pp.lit " ";
                            pp.cbox' 0 -1 {.
                                pp.lit "e1234";         pp.txt " ";
                                pp.lit "f1234";         pp.txt " ";
                                pp.lit "g1234";         pp.txt " ";
                                pp.lit "h1234";         pp.txt " ";
                                pp.lit "i1234";         pp.txt " ";
                                pp.lit "j1234";         pp.txt " ";
                            };
                        };
                    };
                assert_streq s  "a1234 b1234 c1234 d1234 e1234\n\
                                \                        f1234\n\
                                \                        g1234\n\
                                \                        h1234\n\
                                \                        i1234\n\
                                \                        j1234"
                ;
    
                t = pp::prettyprint_to_string [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  30 ] {.
                        pp = #pp;
                        pp.wrap' 0 -1 {.
                            pp.lit "a1234";             pp.lit " ";
                            pp.lit "b1234";             pp.lit " ";
                            pp.lit "c1234";             pp.lit " ";
                            pp.lit "d1234";             pp.lit " ";
                            pp.cbox' 0 -1 {.
                                pp.lit "e1234";         pp.txt " ";
                                pp.lit "f1234";         pp.txt " ";
                                pp.lit "g1234";         pp.txt " ";
                                pp.lit "h1234";         pp.txt " ";
                                pp.lit "i1234";         pp.txt ".";
                            };
                        };
                    };
                assert_streq t  "a1234 b1234 c1234 d1234 e1234 f1234 g1234 h1234 i1234.";
            };

        fun test_simple_expression_prettyprinter  ()
            =
            {
                Expression
                  = VARIABLE    String
                  | INT         Int
                  | BINOP       Binop
                  | LIST        List( Expression )

                also
                Statement
                  = ASSIGNMENT  { lhs:      Expression,
                                  rhs:      Expression
                                }
                  | BLOCK       List( Statement )

                withtype
                Binop
                  =
                  { op:      String,
                    left:    Expression,
                    right:   Expression
                  };

                statement_1 =   BLOCK [
                                    ASSIGNMENT { lhs => VARIABLE "alpha",
                                                 rhs => LIST  [ VARIABLE "beta",
                                                                LIST [ INT 123, INT 456, INT 789 ],
                                                                BINOP { op    => "+",
                                                                        left  =>  VARIABLE "gamma",
                                                                        right =>  INT 131
                                                                      }
                                                              ]
                                               },

                                    ASSIGNMENT { lhs => VARIABLE "omega",
                                                 rhs => LIST  [ VARIABLE "lambda",
                                                                LIST [ INT 987, INT 654, INT 321 ],
                                                                BINOP { op    => "+",
                                                                        left  =>  VARIABLE "chi",
                                                                        right =>  INT 187
                                                                      }
                                                              ]
                                               }
                                ];

                fun pp_expression (pp:Pp, x: Expression)
                    =
                    case x
                        #
                        VARIABLE s                      =>  pp.lit s;
                        INT      i                      =>  pp.lit (sprintf "%d" i);
                        #
                        BINOP { op, left, right }       =>  {   pp.box' 0 0 {.                                                                          pp.rulename "b2";
                                                                    pp.lit "(";
                                                                    pp_expression (pp, left);
                                                                    pp.txt' 0 -1 (sprintf " %s " op);
                                                                    pp_expression (pp, right);
                                                                    pp.endlit ")";
                                                                };
                                                            };
                        LIST xs                         =>  {   pp.box' 0 2 {.                                                                          pp.rulename "b3";
                                                                    pp.lit "[ ";
                                                                    pp.ind 2;
                                                                    pp::seqx {. pp.endlit ","; pp.txt " "; }  {. pp_expression (pp, #x); }  xs;
                                                                    pp.ind -2;
                                                                    pp.txt " ";
                                                                    pp.lit "]";
                                                                };
                                                            };
                    esac;

                fun pp_statement (pp:Pp, s: Statement)
                    =
                    case s
                        #
                        ASSIGNMENT { lhs, rhs }         =>  {   pp.box' 0 0 {.                                                                          pp.rulename "b4";
                                                                    pp_expression (pp, lhs);
                                                                    pp.ind 4; 
                                                                    pp.txt " = ";
                                                                    pp_expression (pp, rhs);
                                                                    pp.endlit ";";
                                                                };
                                                            };
                        BLOCK xs                        =>  {   pp.box' 0 0 {.                                                                          pp.rulename "b5";
                                                                    pp.lit "{ ";
                                                                    pp.ind 4; 
                                                                    pp::seqx  {. pp.txt' 0 -1 "   "; }  {. pp_statement (pp, #x); }   xs;
                                                                    pp.ind 0;
                                                                    pp.txt " "; 
                                                                    pp.lit "}";
                                                                };
                                                            };
                    esac;


                assert_streq "test"      (prettyprint_to_string [] (\\ pp = { pp::lit pp "test"; }));

                fun ps s i
                    =
                    printf "\ns%03d:\nvvvvvvvv\n%s\n^^^^^^^\n" i s;

                s120 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  120 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s120 120;
                s100 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH  100 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s100 100;
                s060 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH   60 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s060  60;
                s050 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH   50 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s050  50;
                s030 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH   30 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s030  30;
                s015 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH   15 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s015  15;
                s007 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH    7 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s007   7;
                s003 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH    3 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s003   3;
                s001 =  prettyprint_to_string  [ pp::typ::DEFAULT_TARGET_BOX_WIDTH    1 ]    (\\ pp =  pp_statement  (pp, statement_1));        # ps s001   1;


                assert_streq  s120  "{ alpha = [ beta, [ 123, 456, 789 ], (gamma + 131) ];   omega = [ lambda, [ 987, 654, 321 ], (chi + 187) ]; }";

                assert_streq  s100  "{   alpha = [ beta, [ 123, 456, 789 ], (gamma + 131) ];\n\
                                    \    omega = [ lambda, [ 987, 654, 321 ], (chi + 187) ];\n\
                                    \}";

                assert_streq  s060  "{   alpha = [ beta, [ 123, 456, 789 ], (gamma + 131) ];\n\
                                    \    omega = [ lambda, [ 987, 654, 321 ], (chi + 187) ];\n\
                                    \}";


                assert_streq  s050  "{   alpha\n\
                                    \        =\n\
                                    \        [ beta, [ 123, 456, 789 ], (gamma + 131) ];\n\
                                    \    omega\n\
                                    \        =\n\
                                    \        [ lambda, [ 987, 654, 321 ], (chi + 187) ];\n\
                                    \}";

                assert_streq  s030  "{   alpha\n\
                                    \        = [ beta,\n\
                                    \            [ 123, 456, 789 ],\n\
                                    \            (gamma + 131)\n\
                                    \          ];\n\
                                    \    omega\n\
                                    \        = [ lambda,\n\
                                    \            [ 987, 654, 321 ],\n\
                                    \            (chi + 187)\n\
                                    \          ];\n\
                                    \}";

                assert_streq  s015  "{   alpha\n\
                                    \        = [ beta,\n\
                                    \              [ 123,\n\
                                    \                456,\n\
                                    \                789\n\
                                    \              ],\n\
                                    \            (gamma + 131)\n\
                                    \          ];\n\
                                    \    omega\n\
                                    \        = [ lambda,\n\
                                    \              [ 987,\n\
                                    \                654,\n\
                                    \                321\n\
                                    \              ],\n\
                                    \            (chi + 187)\n\
                                    \          ];\n\
                                    \}";


                assert_streq  s007  "{   alpha\n\
                                    \        = [ beta,\n\
                                    \              [ 123,\n\
                                    \                456,\n\
                                    \                789\n\
                                    \              ],\n\
                                    \            (gamma\n\
                                    \            +\n\
                                    \            131)\n\
                                    \          ];\n\
                                    \    omega\n\
                                    \        = [ lambda,\n\
                                    \              [ 987,\n\
                                    \                654,\n\
                                    \                321\n\
                                    \              ],\n\
                                    \            (chi\n\
                                    \            +\n\
                                    \            187)\n\
                                    \          ];\n\
                                    \}";


                assert_streq  s007  "{   alpha\n\
                                    \        = [ beta,\n\
                                    \              [ 123,\n\
                                    \                456,\n\
                                    \                789\n\
                                    \              ],\n\
                                    \            (gamma\n\
                                    \            +\n\
                                    \            131)\n\
                                    \          ];\n\
                                    \    omega\n\
                                    \        = [ lambda,\n\
                                    \              [ 987,\n\
                                    \                654,\n\
                                    \                321\n\
                                    \              ],\n\
                                    \            (chi\n\
                                    \            +\n\
                                    \            187)\n\
                                    \          ];\n\
                                    \}";


                assert_streq  s003  "{   alpha\n\
                                    \        = [ beta,\n\
                                    \              [ 123,\n\
                                    \                456,\n\
                                    \                789\n\
                                    \              ],\n\
                                    \            (gamma\n\
                                    \            +\n\
                                    \            131)\n\
                                    \          ];\n\
                                    \    omega\n\
                                    \        = [ lambda,\n\
                                    \              [ 987,\n\
                                    \                654,\n\
                                    \                321\n\
                                    \              ],\n\
                                    \            (chi\n\
                                    \            +\n\
                                    \            187)\n\
                                    \          ];\n\
                                    \}";

                assert_streq  s001  "{   alpha\n\
                                    \        = [ beta,\n\
                                    \              [ 123,\n\
                                    \                456,\n\
                                    \                789\n\
                                    \              ],\n\
                                    \            (gamma\n\
                                    \            +\n\
                                    \            131)\n\
                                    \          ];\n\
                                    \    omega\n\
                                    \        = [ lambda,\n\
                                    \              [ 987,\n\
                                    \                654,\n\
                                    \                321\n\
                                    \              ],\n\
                                    \            (chi\n\
                                    \            +\n\
                                    \            187)\n\
                                    \          ];\n\
                                    \}";

                assert TRUE;
            };


        fun run ()
            =
            {   printf "\nDoing %s:\n" name;   
                #
                test_basic_newline_handling  ();
                test_basic_box_handling  ();
                test_basic_wrap_handling  ();
                test_basic_cwrap_handling  ();
                test_basic_cbox_handling  ();
                test_simple_expression_prettyprinter  ();

                summarize_unit_tests  name;
            };
    };
end;


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext