Top Level Namespace

Defined Under Namespace

Modules: Ppr Classes: KeywordSearcher, SaferGenerator

Instance Method Summary collapse

Instance Method Details

#test_preprocessor(preprocessor, input, expected) ⇒ Object

Function for testing a preprocessor



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ppr/test_ppr.rb', line 9

def test_preprocessor(preprocessor,input,expected)
    # Prepare the output and the input streams
    puts "Preparing the input and ouput streams..."
    output = StringIO.new("")
    # Process the input and exepected arguments.
    if !input.respond_to?(:each_line) or input.is_a?(String) then
        # input is actually a file name, open it.
        input = File.open(input.to_s,"r")
    end
    if !expected.respond_to?(:each_line) or expected.is_a?(String) then
        # expected is actually a file name, open it.
        expected = StringIO.new(File.read(expected.to_s))
    end

    # Apply the preprocessor
    puts "Applying the preprocessor..."
    preprocessor.preprocess(input,output)

    # Check the result
    puts "Checking the result..."
    output.rewind
    check = output.string == expected.read

    unless check
        puts "*Error*: invalid expansion result."
        iline = output.string.each_line
        expected.rewind
        expected.each_line.with_index do |exp_line,i|
            line = iline.next
            puts "exp_line=#{exp_line}"
            puts "line=#{line}"
            if exp_line != line then
                puts "Expected line #{i+1}:\n#{exp_line}"
                puts "got:\n#{line}"
            end
        end
        return false
    end
    return true
end

#test_preprocessor_exception(preprocessor, string, exception) ⇒ Object

Function for testing a preprocessor on string which should raise an exception string.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ppr/test_ppr.rb', line 52

def test_preprocessor_exception(preprocessor,string,exception)
    input = StringIO.new(string)
    output = StringIO.new("")
    begin
        $ppr.preprocess(input,output)
        puts "*Error*: preprocessed without exception."
        return false
    rescue Exception => e
        if e.to_s.include?(exception.to_s) then
            puts "Got exception: <#{e}> as expected."
            return true
        else
            puts "*Error*: unexpected exception.", 
                 "Got <#{e}> but expecting <#{exception}>."
            return false
        end
    end
end