class Pione::TestHelper::Transformer::Spec

Public Class Methods

new(testcases, name, parser_name, option, context) click to toggle source
# File lib/pione/test-helper/transformer-helper.rb, line 51
def initialize(testcases, name, parser_name, option, context)
  @testcases = testcases
  @name = name
  @parser_name = parser_name
  @option = Hash.new
  @option[:parser_class] = option[:parser_class] || Pione::Lang::DocumentParser
  @option[:transformer_class] = option[:transformer_class] || Pione::Lang::DocumentTransformer
  @option[:package_name] = option[:package_name] || "Test"
  @option[:filename] = option[:filename] || "Test"
  @context = context
end

Public Instance Methods

declare() click to toggle source

declare specitification in the context

# File lib/pione/test-helper/transformer-helper.rb, line 64
def declare
  @testcases.each do |tc|
    case tc
    when Naming::Eq, Naming::Block
      test_case(tc)
    when SucceedCase
      succeed_case(tc)
    when FailCase
      fail_case(tc)
    end
  end
end

Private Instance Methods

fail_case(tc) click to toggle source

declare by FailCase

# File lib/pione/test-helper/transformer-helper.rb, line 114
def fail_case(tc)
  block = Proc.new {parse(tc.string)}
  msg = "should fail in %s transformation with %s:%s%s"
  msg_args = [@name, tc.exception_type, tc.string.include?("\n") ? "\n" : " ", tc.string.chomp]

  @context.it(msg % msg_args) { should.raise(tc.exception_type, &block) }
end
parse(string) click to toggle source
# File lib/pione/test-helper/transformer-helper.rb, line 79
def parse(string)
  # cut indentations
  string = Util::Indentation.cut(string)
  parser = parser_class.new.send(@parser_name).parse(string)
  transformer_class.new.apply(parser, package_name: package_name, filename: filename)
end
succeed_case(tc) click to toggle source

declare by SucceedCase

# File lib/pione/test-helper/transformer-helper.rb, line 105
def succeed_case(tc)
  block = Proc.new {parse(tc.string)}
  msg = "should succeed in %s transformation:%s%s"
  msg_args = [@name, tc.string.include?("\n") ? "\n" : " ", tc.string.chomp]

  @context.it(msg % msg_args) { should.not.raise(&block) }
end
test_case(tc) click to toggle source

declare by TestCase

# File lib/pione/test-helper/transformer-helper.rb, line 87
def test_case(tc)
  string = tc.value.string
  expected = tc.value.expected
  res = parse(string)
  msg = "should get %s:%s%s"
  msg_args = [@name, string.include?("\n") ? "\n" : " ", string.chomp]

  @context.it(msg % msg_args) do
    case tc
    when Naming::Eq
      res.should == expected
    when Naming::Block
      expected.call(res)
    end
  end
end