Skip to content
This repository
Browse code

Merge branch 'flexible_conditionals'

Conflicts:
	lib/object.fy
  • Loading branch information...
commit a1250ad58085ad59cc75a67d418b69bdec02ee27 2 parents 82d45dd + 18dbea2
Christopher Bertels authored April 28, 2011
6  bin/ifancy
@@ -58,14 +58,14 @@ load_history
58 58
 try {
59 59
   bnd = binding()
60 60
 
61  
-  { Readline readline(">> ", true) } while_do: |line| {
  61
+  while: { Readline readline(">> ", true) } do: |line| {
62 62
     HISTORY << line
63  
-    double_or_empty?: line . if_true: {
  63
+    if: (double_or_empty?: line) then: {
64 64
       Readline::HISTORY pop()
65 65
       HISTORY pop()
66 66
     }
67 67
 
68  
-    line =~ /^\s*$/ if_false: {
  68
+    unless: (line =~ /^\s*$/) do: {
69 69
       try {
70 70
         Fancy eval: line binding: bnd . inspect println
71 71
       } catch Exception => e {
2  examples/echo.fy
... ...
@@ -1,7 +1,7 @@
1 1
 # echo.fy
2 2
 # Outputs contents of files
3 3
 
4  
-ARGV[1] if_do: |filename| {
  4
+ARGV[1] if_true: |filename| {
5 5
  try {
6 6
     File open: filename modes: ['read] with: |f| {
7 7
       until: { f eof? } do: {
4  examples/game_of_life.fy
@@ -72,7 +72,7 @@ class World {
72 72
   def was_alive?: pos {
73 73
     "Indicates, if a cell ([row,column]) was alive in the last generation."
74 74
 
75  
-    @last_alive[pos[0]] if_do: |row| {
  75
+    @last_alive[pos[0]] if_true: |row| {
76 76
       row[pos[1]] == 1
77 77
     }
78 78
   }
@@ -115,7 +115,7 @@ class World {
115 115
     column = pos[1]
116 116
 
117 117
     neighbors = @offsets map: |o| {
118  
-      @matrix[row + (o[0])] if_do: |r| {
  118
+      @matrix[row + (o[0])] if_true: |r| {
119 119
         r[column + (o[1])]
120 120
       }
121 121
     }
4  lib/argv.fy
... ...
@@ -1,9 +1,9 @@
1 1
 def ARGV for_option: op_name do: block {
2 2
   "Runs a given block if an option is in ARGV."
3 3
 
4  
-  ARGV index: op_name . if_do: |idx| {
  4
+  ARGV index: op_name . if_true: |idx| {
5 5
     if: (block argcount > 0) then: {
6  
-      ARGV[idx + 1] if_do: |arg| {
  6
+      ARGV[idx + 1] if_true: |arg| {
7 7
         block call: [arg]
8 8
         ARGV remove_at: idx
9 9
         ARGV remove_at: idx
2  lib/block.fy
@@ -36,7 +36,7 @@ class Block {
36 36
 
37 37
   def while_true: work {
38 38
     {
39  
-      call if_do: |val| {
  39
+      call if_true: |val| {
40 40
         work call: [val]
41 41
       } else: {
42 42
         break
3  lib/boot.fy
@@ -25,6 +25,7 @@ require: "array"
25 25
 require: "tuple"
26 26
 require: "block"
27 27
 require: "iteration"
  28
+require: "enumerator"
28 29
 require: "file"
29 30
 require: "directory"
30 31
 require: "fancy_spec"
@@ -46,4 +47,4 @@ require: "argv"
46 47
 
47 48
 require: "documentation"
48 49
 
49  
-require: "package.fy"
  50
+require: "package.fy"
2  lib/compiler/ast/class_def.fy
@@ -18,7 +18,7 @@ class Fancy AST {
18 18
     def bytecode: g {
19 19
       pos(g)
20 20
       docstring = body() body() shift_docstring
21  
-      docstring if_do: {
  21
+      docstring if_true: {
22 22
         setdoc = MessageSend new: @line \
23 23
                              message: (Identifier from: "for:append:" line: @line) \
24 24
                              to: (Identifier from: "Fancy::Documentation" line: @line) \
4  lib/compiler/ast/identifier.fy
@@ -25,10 +25,10 @@ class Fancy AST {
25 25
     }
26 26
 
27 27
     def method_name: receiver ruby_send: ruby (false) {
28  
-      ruby || @ruby_ident if_do: {
  28
+      ruby || @ruby_ident if_true: {
29 29
         @string to_sym()
30 30
       } else: {
31  
-        @string =~ /:$/ . if_do: {
  31
+        @string =~ /:$/ . if_true: {
32 32
           @string to_sym()
33 33
         } else: {
34 34
           ":" + @string . to_sym()
4  lib/compiler/ast/method_def.fy
@@ -27,10 +27,10 @@ class Fancy AST {
27 27
       g send(@access, 0)
28 28
       g pop()
29 29
 
30  
-      @name to_s =~ /^initialize:(\S)+/ if_do: {
  30
+      @name to_s =~ /^initialize:(\S)+/ if_true: {
31 31
         define_constructor_class_method: g
32 32
       }
33  
-      @name to_s =~ /^unknown_message:with_params:$/ if_do: {
  33
+      @name to_s =~ /^unknown_message:with_params:$/ if_true: {
34 34
         define_method_missing: g
35 35
       }
36 36
 
2  lib/documentation.fy
@@ -63,7 +63,7 @@ class Fancy Documentation {
63 63
       If obj has no documentation, one is created for it.
64 64
     """
65 65
     doc = for: obj
66  
-    doc if_do: {
  66
+    if: doc then: {
67 67
       doc docs << docstring
68 68
     } else: {
69 69
       doc  = for: obj is: docstring
2  lib/enumerable.fy
@@ -77,7 +77,7 @@ class FancyEnumerable {
77 77
     """
78 78
 
79 79
     each: |x| {
80  
-      block call: [x] . if_do: |item| {
  80
+      if: (block call: [x]) then: |item| {
81 81
         return item
82 82
       }
83 83
     }
77  lib/enumerator.fy
... ...
@@ -0,0 +1,77 @@
  1
+class FancyEnumerator {
  2
+  def initialize: @object {
  3
+    @iterator = 'each:
  4
+    rewind
  5
+  }
  6
+
  7
+  def initialize: @object with: @iterator {
  8
+    rewind
  9
+  }
  10
+
  11
+  def next {
  12
+    if: @peeked then: {
  13
+      @peeked = false
  14
+      @peek
  15
+    } else: {
  16
+      result = @fiber resume
  17
+      if: (@fiber alive?) then: {
  18
+        return result
  19
+      } else: {
  20
+        (Fancy StopIteration new: result) raise!
  21
+      }
  22
+    }
  23
+  }
  24
+
  25
+  def peek {
  26
+    """
  27
+    Returns the next object in the FancyEnumerator, but doesn't move the
  28
+    internal position forward.
  29
+    When the position reaches the end, a Fancy StopIteration exception is
  30
+    raised.
  31
+
  32
+    a = [1,2,3]
  33
+    e = a to_enum
  34
+    e next p #=> 1
  35
+    e peek p #=> 2
  36
+    e peek p #=> 2
  37
+    e peek p #=> 2
  38
+    e next p #=> 2
  39
+    e next p #=> 3
  40
+    e next p #=> raises Fancy StopIteration
  41
+    """
  42
+
  43
+    unless: @peeked do: {
  44
+      @peeked = true
  45
+      @peek = @fiber resume
  46
+      if: (@fiber alive?) then: {
  47
+        return @peek
  48
+      } else: {
  49
+        (Fancy StopIteration new: result) raise!
  50
+      }
  51
+    }
  52
+
  53
+    return @peek
  54
+  }
  55
+
  56
+  def rewind {
  57
+    @peeked = false
  58
+    @peek = nil
  59
+
  60
+    @fiber = Fiber new: {
  61
+      param = |element| { yield: element }
  62
+      @object send_message: @iterator with_params: [param]
  63
+    }
  64
+  }
  65
+
  66
+  def with: object each: block {
  67
+    loop: {
  68
+      try {
  69
+        block call: [next, object]
  70
+      } catch (Fancy StopIteration) => ex {
  71
+        return object
  72
+      }
  73
+    }
  74
+
  75
+    return object
  76
+  }
  77
+}
28  lib/false_class.fy
... ...
@@ -1,9 +1,10 @@
1 1
 class FalseClass {
2 2
   "FalseClass. The class of the singleton @false value."
3 3
 
4  
-  def if_true: then_block else: else_block {
5  
-    "Calls @else_block."
6  
-    else_block call
  4
+  def FalseClass new {
  5
+    # always return false singleton object when trying to create a new
  6
+    # FalseClass instance
  7
+    false
7 8
   }
8 9
 
9 10
   def if_true: block {
@@ -11,19 +12,17 @@ class FalseClass {
11 12
     nil
12 13
   }
13 14
 
14  
-  def if_false: block {
15  
-    "Calls @block."
16  
-    block call
  15
+  def if_true: then_block else: else_block {
  16
+    "Calls @else_block."
  17
+    else_block call
17 18
   }
18 19
 
19  
-  def if_nil: block {
20  
-    "Calls @block."
21  
-    nil
  20
+  def if_false: block {
  21
+    block call: [self]
22 22
   }
23 23
 
24  
-  def nil? {
25  
-    "Returns @false."
26  
-    false
  24
+  def if_false: then_block else: else_block {
  25
+    then_block call: [self]
27 26
   }
28 27
 
29 28
   def false? {
@@ -31,11 +30,6 @@ class FalseClass {
31 30
     true
32 31
   }
33 32
 
34  
-  def true? {
35  
-    "Returns @false."
36  
-    false
37  
-  }
38  
-
39 33
   def to_s {
40 34
     "Returns @false as a @String@."
41 35
     "false"
2  lib/fancy_spec.fy
@@ -29,7 +29,7 @@ class FancySpec {
29 29
   def it: spec_info_string for: method_name when: spec_block {
30 30
     test = SpecTest new: spec_info_string block: spec_block
31 31
     # try {
32  
-    #   @test_obj method: method_name . if_do: |method| {
  32
+    #   @test_obj method: method_name . if_true: |method| {
33 33
     #     method tests << test
34 34
     #   }
35 35
     # } catch MethodNotFoundError => e {
4  lib/fdoc.fy
@@ -284,8 +284,8 @@ class Fancy FDoc {
284 284
              method = method sub(/^:/, "")
285 285
            }
286 286
            sigil = ""
287  
-           name =~ (Regexp.new("^#")) . if_do: { sigil = "<small>#</small>" }
288  
-           type = n[1] include?(":") . if_do: {
  287
+           name =~ (Regexp.new("^#")) . if_true: { sigil = "<small>#</small>" }
  288
+           type = n[1] include?(":") . if_true: {
289 289
              if: (sigil == "") then: {
290 290
                "singleton-method-ref"
291 291
              } else: {
49  lib/iteration.fy
@@ -8,4 +8,53 @@ class Fancy {
8 8
     read_slots: ['return_value]
9 9
     def initialize: @return_value {}
10 10
   }
  11
+
  12
+  class StopIteration : StdError {
  13
+    """
  14
+    Raised to stop the iteration, in particular by Enumerator#next.
  15
+    It is rescued by Block#loop.
  16
+
  17
+    Example:
  18
+      {
  19
+        'Hello println
  20
+        StopIteration new raise!
  21
+        'World println
  22
+      } loop
  23
+      'Done! println
  24
+
  25
+    Produces:
  26
+
  27
+      Hello
  28
+      Done!
  29
+    """
  30
+
  31
+    def initialize { @result = nil }
  32
+    def initialize: @result { }
  33
+
  34
+    def result {
  35
+      """
  36
+      Returns the return value of the iterator.
  37
+
  38
+        o = Object new
  39
+        def o each: block {
  40
+          block call: 1
  41
+          block call: 2
  42
+          block call: 3
  43
+          100
  44
+        }
  45
+
  46
+        e = o to_enum
  47
+        e next p #=> 1
  48
+        e next p #=> 2
  49
+        e next p #=> 3
  50
+        try {
  51
+          e next
  52
+        } catch Fancy StopIteration => ex {
  53
+          ex result p #=> 100
  54
+        }
  55
+      """
  56
+
  57
+      @result
  58
+    }
  59
+  }
11 60
 }
8  lib/main.fy
@@ -39,7 +39,7 @@ ARGV for_option: "-e" do: |eval_string| {
39 39
 }
40 40
 
41 41
 ARGV for_option: "-c" do: {
42  
-  ARGV index: "-c" . if_do: |idx| {
  42
+  ARGV index: "-c" . if_true: |idx| {
43 43
     ARGV[[idx + 1, -1]] each: |filename| {
44 44
       "Compiling " ++ filename println
45 45
       Fancy Compiler compile_file: filename to: nil line: 1 print: false
@@ -49,7 +49,7 @@ ARGV for_option: "-c" do: {
49 49
 }
50 50
 
51 51
 ARGV for_option: "-cv" do: {
52  
-  ARGV index: "-cv" . if_do: |idx| {
  52
+  ARGV index: "-cv" . if_true: |idx| {
53 53
     ARGV[[idx + 1, -1]] each: |filename| {
54 54
       "Compiling " ++ filename println
55 55
       Fancy Compiler compile_file: filename to: nil line: 1 print: true
@@ -77,7 +77,7 @@ ARGV for_option: "list-packages" do: {
77 77
 Fancy Package add_to_loadpath
78 78
 
79 79
 # Load a source file, if any given:
80  
-ARGV first if_do: |file| {
  80
+ARGV first if_true: |file| {
81 81
   try {
82 82
     Fancy CodeLoader load_compiled_file: file
83 83
   } catch Fancy Parser ParseError => e {
@@ -85,6 +85,6 @@ ARGV first if_do: |file| {
85 85
   }
86 86
 }
87 87
 
88  
-ARGV empty? if_do: {
  88
+ARGV empty? if_true: {
89 89
   require: "../bin/ifancy"
90 90
 }
26  lib/nil_class.fy
@@ -7,41 +7,29 @@ class NilClass {
7 7
     nil
8 8
   }
9 9
 
10  
-  def if_true: then_block else: else_block {
11  
-    "Calls @else_block."
12  
-    else_block call
13  
-  }
14  
-
15 10
   def if_true: block {
16 11
     "Returns @nil."
17 12
     nil
18 13
   }
19 14
 
20  
-  def if_false: block {
21  
-    "Calls @block."
22  
-    block call
  15
+  def if_true: then_block else: else_block {
  16
+    "Calls @else_block."
  17
+    else_block call
23 18
   }
24 19
 
25 20
   def if_nil: block {
26  
-    "Calls @block."
27  
-    block call
  21
+    block call: [self]
28 22
   }
29 23
 
30  
-  def nil? {
31  
-    "Returns @true."
32  
-    true
  24
+  def if_nil: then_block else: else_block {
  25
+    then_block call: [self]
33 26
   }
34 27
 
35  
-  def false? {
  28
+  def nil? {
36 29
     "Returns @true."
37 30
     true
38 31
   }
39 32
 
40  
-  def true? {
41  
-    "Returns @nil."
42  
-    false
43  
-  }
44  
-
45 33
   def to_s {
46 34
     "Returns an empty @String@."
47 35
     ""
90  lib/object.fy
@@ -35,50 +35,49 @@ class Object {
35 35
     self == other not
36 36
   }
37 37
 
38  
-  def if_false: block {
39  
-    "Calls the block."
40  
-    nil
  38
+  def if_true: block {
  39
+    "Calls the @block if @true? returns @true"
  40
+    block call: [self]
41 41
   }
42 42
 
43  
-  def if_nil: block {
44  
-    "Returns nil."
45  
-    nil
  43
+  def if_true: then_block else: else_block {
  44
+    "Calls the @then_block if @true? returns @true - otherwise @else_block is called"
  45
+    then_block call: [self]
46 46
   }
47 47
 
48  
-  def nil? {
49  
-    "Returns nil."
  48
+  def if_false: block {
  49
+    "Calls the @block if @false? returns @true@"
50 50
     nil
51 51
   }
52 52
 
53  
-  def false? {
54  
-    "Returns nil."
55  
-    nil
  53
+  def if_false: then_block else: else_block {
  54
+    "Calls the @then_block if @false? returns @true - otherwise @else_block is called"
  55
+    else_block call
56 56
   }
57 57
 
58  
-  def true? {
59  
-    "Returns nil."
  58
+  def if_nil: block {
  59
+    "Calls the @block if @nil? returns @true@"
60 60
     nil
61 61
   }
62 62
 
63  
-  def if_do: block {
64  
-    "If the object is non-nil, it calls the given block with itself as argument."
  63
+  def if_nil: then_block else: else_block {
  64
+    "Calls the @then_block if @nil? returns @true - otherwise @else_block is called"
  65
+    else_block call
  66
+  }
65 67
 
66  
-    match self {
67  
-      case nil -> nil
68  
-      case false -> nil
69  
-      case _ -> block call: [self]
70  
-    }
  68
+  def nil? {
  69
+    "Returns @false."
  70
+    false
71 71
   }
72 72
 
73  
-  def if_do: then_block else: else_block {
74  
-    """If the object is non-nil, it calls the given then_block with itself as argument.
75  
-       Otherwise it calls the given else_block."""
  73
+  def false? {
  74
+    "Returns @false."
  75
+    false
  76
+  }
76 77
 
77  
-    match self {
78  
-      case nil -> else_block call: [self]
79  
-      case false -> else_block call: [self]
80  
-      case _ -> then_block call: [self]
81  
-    }
  78
+  def true? {
  79
+    "Returns @false."
  80
+    false
82 81
   }
83 82
 
84 83
   def or_take: other {
@@ -103,13 +102,21 @@ class Object {
103 102
     0
104 103
   }
105 104
 
  105
+  def to_enum {
  106
+    FancyEnumerator new: self
  107
+  }
  108
+
  109
+  def to_enum: iterator {
  110
+    FancyEnumerator new: self with: iterator
  111
+  }
  112
+
106 113
   def and: other {
107 114
     """
108 115
     Boolean conjunction.
109 116
     Returns @other if @self and @other are true-ish, otherwise @false.
110 117
     """
111 118
 
112  
-    if: self then: {
  119
+    if_true: {
113 120
       { other = other call } if: (other is_a?: Block)
114 121
       return other
115 122
     }
@@ -121,6 +128,19 @@ class Object {
121 128
     Boolean disjunction.
122 129
     Returns true if either @self or other is true, otherwise nil.
123 130
     """
  131
+    if_true: {
  132
+      return self
  133
+    } else: {
  134
+      { other = other call } if: (other is_a?: Block)
  135
+      return other
  136
+    }
  137
+  }
  138
+
  139
+  def or: other {
  140
+    """
  141
+    Boolean disjunction.
  142
+    Returns true if either @self or other is true, otherwise nil.
  143
+    """
124 144
     unless: self do: {
125 145
       { other = other call } if: (other is_a?: Block)
126 146
       return other
@@ -134,17 +154,17 @@ class Object {
134 154
   def if: cond then: block {
135 155
     """
136 156
     Same as:
137  
-        cond if_do: block
  157
+        cond if_true: block
138 158
     """
139  
-    cond if_do: block
  159
+    cond if_true: block
140 160
   }
141 161
 
142 162
   def if: cond then: then_block else: else_block {
143 163
     """
144 164
     Same as:
145  
-        cond if_do: then_block else: else_block
  165
+        cond if_true: then_block else: else_block
146 166
     """
147  
-    cond if_do: then_block else: else_block
  167
+    cond if_true: then_block else: else_block
148 168
   }
149 169
 
150 170
   def while: cond_block do: body_block {
@@ -168,10 +188,10 @@ class Object {
168 188
   def unless: cond do: block {
169 189
     """
170 190
     Same as:
171  
-      cond if_do: { nil } else: block
  191
+      cond if_true: { nil } else: block
172 192
     """
173 193
 
174  
-    cond if_do: { nil } else: block
  194
+    cond if_true: { nil } else: block
175 195
   }
176 196
 
177 197
   def method: method_name {
2  lib/package/installer.fy
@@ -168,7 +168,7 @@ class Fancy Package {
168 168
       If no @.fancypack file is found, raise an error.
169 169
       """
170 170
 
171  
-      Dir glob(installed_path ++ "/*.fancypack") first if_do: |fpackfile| {
  171
+      Dir glob(installed_path ++ "/*.fancypack") first if_true: |fpackfile| {
172 172
         require: fpackfile
173 173
       }
174 174
 
4  lib/parser/methods.fy
@@ -147,7 +147,7 @@ class Fancy {
147 147
     }
148 148
 
149 149
     def ast: line assign: rvalue to: lvalue many: many (false) {
150  
-      ast = many if_do: { AST MultipleAssignment } else: { AST Assignment }
  150
+      ast = many if_true: { AST MultipleAssignment } else: { AST Assignment }
151 151
       ast new: line var: lvalue value: rvalue
152 152
     }
153 153
 
@@ -175,7 +175,7 @@ class Fancy {
175 175
     }
176 176
 
177 177
     def ast: line send: message to: receiver (AST Self new: line) ruby: ruby (nil) {
178  
-      args = ruby if_do: {
  178
+      args = ruby if_true: {
179 179
         unless: receiver do: {
180 180
           receiver = AST Self new: line
181 181
         }
2  lib/string.fy
@@ -25,7 +25,7 @@ class String {
25 25
   def blank? {
26 26
     "Indicates, if a String consists only of whitespace."
27 27
 
28  
-    self =~ /^\s*$/ if_do: {
  28
+    self =~ /^\s*$/ if_true: {
29 29
       true
30 30
     } else: {
31 31
       false
31  lib/true_class.fy
@@ -7,34 +7,29 @@ class TrueClass {
7 7
     true
8 8
   }
9 9
 
10  
-  def if_true: then_block else: else_block {
11  
-    "Calls @then_block."
12  
-    then_block call
13  
-  }
14  
-
15 10
   def if_true: block {
16 11
     "Calls @block."
17  
-    block call
  12
+    block call: [self]
18 13
   }
19 14
 
20  
-  def if_false: block {
21  
-    "Returns @nil."
22  
-    nil
  15
+  def if_true: then_block else: else_block {
  16
+    "Calls @then_block."
  17
+    then_block call: [self]
23 18
   }
24 19
 
25  
-  def nil? {
26  
-    "Returns @false."
27  
-    false
  20
+  def true? {
  21
+    "Returns @true."
  22
+    true
28 23
   }
29 24
 
30  
-  def false? {
31  
-    "Returns @false."
32  
-    false
  25
+  def to_s {
  26
+    "Returns @true as a @String@."
  27
+    "true"
33 28
   }
34 29
 
35  
-  def true? {
36  
-    "Returns @true."
37  
-    true
  30
+  def to_a {
  31
+    "Returns an empty @Array@."
  32
+    []
38 33
   }
39 34
 
40 35
   def not {
40  tests/control_flow.fy
@@ -43,40 +43,64 @@ FancySpec describe: "Control Flow" with: {
43 43
     }
44 44
   }
45 45
 
46  
-  it: "should only call the block if it's a true-ish value" for: 'if_do: when: {
47  
-    1 if_do: |num| {
  46
+  it: "should only call the block if it's a true-ish value" for: 'if_true: when: {
  47
+    1 if_true: |num| {
48 48
       num * 10
49 49
     } . should == 10
50 50
 
51  
-    nil if_do: {
  51
+    nil if_true: {
52 52
       "nope"
53 53
     } . should == nil
54 54
 
55  
-    false if_do: {
  55
+    false if_true: {
56 56
       "nope again"
57 57
     } . should == nil
58 58
   }
59 59
 
60  
-  it: "should call the then_block if it's a true-ish value and call the else_block otherwise" for: 'if_do:else: when: {
61  
-    1 if_do: |num| {
  60
+  it: "should call the then_block if it's a true-ish value and call the else_block otherwise" for: 'if_true:else: when: {
  61
+    1 if_true: |num| {
62 62
       num * 10
63 63
     } else: {
64 64
       nil
65 65
     } . should == 10
66 66
 
67  
-    nil if_do: {
  67
+    nil if_true: {
68 68
       "nope"
69 69
     } else: {
70 70
       "yup"
71 71
     } . should == "yup"
72 72
 
73  
-    false if_do: {
  73
+    false if_true: {
74 74
       "nope again"
75 75
     } else: {
76 76
       "yup again"
77 77
     } . should == "yup again"
78 78
   }
79 79
 
  80
+  it: "should be possible to override the if_true:else: method and work accordingly in conditionals" when: {
  81
+    class AClasThatIsLikeFalse {
  82
+      def if_true: block else: another_block {
  83
+        another_block call
  84
+      }
  85
+    }
  86
+
  87
+    obj = AClasThatIsLikeFalse new
  88
+    if: obj then: {
  89
+      'fail
  90
+    } else: {
  91
+      'success
  92
+    } . should == 'success
  93
+
  94
+    # let's get rid of this custom if_true:else: method
  95
+    AClasThatIsLikeFalse undefine_method: 'if_true:else:
  96
+
  97
+    if: obj then: {
  98
+      'now_this_is_success
  99
+    } else: {
  100
+      'fail
  101
+    } . should == 'now_this_is_success
  102
+  }
  103
+
80 104
   it: "should break from an iteration" for: 'break when: {
81 105
     x = 0
82 106
     until: { x == 10 } do: {
85  tests/enumerator.fy
... ...
@@ -0,0 +1,85 @@
  1
+FancySpec describe: FancyEnumerator with: {
  2
+  it: "iterates with 'next" for: 'new: when: {
  3
+    enum = FancyEnumerator new: (42..50)
  4
+    enum next should == 42
  5
+    enum next should == 43
  6
+  }
  7
+
  8
+  it: "peeks to find next element" for: 'peek when: {
  9
+    enum = FancyEnumerator new: (42..50)
  10
+    enum peek should == 42
  11
+    enum peek should == 42
  12
+    enum next should == 42
  13
+
  14
+    enum peek should == 43
  15
+    enum peek should == 43
  16
+    enum next should == 43
  17
+  }
  18
+
  19
+  it: "turns an object with 'each: into an FancyEnumerator" for: 'to_enum when: {
  20
+    enum = (42..45) to_enum
  21
+    enum next should == 42
  22
+  }
  23
+
  24
+  it: "turns an object with given method into an FancyEnumerator" for: 'to_enum: when: {
  25
+    o = {}
  26
+    def o iter: block {
  27
+      1 upto: 10 do: block
  28
+    }
  29
+    enum = o to_enum: 'iter:
  30
+    enum next should == 1
  31
+  }
  32
+
  33
+  it: "rewinds to the beginning of the iteration" for: 'rewind when: {
  34
+    enum = (42..45) to_enum
  35
+    check = {
  36
+      enum peek should == 42
  37
+      enum next should == 42
  38
+      enum peek should == 43
  39
+      enum next should == 43
  40
+    }
  41
+
  42
+    check call
  43
+    enum rewind
  44
+    check call
  45
+  }
  46
+
  47
+  it: "raises Fancy StopIteration when out of values" for: 'next when: {
  48
+    o = Object new
  49
+    def o each: block { block call: [1] }
  50
+    e = o to_enum
  51
+
  52
+    e next should == 1
  53
+    try {
  54
+      e next
  55
+      "We should not reach this line" should == true
  56
+    } catch (Fancy StopIteration) => ex {
  57
+      ex result should == nil
  58
+    }
  59
+  }
  60
+
  61
+  it: "sets result on Fancy StopIteration when out of values" for: 'next when: {
  62
+    o = Object new
  63
+    def o each: block {
  64
+      block call: [1]
  65
+      42
  66
+    }
  67
+    e = o to_enum
  68
+
  69
+    e next should == 1
  70
+    try {
  71
+      e next
  72
+      "We should not reach this line" should == true
  73
+    } catch (Fancy StopIteration) => ex {
  74
+      ex result should == 42
  75
+    }
  76
+  }
  77
+
  78
+  it: "iterates with an object" for: 'with:each: when: {
  79
+    enum = (42..45) to_enum
  80
+    result = enum with: [] each: |val, obj| { obj << val }
  81
+    result should == [42, 43, 44, 45]
  82
+  }
  83
+}
  84
+
  85
+# => [:each, :each_with_index, :each_with_object, :with_index, :with_object, :next_values, :peek_values, :next, :peek, :feed, :rewind, :inspect]
6  tests/nil_class.fy
@@ -40,8 +40,8 @@ FancySpec describe: NilClass with: {
40 40
     nil if_true: { 'then } . should == nil
41 41
   }
42 42
 
43  
-  it: "should call the block" for: 'if_false: when: {
44  
-    nil if_false: { 'false } . should == 'false
  43
+  it: "should not call the block" for: 'if_false: when: {
  44
+    nil if_false: { 'false } . should == nil
45 45
   }
46 46
 
47 47
   it: "should be nil" for: 'nil? when: {
@@ -49,7 +49,7 @@ FancySpec describe: NilClass with: {
49 49
   }
50 50
 
51 51
   it: "should be false" for: 'false? when: {
52  
-    nil false? should == true
  52
+    nil false? should == false
53 53
   }
54 54
 
55 55
   it: "should NOT be true" for: 'true? when: {
14  tests/object.fy
@@ -104,19 +104,19 @@ FancySpec describe: Object with: {
104 104
   # end boolean messages
105 105
 
106 106
   it: "should NOT be nil for non-nil values" for: 'nil? when: {
107  
-    'foo nil? should == nil
108  
-    1 nil? should == nil
109  
-    "hello" nil? should == nil
  107
+    'foo nil? should == false
  108
+    1 nil? should == false
  109
+    "hello" nil? should == false
110 110
   }
111 111
 
112 112
   it: "should NOT be false for non-nil values" for: 'false? when: {
113  
-    'foo false? should == nil
114  
-    "hello, world" false? should == nil
  113
+    'foo false? should == false
  114
+    "hello, world" false? should == false
115 115
   }
116 116
 
117 117
   it: "should not be true" for: 'true? when: {
118  
-    'foo true? should == nil
119  
-    "hello, world" true? should == nil
  118
+    'foo true? should == false
  119
+    "hello, world" true? should == false
120 120
   }
121 121
 
122 122
   it: "should return the correct value" for: 'returning:do: when: {

0 notes on commit a1250ad

Please sign in to comment.
Something went wrong with that request. Please try again.