Skip to content
This repository
tree: 277a8289ff
Fetching contributors…

Cannot retrieve contributors at this time

file 291 lines (250 sloc) 9.605 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
require 'spec_helper'

module CharacterClassSpec
  class Foo < Treetop::Runtime::SyntaxNode
  end

  describe "a character class followed by a node class declaration and a block" do

    testing_expression "[A-Z] <CharacterClassSpec::Foo> { def a_method; end }"

    it "matches single characters within that range, returning instances of the declared node class that respond to the method defined in the inline module" do
      result = parse('A')
      result.should be_an_instance_of(Foo)
      result.should respond_to(:a_method)
      result = parse('N')
      result.should be_an_instance_of(Foo)
      result.should respond_to(:a_method)
      result = parse('Z')
      result.should be_an_instance_of(Foo)
      result.should respond_to(:a_method)
    end

    it "does not match single characters outside of that range" do
      parse('8').should be_nil
      parse('a').should be_nil
    end

    it "matches a single character within that range at index 1" do
      parse(' A', :index => 1).should_not be_nil
    end

    it "fails to match a single character out of that range at index 1" do
      parse(' 1', :index => 1).should be_nil
    end
  end

  module ModFoo
  end

  describe "a character class followed by a node module declaration and a block" do

    testing_expression "[A-Z] <CharacterClassSpec::ModFoo> { def a_method; end }"

    it "matches single characters within that range, returning instances of SyntaxNode extended by the specified module" do
      result = parse('A')
      result.should be_an_instance_of(Treetop::Runtime::SyntaxNode)
      result.should be_a_kind_of(ModFoo)
      result.should respond_to(:a_method)
      result = parse('N')
      result.should be_an_instance_of(Treetop::Runtime::SyntaxNode)
      result.should be_a_kind_of(ModFoo)
      result.should respond_to(:a_method)
      result = parse('Z')
      result.should be_an_instance_of(Treetop::Runtime::SyntaxNode)
      result.should be_a_kind_of(ModFoo)
      result.should respond_to(:a_method)
    end

    it "does not match single characters outside of that range" do
      parse('8').should be_nil
      parse('a').should be_nil
    end

    it "matches a single character within that range at index 1" do
      parse(' A', :index => 1).should_not be_nil
    end

    it "fails to match a single character out of that range at index 1" do
      parse(' 1', :index => 1).should be_nil
    end
  end

  describe "a character class with a POSIX bracket expression" do
    testing_expression "[[:digit:]]"
    it "matches a single character within the class" do
      parse('1').should_not be_nil
    end
    it "does not match a single character outside the class" do
      parse('a').should be_nil
      parse('-').should be_nil
    end
    testing_expression "[[:digit:][:space:]]+"
    it "matches a string with a mix of two character classes" do
      parse('1 4 9').should_not be_nil
    end
  end

  describe "a character class followed by a node class declaration and a block" do

    testing_expression "[A-Z] <CharacterClassSpec::Foo>"
    
    it "actively generates nodes for the character when it is the primary node" do
      result = parse('A')
      result.should be_a(Treetop::Runtime::SyntaxNode)
      result.elements.should be_nil
    end
    
  end

  describe "A character class containing quotes" do
    testing_expression "[\"']"

    it "matches a quote" do
      parse("'").should_not be_nil
    end

    it "matches a double-quote" do
      parse('"').should_not be_nil
    end
  end

  describe "A character class containing a special character" do
    testing_expression "[\t]"
    it "matches that character only" do
      parse("\t").should_not be_nil
      parse('t').should be_nil
    end
  end

  describe "A character class containing an escaped backslash" do
    slash = "\\" # Make it explicit that there are *two* backslashes here
    testing_expression "[#{slash}#{slash}]"
    it "matches a backslash only" do
      parse("\\").should_not be_nil
      parse('t').should be_nil
    end
  end

  describe "A character class containing a hex escape" do
    slash = "\\"
    testing_expression "[#{slash}x41]"
    it "matches that character only" do
      parse('A').should_not be_nil
      parse('\\').should be_nil
      parse('x').should be_nil
      parse('4').should be_nil
      parse('1').should be_nil
    end
  end

  describe "A character class containing an octal escape" do
    slash = "\\"
    testing_expression "[#{slash}101]"
    it "matches that character only" do
      parse('A').should_not be_nil
      parse('\\').should be_nil
      parse('1').should be_nil
      parse('0').should be_nil
    end
  end

  describe "A character class containing a \\c control-char escape" do
    slash = "\\"
    testing_expression "[#{slash}cC]"
    it "matches that character only" do
      parse("\003").should_not be_nil
      parse('\\').should be_nil
      parse('c').should be_nil
      parse('C').should be_nil
    end
  end

  describe "A character class containing a \\C- control-char escape" do
    slash = "\\"
    testing_expression "[#{slash}C-C]"
    it "matches that character only" do
      parse("\003").should_not be_nil
      parse('\\').should be_nil
      parse('C').should be_nil
      parse('-').should be_nil
    end
  end

  if RUBY_VERSION =~ /\A1\.8\./
    describe "A character class containing a \\M- meta-char escape" do
      slash = "\\"
      testing_expression "[#{slash}M- ]"
      it "matches that character only" do
        parse("\240").should_not be_nil
        parse('\\').should be_nil
        parse('M').should be_nil
        parse('-').should be_nil
        parse(' ').should be_nil
      end
    end
  end

  describe "A character class containing an escaped non-special character" do
    slash = "\\"
    testing_expression "[#{slash}y]"
    it "matches that character only" do
      parse("y").should_not be_nil
      parse('\\').should be_nil
    end
  end

  describe "A character class containing an \#{...} insertion" do
    testing_expression "[\#{raise 'error'}]"
    it "doesn't evaluate the insertion" do
      x = true
      lambda{
x = parse("y")
      }.should_not raise_error
      x.should be_nil
      parse('#').should_not be_nil
      parse("'").should_not be_nil
      parse("0").should be_nil
    end
  end
  
  describe "a character class" do
    testing_expression "[A-Z]"
    it "actively generates a node for the character because it is the primary node" do
      result = parse('A')
      result.should be_a(Treetop::Runtime::SyntaxNode)
      result.elements.should be_nil
    end
  end
  
  describe "a character class mixed with other expressions" do
    testing_expression '[A-Z] "a"'
    it "lazily instantiates a node for the character" do
      result = parse('Aa')
      result.instance_variable_get("@elements").should include(true)
      result.elements.should_not include(true)
      result.elements.size.should == 2
    end
  end
  
  describe "a character class with a node class declaration mixed with other expressions" do
    testing_expression '([A-Z] <CharacterClassSpec::Foo>) "a"'
    it "actively generates a node for the character because it has a node class declared" do
      result = parse('Aa')
      result.instance_variable_get("@elements").should_not include(true)
      result.elements.should_not include(true)
      result.elements.size.should == 2
    end
  end
  
  describe "a character class with a node module declaration mixed with other expressions" do
    testing_expression '([A-Z] <CharacterClassSpec::ModFoo>) "a"'
    it "actively generates a node for the character because it has a node module declared" do
      result = parse('Aa')
      result.instance_variable_get("@elements").should_not include(true)
      result.elements.should_not include(true)
      result.elements.size.should == 2
    end
  end
  
  describe "a character class with an inline block mixed with other expressions" do
    testing_expression '([A-Z] { def a_method; end }) "a"'
    it "actively generates a node for the character because it has an inline block" do
      result = parse('Aa')
      result.instance_variable_get("@elements").should_not include(true)
      result.elements.should_not include(true)
      result.elements.size.should == 2
    end
  end
  
  describe "a character class with a label mixed with other expressions" do
    testing_expression 'upper:([A-Z]) "b"'
    it "returns the correct element for the labeled expression" do
      result = parse('Ab')
      result.upper.text_value.should == "A"
      result.elements.size.should == 2
    end
  end
  
  describe "a character class repetition mixed with other expressions" do
    testing_expression '[A-Z]+ "a"'
    it "lazily instantiates a node for the character" do
      result = parse('ABCa')
      result.elements[0].instance_variable_get("@elements").should include(true)
      result.elements[0].elements.should_not include(true)
      result.elements[0].elements.size.should == 3
      result.elements.size.should == 2
      result.elements.inspect.should == %Q{[SyntaxNode offset=0, "ABC":\n SyntaxNode offset=0, "A"\n SyntaxNode offset=1, "B"\n SyntaxNode offset=2, "C", SyntaxNode offset=3, "a"]}
    end
  end
  
  describe "a character class that gets cached because of a choice" do
    testing_expression "[A-Z] 'a' / [A-Z]"
    
    it "generates a node for the lazily-instantiated character when it is the primary node" do
      result = parse('A')
      result.should be_a(Treetop::Runtime::SyntaxNode)
      result.elements.should be_nil
    end
  end

end
Something went wrong with that request. Please try again.