From 5a9a7532e279d178647ca00b54c27eaa76c0c355 Mon Sep 17 00:00:00 2001 From: Kaspar Schiess Date: Mon, 28 Feb 2011 20:18:09 +0100 Subject: [PATCH] + Cleaning up txus patch Mostly code formatting and style transmogrification. Thanks for the patch! We should have had examples as spec much earlier. --- example/comments.rb | 3 +-- example/documentation.rb | 3 +-- example/email_parser.rb | 3 +-- example/empty.rb | 3 +-- example/erb.rb | 5 ++-- example/ip_address.rb | 3 +-- example/local.rb | 3 +-- example/minilisp.rb | 3 +-- example/output/documentation.err | 5 +++- example/output/documentation.out | 1 + example/output/empty.err | 2 +- example/parens.rb | 3 +-- example/readme.rb | 3 +-- example/seasons.rb | 3 +-- example/simple_xml.rb | 3 +-- example/string_parser.rb | 9 +++---- spec/acceptance/examples_spec.rb | 46 +++++++++++++++++++------------- 17 files changed, 50 insertions(+), 51 deletions(-) create mode 100644 example/output/documentation.out diff --git a/example/comments.rb b/example/comments.rb index 50399d3..0165e0a 100644 --- a/example/comments.rb +++ b/example/comments.rb @@ -1,8 +1,7 @@ # A small example on how to parse common types of comments. The example # started out with parser code from Stephen Waits. -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'pp' require 'parslet' diff --git a/example/documentation.rb b/example/documentation.rb index 14558bc..4ce66e4 100644 --- a/example/documentation.rb +++ b/example/documentation.rb @@ -1,8 +1,7 @@ # A small example that shows a really small parser and what happens on parser # errors. -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'pp' require 'parslet' diff --git a/example/email_parser.rb b/example/email_parser.rb index 2e4ab82..bcd46ca 100644 --- a/example/email_parser.rb +++ b/example/email_parser.rb @@ -2,8 +2,7 @@ # Example contributed by Hal Brodigan (postmodern). Thanks! -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'parslet' class EmailParser < Parslet::Parser diff --git a/example/empty.rb b/example/empty.rb index 7bbc87d..2b60a7e 100644 --- a/example/empty.rb +++ b/example/empty.rb @@ -1,8 +1,7 @@ # Basically just demonstrates that you can leave rules empty and get a nice # NotImplementedError. A way to quickly spec out your parser rules? -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'parslet' diff --git a/example/erb.rb b/example/erb.rb index 2886a13..e88ab43 100644 --- a/example/erb.rb +++ b/example/erb.rb @@ -1,5 +1,6 @@ -$: << 'lib' -$: << '../lib' +# Example that demonstrates how a simple erb-like parser could be constructed. + +$:.unshift File.dirname(__FILE__) + "/../lib" require 'parslet' diff --git a/example/ip_address.rb b/example/ip_address.rb index 4cfbbe2..3873500 100644 --- a/example/ip_address.rb +++ b/example/ip_address.rb @@ -6,8 +6,7 @@ # # See http://tools.ietf.org/html/rfc3986#appendix-A for more information. -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'pp' require 'parslet' diff --git a/example/local.rb b/example/local.rb index 956d8fc..53b1932 100644 --- a/example/local.rb +++ b/example/local.rb @@ -6,8 +6,7 @@ # b) Constructing non-greedy or non-blind parsers by transforming the # grammar. -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'parslet' include Parslet diff --git a/example/minilisp.rb b/example/minilisp.rb index 5748bce..1f9c8ed 100644 --- a/example/minilisp.rb +++ b/example/minilisp.rb @@ -1,8 +1,7 @@ # Reproduces [1] using parslet. # [1] http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'pp' require 'parslet' diff --git a/example/output/documentation.err b/example/output/documentation.err index 080cc21..16b382f 100644 --- a/example/output/documentation.err +++ b/example/output/documentation.err @@ -1 +1,4 @@ -Don't know what to do with bbbb at line 1 char 1. (Parslet::ParseFailed) +/Users/kaspar/git_work/own/parslet/lib/parslet/atoms/base.rb:326:in `parse_failed': Don't know what to do with bbbb at line 1 char 1. (Parslet::ParseFailed) + from /Users/kaspar/git_work/own/parslet/lib/parslet/atoms/base.rb:55:in `parse' + from example/documentation.rb:13:in `parse' + from example/documentation.rb:18:in `
' diff --git a/example/output/documentation.out b/example/output/documentation.out new file mode 100644 index 0000000..e88f69b --- /dev/null +++ b/example/output/documentation.out @@ -0,0 +1 @@ +slice("aaaa", 0) diff --git a/example/output/empty.err b/example/output/empty.err index dc81bf7..61658ec 100644 --- a/example/output/empty.err +++ b/example/output/empty.err @@ -1 +1 @@ -empty.rb:14:in `
': rule(:empty) { ... } returns nil. Still not implemented, but already used? (NotImplementedError) +example/empty.rb:13:in `
': rule(:empty) { ... } returns nil. Still not implemented, but already used? (NotImplementedError) diff --git a/example/parens.rb b/example/parens.rb index 23a1a4c..bcc20f4 100644 --- a/example/parens.rb +++ b/example/parens.rb @@ -2,8 +2,7 @@ # uses '.as(:name)' to construct a tree that can reliably be matched # afterwards. -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'pp' require 'parslet' diff --git a/example/readme.rb b/example/readme.rb index 891e15e..c75c9b4 100644 --- a/example/readme.rb +++ b/example/readme.rb @@ -1,8 +1,7 @@ # The example from the readme. With this, I am making sure that the readme # 'works'. Is this too messy? -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" # cut here ------------------------------------------------------------------- require 'parslet' diff --git a/example/seasons.rb b/example/seasons.rb index d6f1e7c..41c525b 100644 --- a/example/seasons.rb +++ b/example/seasons.rb @@ -1,5 +1,4 @@ -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'parslet' require 'pp' diff --git a/example/simple_xml.rb b/example/simple_xml.rb index 22fe918..e27f9f1 100644 --- a/example/simple_xml.rb +++ b/example/simple_xml.rb @@ -1,8 +1,7 @@ # A simple xml parser. It is simple in the respect as that it doesn't address # any of the complexities of XML. This is ruby 1.9. -$: << 'lib' -$: << '../lib' +$:.unshift File.dirname(__FILE__) + "/../lib" require 'pp' require 'parslet' diff --git a/example/string_parser.rb b/example/string_parser.rb index eb56f88..4506d2b 100644 --- a/example/string_parser.rb +++ b/example/string_parser.rb @@ -4,10 +4,7 @@ require 'pp' -$: << 'lib' -$: << '../lib' -$: << 'example' - +$:.unshift File.dirname(__FILE__) + "/../lib" require 'parslet' include Parslet @@ -53,8 +50,8 @@ class LiteralsParser < Parslet::Parser root :literals end -file = File.exist?('simple.lit') ? File.read('simple.lit')\ - : File.read('example/simple.lit') +input_name = File.join(File.dirname(__FILE__), 'simple.lit') +file = File.read(input_name) parsetree = LiteralsParser.new.parse(file) diff --git a/spec/acceptance/examples_spec.rb b/spec/acceptance/examples_spec.rb index 64c094e..77aa932 100644 --- a/spec/acceptance/examples_spec.rb +++ b/spec/acceptance/examples_spec.rb @@ -2,28 +2,36 @@ require 'open3' describe "Regression examples from example/ directory" do - Dir["example/*.rb"].each do |example| - - it "parses #{example} successfully" do - stdin, stdout, stderr = Open3.popen3("ruby #{example}") - - expected_output_file = example.gsub('.rb', '.out').gsub('example/','example/output/') - expected_error_file = example.gsub('.rb', '.err').gsub('example/','example/output/') - - if File.exists?(expected_output_file) - - stdout.readlines.join.strip.should include(File.read(expected_output_file).strip) - - elsif File.exists?(expected_error_file) - stderr.readlines.join.strip.should include(File.read(expected_error_file).strip) - else - error = "Neither #{expected_output_file} nor #{expected_error_file} exists. Cannot compare results with any output or error." - fail error + context example do + # Generates a product path for a given example file. + def product_path(str, ext) + str. + gsub('.rb', ".#{ext}"). + gsub('example/','example/output/') end + it "runs successfully" do + stdin, stdout, stderr = Open3.popen3("ruby #{example}") + + handle_map = { + stdout => :out, + stderr => :err + } + expectation_found = handle_map.any? do |io, ext| + name = product_path(example, ext) + + if File.exists?(name) + io.read.strip.should == File.read(name).strip + true + end + end + + unless expectation_found + fail "Example doesn't have either an .err or an .out file. "+ + "Please create in examples/output!" + end + end end - end - end