summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2017-12-20 09:22:15 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2017-12-20 09:22:15 -0800
commit3628243bc0af1eb19f1844c7e22da6aa6d46e074 (patch)
tree5777c44e8b32e93c0c97875c9b2d5933a3d9106e /test
parent39b1e826d2e505fb35022fd9bf4f3a6dc0ee5702 (diff)
downloadpsych-3628243bc0af1eb19f1844c7e22da6aa6d46e074.zip
Add a predicate method to each node
This allows the AST to be searched via a predicate method rather than hardcoding the class name and doing is_a? checks. For example, rather than: ``` ast.grep(Psych::Nodes::Scalar).each do |node| # .. do something end ``` Now you can do: ``` ast.find_all(&:scalar?).each do |node| # .. do something end ``` Your code no longer needs to know the exact class used in the AST.
Diffstat (limited to 'test')
-rw-r--r--test/psych/test_stream.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/test/psych/test_stream.rb b/test/psych/test_stream.rb
index 3bd557c..9b71c6d 100644
--- a/test/psych/test_stream.rb
+++ b/test/psych/test_stream.rb
@@ -3,6 +3,22 @@ require_relative 'helper'
module Psych
class TestStream < TestCase
+ [
+ [Psych::Nodes::Alias, :alias?],
+ [Psych::Nodes::Document, :document?],
+ [Psych::Nodes::Mapping, :mapping?],
+ [Psych::Nodes::Scalar, :scalar?],
+ [Psych::Nodes::Sequence, :sequence?],
+ [Psych::Nodes::Stream, :stream?],
+ ].each do |klass, block|
+ define_method :"test_predicate_#{block}" do
+ rb = Psych.parse_stream("---\n- foo: bar\n- &a !!str Anchored\n- *a")
+ nodes = rb.grep(klass)
+ assert_operator nodes.length, :>, 0
+ assert_equal nodes, rb.find_all(&block)
+ end
+ end
+
def test_parse_partial
rb = Psych.parse("--- foo\n...\n--- `").to_ruby
assert_equal 'foo', rb