Sha256: 5094a0709860055dab30172d9c431d9ad58771b219b524d4debf58d29daaa9e0

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module TestUtil
  module Parser
    # Makes a test parser class by the sub-parser module.
    def make_test_parser(parser_module)
      klass = Class.new(Parslet::Parser)
      klass.instance_eval do
        include parser_module
      end
      return klass
    end
    module_function :make_test_parser

    def spec(mod, rb, context)
      #parser = make_test_parser(mod)
      parser = Pione::Parser::DocumentParser
      basename = File.basename(rb, ".rb")
      path = File.join(File.dirname(rb), basename + ".yml")
      YAML.load(File.read(path)).each do |name, testcase|
        context.describe name do
          if strings = testcase["valid"]
            strings.each do |string|
              it "should parse as %s:%s%s" % [name, string.include?("\n") ? "\n" : " ", string.chomp] do
                should.not.raise(Parslet::ParseFailed) do
                  parser.new.send(name).parse(string)
                end
              end
            end
          end

          if strings = testcase["invalid"]
            strings.each do |string|
              it "should fail when parsing as %s:%s%s" % [name, string.include?("\n") ? "\n" : "", string.chomp] do
                should.raise(Parslet::ParseFailed) do
                  parser.new.send(name).parse(string)
                end
              end
            end
          end
        end
      end
    end
    module_function :spec
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pione-0.2.2 test/test-util/parser.rb