Class: Funit::TestSuite

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

Overview

Create testsuite wrapper code

Constant Summary collapse

KEYWORDS =
Regexp.union(/(end\s+)?(setup|teardown|test)/i,
Assertions::ASSERTION_PATTERN)
COMMENT_LINE =
/^\s*!/

Constants included from Funit

MAKEFILE, TEST_RUNNER, VERSION

Constants included from Assertions

Assertions::ASSERTION_PATTERN

Instance Method Summary collapse

Methods included from Funit

#clean_genFiles, #compile_tests, #funit_exists?, #parse_command_line, #print_help, #requested_modules, #run_tests, #syntax_error, #warning, #write_test_runner

Methods included from Assertions

#assert_array_equal, #assert_equal, #assert_equal_within, #assert_false, #assert_real_equal, #assert_true, #get_args, #write_assert

Constructor Details

#initialize(suite_name, suite_content, wrap_with_module) ⇒ TestSuite

Returns a new instance of TestSuite.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/funit/testsuite.rb', line 18

def initialize( suite_name, suite_content, wrap_with_module )
  @line_number = 'blank'
  @suite_name = suite_name
  @suite_content = suite_content
  return nil unless funit_exists?(suite_name)
  File.delete(suite_name+"_fun.f90") if File.exists?(suite_name+"_fun.f90")
  super(suite_name+"_fun.f90","w")
  @tests, @setup, @teardown = [], [], []
  header
  @wrap_with_module = wrap_with_module
  module_wrapper if @wrap_with_module
  top_wrapper
  expand
  close
end

Instance Method Details

#a_test(test_name, funit_contents) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/funit/testsuite.rb', line 130

def a_test test_name, funit_contents
  @test_name = test_name
  @tests.push test_name
  syntax_error("test name #@test_name not unique",@suite_name) if (@tests.uniq!)

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

  num_of_asserts = 0
  
  while (line = funit_contents.shift) && line !~ /end\s+test/i
    case line
    when COMMENT_LINE
      puts line
    when Assertions::ASSERTION_PATTERN
      @line_number = @funit_total_lines - funit_contents.length
      num_of_asserts += 1
      puts send( $1.downcase, line )
    else
      puts line
    end
  end
  warning("no asserts in test", @suite_name) if num_of_asserts == 0

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

#add_to_setup(funit_contents) ⇒ Object



113
114
115
116
117
# File 'lib/funit/testsuite.rb', line 113

def add_to_setup funit_contents
  while (line = funit_contents.shift) && line !~ /end\s+setup/i
    @setup.push line
  end
end

#add_to_teardown(funit_contents) ⇒ Object



119
120
121
122
123
# File 'lib/funit/testsuite.rb', line 119

def add_to_teardown funit_contents
  while (line = funit_contents.shift) && line !~ /end\s+teardown/i
    @teardown.push line
  end
end

#closeObject



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
191
192
193
194
195
196
197
# File 'lib/funit/testsuite.rb', line 157

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

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

  puts <<-NEXTONE

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

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

  continue
  NEXTONE

  @tests.each do |test_name|
    puts "\n  call funit_setup"
    puts "  call #{test_name}"
    puts "  call funit_teardown"
  end

  puts <<-LASTONE

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

 end subroutine test_#{@suite_name}

end module #{@suite_name}_fun
  LASTONE
  super
end

#expandObject



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
105
106
107
108
109
110
111
# File 'lib/funit/testsuite.rb', line 75

def expand
  $stderr.print "expanding test suite: #{@suite_name}..."
  funit_contents = @suite_content.split("\n")
  @funit_total_lines = funit_contents.length

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

  funit_contents.unshift line

  puts " contains\n\n"

  while (line = funit_contents.shift)
    case line
    when COMMENT_LINE
      puts line
    when /^setup/i
      add_to_setup funit_contents
    when /^teardown/i
      add_to_teardown funit_contents
    when /^Xtest\s+(\w+)/i
      ignore_test($1,funit_contents)
    when /^test\s+(\w+)/i
      a_test($1,funit_contents)
    when /^test/i
      syntax_error "no name given for test", @suite_name
    when /^end\s+(setup|teardown|test)/i
      syntax_error "no matching #$1 for an #$&", @suite_name
    when Assertions::ASSERTION_PATTERN
      syntax_error "#$1 assertion not in a test block", @suite_name
    else
      puts line
    end
  end
  $stderr.puts "done."
end

#headerObject



34
35
36
37
38
39
40
41
# File 'lib/funit/testsuite.rb', line 34

def header
  puts <<-HEADER
! #{@suite_name}_fun.f90 - a unit test suite for #{@suite_name}.f90
!
! #{File.basename $0} generated this file from #{@suite_name}.fun

  HEADER
end

#ignore_test(test_name, funit_contents) ⇒ Object



125
126
127
128
# File 'lib/funit/testsuite.rb', line 125

def ignore_test test_name, funit_contents
  warning("Ignoring test: #{test_name}", @suite_name)
  line = funit_contents.shift while line !~ /end\s+Test/i
end

#module_wrapperObject



43
44
45
46
47
48
49
50
51
# File 'lib/funit/testsuite.rb', line 43

def module_wrapper
  puts <<-MODULE_WRAPPER
module #{@suite_name}_mod
contains
  include '#@suite_name.f90'
end module #{@suite_name}_mod

  MODULE_WRAPPER
end

#top_wrapperObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/funit/testsuite.rb', line 53

def top_wrapper
  puts <<-TOP
module #{@suite_name}_fun

 use #{ @wrap_with_module ? @suite_name+'_mod' : @suite_name }

 implicit none

 logical :: noAssertFailed

 public :: test_#@suite_name

 private

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

  TOP
end