Class: Testgen::CLI

Inherits:
Object
  • Object
show all
Includes:
LanguageParser
Defined in:
lib/testgen/cli.rb

Constant Summary

Constants included from LanguageParser

LanguageParser::VERSION

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



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
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/testgen/cli.rb', line 166

def self.execute(stdout, arguments=[])
  # NOTE: the option -p/--path= is given as an example, and should be replaced in your application.
  options = {
    :path     => '~'
  }
  mandatory_options = %w(  )
  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      This application is wonderful because...

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    opts.separator ""
    opts.on("-p", "--path=PATH", String,
            "This is a sample message.",
            "For multiple lines, add more strings.",
            "Default: ~") { |arg| options[:path] = arg }
    opts.on("-s", "--source=Source", String,
            "The source file.") { |arg| options[:source] = arg }
    opts.on("-t", "--template=Template", String,
            "The template file for the Unit Test.") { |arg| options[:template] = arg }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      stdout.puts opts; exit
    end
  end

  path = options[:path]
  source = options[:source]
  template = options[:template]

  # do stuff
  if source
    generate_tests( source )
  else
    print "Must specify an input C file\n"
  end
end

.find_tests(comments) ⇒ Object

find_tests( comments )

comments - The array of comments returned from the Prototype object

This finds all of the tests in all of the comments and returns an array of TestData objects. One for each test.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/testgen/cli.rb', line 87

def self.find_tests( comments )
  tests = []
  # Iterate through the comments
  comments.each { |comment|
    # Search for the test data block XML
    found = comment.to_s.scan( /(<test.*?<\/test>)/ )
    # If we found some then use them to create the TestData objects
    if ( found )
      found.each { |items|
        data = parse_data( items[0] )
        tests.push( data ) if ( data )
      }
    end
  }
  # Return the comments
  tests
end

.generate_tests(file_name) ⇒ Object

generate_tests( file_name )

file_name - The name of the file to scan and alter

This generates test case running code from test case comments around the function prototypes.



110
111
112
113
114
115
116
117
118
119
120
121
122
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/testgen/cli.rb', line 110

def self.generate_tests( file_name )
  # Read the file contents
  fh = File.open( file_name )
  c_text = fh.read()
  fh.close()
  # Tokenize the file
  tokenizer = CTokenizer.new( )
  tokenizer.parse( c_text )
  # Build the language scanner
  languagescanner = CLanguageScanner.new()
  # Use our prototype class when prototypes are built instead
  # of the base class
  languagescanner.prototypeClass = PrototypeWithTests
  # Get the prototypes
  languagescanner.parse( tokenizer.tokens )
  # Iterate through the prototypes and turn the comments
  # into tests
  count = 0
  languagescanner.prototypes.each { |proto|
    # Parse the comments into tests and add them
    tests = find_tests( proto.comments )
    proto.add_tests( tests )
    # Create unique names for the tests that don't
    # have them
    index = 1
    proto.tests.each { |item|
      name = "#{proto.method_name}_#{index}"
      item.name = name unless ( item.name )
      index += 1
      count += 1
    }
  }

  # Create the testing code by calling the template.  The
  # templates reference 'prototypes'.  They can do this because
  # we pass 'binding' into ERb so that the template is evaluated
  # in our context so that it has access to our locals.
  prototypes = languagescanner.prototypes
  erb = ERB.new( File.new( File.dirname(__FILE__)+"/../../templates/ut/c.template" ).read )
  #erb = ERB.new(CGIA_UT_Template::C_UT_TEMPLATE)
  template_result = erb.result( binding )
  # Add in the prefix
  template_result = "// <test_start>\n#{template_result}\n// </test_start>"
  # Backup the original file
  File.copy file_name, "#{file_name}.bak"
  # Insert the new template result
  if ( c_text =~ /\/\/ <test_start>.*?\/\/ <\/test_start>/m )
    c_text.sub!( /\/\/ <test_start>.*?\/\/ <\/test_start>/m, template_result )
  else
    c_text += template_result
  end
  # Output the finished code
  File.open( file_name, "w" ).write( c_text )
  # Tell the user what we did
  print "#{file_name}: #{count} tests found\n"
end

.parse_data(text) ⇒ Object

parse_data( text )

text - The text of the comment

This builds a TestData object from a text string



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/testgen/cli.rb', line 62

def self.parse_data( text )
  begin
    # Create the new test data
    data = TestData.new()
    # Parse the XML of the comment 
    doc = REXML::Document.new( text )
    # Get the result and name
    data.result = doc.root.attributes[ 'result' ]
    data.name = doc.root.attributes[ 'name' ]
    # Get the test data
    doc.root.elements.each { |elem|
      data.add_argument( elem.name, elem.text.strip )
    }
    # Return the new TestData object
    data
  rescue
    nil
  end
end