spec/sql/emitter_spec.rb in ronin-sql-1.0.0 vs spec/sql/emitter_spec.rb in ronin-sql-1.1.0

- old
+ new

@@ -72,23 +72,23 @@ end end end describe "#emit_operator" do - context "when the operator is upper-case alphabetic text" do + context "when the operator is a symbol" do + it "should emit a String" do + subject.emit_operator(:"!=").should == '!=' + end + end + + context "otherwise" do subject { described_class.new(case: :lower) } it "should emit a keyword" do subject.emit_operator(:AS).should == 'as' end end - - context "otherwise" do - it "should emit a String" do - subject.emit_operator(:"!=").should == '!=' - end - end end describe "#emit_null" do it "should emit the NULL keyword" do subject.emit_null.should == 'NULL' @@ -132,13 +132,26 @@ end end end describe "#emit_field" do - it "should emit a String" do - subject.emit_field(:id).should == 'id' + subject { described_class.new(case: :upper) } + + let(:field) { SQL::Field.new(:id) } + + it "should emit the name as a keyword" do + subject.emit_field(field).should == 'ID' end + + context "when the field has a parent" do + let(:parent) { SQL::Field.new(:users) } + let(:field) { SQL::Field.new(:id,parent) } + + it "should emit the parent then the field name" do + subject.emit_field(field).should == 'USERS.ID' + end + end end describe "#emit_list" do it "should emit a ',' separated list" do subject.emit_list([1,2,3,'foo']).should == "(1,2,3,'foo')" @@ -151,19 +164,37 @@ it "should emit a list of column names and values" do subject.emit_assignments(values).should == 'x=1,y=2' end end + describe "#emit_argument" do + context "when the value is a Statement" do + let(:stmt) { SQL::Statement.new(:SELECT,1) } + + it "should wrap the statement in ( )" do + subject.emit_argument(stmt).should == '(SELECT 1)' + end + end + + context "otherwise" do + let(:value) { 'hello' } + + it "should emit the value" do + subject.emit_argument(value).should == "'hello'" + end + end + end + describe "#emit_expression" do context "when the expression is a BinaryExpr" do context "when the operator is alphabetic" do subject { described_class.new(case: :upper) } let(:expr) { SQL::BinaryExpr.new(:id,:is,1) } it "should emit the operands and operator as a keyword with spaces" do - subject.emit_expression(expr).should == 'id IS 1' + subject.emit_expression(expr).should == 'ID IS 1' end end context "when the operator is symbolic" do let(:expr) { SQL::BinaryExpr.new(:id,:"=",1) } @@ -325,10 +356,18 @@ it "should emit an expression" do subject.emit(expr).should == 'NOT admin' end end + context "when passed a Function" do + let(:func) { SQL::Function.new(:MAX,1,2) } + + it "should emit the function" do + subject.emit(func).should == 'MAX(1,2)' + end + end + context "when passed a Statment" do let(:stmt) { SQL::Statement.new(:SELECT,1) } it "should emit a statement" do subject.emit(stmt).should == 'SELECT 1' @@ -419,9 +458,25 @@ context "with an argument" do let(:stmt) { SQL::Statement.new(:SELECT,1) } it "should emit the statment argument" do subject.emit_statement(stmt).should == 'select 1' + end + + context "when the argument is an Array" do + let(:stmt) { SQL::Statement.new(:SELECT,[1,2,3]) } + + it "should emit a list" do + subject.emit_statement(stmt).should == 'select (1,2,3)' + end + + context "with only one element" do + let(:stmt) { SQL::Statement.new(:SELECT,[1]) } + + it "should emit the element" do + subject.emit_statement(stmt).should == 'select 1' + end + end end context "with custom :space" do subject { described_class.new(case: :lower, space: '/**/') }