Class: MxxRu::TextfileUnittestTarget

Inherits:
AbstractTarget show all
Defined in:
lib/mxx_ru/textfile_unittest.rb

Overview

The class of a target, which is binary unit-test application, which create one or many text files.

Given target performs a build of application, and then runs it and compares results with files which contains correct results.

The basic idea consists in the presence of project file, which controls the build of unit-test application. In that project file, target object is created, inherited from MxxRu::BinaryTarget. To start unit-test it’s necessary to create one more file, where target object of MxxRu::TextfileUnittestTarget class is created. For example:

Unit-test application build file:

MxxRu::setup_target(
  MxxRu::Cpp::ExeTarget.new( "test/pack/prj.rb" ) {
    ...
  }
)

File to run unit-test:

MxxRu::setup_target(
  MxxRu::TextfileUnittestTarget.new( 
    "test/pack/pack.ut.rb",
    "test/pack/prj.rb" ) {

    launch( "--loops 16 --out test/pack/out/16.txt",
      [ pair( "test/pack/out/16.txt",
          "test/pack/out/etalon/16.txt" ) ] )

    launch( "--loops 32 --out test/pack/out/32.txt",
      [ pair( "test/pack/out/32.txt",
          "test/pack/out/etalon/32.txt" ) ] )
  }
)

File which is using that unit-test.

MxxRu::setup_target(
  MxxRu::Cpp::CompositeTarget.new( MxxRu::BUILD_ROOT ) {
    required_prj( "some/project/prj.rb" )
    required_prj( "test/pack/prj.ut.rb" )
  }
)

Defined Under Namespace

Classes: FileMismatchEx, Launch, MustBeOneTargetNameEx, Pair

Constant Summary collapse

Must_be_one_target_name_ex =

For compatibility with previous versions.

MustBeOneTargetNameEx
File_mismatch_ex =

For compatibility with previous versions.

FileMismatchEx

Instance Attribute Summary collapse

Attributes inherited from AbstractTarget

#mxx_full_targets_names, #mxx_generators, #mxx_required_prjs

Instance Method Summary collapse

Methods inherited from AbstractTarget

define_plural_form_method, #generator, #mxx_add_full_target_name, #prj_alias, #required_prj, run

Constructor Details

#initialize(a_alias, a_target_project, &block) ⇒ TextfileUnittestTarget

Constructor.

a_alias

Self alias.

a_target_project

Project, responsible for build of unit-test application.



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mxx_ru/textfile_unittest.rb', line 158

def initialize( a_alias, a_target_project, &block )
  super( a_alias )

  @mxx_build_state = nil

  @mxx_target_project = required_prj( a_target_project )

  @mxx_launches = Array.new

  instance_eval( &block ) if block
end

Instance Attribute Details

#mxx_build_stateObject (readonly)

True if build method was already executed.



149
150
151
# File 'lib/mxx_ru/textfile_unittest.rb', line 149

def mxx_build_state
  @mxx_build_state
end

#mxx_launchesObject (readonly)

Description of test application runs. Array of Launch.



152
153
154
# File 'lib/mxx_ru/textfile_unittest.rb', line 152

def mxx_launches
  @mxx_launches
end

#mxx_target_projectObject (readonly)

Target, responsible for build of unit-test application.



147
148
149
# File 'lib/mxx_ru/textfile_unittest.rb', line 147

def mxx_target_project
  @mxx_target_project
end

Instance Method Details

#buildObject

Build subordinated project and run it.

If unit-test application returns exit code not equal to 0, exception is thrown.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mxx_ru/textfile_unittest.rb', line 196

def build
  if !@mxx_build_state
    @mxx_target_project.build

    # Determining the name of application, which should be run.
    full_names = @mxx_target_project.mxx_full_targets_names
    if 1 != full_names.size
      raise MustBeOneTargetNameEx.new( full_names )
    end

    # No execution of commands in dry-run mode.
    if !MxxRu::Util::Mode.instance.is_dry_run
      puts "running unit test: #{full_names[0]}..."
      do_all_launches( full_names[ 0 ] )
    end

    @mxx_build_state = TargetState.new( TargetState::REBUILT )
  end

  return @mxx_build_state
end

#cleanObject

Cleanup the project.

Executes clean method from subordinated project. Deletes all files, defined as result files of unit-test application.



222
223
224
225
226
227
228
229
230
# File 'lib/mxx_ru/textfile_unittest.rb', line 222

def clean
  @mxx_target_project.clean

  @mxx_launches.each { |launch|
    launch.pairs.each { |pair|
      Util.delete_file( pair.produced )
    }
  }
end

#launch(a_cmdline, a_pairs) ⇒ Object

Add description of sequental run of test application.

a_cmdline

Command line parameters. May be empty string of nil if no parameters required.

a_pairs

A list of pairs of file names to compare. Should be Array of values returned by pair() method.

Comparation will be performed in order they are defined in a_pairs. Comparation is interrupted on first mismatch.



187
188
189
190
# File 'lib/mxx_ru/textfile_unittest.rb', line 187

def launch( a_cmdline, a_pairs )
  a_cmdline = "" if !a_cmdline
  @mxx_launches << Launch.new( a_cmdline, a_pairs )
end

#pair(a_produced, a_etalon) ⇒ Object

Create description of file pairs to compare.

a_produced

Name of generated file.

a_etalon

Name of correct file.



174
175
176
# File 'lib/mxx_ru/textfile_unittest.rb', line 174

def pair( a_produced, a_etalon )
  return Pair.new( a_produced, a_etalon )
end

#resetObject

Reset build status.



233
234
235
236
# File 'lib/mxx_ru/textfile_unittest.rb', line 233

def reset
  @mxx_target_project.reset
  @mxx_build_state = nil
end