1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership. The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
17 require File.join(File.dirname(__FILE__), 'spec_helpers')
19 describe Buildr::VersionRequirement, '.create' do
21 Buildr::VersionRequirement.create(str)
24 it 'should complain on invalid input' do
25 lambda { create }.should raise_error(Exception)
26 lambda { create('%') }.should raise_error(Exception, /invalid character/)
27 lambda { create('1#{0}') }.should raise_error(Exception, /invalid character/)
28 lambda { create('1.0rc`exit`') }.should raise_error(Exception, /invalid character/)
29 lambda { create(1.0) }.should raise_error(Exception)
30 lambda { create('1.0') }.should_not raise_error(Exception)
31 lambda { create('1.0rc3') }.should_not raise_error(Exception)
34 it 'should allow versions using hyphen' do
35 lambda { create('1.0-rc3') }.should_not raise_error(Exception)
38 it 'should create a single version requirement' do
39 create('1.0').should_not be_composed
42 it 'should create a composed version requirement' do
43 create('1.0 | 2.1').should be_composed
47 describe Buildr::VersionRequirement, '#satisfied_by?' do
48 def should_satisfy(str, valids = [], invalids = [])
49 req = Buildr::VersionRequirement.create(str)
50 valids.each { |v| req.should be_satisfied_by(v) }
51 invalids.each { |v| req.should_not be_satisfied_by(v) }
54 it 'should accept Gem version operators' do
55 should_satisfy '1.0', %w(1 1.0), %w(1.1 0.1)
56 should_satisfy '=1.0', %w(1 1.0), %w(1.1 0.1)
57 should_satisfy '= 1.0', %w(1 1.0), %w(1.1 0.1)
58 should_satisfy '!= 1.0', %w(0.9 1.1 2), %w(1 1.0 1.0.0)
60 should_satisfy '>1.0', %w(1.0.1), %w(1 1.0 0.1)
61 should_satisfy '>=1.0', %w(1.0.1 1 1.0), %w(0.9)
63 should_satisfy '<1.0', %w(0.9 0.9.9), %w(1 1.0 1.1 2)
64 should_satisfy '<=1.0', %w(0.9 0.9.9 1 1.0), %w(1.1 2)
66 should_satisfy '~> 1.2.3', %w(1.2.3 1.2.3.4 1.2.4), %w(1.2.1 0.9 1.4 2)
69 it 'should accept logic not operator' do
70 should_satisfy 'not 0.5', %w(0 1), %w(0.5)
71 should_satisfy '! 0.5', %w(0 1), %w(0.5)
72 should_satisfy '!= 0.5', %w(0 1), %w(0.5)
73 should_satisfy '!<= 0.5', %w(0.5.1 2), %w(0.5)
76 it 'should accept logic or operator' do
77 should_satisfy '0.5 or 2.0', %w(0.5 2.0), %w(1.0 0.5.1 2.0.9)
78 should_satisfy '0.5 | 2.0', %w(0.5 2.0), %w(1.0 0.5.1 2.0.9)
81 it 'should accept logic and operator' do
82 should_satisfy '>1.5 and <2.0', %w(1.6 1.9), %w(1.5 2 2.0)
83 should_satisfy '>1.5 & <2.0', %w(1.6 1.9), %w(1.5 2 2.0)
86 it 'should assume logic and if missing operator between expressions' do
87 should_satisfy '>1.5 <2.0', %w(1.6 1.9), %w(1.5 2 2.0)
90 it 'should allow combining logic operators' do
91 should_satisfy '>1.0 | <2.0 | =3.0', %w(1.5 3.0 1 2 4)
92 should_satisfy '>1.0 & <2.0 | =3.0', %w(1.3 3.0), %w(1 2)
93 should_satisfy '=1.0 | <2.0 & =0.5', %w(0.5 1.0), %w(1.1 0.1 2)
94 should_satisfy '~>1.1 | ~>1.3 | ~>1.5 | 2.0', %w(2 1.5.6 1.1.2 1.1.3), %w(1.0.9 0.5 2.2.1)
95 should_satisfy 'not(2) | 1', %w(1 3), %w(2)
98 it 'should allow using parens to group logic expressions' do
99 should_satisfy '(1.0)', %w(1 1.0), %w(0.9 1.1)
100 should_satisfy '!( !(1.0) )', %w(1 1.0), %w(0.9 1.1)
101 should_satisfy '1 | !(2 | 3)', %w(1), %w(2 3)
102 should_satisfy '!(2 | 3) | 1', %w(1), %w(2 3)
106 describe Buildr::VersionRequirement, '#default' do
107 it 'should return nil if missing default requirement' do
108 Buildr::VersionRequirement.create('>1').default.should be_nil
109 Buildr::VersionRequirement.create('<1').default.should be_nil
110 Buildr::VersionRequirement.create('!1').default.should be_nil
111 Buildr::VersionRequirement.create('!<=1').default.should be_nil
114 it 'should return the last version with a = requirement' do
115 Buildr::VersionRequirement.create('1').default.should == '1'
116 Buildr::VersionRequirement.create('=1').default.should == '1'
117 Buildr::VersionRequirement.create('<=1').default.should == '1'
118 Buildr::VersionRequirement.create('>=1').default.should == '1'
119 Buildr::VersionRequirement.create('1 | 2 | 3').default.should == '3'
120 Buildr::VersionRequirement.create('1 2 | 3').default.should == '3'
121 Buildr::VersionRequirement.create('1 & 2 | 3').default.should == '3'