PreviousUpNext

15.4.824  src/lib/make-library-glue/planfile-unit-test.pkg

## planfile-unit-test.pkg

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

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

stipulate
    package paf =  patchfile;                                           # patchfile                     is from   src/lib/make-library-glue/patchfile.pkg
    package pfj =  planfile_junk;                                       # planfile_junk                 is from   src/lib/make-library-glue/planfile-junk.pkg
    package pfs =  patchfiles;                                          # patchfiles                    is from   src/lib/make-library-glue/patchfile.pkg
    package plf =  planfile;                                            # planfile                      is from   src/lib/make-library-glue/planfile.pkg
    package sm  =  string_map;                                          # string_map                    is from   src/lib/src/string-map.pkg
    #
    planfile_name       =  "src/lib/make-library-glue/planfile-unit-test.plan";

    filename1           =  "src/lib/make-library-glue/planfile-unit-test-file1.txt";
    filename2           =  "src/lib/make-library-glue/planfile-unit-test-file2.txt";

    patch_id_f1_one     =  { filename => filename1, patchname => "one"   };
    patch_id_f1_two     =  { filename => filename1, patchname => "two"   };

    patch_id_f2_alpha   =  { filename => filename2, patchname => "alpha" };
    patch_id_f2_beta    =  { filename => filename2, patchname => "beta"  };
    patch_id_f2_gamma   =  { filename => filename2, patchname => "gamma" };

herein

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

        name =  "src/lib/make-library-glue/planfile-unit-test.pkg";


        fun print_strings         [] =>   printf "[]\n";
            print_strings      [ s ] =>   printf "[ \"%s\" ]\n" s;
            print_strings (s ! rest)
                =>
                {   printf "[ \"%s\"" s;
                    apply (\\ s = printf ", \"%s\"" s) rest;
                    printf "]\n";
                };
        end;

        fun test_duplicate_paragraph_names ()
            =
            {   paragraph_definitions
                    =
                    [ { name   => "foo",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "foo1",  traits => [] },
                                    { fieldname => "foo2",  traits => [ plf::OPTIONAL, plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      },

                      { name   => "foo",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "bar1",  traits => [ plf::OPTIONAL ] },
                                    { fieldname => "bar2",  traits => [ plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      }
                    ];

                {   digested_defs =  plf::digest_paragraph_definitions  sm::empty  "planfile-unit-test.pkg"  paragraph_definitions;
                    #   
                    assert FALSE;                                       # Oops, duplicate paragraph names not detected.
                }
                except _ = assert TRUE;                                 # Indigestion is expected.
            };

        fun test_duplicate_field_names ()
            =
            {   paragraph_definitions
                    =
                    [ { name   => "foo",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "foo1",  traits => [] },
                                    { fieldname => "foo1",  traits => [ plf::OPTIONAL, plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      },

                      { name   => "bar",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "bar1",  traits => [ plf::OPTIONAL ] },
                                    { fieldname => "bar2",  traits => [ plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      }
                    ];

                {   digested_defs =  plf::digest_paragraph_definitions  sm::empty  "planfile-unit-test.pkg"  paragraph_definitions;
                    #   
                    assert FALSE;                                       # Oops, duplicate fieldnames not detected.
                }
                except _ = assert TRUE;                                 # Indigestion is expected.
            };

        fun test_bogus_field_names ()
            =
            {   paragraph_definitions
                    =
                    [ { name   => "foo",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "foo1",  traits => [] },
                                    { fieldname => "foo?",  traits => [ plf::OPTIONAL, plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      },

                      { name   => "bar",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "bar1",  traits => [ plf::OPTIONAL ] },
                                    { fieldname => "bar2",  traits => [ plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      }
                    ];

                {   digested_defs =  plf::digest_paragraph_definitions  sm::empty  "planfile-unit-test.pkg"  paragraph_definitions;
                    #   
                    assert FALSE;                                       # Oops, bogus fieldname not detected.
                }
                except _ = assert TRUE;                                 # Indigestion is expected.
            };

        fun test_basic_paragraph_digestion ()
            =
            {   paragraph_definitions
                    =
                    [ { name   => "bar",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "foo1",  traits => [ plf::OPTIONAL               ] },
                                    { fieldname => "bar2",  traits => [ plf::DO_NOT_TRIM_WHITESPACE ] },
                                    { fieldname => "zot3",  traits => [ plf::ALLOW_MULTIPLE_LINES   ] }
                                  ]
                      },

                      { name   => "foo",
                        do     => (\\ { patchfiles, paragraph, x } = patchfiles),
                        fields => [ { fieldname => "foo1",  traits => [] },
                                    { fieldname => "foo2",  traits => [ plf::OPTIONAL, plf::DO_NOT_TRIM_WHITESPACE, plf::ALLOW_MULTIPLE_LINES ] }
                                  ]
                      }

                    ];

                {   digested_defs =  plf::digest_paragraph_definitions  sm::empty  "planfile-unit-test.pkg"  paragraph_definitions;
                    #   
                    assert TRUE;                                                                                        # We digested without throwing an exception, at least.

                    assert  (length  paragraph_definitions   ==   length  (sm::keys_list  digested_defs));              # We expect as many digested paragraphs as raw paragraphs.

                    assert  (map (\\ p = p.name) paragraph_definitions   ==   sm::keys_list  digested_defs);            # We expect digested paragraphs to have same names as raw paragraphs.

                    bar =  the (sm::get (digested_defs, "bar"));
                    foo =  the (sm::get (digested_defs, "foo"));

                    assert (bar.name == "bar");
                    assert (foo.name == "foo");
        
                    assert (length (sm::keys_list  bar.fields) == 3);
                    assert (length (sm::keys_list  foo.fields) == 2);

                    fun assert_exists (map, key)
                        =
                        case (sm::get (map, key))
                            THE _ => assert TRUE;
                            NULL  => assert FALSE;
                        esac;

                    assert_exists (bar.fields, "foo1");
                    assert_exists (bar.fields, "bar2");
                    assert_exists (bar.fields, "zot3");

                    assert_exists (foo.fields, "foo1");
                    assert_exists (foo.fields, "foo2");

                    bar_foo1 =  the (sm::get (bar.fields, "foo1"));
                    bar_bar2 =  the (sm::get (bar.fields, "bar2"));
                    bar_zot3 =  the (sm::get (bar.fields, "zot3"));

                    foo_foo1 =  the (sm::get (foo.fields, "foo1"));
                    foo_foo2 =  the (sm::get (foo.fields, "foo2"));

                    assert (    bar_foo1.optional);         assert (    bar_foo1.trim_whitespace);      assert (not bar_foo1.allow_multiple_lines);
                    assert (not bar_bar2.optional);         assert (not bar_bar2.trim_whitespace);      assert (not bar_bar2.allow_multiple_lines);
                    assert (not bar_zot3.optional);         assert (    bar_zot3.trim_whitespace);      assert (    bar_zot3.allow_multiple_lines);

                    assert (not foo_foo1.optional);         assert (    foo_foo1.trim_whitespace);      assert (not foo_foo1.allow_multiple_lines);
                    assert (    foo_foo2.optional);         assert (not foo_foo2.trim_whitespace);      assert (    foo_foo2.allow_multiple_lines);
                }
                except _ = assert FALSE;                                # Indigestion.
            };

        fun test_basic_patchfile_io ()
            =
            {   
                {   patchfile =  paf::read_patchfile "no such file";
                    assert FALSE;
                }
                except _ = assert TRUE;

                {
                    ####################################################################################
                    # Test basic input:

                    patchfile1 =  paf::read_patchfile filename1;                assert TRUE;
                    patchfile2 =  paf::read_patchfile filename2;                assert TRUE;

                    assert (length  (paf::get_patch_names  patchfile1) == 2);
                    assert (length  (paf::get_patch_names  patchfile2) == 3);

                    assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
                    assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);



                    ####################################################################################
                    # Test empty_all_patches and append_to_patch:

                    patchfile1 =  paf::empty_all_patches  patchfile1;
                    patchfile2 =  paf::empty_all_patches  patchfile2;

                    patchfile1 =  paf::append_to_patch (patchfile1, "one", [ "one one one\n" ]);
                    patchfile1 =  paf::append_to_patch (patchfile1, "two", [ "two two two\n" ]);

                    patchfile2 =  paf::append_to_patch (patchfile2, "alpha", [ "alpha alpha alpha\n" ]);
                    patchfile2 =  paf::append_to_patch (patchfile2, "beta",  [ "beta beta beta\n" ]);
                    patchfile2 =  paf::append_to_patch (patchfile2, "gamma", [ "gamma gamma gamma\n" ]);

                    { paf::write_patchfile   patchfile1; (); }          except _ = assert FALSE;
                    { paf::write_patchfile   patchfile2; (); }          except _ = assert FALSE;



                    patchfile1 =  paf::read_patchfile filename1;                assert TRUE;
                    patchfile2 =  paf::read_patchfile filename2;                assert TRUE;

                    assert (length  (paf::get_patch_names  patchfile1) == 2);
                    assert (length  (paf::get_patch_names  patchfile2) == 3);

                    assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
                    assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);

                    {   paf::get_patch (patchfile1, "nonsuch");
                        assert FALSE;                                   # Fetching nonexistent field should throw an exception.
                    }   except _ = assert TRUE;

                    file1_one   = paf::get_patch (patchfile1, "one");
                    file1_two   = paf::get_patch (patchfile1, "two");

                    file2_alpha = paf::get_patch (patchfile2, "alpha");
                    file2_beta  = paf::get_patch (patchfile2, "beta" );
                    file2_gamma = paf::get_patch (patchfile2, "gamma");

                    assert (file1_one.lines    ==  [ "one one one\n"       ]);
                    assert (file1_two.lines    ==  [ "two two two\n"       ]);

                    assert (file2_alpha.lines  ==  [ "alpha alpha alpha\n" ]);
                    assert (file2_beta.lines   ==  [ "beta beta beta\n"    ]);
                    assert (file2_gamma.lines  ==  [ "gamma gamma gamma\n" ]);


                    patchfile1 =  paf::append_to_patch (patchfile1, "one", [ "ONE ONE ONE\n" ]);
                    patchfile1 =  paf::append_to_patch (patchfile1, "two", [ "TWO TWO TWO\n" ]);

                    patchfile2 =  paf::append_to_patch (patchfile2, "alpha", [ "ALPHA ALPHA ALPHA\n" ]);
                    patchfile2 =  paf::append_to_patch (patchfile2, "beta",  [ "BETA BETA BETA\n" ]);
                    patchfile2 =  paf::append_to_patch (patchfile2, "gamma", [ "GAMMA GAMMA GAMMA\n" ]);

                    { paf::write_patchfile   patchfile1;  (); }         except _ = assert FALSE;
                    { paf::write_patchfile   patchfile2;  (); }         except _ = assert FALSE;



                    patchfile1 =  paf::read_patchfile filename1;                assert TRUE;
                    patchfile2 =  paf::read_patchfile filename2;                assert TRUE;

                    assert (length  (paf::get_patch_names  patchfile1) == 2);
                    assert (length  (paf::get_patch_names  patchfile2) == 3);

                    assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
                    assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);

                    file1_one   = paf::get_patch (patchfile1, "one");
                    file1_two   = paf::get_patch (patchfile1, "two");

                    file2_alpha = paf::get_patch (patchfile2, "alpha");
                    file2_beta  = paf::get_patch (patchfile2, "beta" );
                    file2_gamma = paf::get_patch (patchfile2, "gamma");

                    assert (file1_one.lines    ==  [ "one one one\n",       "ONE ONE ONE\n"       ]);
                    assert (file1_two.lines    ==  [ "two two two\n",       "TWO TWO TWO\n"       ]);

                    assert (file2_alpha.lines  ==  [ "alpha alpha alpha\n", "ALPHA ALPHA ALPHA\n" ]);
                    assert (file2_beta.lines   ==  [ "beta beta beta\n",    "BETA BETA BETA\n"    ]);
                    assert (file2_gamma.lines  ==  [ "gamma gamma gamma\n", "GAMMA GAMMA GAMMA\n" ]);


                    ####################################################################################
                    # Test prepend_to_patch:

                    patchfile1 =  paf::prepend_to_patch (patchfile1, "one", [ "One One One\n" ]);
                    patchfile1 =  paf::prepend_to_patch (patchfile1, "two", [ "Two Two Two\n" ]);

                    patchfile2 =  paf::prepend_to_patch (patchfile2, "alpha", [ "Alpha Alpha Alpha\n" ]);
                    patchfile2 =  paf::prepend_to_patch (patchfile2, "beta",  [ "Beta Beta Beta\n"    ]);
                    patchfile2 =  paf::prepend_to_patch (patchfile2, "gamma", [ "Gamma Gamma Gamma\n" ]);

                    { paf::write_patchfile   patchfile1; (); }          except _ = assert FALSE;
                    { paf::write_patchfile   patchfile2; (); }          except _ = assert FALSE;



                    patchfile1 =  paf::read_patchfile filename1;                assert TRUE;
                    patchfile2 =  paf::read_patchfile filename2;                assert TRUE;

                    assert (length  (paf::get_patch_names  patchfile1) == 2);
                    assert (length  (paf::get_patch_names  patchfile2) == 3);

                    assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
                    assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);

                    file1_one   = paf::get_patch (patchfile1, "one");
                    file1_two   = paf::get_patch (patchfile1, "two");

                    file2_alpha = paf::get_patch (patchfile2, "alpha");
                    file2_beta  = paf::get_patch (patchfile2, "beta" );
                    file2_gamma = paf::get_patch (patchfile2, "gamma");

                    assert (file1_one.lines    ==  [ "One One One\n",       "one one one\n",       "ONE ONE ONE\n"       ]);
                    assert (file1_two.lines    ==  [ "Two Two Two\n",       "two two two\n",       "TWO TWO TWO\n"       ]);

                    assert (file2_alpha.lines  ==  [ "Alpha Alpha Alpha\n", "alpha alpha alpha\n", "ALPHA ALPHA ALPHA\n" ]);
                    assert (file2_beta.lines   ==  [ "Beta Beta Beta\n",    "beta beta beta\n",    "BETA BETA BETA\n"    ]);
                    assert (file2_gamma.lines  ==  [ "Gamma Gamma Gamma\n", "gamma gamma gamma\n", "GAMMA GAMMA GAMMA\n" ]);



                    ####################################################################################
                    # Test paf::map:

                    patchfile1 =  paf::map   (\\ { patch_id, lines } =  map string::to_upper lines)   patchfile1;
                    patchfile2 =  paf::map   (\\ { patch_id, lines } =  map string::to_upper lines)   patchfile2;

                    { paf::write_patchfile   patchfile1;  (); }         except _ = assert FALSE;
                    { paf::write_patchfile   patchfile2;  (); }         except _ = assert FALSE;



                    patchfile1 =  paf::read_patchfile filename1;                assert TRUE;
                    patchfile2 =  paf::read_patchfile filename2;                assert TRUE;

                    assert (length  (paf::get_patch_names  patchfile1) == 2);
                    assert (length  (paf::get_patch_names  patchfile2) == 3);

                    assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
                    assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);

                    file1_one   = paf::get_patch (patchfile1, "one");
                    file1_two   = paf::get_patch (patchfile1, "two");

                    file2_alpha = paf::get_patch (patchfile2, "alpha");
                    file2_beta  = paf::get_patch (patchfile2, "beta" );
                    file2_gamma = paf::get_patch (patchfile2, "gamma");

                    assert (file1_one.lines    ==  [ "ONE ONE ONE\n",       "ONE ONE ONE\n",       "ONE ONE ONE\n"       ]);
                    assert (file1_two.lines    ==  [ "TWO TWO TWO\n",       "TWO TWO TWO\n",       "TWO TWO TWO\n"       ]);

                    assert (file2_alpha.lines  ==  [ "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n" ]);
                    assert (file2_beta.lines   ==  [ "BETA BETA BETA\n",    "BETA BETA BETA\n",    "BETA BETA BETA\n"    ]);
                    assert (file2_gamma.lines  ==  [ "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n" ]);

#                   patchfile1 =  paf::empty_all_patches  patchfile1;
#                   patchfile2 =  paf::empty_all_patches  patchfile2;
#
#                   { paf::write_patchfile   patchfile1;  (); }         except _ = assert FALSE;
#                   { paf::write_patchfile   patchfile2;  (); }         except _ = assert FALSE;



#                   patchfile1 =  paf::read_patchfile filename1;                assert TRUE;
#                   patchfile2 =  paf::read_patchfile filename2;                assert TRUE;
#
#                   assert (length  (paf::get_patch_names  patchfile1) == 2);
#                   assert (length  (paf::get_patch_names  patchfile2) == 3);
#
#                   assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
#                   assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);
#
#                   file1_one   = paf::get_patch (patchfile1, "one");
#                   file1_two   = paf::get_patch (patchfile1, "two");
#
#                   file2_alpha = paf::get_patch (patchfile2, "alpha");
#                   file2_beta  = paf::get_patch (patchfile2, "beta" );
#                   file2_gamma = paf::get_patch (patchfile2, "gamma");
#
#                   assert (file1_one.lines    ==  [ ]);
#                   assert (file1_two.lines    ==  [ ]);
#
#                   assert (file2_alpha.lines  ==  [ ]);
#                   assert (file2_beta.lines   ==  [ ]);
#                   assert (file2_gamma.lines  ==  [ ]);
                }
                except _ = assert FALSE;
                
            };

        fun test_basic_patchfiles_io ()
            =
            {
                # We're depending here on being run right after  test_basic_patchfile_io()  (above).
                # That is probably not a good idea, we should set up each test so it can be run in
                # isolation.   XXX SUCKO FIXME    2013-02-10


                ####################################################################################
                # Test basic input:

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];
                #
                filenames  =  pfs::get_filenames  patchfiles;

                assert (filenames == [ filename1, filename2 ]);

                patchfile1 =  pfs::get_patchfile  patchfiles  filename1;
                patchfile2 =  pfs::get_patchfile  patchfiles  filename2;

                assert (paf::get_patch_names  patchfile1   ==   [ "one", "two" ]);
                assert (paf::get_patch_names  patchfile2   ==   [ "alpha", "beta", "gamma" ]);

                file1_one   = paf::get_patch (patchfile1, "one");
                file1_two   = paf::get_patch (patchfile1, "two");

                file2_alpha = paf::get_patch (patchfile2, "alpha");
                file2_beta  = paf::get_patch (patchfile2, "beta" );
                file2_gamma = paf::get_patch (patchfile2, "gamma");

                assert (file1_one.lines    ==  [ "ONE ONE ONE\n",       "ONE ONE ONE\n",       "ONE ONE ONE\n"       ]);
                assert (file1_two.lines    ==  [ "TWO TWO TWO\n",       "TWO TWO TWO\n",       "TWO TWO TWO\n"       ]);

                assert (file2_alpha.lines  ==  [ "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n" ]);
                assert (file2_beta.lines   ==  [ "BETA BETA BETA\n",    "BETA BETA BETA\n",    "BETA BETA BETA\n"    ]);
                assert (file2_gamma.lines  ==  [ "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n" ]);


                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "ONE ONE ONE\n",       "ONE ONE ONE\n",       "ONE ONE ONE\n"       ]);
                assert (file1_two.lines    ==  [ "TWO TWO TWO\n",       "TWO TWO TWO\n",       "TWO TWO TWO\n"       ]);

                assert (file2_alpha.lines  ==  [ "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n" ]);
                assert (file2_beta.lines   ==  [ "BETA BETA BETA\n",    "BETA BETA BETA\n",    "BETA BETA BETA\n"    ]);
                assert (file2_gamma.lines  ==  [ "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n" ]);



                ####################################################################################
                # Test append_to_patch:

                patchfiles =  pfs::append_to_patch  patchfiles  { patch_id => patch_id_f1_one,  lines => [ "One One One\n" ] };
                patchfiles =  pfs::append_to_patch  patchfiles  { patch_id => { filename => filename1, patchname => "two"   },  lines => [ "Two Two Two\n" ] };

                patchfiles =  pfs::append_to_patch  patchfiles  { patch_id => patch_id_f2_alpha,  lines => [ "Alpha Alpha Alpha\n" ] };
                patchfiles =  pfs::append_to_patch  patchfiles  { patch_id => patch_id_f2_beta,   lines => [ "Beta Beta Beta\n"    ] };
                patchfiles =  pfs::append_to_patch  patchfiles  { patch_id => patch_id_f2_gamma,  lines => [ "Gamma Gamma Gamma\n" ] };

                pfs::write_patchfiles  patchfiles;

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "ONE ONE ONE\n",       "ONE ONE ONE\n",       "ONE ONE ONE\n",         "One One One\n"       ]);
                assert (file1_two.lines    ==  [ "TWO TWO TWO\n",       "TWO TWO TWO\n",       "TWO TWO TWO\n",         "Two Two Two\n"       ]);

                assert (file2_alpha.lines  ==  [ "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n",   "Alpha Alpha Alpha\n" ]);
                assert (file2_beta.lines   ==  [ "BETA BETA BETA\n",    "BETA BETA BETA\n",    "BETA BETA BETA\n",      "Beta Beta Beta\n"    ]);
                assert (file2_gamma.lines  ==  [ "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n",   "Gamma Gamma Gamma\n" ]);



                ####################################################################################
                # Test prepend_to_patch:

                patchfiles =  pfs::prepend_to_patch  patchfiles  { patch_id => patch_id_f1_one,    lines => [ "one one one\n"       ] };
                patchfiles =  pfs::prepend_to_patch  patchfiles  { patch_id => patch_id_f1_two,    lines => [ "two two two\n"       ] };

                patchfiles =  pfs::prepend_to_patch  patchfiles  { patch_id => patch_id_f2_alpha,  lines => [ "alpha alpha alpha\n" ] };
                patchfiles =  pfs::prepend_to_patch  patchfiles  { patch_id => patch_id_f2_beta,   lines => [ "beta beta beta\n"    ] };
                patchfiles =  pfs::prepend_to_patch  patchfiles  { patch_id => patch_id_f2_gamma,  lines => [ "gamma gamma gamma\n" ] };

                pfs::write_patchfiles  patchfiles;

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "one one one\n",       "ONE ONE ONE\n",       "ONE ONE ONE\n",       "ONE ONE ONE\n",          "One One One\n"       ]);
                assert (file1_two.lines    ==  [ "two two two\n",       "TWO TWO TWO\n",       "TWO TWO TWO\n",       "TWO TWO TWO\n",          "Two Two Two\n"       ]);

                assert (file2_alpha.lines  ==  [ "alpha alpha alpha\n", "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n", "ALPHA ALPHA ALPHA\n",    "Alpha Alpha Alpha\n" ]);
                assert (file2_beta.lines   ==  [ "beta beta beta\n",    "BETA BETA BETA\n",    "BETA BETA BETA\n",    "BETA BETA BETA\n",       "Beta Beta Beta\n"    ]);
                assert (file2_gamma.lines  ==  [ "gamma gamma gamma\n", "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n", "GAMMA GAMMA GAMMA\n",    "Gamma Gamma Gamma\n" ]);



                ####################################################################################
                # Test pfs::map:

                patchfiles =  pfs::map   (\\ { patch_id, lines } =  map string::to_lower lines)   patchfiles;

                pfs::write_patchfiles  patchfiles;

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "one one one\n",       "one one one\n",       "one one one\n",       "one one one\n",          "one one one\n"       ]);
                assert (file1_two.lines    ==  [ "two two two\n",       "two two two\n",       "two two two\n",       "two two two\n",          "two two two\n"       ]);

                assert (file2_alpha.lines  ==  [ "alpha alpha alpha\n", "alpha alpha alpha\n", "alpha alpha alpha\n", "alpha alpha alpha\n",    "alpha alpha alpha\n" ]);
                assert (file2_beta.lines   ==  [ "beta beta beta\n",    "beta beta beta\n",    "beta beta beta\n",    "beta beta beta\n",       "beta beta beta\n"    ]);
                assert (file2_gamma.lines  ==  [ "gamma gamma gamma\n", "gamma gamma gamma\n", "gamma gamma gamma\n", "gamma gamma gamma\n",    "gamma gamma gamma\n" ]);



                ####################################################################################
                # Test pfs::empty_all_patches:

                patchfiles =  pfs::empty_all_patches  patchfiles;

                pfs::write_patchfiles  patchfiles;

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ ]);
                assert (file1_two.lines    ==  [ ]);

                assert (file2_alpha.lines  ==  [ ]);
                assert (file2_beta.lines   ==  [ ]);
                assert (file2_gamma.lines  ==  [ ]);



                ####################################################################################
                # Test pfs::apply_patches:

                patchfiles =  pfs::apply_patches  patchfiles  [ { patch_id => patch_id_f1_one,    lines => [ "one one one\n"       ] },
                                                                { patch_id => patch_id_f1_two,    lines => [ "two two two\n"       ] },
                                                                #
                                                                { patch_id => patch_id_f2_alpha,  lines => [ "alpha alpha alpha\n" ] },
                                                                { patch_id => patch_id_f2_beta,   lines => [ "beta beta beta\n"    ] },
                                                                { patch_id => patch_id_f2_gamma,  lines => [ "gamma gamma gamma\n" ] }
                                                              ];

                pfs::write_patchfiles  patchfiles;

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "one one one\n" ]);
                assert (file1_two.lines    ==  [ "two two two\n" ]);

                assert (file2_alpha.lines  ==  [ "alpha alpha alpha\n" ]);
                assert (file2_beta.lines   ==  [ "beta beta beta\n"    ]);
                assert (file2_gamma.lines  ==  [ "gamma gamma gamma\n" ]);



                ####################################################################################
                # Test pfs::apply_patch:

                patchfiles =  pfs::apply_patch    patchfiles  { patch_id => patch_id_f1_one,  lines => [ "One One One\n" ] };

                pfs::write_patchfiles  patchfiles;

                patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "One One One\n" ]);
                assert (file1_two.lines    ==  [ "two two two\n" ]);

                assert (file2_alpha.lines  ==  [ "alpha alpha alpha\n" ]);
                assert (file2_beta.lines   ==  [ "beta beta beta\n"    ]);
                assert (file2_gamma.lines  ==  [ "gamma gamma gamma\n" ]);
            };

        fun test_basic_planfile_operation  ()
            =
            {   patchfiles =  pfs::load_patchfiles  [ filename1, filename2 ];
                #

                paragraph_defs
                    =
                    plf::digest_paragraph_definitions   sm::empty   "planfile-unit-test.pkg"
                        #
                        [ pfj::set_patch__definition,
                          pfj::append_patch__definition,
                          pfj::copy_patch__definition
                        ];

                plan =  plf::read_planfile  paragraph_defs  planfile_name;

                plan_paragraph_names
                    =
                    map (\\ { do, paragraph } = { field = the (sm::get (paragraph.fields, "do"));  head field.lines; })
                        plan;

                assert (plan_paragraph_names == [ "set_patch", "set_patch", "append_patch", "append_patch", "copy_patch", "copy_patch" ]);

                assert ((the (sm::get ((list::nth (plan, 0)).paragraph.fields, "text"))).lines == [ "Line 1 in patch one.\n", "Line 2 in patch one.\n" ]);
                assert ((the (sm::get ((list::nth (plan, 1)).paragraph.fields, "text"))).lines == [ "Line 1 in patch two.\n", "Line 2 in patch two.\n" ]);
                assert ((the (sm::get ((list::nth (plan, 2)).paragraph.fields, "text"))).lines == [ "Line 3 in patch one.\n", "Line 4 in patch one.\n" ]);
                assert ((the (sm::get ((list::nth (plan, 3)).paragraph.fields, "text"))).lines == [ "Line 3 in patch two.\n", "Line 4 in patch two.\n" ]);

                patchfiles  =  pfs::empty_all_patches  patchfiles;

                patchfiles  =  plf::map_patchfiles_per_plan  0  patchfiles  plan;

                file1_one   =  pfs::get_patch  patchfiles  patch_id_f1_one;
                file1_two   =  pfs::get_patch  patchfiles  patch_id_f1_two;

                file2_alpha =  pfs::get_patch  patchfiles  patch_id_f2_alpha;
                file2_beta  =  pfs::get_patch  patchfiles  patch_id_f2_beta;
                file2_gamma =  pfs::get_patch  patchfiles  patch_id_f2_gamma;

                assert (file1_one.lines    ==  [ "Line 1 in patch one.\n", "Line 2 in patch one.\n", "Line 3 in patch one.\n", "Line 4 in patch one.\n" ]);
                assert (file1_two.lines    ==  [ "Line 1 in patch two.\n", "Line 2 in patch two.\n", "Line 3 in patch two.\n", "Line 4 in patch two.\n" ]);

                assert (file2_alpha.lines  ==  [ "Line 1 in patch one.\n", "Line 2 in patch one.\n", "Line 3 in patch one.\n", "Line 4 in patch one.\n" ]);
                assert (file2_beta.lines   ==  [ "Line 1 in patch two.\n", "Line 2 in patch two.\n", "Line 3 in patch two.\n", "Line 4 in patch two.\n" ]);
                assert (file2_gamma.lines  ==  [  ]);
            };


        fun run ()
            =
            {   printf "\nDoing %s:\n" name;   
                #
                test_duplicate_paragraph_names ();
                test_duplicate_field_names     ();
                test_bogus_field_names         ();
                test_basic_paragraph_digestion ();
                test_basic_patchfile_io        ();
                test_basic_patchfiles_io       ();
                test_basic_planfile_operation  ();

# Next up: checking out actual patchfile modification under planfile control.

                assert TRUE;

                summarize_unit_tests  name;
            };
    };
end;


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext