\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/spec/unit/

Viewing File: version_class_spec.rb

#
# Author:: Seth Falcon (<seth@chef.io>)
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "spec_helper"
require "chef/version_class"

describe Chef::Version do
  before do
    @v0 = Chef::Version.new "0.0.0"
    @v123 = Chef::Version.new "1.2.3"
  end

  it "should turn itself into a string" do
    expect(@v0.to_s).to eq("0.0.0")
    expect(@v123.to_s).to eq("1.2.3")
  end

  it "should make a round trip with its string representation" do
    a = Chef::Version.new(@v123.to_s)
    expect(a).to eq(@v123)
  end

  it "should transform 1.2 to 1.2.0" do
    expect(Chef::Version.new("1.2").to_s).to eq("1.2.0")
  end

  it "should transform 01.002.0003 to 1.2.3" do
    a = Chef::Version.new "01.002.0003"
    expect(a).to eq(@v123)
  end

  describe "when creating valid Versions" do
    good_versions = %w{1.2 1.2.3 1000.80.50000 0.300.25 001.02.00003}
    good_versions.each do |v|
      it "should accept '#{v}'" do
        Chef::Version.new v
      end
    end
  end

  describe "when given bogus input" do
    bad_versions = ["1.2.3.4", "1.2.a4", "1", "a", "1.2 3", "1.2 a",
                    "1 2 3", "1-2-3", "1_2_3", "1.2_3", "1.2-3"]
    the_error = Chef::Exceptions::InvalidCookbookVersion
    bad_versions.each do |v|
      it "should raise #{the_error} when given '#{v}'" do
        expect { Chef::Version.new v }.to raise_error(the_error)
      end
    end
  end

  describe "<=>" do

    it "should equate versions 1.2 and 1.2.0" do
      expect(Chef::Version.new("1.2")).to eq(Chef::Version.new("1.2.0"))
    end

    it "should equate version 1.04 and 1.4" do
      expect(Chef::Version.new("1.04")).to eq(Chef::Version.new("1.4"))
    end

    it "should treat versions as numbers in the right way" do
      expect(Chef::Version.new("2.0")).to be < Chef::Version.new("11.0")
    end

    it "should sort based on the version number" do
      examples = [
                  # smaller, larger
                  ["1.0", "2.0"],
                  ["1.2.3", "1.2.4"],
                  ["1.2.3", "1.3.0"],
                  ["1.2.3", "1.3"],
                  ["1.2.3", "2.1.1"],
                  ["1.2.3", "2.1"],
                  ["1.2", "1.2.4"],
                  ["1.2", "1.3.0"],
                  ["1.2", "1.3"],
                  ["1.2", "2.1.1"],
                  ["1.2", "2.1"],
                 ]
      examples.each do |smaller, larger|
        sm = Chef::Version.new(smaller)
        lg = Chef::Version.new(larger)
        expect(sm).to be < lg
        expect(lg).to be > sm
        expect(sm).not_to eq(lg)
      end
    end

    it "should sort an array of versions" do
      a = %w{0.0.0 0.0.1 0.1.0 0.1.1 1.0.0 1.1.0 1.1.1}.map do |s|
        Chef::Version.new(s)
      end
      got = a.sort.map(&:to_s)
      expect(got).to eq(%w{0.0.0 0.0.1 0.1.0 0.1.1 1.0.0 1.1.0 1.1.1})
    end

    it "should sort an array of versions, part 2" do
      a = %w{9.8.7 1.0.0 1.2.3 4.4.6 4.5.6 0.8.6 4.5.5 5.9.8 3.5.7}.map do |s|
        Chef::Version.new(s)
      end
      got = a.sort.map(&:to_s)
      expect(got).to eq(%w{0.8.6 1.0.0 1.2.3 3.5.7 4.4.6 4.5.5 4.5.6 5.9.8 9.8.7})
    end

    describe "comparison examples" do
      [
       [ "0.0.0", :>, "0.0.0", false ],
       [ "0.0.0", :>=, "0.0.0", true ],
       [ "0.0.0", :==, "0.0.0", true ],
       [ "0.0.0", :<=, "0.0.0", true ],
       [ "0.0.0", :<, "0.0.0", false ],
       [ "0.0.0", :>, "0.0.1", false ],
       [ "0.0.0", :>=, "0.0.1", false ],
       [ "0.0.0", :==, "0.0.1", false ],
       [ "0.0.0", :<=, "0.0.1", true ],
       [ "0.0.0", :<, "0.0.1", true ],
       [ "0.0.1", :>, "0.0.1", false ],
       [ "0.0.1", :>=, "0.0.1", true ],
       [ "0.0.1", :==, "0.0.1", true ],
       [ "0.0.1", :<=, "0.0.1", true ],
       [ "0.0.1", :<, "0.0.1", false ],
       [ "0.1.0", :>, "0.1.0", false ],
       [ "0.1.0", :>=, "0.1.0", true ],
       [ "0.1.0", :==, "0.1.0", true ],
       [ "0.1.0", :<=, "0.1.0", true ],
       [ "0.1.0", :<, "0.1.0", false ],
       [ "0.1.1", :>, "0.1.1", false ],
       [ "0.1.1", :>=, "0.1.1", true ],
       [ "0.1.1", :==, "0.1.1", true ],
       [ "0.1.1", :<=, "0.1.1", true ],
       [ "0.1.1", :<, "0.1.1", false ],
       [ "1.0.0", :>, "1.0.0", false ],
       [ "1.0.0", :>=, "1.0.0", true ],
       [ "1.0.0", :==, "1.0.0", true ],
       [ "1.0.0", :<=, "1.0.0", true ],
       [ "1.0.0", :<, "1.0.0", false ],
       [ "1.0.0", :>, "0.0.1", true ],
       [ "1.0.0", :>=, "1.9.2", false ],
       [ "1.0.0", :==, "9.7.2", false ],
       [ "1.0.0", :<=, "1.9.1", true ],
       [ "1.0.0", :<, "1.9.0", true ],
       [ "1.2.2", :>, "1.2.1", true ],
       [ "1.2.2", :>=, "1.2.1", true ],
       [ "1.2.2", :==, "1.2.1", false ],
       [ "1.2.2", :<=, "1.2.1", false ],
       [ "1.2.2", :<, "1.2.1", false ],
      ].each do |spec|
        it "(#{spec.first(3).join(" ")}) should be #{spec[3]}" do
          got = Chef::Version.new(spec[0]).send(spec[1],
            Chef::Version.new(spec[2]))
          expect(got).to eq(spec[3])
        end
      end
    end
  end
end