spec/piglet_spec.rb in piglet-0.2.4 vs spec/piglet_spec.rb in piglet-0.2.5

- old
+ new

@@ -1,5 +1,7 @@ +# encoding: utf-8 + require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe Piglet do @@ -102,10 +104,41 @@ @interpreter.interpret { explain } @interpreter.to_pig_latin.should match(/EXPLAIN;/) end end end + + context 'parameter declarations:' do + %w(declare default).each do |op| + describe "%#{op}" do + it "outputs a %#{op} statement" do + @interpreter.interpret { self.send(op, :my_var, 'my_value') } + @interpreter.to_pig_latin.should match(/%#{op} my_var 'my_value'/) + end + + it "outputs a %#{op} statement with single quotes escaped" do + @interpreter.interpret { self.send(op, :my_var, "my 'value'") } + @interpreter.to_pig_latin.should match(/%#{op} my_var 'my \\'value\\''/) + end + + it "outputs a %#{op} statement with an numeric value unquoted" do + @interpreter.interpret { self.send(op, :my_var, 1) } + @interpreter.to_pig_latin.should match(/%#{op} my_var 1/) + end + + it "outputs a %#{op} statement with an symbol value quoted" do + @interpreter.interpret { self.send(op, :my_var, :x) } + @interpreter.to_pig_latin.should match(/%#{op} my_var 'x'/) + end + + it "outputs a %#{op} statement with the value quoted in backticks, if the option :backticks => true is passed" do + @interpreter.interpret { self.send(op, :my_var, 'cut -f 4', :backticks => true) } + @interpreter.to_pig_latin.should match(/%#{op} my_var `cut -f 4`/) + end + end + end + end context 'relation operators:' do describe 'GROUP' do it 'outputs a GROUP statement with one grouping field' do @interpreter.interpret { store(load('in').group(:a), 'out') } @@ -476,9 +509,19 @@ :ship => 'to/here', :cache => ['first', 'second', 'third'] ) end output.should include('DEFINE plunk `plunk.rb` INPUT(\'some/path\' USING PigStorage, stdin USING HelloWorld(\'test\')) OUTPUT(\'some/other/path\' USING BinStorage, stdout USING SomeOtherMechanism()) SHIP(\'to/here\') CACHE(\'first\', \'second\', \'third\')') + end + + it 'makes the defined UDF available as a method in the interpreter scope, so that it can be used in a FOREACH and it\'s result renamed using AS' do + output = @interpreter.to_pig_latin do + define('my_udf', :function => 'com.example.My') + a = load('in') + b = a.foreach { |r| [my_udf('foo', 3, 'hello \'world\'', r[0]).as(:bar)]} + store(b, 'out') + end + output.should match(/FOREACH \w+ GENERATE my_udf\('foo', 3, 'hello \\'world\\'', \$0\) AS bar/) end end describe 'REGISTER' do it 'outputs a REGISTER statement with the path to the specified JAR' do