lib/assert/view/base.rb in assert-view-0.2.0 vs lib/assert/view/base.rb in assert-view-0.3.0
- old
+ new
@@ -1,11 +1,55 @@
require 'assert/options'
-require 'assert/view/renderer'
module Assert::View
+ # this module is mixed in to the Assert::View::Base class
+ # it use Undies to define and render view templates
+ module Renderer
+ require 'undies'
+
+ def self.included(receiver)
+ receiver.send(:extend, ClassMethods)
+ end
+
+ # define rendering template class to use for rendering
+ # need to overwrite the '_' and '__' meths to add trailing newlines
+ # b/c streaming output doesn't add any whitespace
+ class Template < ::Undies::Template
+
+ def _(data="", nl=true); super(data.to_s + (nl ? "\n" : "")); end
+ def __(data="", nl=true); super(data.to_s + (nl ? "\n" : "")); end
+
+ end
+
+ # this method is required by assert and is called by the test runner
+ # use Undies to render the template
+ # using the view's template file
+ # streaming to the view's output io
+ # passing in the view itself and any runner_callback as locals
+ def render(*args, &runner_callback)
+ locals = {
+ :view => self,
+ :runner => runner_callback
+ }
+ Template.new(self.output_io, locals, &self.class.template)
+ end
+
+ module ClassMethods
+
+ # make any helper methods available to the template
+ def helper(helper_klass)
+ Template.send(:include, helper_klass)
+ end
+
+ end
+
+ end
+
+
class Base
+
include Assert::Options
options do
default_passed_abbrev '.'
default_failed_abbrev 'F'
default_ignored_abbrev 'I'
@@ -17,10 +61,19 @@
# work with the view. It provides:
# * 'render': called by the runner to render the view
# * 'self.helper': used to provide helper mixins to the renderer template
include Renderer
+ # set the view's template by passing a block, get by calling w/ no args
+ def self.template(&block)
+ if block
+ @template = block
+ else
+ @template
+ end
+ end
+
attr_accessor :suite, :output_io, :runtime_result_callback
def initialize(output_io, suite=Assert.suite)
self.suite = suite
self.output_io = output_io
@@ -28,21 +81,10 @@
def view
self
end
-
-
- # TODO: look for files in the .assert dir
- # TODO: allow option for specifying which template to use
- # TODO: test
- def template_file
- File.expand_path("./templates/#{self.options.template}.rb", File.dirname(__FILE__))
- end
-
-
-
# called by the view template
# store off any result_callback
# call the runner callback to actually run the tests
def run_tests(runner_callback, &result_callback)
self.runtime_result_callback = result_callback
@@ -66,10 +108,9 @@
def count(type)
self.suite.count(type)
end
- # TODO: test
def tests?
self.count(:tests) > 0
end
def all_passed?