Browse files

+ Cleaning up txus patch

Mostly code formatting and style transmogrification. Thanks for the
patch! We should have had examples as spec much earlier.
  • Loading branch information...
1 parent 01f1232 commit 5a9a7532e279d178647ca00b54c27eaa76c0c355 @kschiess committed Feb 28, 2011
View
3 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'
View
3 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'
View
3 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
View
3 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'
View
5 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'
View
3 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'
View
3 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
View
3 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'
View
5 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 `<main>'
View
1 example/output/documentation.out
@@ -0,0 +1 @@
+slice("aaaa", 0)
View
2 example/output/empty.err
@@ -1 +1 @@
-empty.rb:14:in `<main>': rule(:empty) { ... } returns nil. Still not implemented, but already used? (NotImplementedError)
+example/empty.rb:13:in `<main>': rule(:empty) { ... } returns nil. Still not implemented, but already used? (NotImplementedError)
View
3 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'
View
3 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'
View
3 example/seasons.rb
@@ -1,5 +1,4 @@
-$: << 'lib'
-$: << '../lib'
+$:.unshift File.dirname(__FILE__) + "/../lib"
require 'parslet'
require 'pp'
View
3 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'
View
9 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)
View
46 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

0 comments on commit 5a9a753

Please sign in to comment.