Permalink
Browse files

use Jumpstart.doc_to_spec

  • Loading branch information...
1 parent 8a5b0d0 commit b682e26e6e6ed327c9bcb9d36f7db934eb6ce8fe @quix committed Aug 6, 2009
Showing with 47 additions and 31 deletions.
  1. +25 −0 README.rdoc
  2. +22 −31 spec/readme_spec.rb
View
@@ -68,6 +68,27 @@ The pseudo-keyword `fun' is provided for defining a pure function
whose name or arguments are not known at compile time. See
Pure::PurePrivate::SingletonFeatures.
+===== Dynamic Example
+
+ require 'pure'
+ include Pure
+
+ stats = pure do
+ files = Dir["*"]
+
+ files.each { |file|
+ fun file do
+ File.size(file)
+ end
+ }
+
+ fun :total_size => files do |*sizes|
+ sizes.inject(0) { |acc, size| acc + size }
+ end
+ end
+
+ puts stats.compute(:total_size, 3) # => 39355
+
== Parsing engines
In order to find the names of method arguments, Pure will attempt to
@@ -95,10 +116,14 @@ threads will be executed.
For second point above, consider:
+===== Sexp Example
+
require 'pp'
require 'pure'
include Pure
+ Pure.engine = :ruby_parser
+
adder = pure do
def add(left, right)
left + right
View
@@ -1,35 +1,26 @@
-require 'pathname'
-require 'tempfile'
+require File.dirname(__FILE__) + "/common"
-here = Pathname(__FILE__).dirname
-require here + "common"
-root = here + ".."
-readme = root + "README.rdoc"
-lib = root + "lib"
+$LOAD_PATH.unshift File.dirname(__FILE__) + "/../devel"
+require "jumpstart"
-describe readme do
- $LOAD_PATH.unshift root + "devel"
- require "jumpstart/ruby"
- ["Synopsis"].each { |section|
- describe section do
- it "should run as claimed" do
- contents = readme.read
-
- code = %{
- $LOAD_PATH.unshift "#{lib.expand_path}"
- require 'rubygems'
- } + contents.match(%r!== #{section}.*?\n(.*?)^\S!m)[1]
-
- expected = code.scan(%r!\# => (.*?)\n!).flatten.join("\n")
-
- Tempfile.open("pure-readme") { |file|
- file.puts code
- file.close
- result = `"#{Jumpstart::Ruby::EXECUTABLE}" "#{file.path}"`
- raise unless $?.exitstatus
- result.chomp.should == expected
- }
- end
+readme = "README.rdoc"
+
+Jumpstart.doc_to_spec(readme, "Synopsis")
+
+Jumpstart.doc_to_spec(readme, "Dynamic Example") { |expected, actual|
+ # check for numbers, not values
+ [actual, expected].each { |expr|
+ unless expr =~ %r!\A\d+\Z!
+ raise "readme failed"
end
}
-end
+ [nil, nil]
+}
+
+Jumpstart.doc_to_spec(readme, "Sexp Example") { |*expressions|
+ expressions.map { |expresssion|
+ result = eval(expresssion, TOPLEVEL_BINDING)
+ result[:add].merge!(:file => nil, :line => nil)
+ result
+ }
+}

0 comments on commit b682e26

Please sign in to comment.