;; A simple test script that loads the sample classes and runs them through some exercises. ;; VERY similar to what my testing program does...so make sure this test script works perfectly for you! ;; Just place the test script in the same directory as both your code file and your sample "class definition files". ;; You should then be able to open and run this test script file: it would load your code file, then call various ;; functions you've defined to load the class files and exercise the classes/objects you've defined. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; First let's define a little "test" function that wraps a test in an error-catching env. This is vital so ;; that, when something bad happens, it doesn't abort the script and halt --- it just prints the error and ;; goes on to the next thing on the file. (define test (lambda (expr) (with-handlers ((exn:fail? print-errors)) (eval expr)))) (define print-errors (lambda (ectn) (begin (display "something bad happened:") (newline) (display (exn-message ectn)) (newline) #f))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Ok, now let's get on with the test script! ;; EDIT HERE!! ;; Loads your OOP project code. Insert the name of your code file here. (define sfile "Xoop.scm") (load sfile) (display "Loaded the OOP code file: ") (display sfile) (newline) ;; Ok, now let's use the functions that are required in the OOP project to load the sample class definition files ;; I gave and test them out. (load-classes "CLASS1.TXT") ;; Now create some objects and test em out! (define x (new point 3 4)) (define y (new point 2 5)) ;; should have defined two new points. Now test em! (x 'display) (x 'getx) (x 'polar) (x 'dist y) (x 'setx 67) (x 'display) ;; ok now I'm testing one that I know should give a "method not found" error. So I wrap it in my error-catching ;; "test" call so that this error won't kill my testing. (test '(x 'getz)) ;; Ok, let's give the drunkard a try now (define foo (new drunkard 'Joe 34)) (foo 'say '(what are you blathering about?)) (foo 'say 'Eck '(are you daft, man?)) (define fum (new drunkard 'Michael 20)) (fum 'say '(I try to be a good boy)) (fum 'complain) ;; Now let's load up the second sample class file I gave to show off inheritance and test that. ;; Note that in doing so, we REDEFINE the point class, blowing away the previous definition. Should ;; not be a problem! (newline) (newline) (display "Loading up the next test class file now!") (newline)(newline) (load-classes "inherit.txt") ;; And run some tests (define p1 (new point 2 3)) (define p2 (new 3dpoint 4 8 10)) (display "Defined a couple of points. Now testing them...") (newline) (p1 'getx) (newline) (p2 'getx) (newline) ;; I expect the following to error out -- simple points have no z-coord. So I wrap this call in "test" (test '(p1 'getz)) (newline) (p2' getz) (newline) (p1 'show) (newline) (p2 'show) (newline) ;; All done! (newline) (display "All done testing") (newline)