Sha256: c1a449b2f8cf7e4c2dc774bcb0b12492cdaf826a0d73ff80dc4f2b22057893ef

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'spec_helper'
module Qrb
  describe Syntax, "constraints" do

    subject{
      Syntax.parse(input, root: "constraints")
    }

    let(:compiled){
      subject.compile("a")
    }

    let(:ast){
      subject.to_ast('a')
    }

    context 'a >= 10' do
      let(:input){ 'a >= 10' }

      it 'compiles to an Hash' do
        compiled.should be_a(Hash)
        compiled.keys.should eq([:predicate])
      end

      it 'has the expected AST' do
        ast.should eq([
          :constraint,
          "default",
          [:fn, [:parameters, "a"], [:source, "a >= 10"]]
        ])
      end
    end

    context 'foo: a >= 10' do
      let(:input){ 'foo: a >= 10' }

      it 'compiles to an Hash' do
        compiled.should be_a(Hash)
        compiled.keys.should eq([:foo])
      end

      it 'has the expected AST' do
        ast.should eq([[
          :constraint,
          "foo",
          [:fn, [:parameters, "a"], [:source, "a >= 10"]]
        ]])
      end
    end

    context 'foo: a >= 10, bar: a <= 255' do
      let(:input){ 'foo: a >= 10, bar: a <= 255' }

      it 'compiles to an Hash' do
        compiled.should be_a(Hash)
        compiled.keys.should eq([:foo, :bar])
      end

      it 'has the expected AST' do
        ast.should eq([
          [
            :constraint,
            "foo",
            [:fn, [:parameters, "a"], [:source, "a >= 10"]]
          ],
          [
            :constraint,
            "bar",
            [:fn, [:parameters, "a"], [:source, "a <= 255"]]
          ]
        ])
      end
    end

    context 'foo: a >= 10, foo: a <= 255' do
      let(:input){ 'foo: a >= 10, foo: a <= 255' }

      it 'compiles to an Hash' do
        ->{
          compiled
        }.should raise_error("Duplicate constraint name `foo`")
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qrb-0.3.0 spec/unit/syntax/nodes/test_constraints.rb