jbender/motion-spec

View on GitHub
lib/motion-spec.rb

Summary

Maintainability
A
0 mins
Test Coverage
# -*- encoding : utf-8 -*-
unless defined?(Motion::Project::Config)
  fail 'The MotionSpec gem must be required within a RubyMotion project Rakefile.'
end

require 'motion-require'

# Proper load order of all the classes/modules
###
def require_lib_files(files)
  case files
  when String
    file_paths = expanded_file_path(files)
    file_paths = Dir.glob(file_paths) if files.include? '*'
  when Array
    file_paths = files.map { |f| expanded_file_path(f) }
  end

  Motion::Require.all(file_paths)
end

def expanded_file_path(file)
  File.expand_path("../motion-spec/#{file}.rb", __FILE__)
end

# Let's start off with what version we're running
require_lib_files('version')

# Load the output before the core so the core knows how to print
require_lib_files('output/*')

# Add on to the context of specs
require_lib_files('context_helper/*')

# All the other core modules in the proper order
require_lib_files(%w(
  core error specification platform context should expectation
  fail_message_renderer
))

# Load expectation matchers
require_lib_files('matcher/single_method')
require_lib_files('matcher/*')

# Monkeypatch core objects to respond to test methods
require_lib_files('extensions/*')

# Allow method mocks and stubs
require_lib_files('mock/*')

# FIXME : Need better detection for iPhone Simulator
if defined?(UIDevice) &&
    UIDevice.respond_to?('currentDevice') &&
    !UIDevice.currentDevice.name =~ /(iPhone|iPad) Simulator/

  module Kernel
    def puts(*args)
      NSLog(args.join("\n"))
    end

    def print(*args)
      puts *args # TODO
    end
  end
end

# Remove the 'spec' file from the core load path so that the copy of 'Bacon'
# included in RubyMotion is not automatically loaded. That file not only adds
# Bacon but monkeypatches things like Kernel.describe that we don't want.
module Motion
  module Project
    class Config
      def spec_core_files
        @spec_core_files ||= begin
          # Core library + core helpers.
          Dir.chdir(File.join(File.dirname(__FILE__), '..')) do
            # NOTE: This line is commented out to avoid loading Bacon.
            ( # ['spec.rb'] +
            Dir.glob(File.join('spec', 'helpers', '*.rb')) +
            Dir.glob(File.join('project', 'template', App.template.to_s, 'spec-helpers', '*.rb')))
              .map { |x| File.expand_path(x) }
          end
        end
      end
    end

    # 'Bacon' is hard-coded in the string generated by main_cpp_file_txt as
    # the class to run our specs. This updates the method to replace 'Bacon'
    # with 'MotionSpec'.
    class IOSConfig
      alias_method :old_main_cpp_file_txt, :main_cpp_file_txt

      def main_cpp_file_txt(spec_objs)
        old_main_cpp_file_txt(spec_objs).gsub(/Bacon/, 'MotionSpec')
      end
    end
  end
end