Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Updated test

   * removed yield next test - this was obsolete
   * fixed bugs in markdown syntax
   * added test for meta api
   * fixed problems in thread scope test
   * Literate Maruku HTML files have a layout now
 * Version No 0.1.9 - a 0.2.0 preview release
 * updated manifest
 * let inner_class catch fetch method with blocks as well
 


git-svn-id: svn+ssh://rubyforge.org/var/svn/contextr/trunk@76 b232672f-569e-4225-9534-19f781cde581
  • Loading branch information...
commit eff7896d6ea0bf9d0a3f76008482b2f9debf6725 1 parent ca1977a
schmidt authored
View
4 Manifest.txt
@@ -41,7 +41,7 @@ test/test_introduction.mkd
test/test_introduction.rb
test/test_layer_state.mkd
test/test_layer_state.rb
+test/test_meta_api.mkd
+test/test_meta_api.rb
test/test_ordering.mkd
test/test_ordering.rb
-test/test_yield_next.mkd
-test/test_yield_next.rb
View
41 benchmark/contextr/bench.rb
@@ -26,7 +26,7 @@ def claimed
true
end
- module OneMethods
+ in_layer :once do
def once
true
end
@@ -35,20 +35,17 @@ def twice
end
end
- module TwoMethods
+ in_layer :two do
def twice
true
end
end
-
- include OneMethods => :one,
- TwoMethods => :two
end
f = Foo.new
n = 100_000
-Benchmark.bm(20) do |x|
+Benchmark.bmbm(20) do |x|
x.report("Ordinary:") {
n.times { f.ordinary }
}
@@ -75,24 +72,20 @@ def twice
end
__END__
-n = 1_000_000
+n = 100_000 (bmbm to warm up the jvm)
+
+ruby 1.8.6
user system total real
-Ordinary: 0.410000 0.000000 0.410000 ( 0.406774)
-Once (w/o): 6.080000 0.010000 6.090000 ( 6.148515)
-Once (ctx): 16.560000 0.020000 16.580000 ( 16.700489)
-Twice (w/o): 6.100000 0.010000 6.110000 ( 6.113543)
-Twice (ctx): 18.620000 0.010000 18.630000 ( 18.686153)
-Wrapped (w/o): 6.140000 0.010000 6.150000 ( 6.152476)
-Wrapped (ctx): 29.740000 0.030000 29.770000 ( 29.816371)
-All wrappers (ctx): 37.250000 0.040000 37.290000 ( 37.344437)
+Ordinary: 0.040000 0.000000 0.040000 ( 0.041294)
+Once (w/o): 1.030000 0.010000 1.040000 ( 1.043971)
+Once (ctx): 1.540000 0.000000 1.540000 ( 1.598743)
+Twice (w/o): 1.030000 0.010000 1.040000 ( 1.055684)
+Twice (ctx): 2.780000 0.010000 2.790000 ( 2.839568)
-n = 100_000
+jruby -O -J-server 1.0.2
user system total real
-Ordinary: 0.040000 0.000000 0.040000 ( 0.040648)
-Once (w/o): 0.610000 0.000000 0.610000 ( 0.613713)
-Once (ctx): 1.660000 0.000000 1.660000 ( 1.689277)
-Twice (w/o): 0.610000 0.000000 0.610000 ( 0.608093)
-Twice (ctx): 1.870000 0.010000 1.880000 ( 1.918604)
-Wrapped (w/o): 0.600000 0.000000 0.600000 ( 0.605630)
-Wrapped (ctx): 2.960000 0.010000 2.970000 ( 2.979563)
-All wrappers (ctx): 3.750000 0.000000 3.750000 ( 3.761805)
+Ordinary: 0.108000 0.000000 0.108000 ( 0.108000)
+Once (w/o): 2.336000 0.000000 2.336000 ( 2.335000)
+Once (ctx): 3.390000 0.000000 3.390000 ( 3.390000)
+Twice (w/o): 2.439000 0.000000 2.439000 ( 2.439000)
+Twice (ctx): 5.191000 0.000000 5.191000 ( 5.190000)
View
2  lib/contextr/inner_class.rb
@@ -40,7 +40,7 @@ def reveal(name)
instance_methods.each { |m| hide(m) }
- def method_missing(method_name, *rest_args, &block)
+ def method_missing(method_name, *rest_args)
yield(:next, *rest_args)
end
end
View
4 lib/contextr/version.rb
@@ -1,8 +1,8 @@
module ContextR #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
- MINOR = 2
- TINY = 0
+ MINOR = 1
+ TINY = 9
STRING = [MAJOR, MINOR, TINY].join('.')
end
View
102 test/lib/literate_maruku_test.rb
@@ -1,12 +1,108 @@
+gem "literate_maruku"
require "literate_maruku"
+require 'markaby'
+require 'active_support'
+
+class Fixnum
+ def ordinal
+ # teens
+ return 'th' if (10..19).include?(self % 100)
+ # others
+ case self % 10
+ when 1: return 'st'
+ when 2: return 'nd'
+ when 3: return 'rd'
+ else return 'th'
+ end
+ end
+end
+
+class Time
+ def pretty
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
+ end
+end
module LiterateMarukuTest
BASE_DIR = File.dirname(__FILE__) + "/../"
TARGET_DIR = File.dirname(__FILE__) + "/../../website/test/"
def self.load(file)
- LiterateMaruku.require(BASE_DIR + "#{File.basename(file, '.rb')}.mkd",
- :output => TARGET_DIR,
- :attributes => {:execute => true})
+ content = LiterateMaruku.require(
+ BASE_DIR + "#{File.basename(file, '.rb')}.mkd",
+ :inline => true,
+ :attributes => {:execute => true})
+
+ download = "http://rubyforge.org/projects/contextr"
+ version = ContextR::VERSION::STRING
+ modified = Time.now
+ sub_title = File.basename(file, '.rb').gsub("test_", "").titleize
+
+ doc = Markaby::Builder.new.xhtml_strict do
+ head do
+ title "ContextR - #{sub_title} - Documentation"
+ link :href => "../stylesheets/screen.css", :rel=>'stylesheet',
+ :type=>'text/css', :media => "screen"
+ script :src => "../javascripts/rounded_corners_lite.inc.js",
+ :type =>"text/javascript"
+ script %Q{
+ window.onload = function() {
+ settings = {
+ tl: { radius: 10 },
+ tr: { radius: 10 },
+ bl: { radius: 10 },
+ br: { radius: 10 },
+ antiAlias: true,
+ autoPad: true,
+ validTags: ["div"]
+ }
+ var versionBox = new curvyCorners(settings,
+ document.getElementById("version"));
+ versionBox.applyCornersToAll();
+ }
+ }, :type => "text/javascript"
+ end
+ body do
+ div.main! do
+ h1 sub_title
+ div.version! :class => "clickable",
+ :onclick => "document.location='#{download}'; return false" do
+ p "Get Version"
+ a version, :href => download, :class => "numbers"
+ end
+ h1 do
+ self << "&#x2192; &#8216;"
+ a "contextr", :href => "http://contextr.rubyforge.org/"
+ self << "&#8217;"
+ end
+
+ ul.navi! do
+ Dir[File.dirname(file) + "/test_*.mkd"].each do |mkd_file_name|
+ li do
+ name = File.basename(mkd_file_name, ".mkd").gsub("test_", "")
+ a name.titleize, :href => name + ".html"
+ end
+ end
+ end
+
+ self << content
+ p.coda do
+ text modified.pretty
+ br
+ text "Theme extended from "
+ a "Paul Battley", :href => "http://rb2js.rubyforge.org/"
+ end
+ end
+ end
+ end
+ File.open(TARGET_DIR +
+ "#{File.basename(file, '.rb').gsub("test_", "")}.html", "w") do |f|
+ f.puts(%q{<!DOCTYPE html
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">})
+ doc.to_s.each do |chunk|
+ f.puts(chunk)
+ end
+ end
end
end
View
51 test/test_dynamic_scope.mkd
@@ -6,11 +6,6 @@ I tried to improve it by defining a little helper method called `step`. It shall
test if all chunks are executed in the expected order and additionally check
if the expected layers are activated.
- def step(index, *layers)
- $step ||= 0
- assert_equal(index, $step += 1)
- assert_equal(layers, ContextR::active_layers)
- end
Layer activation shall be dynamically scoped. This is basically no problem, but
it gets messy when doing thread programming and switching between scopes. The
@@ -23,31 +18,43 @@ As you may see, leaving the inner block in step 5 results in the "lost" layer
the rest is support code.
example do
+ def step(index, *layers)
+ @step ||= 0
+ assert_equal(index, @step += 1) if index
+ assert_equal(layers, ContextR::active_layers)
+ end
+
mutex = Mutex.new
+ one_block = lambda do
+ mutex.lock
+ ContextR::with_layer :b do
+ step(3, :a, :b)
+ mutex.unlock
+ sleep(0.1)
+ mutex.lock
+ step(5, :a, :b)
+ end
+ end
+
+ two_block = lambda do
+ mutex.lock
+ step(4, :a)
+ mutex.unlock
+ end
+
step(1)
ContextR::with_layer :a do
step(2, :a)
- a = Thread.new do
- mutex.lock
- ContextR::with_layer :b do
- step(3, :a, :b)
- mutex.unlock
- sleep(0.1)
- mutex.lock
- step(5, :a, :b)
- end
- end
+ one = Thread.new(&one_block)
+ two = Thread.new(&two_block)
+
+ step(nil, :a)
- b = Thread.new do
- mutex.lock
- step(4, :a)
- mutex.unlock
- end
+ one.join
+ two.join
- a.join
- b.join
step(6, :a)
end
step(7)
View
32 test/test_introduction.mkd
@@ -29,10 +29,10 @@ This could mean print the university a student belongs to or attach the
address to the output.
Additonal methods
-=================
+-----------------
In a plain old Ruby project, this would result in additional methods,
-propably encapsulated in modules, that will be included into our classes.
+probably encapsulated in modules, that will be included into our classes.
This allows reuse and better encapsulation.
module AddressOutput
@@ -59,8 +59,8 @@ instead of to\_s if you would like to have additional information.
end
end
-The same for each student. #to\_s\_with\_unversity and
-#to\_s\_with\_university\_and\_address give as well additional output.
+The same for each student. #to\_s\_with\_unversity
+and #to\_s\_with\_university\_and\_address give as well additional output.
So how can you use it. Let's create some instances first.
@@ -69,13 +69,13 @@ So how can you use it. Let's create some instances first.
An now some output.
-**Note**: This could live inside an erb template, a graphical
-ui or printed to the command line. In all these cases to\_s is called
-automatically by the standard libary to receive a good representation of
+**Note**: This could live inside an erb template, a graphical user
+interface or printed to the command line. In all these cases to\_s is called
+automatically by the standard library to receive a good representation of
the object.
The output method defined in test\_helper.rb simulates this behaviour. All
examples are converted to test class automatically, so we can be sure, that
-this document stays in sync with the libary.
+this document stays in sync with the library.
puts $gregor # => prints "Gregor"
"#{$gregor}" # => evaluates to "Gregor"
@@ -92,14 +92,14 @@ Assume, we would like to print an address list now.
output_of($gregor.to_s_with_address) == "Gregor (Berlin)"
end
-If you want a list with university and addresses, you would use
-#to\_s\_with\_university\_and\_address. No automatic call to to\_s anymore. If
-you have your layout in an erb template, you have to change each and every
+If you want a list with university and addresses, you would
+use #to\_s\_with\_university\_and\_address. No automatic call to to\_s anymore.
+If you have your layout in an erb template, you have to change each and every
occurrence of your variables.
Redefining to\_s
-===============
+---------------
To solve this problem you could redefine to\_s on demand. I will demonstrate
this with some meta programming in a fresh class.
@@ -180,7 +180,7 @@ thread safe solution.
ContextR
-========
+--------
This is were context-oriented programming comes into play. I will again
start from the scratch. It is not much and we all know the problem space
@@ -311,7 +311,11 @@ deactivate layers again.
end
end
-These encaspulations may be as complex as your application. ContextR will
+ example do
+ assert_equal(["to_s"], Religion.in_layer(:location).instance_methods)
+ end
+
+These encapsulations may be as complex as your application. ContextR will
keep track of all activations and deactivations within the blocks and
restore the settings after the block was executed.
View
21 test/test_meta_api.mkd
@@ -0,0 +1,21 @@
+ContextR knows two basic reflection mechanisms. One is to query the currently
+active layers.
+
+ example do
+ ContextR::with_layer :a do
+ assert_equal([:a], ContextR::active_layers)
+ ContextR::with_layer :b do
+ assert_equal([:a, :b], ContextR::active_layers)
+ end
+ assert_equal([:a], ContextR::active_layers)
+ end
+ end
+
+The second is to query all layers, that where ever defined.
+
+ example do
+ assert(ContextR::layers.include?(:a))
+ assert(ContextR::layers.include?(:b))
+ end
+
+
View
2  test/test_yield_next.rb → test/test_meta_api.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + "/test_helper.rb"
-test_class(:TestYieldNext)
+test_class(:TestMetaApi)
LiterateMarukuTest.load(__FILE__)
View
6 test/test_ordering.mkd
@@ -8,7 +8,7 @@ this behaviour. Therefore I will use foo bar code. But I think it is still
easy to get the message.
I. Multiple active layers
-=========================
+-------------------------
Define the basis first.
@@ -101,7 +101,7 @@ activation is hidden, but is restored again after leaving the block.
II. Multiple modules per layer and class
-========================================
+----------------------------------------
It is also possible to have more than one module define the context-dependent
behaviour of a class. In this case it may also happen, that multiple modules
@@ -130,7 +130,7 @@ we include them into the same layer and see what happens.
This time the last inclusion defines the outermost method. But this is just Ruby's way of (multiple) inheritance and not part of ContextR. For the same reason a second inclusion of `FooMethod`s will not update the execution order.
III. Conclusion
-===============
+---------------
These should be all case where ordering matters. If you are aware of the
two basic rules, everything should work as expected.
View
7 test/test_yield_next.mkd
@@ -1,7 +0,0 @@
-This document will cover the API for context-dependent method defintions.
-Or in other words: what are you yield-ing all the time, who is :next and
-how can I pass arguments and fetch return values.
-
- example do
- assert true
- end
Please sign in to comment.
Something went wrong with that request. Please try again.