require 'pione/test-helper' describe 'Pione::Lang::DataExpr' do before do @env = TestHelper::Lang.env end it 'should match the same name' do exp = Lang::DataExpr.new('test.a') exp.should.match 'test.a' exp.should.not.match 'test_a' end it 'should not be regular expression' do exp = Lang::DataExpr.new('test-\d.a') exp.should.not.match 'test-1.a' end it 'should handle multi characters matcher (Case 1)' do exp = Lang::DataExpr.new('test-*.a') exp.should.not.match 'test-.a' { 'test-1.a'=> '1', 'test-2.a' => '2', 'test-3.a' => '3', 'test-A.a' => 'A', 'test-abc.a' => 'abc' }.each do |name, val| exp.should.match name exp.match(name)[1].should == val end exp.should.not.match 'test-1_a' exp.should.not.match '-1.a' exp.should.not.match 'test-1.ab' exp.should.not.match 'ttest-1.a' end it 'should handle multi characters matcher (Case 2)'do exp = Lang::DataExpr.new('test-*-*.a') exp.should.match 'test-1-2.a' exp.match('test-1-2.a')[1].should == '1' exp.match('test-1-2.a')[2].should == '2' exp.should.match 'test-abc-xyz.a' exp.match('test-abc-xyz.a')[1].should == 'abc' exp.match('test-abc-xyz.a')[2].should == 'xyz' exp.should.not.match 'test--.a' exp.should.not.match('test-1.a') exp.should.not.match('test-1-2.b') exp.should.not.match('test-1-2') exp.should.not.match('-1-2.a') exp.should.not.match 'test-1-2.ab' exp.should.not.match 'ttest-1-2.a' end it 'should handle single character matcher (Case 1)' do exp = Lang::DataExpr.new('test-?.a') exp.should.match 'test-1.a' exp.match('test-1.a')[1].should == '1' exp.should.match 'test-2.a' exp.match('test-2.a')[1].should == '2' exp.should.match 'test-3.a' exp.match('test-3.a')[1].should == '3' exp.should.match 'test-A.a' exp.match('test-A.a')[1].should == 'A' exp.should.not.match 'test-abc.a' exp.should.not.match 'test-.a' exp.should.not.match 'test-1_a' exp.should.not.match 'test-1.ab' exp.should.not.match 'ttest-1.a' end it 'should handle single character matcher (Case 2)' do exp = Lang::DataExpr.new('test-?-?.a') exp.should.match 'test-1-2.a' exp.match('test-1-2.a')[1].should == '1' exp.match('test-1-2.a')[2].should == '2' exp.should.not.match 'test--.a' exp.should.not.match 'test-abc-a.a' exp.should.not.match 'test-a-abc.a' exp.should.not.match 'test-1-2.ab' exp.should.not.match 'ttest-1-2.a' end it 'should handle character set matcher (Case 1)' do exp = Lang::DataExpr.new('test-[1-3].a') exp.should.not.match 'test-0.a' exp.should.match 'test-1.a' exp.should.match 'test-2.a' exp.should.match 'test-3.a' exp.should.not.match 'test-4.a' exp.should.not.match 'test-5.a' exp.should.not.match 'test-6.a' exp.should.not.match 'test-7.a' exp.should.not.match 'test-8.a' exp.should.not.match 'test-9.a' exp.should.not.match 'test-11.a' end it 'should handle character set matcher (Case 2)' do exp = Lang::DataExpr.new('test-[^1-3].a') exp.should.match 'test-0.a' exp.should.not.match 'test-1.a' exp.should.not.match 'test-2.a' exp.should.not.match 'test-3.a' exp.should.match 'test-4.a' exp.should.match 'test-5.a' exp.should.match 'test-6.a' exp.should.match 'test-7.a' exp.should.match 'test-8.a' exp.should.match 'test-9.a' exp.should.not.match 'test-11.a' end it 'should handle character set matcher (Case 3)' do exp = Lang::DataExpr.new('test-[!1-3].a') exp.should.match 'test-0.a' exp.should.not.match 'test-1.a' exp.should.not.match 'test-2.a' exp.should.not.match 'test-3.a' exp.should.match 'test-4.a' exp.should.match 'test-5.a' exp.should.match 'test-6.a' exp.should.match 'test-7.a' exp.should.match 'test-8.a' exp.should.match 'test-9.a' exp.should.not.match 'test-11.a' end it 'should handle select matcher' do exp = Lang::DataExpr.new('test-{abc,def,ghi}.a') exp.should.not.match 'test-a.a' exp.should.match 'test-abc.a' exp.should.match 'test-def.a' exp.should.match 'test-ghi.a' exp.should.not.match 'test-bcd.a' exp.should.not.match 'test-efg.a' exp.should.not.match 'test-abcd.a' end it 'should handle exceptions (Case 1)' do exp = TestHelper::Lang.expr!(@env, "'test-*.a'.except('test-2.a')") exp.should.match 'test-1.a' exp.should.not.match 'test-2.a' exp.should.match 'test-22.a' end it 'should handle exceptions (Case 2)' do exp = TestHelper::Lang.expr!(@env, "'*'.except('test-1.a' | 'test-2.a')") exp.should.not.match 'test-1.a' exp.should.not.match 'test-2.a' exp.should.match 'test-3.a' end it 'should handle exceptions (Case 3)' do exp = TestHelper::Lang.expr!(@env, "'test-1.a'.except('*')") exp.should.not.match 'test-1.a' end end describe "Lang::DataExprNull" do before do @null = Lang::DataExprNull.new end it "should not match any data" do @null.should.not.match('test.a') end end # # test cases # ymlpath = File.join(File.dirname(__FILE__), "data", "data-expr_match.yml") testcases = YAML.load_file(ymlpath) env = TestHelper::Lang.env describe "Lang::DataExprSequence" do testcases.each do |title, cases| describe title do cases.each do |expr, testcase| data_expr = TestHelper::Lang.expr!(env, expr) testcase['match'].map do |name| it "#{expr} should match #{name}" do data_expr.eval(env).should.match(name) end end if testcase['match'] testcase['unmatch'].map do |name| it "#{expr} should unmatch #{name}" do data_expr.eval(env).should.not.match(name) end end if testcase['unmatch'] end end end TestHelper::Lang.test_pione_method(__FILE__) end