class MIPSUnit::MSpec::Generator

Generates the MIPS assembly code for the provided TestDefinition objects.

Author

Zachary Kurmas

Copyright

Copyright © 2012

Public Class Methods

generate(groups, output) click to toggle source

Generate the MIPS test assembly code

# File lib/mipsunit/mspec/generator.rb, line 85
def self.generate(groups, output)
  # print the file header
  output.puts "#"
  output.puts "# This file automatically generated using MIPSUnit::MSpec"
  output.puts "#"
  output.puts
  output.puts ".globl main"
  output.puts
  output.puts ".text"
  output.puts "main:"


  # generate the code for each TestDefinitionGroup (i.e., +describe+ block)
  groups.each do |group|
    make_group(group, output)
  end

  output.puts

  # This prints the "All tests pass." message
  # and exists.  This is *not* a procedure
  output.puts Assembly::ALL_TESTS_PASS


  # include the FAIL procedure
  output.puts Assembly::FAIL

  print_labels(output)
end
make_group(group, output) click to toggle source

Generate the MIPS test assembly for all the test definitions in the given TestDefinitionGroup (effectively generates the assembly code, recursively, for all TestDefinitions in the corresponding define block)

# File lib/mipsunit/mspec/generator.rb, line 54
def self.make_group(group, output)
  # print the group header
  elipses = group.nested_groups.size > 0 ? "..." : ""
  output.puts
  output.puts '#############################################'
  output.puts '#'
  output.puts "# #{group.cumulative_description} #{elipses}"
  output.puts '#'
  output.puts "#############################################"

  # generate each test at this level
  group.definitions.each do |definition|
    make_test(definition, group, output)
  end

  # process any nested groups
  group.nested_groups.each do |nested_group|
    make_group(nested_group, output)
  end
end
make_test(definition, group, output) click to toggle source

Generate the MIPS test assembly code for the given TestDefinition (which must be in the given TestDefinitionGroup)

# File lib/mipsunit/mspec/generator.rb, line 29
def self.make_test(definition, group, output)
  # print the test header
  output.puts
  output.puts '#'
  output.puts "# #{group.cumulative_description} #{definition.description}"
  output.puts '#'

  # instantiate the TestFactory
  factory = definition.factory_class.new(output, definition.description)

  # run any before blocks
  group.cumulative_before.each do |block|
    factory.instance_eval(&block)
  end

  # enable the data method (so users can add labels in the 'it' blocks)
  TestFactoryHelper.remove_warning_from_data_method(factory)

  # generate the assembly code for the test
  factory.instance_eval(&definition.block)
end
print_labels(output) click to toggle source

print the assembly for all data labels.