/capybara/ruby/1.9.1/gems/treetop-1.4.10/spec/compiler/character_class_spec.rb

https://github.com/vartikasingh/BackchannelApplication-1 · Ruby · 276 lines · 257 code · 19 blank · 0 comment · 1 complexity · 2c35af43b92de6597633691a6df6fd05 MD5 · raw file

  1. require 'spec_helper'
  2. module CharacterClassSpec
  3. class Foo < Treetop::Runtime::SyntaxNode
  4. end
  5. describe "a character class followed by a node class declaration and a block" do
  6. testing_expression "[A-Z] <CharacterClassSpec::Foo> { def a_method; end }"
  7. 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
  8. result = parse('A')
  9. result.should be_an_instance_of(Foo)
  10. result.should respond_to(:a_method)
  11. result = parse('N')
  12. result.should be_an_instance_of(Foo)
  13. result.should respond_to(:a_method)
  14. result = parse('Z')
  15. result.should be_an_instance_of(Foo)
  16. result.should respond_to(:a_method)
  17. end
  18. it "does not match single characters outside of that range" do
  19. parse('8').should be_nil
  20. parse('a').should be_nil
  21. end
  22. it "matches a single character within that range at index 1" do
  23. parse(' A', :index => 1).should_not be_nil
  24. end
  25. it "fails to match a single character out of that range at index 1" do
  26. parse(' 1', :index => 1).should be_nil
  27. end
  28. end
  29. module ModFoo
  30. end
  31. describe "a character class followed by a node module declaration and a block" do
  32. testing_expression "[A-Z] <CharacterClassSpec::ModFoo> { def a_method; end }"
  33. it "matches single characters within that range, returning instances of SyntaxNode extended by the specified module" do
  34. result = parse('A')
  35. result.should be_an_instance_of(Treetop::Runtime::SyntaxNode)
  36. result.should be_a_kind_of(ModFoo)
  37. result.should respond_to(:a_method)
  38. result = parse('N')
  39. result.should be_an_instance_of(Treetop::Runtime::SyntaxNode)
  40. result.should be_a_kind_of(ModFoo)
  41. result.should respond_to(:a_method)
  42. result = parse('Z')
  43. result.should be_an_instance_of(Treetop::Runtime::SyntaxNode)
  44. result.should be_a_kind_of(ModFoo)
  45. result.should respond_to(:a_method)
  46. end
  47. it "does not match single characters outside of that range" do
  48. parse('8').should be_nil
  49. parse('a').should be_nil
  50. end
  51. it "matches a single character within that range at index 1" do
  52. parse(' A', :index => 1).should_not be_nil
  53. end
  54. it "fails to match a single character out of that range at index 1" do
  55. parse(' 1', :index => 1).should be_nil
  56. end
  57. end
  58. describe "a character class followed by a node class declaration and a block" do
  59. testing_expression "[A-Z] <CharacterClassSpec::Foo>"
  60. it "actively generates nodes for the character when it is the primary node" do
  61. result = parse('A')
  62. result.should be_a(Treetop::Runtime::SyntaxNode)
  63. result.elements.should be_nil
  64. end
  65. end
  66. describe "A character class containing quotes" do
  67. testing_expression "[\"']"
  68. it "matches a quote" do
  69. parse("'").should_not be_nil
  70. end
  71. it "matches a double-quote" do
  72. parse('"').should_not be_nil
  73. end
  74. end
  75. describe "A character class containing a special character" do
  76. testing_expression "[\t]"
  77. it "matches that character only" do
  78. parse("\t").should_not be_nil
  79. parse('t').should be_nil
  80. end
  81. end
  82. describe "A character class containing an escaped backslash" do
  83. slash = "\\" # Make it explicit that there are *two* backslashes here
  84. testing_expression "[#{slash}#{slash}]"
  85. it "matches a backslash only" do
  86. parse("\\").should_not be_nil
  87. parse('t').should be_nil
  88. end
  89. end
  90. describe "A character class containing a hex escape" do
  91. slash = "\\"
  92. testing_expression "[#{slash}x41]"
  93. it "matches that character only" do
  94. parse('A').should_not be_nil
  95. parse('\\').should be_nil
  96. parse('x').should be_nil
  97. parse('4').should be_nil
  98. parse('1').should be_nil
  99. end
  100. end
  101. describe "A character class containing an octal escape" do
  102. slash = "\\"
  103. testing_expression "[#{slash}101]"
  104. it "matches that character only" do
  105. parse('A').should_not be_nil
  106. parse('\\').should be_nil
  107. parse('1').should be_nil
  108. parse('0').should be_nil
  109. end
  110. end
  111. describe "A character class containing a \\c control-char escape" do
  112. slash = "\\"
  113. testing_expression "[#{slash}cC]"
  114. it "matches that character only" do
  115. parse("\003").should_not be_nil
  116. parse('\\').should be_nil
  117. parse('c').should be_nil
  118. parse('C').should be_nil
  119. end
  120. end
  121. describe "A character class containing a \\C- control-char escape" do
  122. slash = "\\"
  123. testing_expression "[#{slash}C-C]"
  124. it "matches that character only" do
  125. parse("\003").should_not be_nil
  126. parse('\\').should be_nil
  127. parse('C').should be_nil
  128. parse('-').should be_nil
  129. end
  130. end
  131. if RUBY_VERSION =~ /\A1\.8\./
  132. describe "A character class containing a \\M- meta-char escape" do
  133. slash = "\\"
  134. testing_expression "[#{slash}M- ]"
  135. it "matches that character only" do
  136. parse("\240").should_not be_nil
  137. parse('\\').should be_nil
  138. parse('M').should be_nil
  139. parse('-').should be_nil
  140. parse(' ').should be_nil
  141. end
  142. end
  143. end
  144. describe "A character class containing an escaped non-special character" do
  145. slash = "\\"
  146. testing_expression "[#{slash}y]"
  147. it "matches that character only" do
  148. parse("y").should_not be_nil
  149. parse('\\').should be_nil
  150. end
  151. end
  152. describe "A character class containing an \#{...} insertion" do
  153. testing_expression "[\#{raise 'error'}]"
  154. it "doesn't evaluate the insertion" do
  155. x = true
  156. lambda{
  157. x = parse("y")
  158. }.should_not raise_error
  159. x.should be_nil
  160. parse('#').should_not be_nil
  161. parse("'").should_not be_nil
  162. parse("0").should be_nil
  163. end
  164. end
  165. describe "a character class" do
  166. testing_expression "[A-Z]"
  167. it "actively generates a node for the character because it is the primary node" do
  168. result = parse('A')
  169. result.should be_a(Treetop::Runtime::SyntaxNode)
  170. result.elements.should be_nil
  171. end
  172. end
  173. describe "a character class mixed with other expressions" do
  174. testing_expression '[A-Z] "a"'
  175. it "lazily instantiates a node for the character" do
  176. result = parse('Aa')
  177. result.instance_variable_get("@elements").should include(true)
  178. result.elements.should_not include(true)
  179. result.elements.size.should == 2
  180. end
  181. end
  182. describe "a character class with a node class declaration mixed with other expressions" do
  183. testing_expression '([A-Z] <CharacterClassSpec::Foo>) "a"'
  184. it "actively generates a node for the character because it has a node class declared" do
  185. result = parse('Aa')
  186. result.instance_variable_get("@elements").should_not include(true)
  187. result.elements.should_not include(true)
  188. result.elements.size.should == 2
  189. end
  190. end
  191. describe "a character class with a node module declaration mixed with other expressions" do
  192. testing_expression '([A-Z] <CharacterClassSpec::ModFoo>) "a"'
  193. it "actively generates a node for the character because it has a node module declared" do
  194. result = parse('Aa')
  195. result.instance_variable_get("@elements").should_not include(true)
  196. result.elements.should_not include(true)
  197. result.elements.size.should == 2
  198. end
  199. end
  200. describe "a character class with an inline block mixed with other expressions" do
  201. testing_expression '([A-Z] { def a_method; end }) "a"'
  202. it "actively generates a node for the character because it has an inline block" do
  203. result = parse('Aa')
  204. result.instance_variable_get("@elements").should_not include(true)
  205. result.elements.should_not include(true)
  206. result.elements.size.should == 2
  207. end
  208. end
  209. describe "a character class with a label mixed with other expressions" do
  210. testing_expression 'upper:([A-Z]) "b"'
  211. it "returns the correct element for the labeled expression" do
  212. result = parse('Ab')
  213. result.upper.text_value.should == "A"
  214. result.elements.size.should == 2
  215. end
  216. end
  217. describe "a character class repetition mixed with other expressions" do
  218. testing_expression '[A-Z]+ "a"'
  219. it "lazily instantiates a node for the character" do
  220. result = parse('ABCa')
  221. result.elements[0].instance_variable_get("@elements").should include(true)
  222. result.elements[0].elements.should_not include(true)
  223. result.elements[0].elements.size.should == 3
  224. result.elements.size.should == 2
  225. 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"]}
  226. end
  227. end
  228. describe "a character class that gets cached because of a choice" do
  229. testing_expression "[A-Z] 'a' / [A-Z]"
  230. it "generates a node for the lazily-instantiated character when it is the primary node" do
  231. result = parse('A')
  232. result.should be_a(Treetop::Runtime::SyntaxNode)
  233. result.elements.should be_nil
  234. end
  235. end
  236. end