PreviousUpNext

15.4.1003  src/lib/src/scripting-unit-test.pkg

# scripting-unit-test.pkg 

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

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

# Unit tests for:
#     Basic scripting functionality.

stipulate
    package fil =  file__premicrothread;                                        # file__premicrothread          is from   src/lib/std/src/posix/file--premicrothread.pkg
    package psx =  posixlib;                                                    # posixlib                      is from   src/lib/std/src/psx/posixlib.pkg
herein
    package scripting_unit_test {
        #
        include package   unit_test;                                            # unit_test                     is from   src/lib/src/unit-test.pkg

        bin_sh        =  spawn__premicrothread::bin_sh;
        backticks__op =  spawn__premicrothread::bin_sh;

        name = "src/lib/src/scripting-unit-test.pkg tests";

        script_name = "test~";

        # XXX BUGGO FIXME We in fact wind up invoking
        #                 the installed image /usr/bin/mythryld
        #                 -- it would be much better if we invoked
        #                 the local bin/mythryld so that we could
        #                 test before installing -- and so that we
        #                 don't get confused thinking we're testing
        #                 the latest compile when we're not.
        #
        fun create_script script_text
            =
            {   cwd = winix__premicrothread::file::current_directory ();

                fd = fil::open_for_write script_name;

                fil::write (fd, "#!" + cwd + "/bin/mythryl\n");
                fil::write (fd, script_text + "\n");

                fil::close_output fd;

                psx::chmod (script_name, psx::s::irwxu);
            };

        infix my --> ;

        fun a --> b
            =
            (a, b);

        # Creating and running a script is relatively slow,
        # so where we have a choice, we put tests in
        #     src/lib/src/eval-unit-test.pkg
        # instead:
        #
        internal_script_tests
            =
            [   "printf \"%d\" (2+2);"                        --> "4()",        # I wanted "4"      here but the output changed, so I just accepted it by changing the "proper" answer. :-( XXX SUCKO FIXME 2012-04-02 CrT 
                "printf \"%s\" (\"abc\" + \"def\");"          --> "abcdef()"    # I wanted "abcdef" here but the output changed, so I just accepted it by changing the "proper" answer. :-( XXX SUCKO FIXME 2012-04-02 CrT 
            ];

        external_script_tests
            =
            [   "try/run-subprocess"                        --> "Read from subprocess: 'xyzzy'"
            ];

        fun run_an_internal_script_test (question, answer)
            =
            {   create_script question;
# result = `./test~`;  printf "src/lib/src/scripting-unit-test.pkg: question s='%s'  proper answer s='%s'  actual answer s='%s' script_name s=%s'\n"  question  answer  result  script_name;
                assert (`./test~` == answer);
                winix__premicrothread::file::remove_file script_name;
            };

        fun run_an_external_script_test (question, answer)
            =
            {
# print ("src/lib/src/scripting-unit-test.pkg: run_an_external_script_test: '" + question + "' -> '" + (bin_sh question) + "'\n");
                assert (string::chomp (bin_sh question) == answer);
            };

        fun run ()
            =
            {
                printf "\nDoing %s:\n" name;   

                apply  run_an_internal_script_test  internal_script_tests;
                apply  run_an_external_script_test  external_script_tests;

                summarize_unit_tests  name;
            };
    };
end;

## Code by Jeff Prothero: Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext