# Author:: Marco Tessari . # Copyright:: Copyright (c) 2004 TTK Team. All rights reserved. # License:: Ruby license. # $LastChangedBy: polrop $ # $Id: yregexpath.rb 159 2005-02-18 12:07:23Z polrop $ module YAML class YRegexPath attr_reader :segments, :root, :wanted_node_index def initialize ( str ) raise ArgumentError, 'Argument must be a string' unless str.is_a?(String) @segments = [] @wanted_node_index = nil @root = (str[0] == ?/) while str =~ /^(\/|#)?((?:(?:\\.)|[^\\=\/#])+)(?:=((?:(?:\\.)|[^\\\/#])*))?/ if ($1 == "#") if (@wanted_node_index.nil?) @wanted_node_index = @segments.length else raise ArgumentError, 'More than a "#" given.' end end if ($3.nil?) @segments << Regexp.new($2) else @segments << [ Regexp.new($2), Regexp.new($3) ] end str = $' end @wanted_node_index = @segments.length if @wanted_node_index.nil? end end end # module YAML if defined? TEST_MODE or $0 == __FILE__ require 'test/unit' class YRegexPathTest < Test::Unit::TestCase def compare(str, tab) tab.collect! do |e| if (e.is_a?(Array)) e.collect! { |s| Regexp.new(s) unless s.nil? } else Regexp.new(e) end end desc = "Compare #{str} with #{tab}." assert_equal(tab, YAML::YRegexPath.new(str).segments, desc) end def testit compare('mho', [ 'mho' ]) compare('mho\\', [ 'mho' ]) compare('/mho', [ 'mho' ]) compare('/mho=42', [ [ 'mho', '42'] ]) compare('/mho=[0-9]+', [ [ 'mho', '[0-9]+' ] ]) compare('m\/ho', [ 'm\/ho' ]) compare('mho/foo', [ 'mho', 'foo' ]) compare('mho=35/foo=test', [ [ 'mho' , '35' ], [ 'foo' , 'test' ] ]) compare('test#status=PASS', [ 'test' , [ 'status' , 'PASS' ] ]) compare('mho#', [ 'mho' ]) end def test_errors # Build with something else than a String. assert_raise(ArgumentError) { YAML::YRegexPath.new(42) } # Two '#'. assert_raise(ArgumentError) { YAML::YRegexPath.new('1#2#3') } end end end