Script Library: 1247 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Archive version of: test-simple.r ... version: 2 ... brianwisti 8-Mar-2005

Amendment note: Default behavior is now to print test results to stdout || Publicly available? Yes

REBOL [
    Title: "Simple Test Suite"
    file: %test-simple.r
    date: 8-mar-2005
    author: "Brian Wisti"
    home: none
    email: "brianwisti@yahoo.com"
    Version: 0.1.1
    purpose: {
      Add support for simple test mechanisms to REBOL, similar to Perl's 
      Test::Simple. 
      
      The basic idea? Make testing simple so everybody can make tests.
    }
    Library: [
        level: 'beginner
        platform: 'all
        type: [module tool]
        domain: [debug testing]
        tested-under: [Linux]
        support: [
            { This release should be considered 'alpha' quality.
              I need suggestions for ways to improve it while maintaining 
              the simplicity of the interface.
              
              See the 'test-test function for a sample test run.
              }
        ]
        license: 'mit
        see-also: none
    ]
    changes: [
        [ 7-mar-2005 0.1.0 "Initial Posting on rebol.org" ]
        [ 8-mar-2005 0.1.1 "Default behavior is now to print test results to stdout" ]
    ]
]

test: make object! [
    use-stdout: true
    name: "Test"

    test-count: 0
    pass-count: 0

    ok: func [
        "Test if a condition is true"
        test-condition [block!] "The condition to be tested"
        /log "If you want to log a message on success"
        message        [string!]  "optional string to display on success"
        
        /local test-result output
    ] [
        test-count: test-count + 1
        test-result: do test-condition
        output: join test-count [ 
            " - "
            either test-result == true [
                pass-count: pass-count + 1
                either log [
                    join "OK - " message 
                ] [
                    "OK"
                ]
            ] [
                "Not OK"
            ]
        ]
        if use-stdout [ print output ]
        return output
    ]
    
    wrapup: func [
        "Returns a summary of test results"
        /local message
    ] [
        message: join name [ ": " pass-count  "/" test-count " passed" ]
        if use-stdout [ print message ]
        return message
    ]
]

; Call this function to see test-simple in action.
test-test: func [
    "Test the test object"
    /local my-test
] [
    good-test: make test [
        name: "Good Tests"
        ok     [ 1 == 1 ]
        ok/log [ 1 == 1 ] "All is right in the world."
        wrapup
    ]

    bad-test: make test [
        name: "Bad Tests"
        ok     [ 1 == 2 ]
        ok/log [ 1 == 2 ] "Something is very wrong!"
        wrapup
    ]

]

comment {
    The MIT License

    Copyright (c) 2005 Brian Wisti

    Permission is hereby granted, free of charge, to any person 
    obtaining a copy of this software and associated documentation files 
    (the "Software"), to deal in the Software without restriction, 
    including without limitation the rights to use, copy, modify, merge, 
    publish, distribute, sublicense, and/or sell copies of the Software, 
    and to permit persons to whom the Software is furnished to do so, 
    subject to the following conditions:

    The above copyright notice and this permission notice shall be 
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
    BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
    ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
    SOFTWARE.
}
Notes