Class: Funit::TestSuite

Inherits:
File
  • Object
show all
Includes:
Funit
Defined in:
lib/funit/test_suite.rb

Instance Method Summary collapse

Methods included from Funit

#compileTests, #funit_exists?, #parseCommandLine, #requestedModules, #run_tests, #syntaxError, #warning, #writeTestRunner

Methods included from Assertions

#isequal, #isequalwithin, #isfalse, #isrealequal, #istrue, #writeAssert

Constructor Details

#initialize(suiteName) ⇒ TestSuite

Returns a new instance of TestSuite.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/funit/test_suite.rb', line 24

def initialize suiteName
  @lineNumber = 'blank'
  @suiteName = suiteName
  return nil unless funit_exists?(suiteName)
  File.delete(suiteName+".fun") if File.exists?(suiteName+"_fun.f90")
  super(suiteName+"_fun.f90","w")
  @tests, @setup, @teardown = Array.new, Array.new, Array.new
  topWrapper
  expand
  close
end

Instance Method Details

#addtoSetup(funit_contents) ⇒ Object



106
107
108
109
110
# File 'lib/funit/test_suite.rb', line 106

def addtoSetup funit_contents
  while (line = funit_contents.shift) && line !~ /endSetup/i
    @setup.push line
  end
end

#addtoTeardown(funit_contents) ⇒ Object



112
113
114
115
116
# File 'lib/funit/test_suite.rb', line 112

def addtoTeardown funit_contents
  while (line = funit_contents.shift) && line !~ /endTeardown/i
    @teardown.push line
  end
end

#aTest(testName, funit_contents) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/funit/test_suite.rb', line 123

def aTest testName, funit_contents
  @testName = testName
  @tests.push testName
  syntaxError("test name #@testName not unique",@suiteName) if (@tests.uniq!)

  puts " subroutine #{testName}\n\n"

  numOfAsserts = 0
  
  while (line = funit_contents.shift) && line !~ /endTest/i
    case line
    when $commentLine
      puts line
    when /Is(RealEqual|False|True|EqualWithin|Equal)/i
      @lineNumber = @funit_TotalLines - funit_contents.length
      numOfAsserts += 1
      puts send( $&.downcase!, line )
    else
      puts line
    end
  end
  warning("no asserts in test", @suiteName) if numOfAsserts == 0

  puts "\n  numTests = numTests + 1\n\n"
  puts " end subroutine #{testName}\n\n"
end

#closeObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/funit/test_suite.rb', line 150

def close
  puts "\n subroutine Setup"
  puts @setup
  puts "  noAssertFailed = .true."
  puts " end subroutine Setup\n\n"

  puts "\n subroutine Teardown"
  puts @teardown
  puts " end subroutine Teardown\n\n"

  puts <<-NEXTONE

 subroutine test_#{@suiteName}( nTests, nAsserts, nAssertsTested, nFailures )

  integer :: nTests
  integer :: nAsserts
  integer :: nAssertsTested
  integer :: nFailures

  continue
  NEXTONE

  @tests.each do |testName|
    puts "\n  call Setup"
    puts "  call #{testName}"
    puts "  call Teardown"
  end

  puts <<-LASTONE

  nTests          = numTests
  nAsserts        = numAsserts
  nAssertsTested  = numAssertsTested
  nFailures       = numFailures

 end subroutine test_#{@suiteName}

end module #{@suiteName}_fun
  LASTONE
  super
end

#expandObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/funit/test_suite.rb', line 63

def expand
 
  funit_file = @suiteName+".fun"
  $stderr.print "expanding #{funit_file}..."
   
  funit_contents = IO.readlines(funit_file)
  @funit_TotalLines = funit_contents.length

  while (line = funit_contents.shift) && line !~ $keyword
    puts line
  end

  funit_contents.unshift line

  puts " contains\n\n"

  while (line = funit_contents.shift)
    case line
    when $commentLine
      puts line
    when /beginSetup/i
      addtoSetup funit_contents
    when /beginTeardown/i
      addtoTeardown funit_contents
    when /XbeginTest\s+(\w+)/i
      ignoreTest($1,funit_contents)
    when /beginTest\s+(\w+)/i
      aTest($1,funit_contents)
    when /beginTest/i
      syntaxError "no name given for beginTest", @suiteName
    when /end(Setup|Teardown|Test)/i
      syntaxError "no matching begin#$1 for an #$&", @suiteName
    when $assertRegEx
      syntaxError "#$1 assert not in a test block", @suiteName
    else
      puts line
    end
  end # while

  $stderr.puts "done."

end

#ignoreTest(testName, funit_contents) ⇒ Object



118
119
120
121
# File 'lib/funit/test_suite.rb', line 118

def ignoreTest testName, funit_contents
  warning("Ignoring test: #{testName}", @suiteName)
  line = funit_contents.shift while line !~ /endTest/i
end

#topWrapperObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/funit/test_suite.rb', line 36

def topWrapper
  puts <<-TOP
! #{@suiteName}_fun.f90 - a unit test suite for #{@suiteName}.f90
!
! #{File.basename $0} generated this file from #{@suiteName}.fun
! at #{Time.now}

module #{@suiteName}_fun

 use #{@suiteName}

 implicit none

 logical :: noAssertFailed

 public :: test_#@suiteName

 private

 integer :: numTests          = 0
 integer :: numAsserts        = 0
 integer :: numAssertsTested  = 0
 integer :: numFailures       = 0

  TOP
end