Permalink
Browse files

generated koans

  • Loading branch information...
1 parent ba9ff94 commit 452f4e1eff78afec9005db7737815985e393f50c @sathish316 sathish316 committed Aug 28, 2011
View
@@ -0,0 +1,12 @@
+#!/usr/bin/env ruby
+# -*- ruby -*-
+
+require 'rake/clean'
+require 'rake/testtask'
+
+task :default => :test
+
+task :test do
+ ruby 'path_to_enlightenment.rb'
+end
+
View
@@ -0,0 +1,62 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutBinding < EdgeCase::Koan
+
+ class Foo
+ def initialize
+ @ivar = 22
+ end
+
+ def bar(param)
+ lvar = 11
+ binding
+ end
+ end
+
+ def test_binding_binds_method_parameters
+ binding = Foo.new.bar(99)
+ assert_equal __, eval("param", binding)
+ end
+
+ def test_binding_binds_local_vars
+ binding = Foo.new.bar(99)
+ assert_equal __, eval("lvar", binding)
+ end
+
+ def test_binding_binds_instance_vars
+ binding = Foo.new.bar(99)
+ assert_equal __, eval("@ivar", binding)
+ end
+
+ def test_binding_binds_blocks
+ binding = Foo.new.bar(99) { 33 }
+ assert_equal __, eval("yield", binding)
+ end
+
+ def test_binding_binds_self
+ foo = Foo.new
+ binding = foo.bar(99)
+ assert_equal __, eval("self", binding)
+ end
+
+ def n_times(n)
+ lambda {|value| n * value}
+ end
+
+ def test_lambda_binds_to_the_surrounding_context
+ two_times = n_times(2)
+ assert_equal __, two_times.call(3)
+ end
+
+ def count_with_increment(start, inc)
+ lambda { start += inc}
+ end
+
+ def test_lambda_remembers_state_of_bound_variables
+ counter = count_with_increment(7, 3)
+ assert_equal __, counter.call
+ assert_equal __, counter.call
+ assert_equal __, counter.call
+ end
+
+end
View
@@ -0,0 +1,60 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutBlocks < EdgeCase::Koan
+
+ def test_calling_a_lambda
+ l = lambda {|a| a + 1}
+ assert_equal __, l.call(99)
+ end
+
+ def test_calling_a_proc
+ p = Proc.new {|a| a + 1}
+ assert_equal __, p.call(99)
+ end
+
+ def convert(&block)
+ block
+ end
+
+ def test_block_is_proc
+ b = convert {|a| a + 1}
+ assert_equal __, b.class
+ assert_equal __, b.call(99)
+ end
+
+ def test_proc_takes_fewer_or_more_arguments
+ p = Proc.new {|a, b, c| a.to_i + b.to_i + c.to_i}
+ assert_equal __, p.call(1,2)
+ assert_equal __, p.call(1,2,3,4)
+ end
+
+ def test_lambda_does_not_take_fewer_or_more_arguments
+ l = lambda {|a, b, c| a.to_i + b.to_i + c.to_i}
+ assert_raises(ArgumentError) do
+ l.call(1, 2)
+ end
+
+ assert_raises(ArgumentError) do
+ l.call(1,2,3,4)
+ end
+ end
+
+ def method(lambda_or_proc)
+ lambda_or_proc.call
+ :from_method
+ end
+
+ def test_return_inside_lambda_returns_from_the_lambda
+ l = lambda { return :from_lambda }
+ result = method(l)
+ assert_equal __, result
+ end
+
+ def test_return_inside_proc_returns_from_the_context
+ p = Proc.new { return :from_proc }
+ result = method(p)
+ # The execution never reaches this line because Proc returns
+ # outside the test method
+ assert_equal __, p.call
+ end
+end
@@ -0,0 +1,53 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutClassAsConstant < EdgeCase::Koan
+
+ class Foo
+ def say_hello
+ "Hi"
+ end
+ end
+
+ def test_defined_tells_if_a_class_is_defined_or_not
+ assert_not_nil defined?(Foo)
+ assert_nil defined?(Bar)
+ end
+
+ def test_class_is_a_constant
+ assert_equal __, defined?(Foo)
+ end
+
+ def test_class_constant_can_be_assigned_to_var
+ my_class = Foo
+ assert_equal __, my_class.new.say_hello
+ end
+
+ @@return_value_of_class =
+ class Baz
+ def say_hi
+ "Hello"
+ end
+ 99
+ end
+
+ def test_class_definitions_are_active
+ assert_equal __, @@return_value_of_class
+ end
+
+ @@self_inside_a_class =
+ class Baz
+ def say_hi
+ "Hi"
+ end
+ self
+ end
+
+ def test_self_inside_class_is_class_itself
+ assert_equal __, @@self_inside_a_class
+ end
+
+ def test_class_is_an_object_of_type_class_and_can_be_created_dynamically
+ cls = Class.new
+ assert_match /__/, cls.to_s
+ end
+end
@@ -0,0 +1,27 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutClassInheritance < EdgeCase::Koan
+
+ def test_singleton_class_can_be_used_to_define_singleton_methods
+ animal = "cat"
+ class << animal
+ def speak
+ "miaow"
+ end
+ end
+ assert_equal __, animal.speak
+ end
+
+ class Foo
+ class << self
+ def say_hello
+ "Hello"
+ end
+ end
+ end
+
+ def test_singleton_class_can_be_used_to_define_class_methods
+ assert_equal __, Foo.say_hello
+ end
+
+end
@@ -0,0 +1,23 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutClassMethods < EdgeCase::Koan
+
+ class Foo
+ def self.say_hello
+ "Hello"
+ end
+ end
+
+ def test_class_is_an_instance_of_class_Class
+ assert_equal __, Foo.class == Class
+ end
+
+ def test_class_methods_are_just_singleton_methods_on_the_class
+ assert_equal __, Foo.say_hello
+ end
+
+ def test_classes_are_not_special_and_are_just_like_other_objects
+ assert_equal __, Foo.is_a?(Object)
+ assert_equal __, Foo.superclass == Object
+ end
+end
@@ -0,0 +1,87 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutDefineMethod < EdgeCase::Koan
+
+ class Example
+ def start
+ def stop
+ :stopped
+ end
+ :started
+ end
+ end
+
+ def test_methods_can_define_other_methods
+ o = Example.new
+ assert_raises(NoMethodError) do
+ o.stop
+ end
+
+ o.start
+
+ assert_equal __, o.stop
+ end
+
+ class Example
+ def foo
+ def foo
+ :new_value
+ end
+ :first_value
+ end
+ end
+
+ def test_methods_can_redefine_themselves
+ o = Example.new
+ assert_equal __, o.foo
+ assert_equal __, o.foo
+ end
+
+ class Multiplier
+ def self.create_multiplier(n)
+ define_method "times_#{n}" do |val|
+ val * n
+ end
+ end
+
+ 10.times {|i| create_multiplier(i) }
+ end
+
+ def test_define_method_creates_methods_dynamically
+ m = Multiplier.new
+ assert_equal __, m.times_3(10)
+ assert_equal __, m.times_6(10)
+ assert_equal __, m.times_9(10)
+ end
+
+ module Accessor
+ def my_writer(name)
+ ivar_name = "@#{name}"
+ define_method "#{name}=" do |value|
+ #Write code here to set value of ivar
+ instance_variable_set(ivar_name, value)
+ end
+ end
+
+ def my_reader(name)
+ ivar_name = "@#{name}"
+ define_method name do
+ #Write code here to get value of ivar
+ instance_variable_get(ivar_name)
+ end
+ end
+ end
+
+ class Cat
+ extend Accessor
+ my_writer :name
+ my_reader :name
+ end
+
+ def test_instance_variable_set_and_instance_variable_get_can_be_used_to_access_ivars
+ cat = Cat.new
+ cat.name = 'Fred'
+ assert_equal __, cat.name
+ end
+end
+
Oops, something went wrong.

0 comments on commit 452f4e1

Please sign in to comment.