require "version"

describe Version::Token do
  specify "#inspect" do
    expect(Version::Token.new("foo").inspect).to eq('#<Version::Token "foo">')
  end

  specify "#to_s" do
    expect(Version::Token.new("foo").to_s).to eq("foo")
  end
end

describe Version::NULL do
  it "is always smaller" do
    expect(Version::NULL).to be < Version.create("1")
  end

  it "is never greater" do
    expect(Version::NULL).not_to be > Version.create("0")
  end

  it "isn't equal to itself" do
    expect(Version::NULL).not_to eql(Version::NULL)
  end

  it "creates an empty string" do
    expect(Version::NULL.to_s).to eq("")
  end

  it "produces NaN as a Float" do
    # Float::NAN is not equal to itself so compare object IDs
    expect(Version::NULL.to_f.object_id).to eql(Float::NAN.object_id)
  end
end

describe Version::NullToken do
  specify "#inspect" do
    expect(subject.inspect).to eq("#<Version::NullToken>")
  end

  it "is equal to itself" do
    expect(subject).to be == described_class.new
  end
end

describe Version do
  specify "comparison" do
    expect(Version.create("0.1")).to be == Version.create("0.1.0")
    expect(Version.create("0.1")).to be < Version.create("0.2")
    expect(Version.create("1.2.3")).to be > Version.create("1.2.2")
    expect(Version.create("1.2.4")).to be < Version.create("1.2.4.1")

    expect(Version.create("1.2.3")).to be > Version.create("1.2.3alpha4")
    expect(Version.create("1.2.3")).to be > Version.create("1.2.3beta2")
    expect(Version.create("1.2.3")).to be > Version.create("1.2.3rc3")
    expect(Version.create("1.2.3")).to be < Version.create("1.2.3-p34")
  end

  specify "HEAD" do
    expect(Version.create("HEAD")).to be > Version.create("1.2.3")
    expect(Version.create("HEAD-abcdef")).to be > Version.create("1.2.3")
    expect(Version.create("1.2.3")).to be < Version.create("HEAD")
    expect(Version.create("1.2.3")).to be < Version.create("HEAD-fedcba")
    expect(Version.create("HEAD-abcdef")).to be == Version.create("HEAD-fedcba")
    expect(Version.create("HEAD")).to be == Version.create("HEAD-fedcba")
  end

  specify "comparing alpha versions" do
    expect(Version.create("1.2.3alpha")).to be < Version.create("1.2.3")
    expect(Version.create("1.2.3")).to be < Version.create("1.2.3a")
    expect(Version.create("1.2.3alpha4")).to be == Version.create("1.2.3a4")
    expect(Version.create("1.2.3alpha4")).to be == Version.create("1.2.3A4")
    expect(Version.create("1.2.3alpha4")).to be > Version.create("1.2.3alpha3")
    expect(Version.create("1.2.3alpha4")).to be < Version.create("1.2.3alpha5")
    expect(Version.create("1.2.3alpha4")).to be < Version.create("1.2.3alpha10")

    expect(Version.create("1.2.3alpha4")).to be < Version.create("1.2.3beta2")
    expect(Version.create("1.2.3alpha4")).to be < Version.create("1.2.3rc3")
    expect(Version.create("1.2.3alpha4")).to be < Version.create("1.2.3")
    expect(Version.create("1.2.3alpha4")).to be < Version.create("1.2.3-p34")
  end

  specify "comparing beta versions" do
    expect(Version.create("1.2.3beta2")).to be == Version.create("1.2.3b2")
    expect(Version.create("1.2.3beta2")).to be == Version.create("1.2.3B2")
    expect(Version.create("1.2.3beta2")).to be > Version.create("1.2.3beta1")
    expect(Version.create("1.2.3beta2")).to be < Version.create("1.2.3beta3")
    expect(Version.create("1.2.3beta2")).to be < Version.create("1.2.3beta10")

    expect(Version.create("1.2.3beta2")).to be > Version.create("1.2.3alpha4")
    expect(Version.create("1.2.3beta2")).to be < Version.create("1.2.3rc3")
    expect(Version.create("1.2.3beta2")).to be < Version.create("1.2.3")
    expect(Version.create("1.2.3beta2")).to be < Version.create("1.2.3-p34")
  end

  specify "comparing RC versions" do
    expect(Version.create("1.2.3rc3")).to be == Version.create("1.2.3RC3")
    expect(Version.create("1.2.3rc3")).to be > Version.create("1.2.3rc2")
    expect(Version.create("1.2.3rc3")).to be < Version.create("1.2.3rc4")
    expect(Version.create("1.2.3rc3")).to be < Version.create("1.2.3rc10")

    expect(Version.create("1.2.3rc3")).to be > Version.create("1.2.3alpha4")
    expect(Version.create("1.2.3rc3")).to be > Version.create("1.2.3beta2")
    expect(Version.create("1.2.3rc3")).to be < Version.create("1.2.3")
    expect(Version.create("1.2.3rc3")).to be < Version.create("1.2.3-p34")
  end

  specify "comparing patch-level versions" do
    expect(Version.create("1.2.3-p34")).to be == Version.create("1.2.3-P34")
    expect(Version.create("1.2.3-p34")).to be > Version.create("1.2.3-p33")
    expect(Version.create("1.2.3-p34")).to be < Version.create("1.2.3-p35")
    expect(Version.create("1.2.3-p34")).to be > Version.create("1.2.3-p9")

    expect(Version.create("1.2.3-p34")).to be > Version.create("1.2.3alpha4")
    expect(Version.create("1.2.3-p34")).to be > Version.create("1.2.3beta2")
    expect(Version.create("1.2.3-p34")).to be > Version.create("1.2.3rc3")
    expect(Version.create("1.2.3-p34")).to be > Version.create("1.2.3")
  end

  specify "comparing unevenly-padded versions" do
    expect(Version.create("2.1.0-p194")).to be < Version.create("2.1-p195")
    expect(Version.create("2.1-p195")).to be > Version.create("2.1.0-p194")
    expect(Version.create("2.1-p194")).to be < Version.create("2.1.0-p195")
    expect(Version.create("2.1.0-p195")).to be > Version.create("2.1-p194")
    expect(Version.create("2-p194")).to be < Version.create("2.1-p195")
  end

  it "can be compared against nil" do
    expect(Version.create("2.1.0-p194")).to be > nil
  end

  it "can be compared against strings" do
    expect(Version.create("2.1.0-p194")).to be == "2.1.0-p194"
    expect(Version.create("1")).to be == 1
  end

  specify "comparison returns nil for non-version" do
    v = Version.create("1.0")
    expect(v <=> Object.new).to be nil
    expect { v > Object.new }.to raise_error(ArgumentError)
  end

  specify "erlang versions" do
    versions = %w[R16B R15B03-1 R15B03 R15B02 R15B01 R14B04 R14B03
                  R14B02 R14B01 R14B R13B04 R13B03 R13B02-1].reverse
    expect(versions.sort_by { |v| Version.create(v) }).to eq(versions)
  end

  specify "hash equality" do
    v1 = Version.create("0.1.0")
    v2 = Version.create("0.1.0")
    v3 = Version.create("0.1.1")

    expect(v1).to eql(v2)
    expect(v1).not_to eql(v3)
    expect(v1.hash).to eq(v2.hash)
    expect(v1.hash).not_to eq(v3.hash)

    h = { v1 => :foo }
    expect(h[v2]).to eq(:foo)
  end

  describe "::create" do
    it "accepts objects responding to #to_str" do
      value = double(to_str: "0.1")
      expect(Version.create(value).to_s).to eq("0.1")
    end

    it "raises a TypeError for non-string objects" do
      expect { Version.create(1.1) }.to raise_error(TypeError)
      expect { Version.create(1) }.to raise_error(TypeError)
      expect { Version.create(:symbol) }.to raise_error(TypeError)
    end

    it "parses a version from a string" do
      v = Version.create("1.20")
      expect(v).not_to be_head
      expect(v.to_str).to eq("1.20")
    end

    specify "HEAD with commit" do
      v = Version.create("HEAD-abcdef")
      expect(v.commit).to eq("abcdef")
      expect(v.to_str).to eq("HEAD-abcdef")
    end

    specify "HEAD without commit" do
      v = Version.create("HEAD")
      expect(v.commit).to be nil
      expect(v.to_str).to eq("HEAD")
    end
  end

  specify "#detected_from_url?" do
    expect(Version.create("1.0")).not_to be_detected_from_url
    expect(Version::FromURL.new("1.0")).to be_detected_from_url
  end

  specify "#head?" do
    v1 = Version.create("HEAD-abcdef")
    v2 = Version.create("HEAD")

    expect(v1).to be_head
    expect(v2).to be_head
  end

  specify "#update_commit" do
    v1 = Version.create("HEAD-abcdef")
    v2 = Version.create("HEAD")

    v1.update_commit("ffffff")
    expect(v1.commit).to eq("ffffff")
    expect(v1.to_str).to eq("HEAD-ffffff")

    v2.update_commit("ffffff")
    expect(v2.commit).to eq("ffffff")
    expect(v2.to_str).to eq("HEAD-ffffff")
  end

  describe "::parse" do
    it "returns a NULL version when the URL cannot be parsed" do
      expect(Version.parse("http://example.com/blah.tar")).to be_null
      expect(Version.parse("foo")).to be_null
    end
  end

  describe "::detect" do
    matcher :be_detected_from do |url, specs = {}|
      match do |version|
        Version.detect(url, specs) == version
      end
    end

    specify "version all dots" do
      expect(Version.create("1.14"))
        .to be_detected_from("http://example.com/foo.bar.la.1.14.zip")
    end

    specify "version underscore separator" do
      expect(Version.create("1.1"))
        .to be_detected_from("http://example.com/grc_1.1.tar.gz")
    end

    specify "boost version style" do
      expect(Version.create("1.39.0"))
        .to be_detected_from("http://example.com/boost_1_39_0.tar.bz2")
    end

    specify "erlang version style" do
      expect(Version.create("R13B"))
        .to be_detected_from("http://erlang.org/download/otp_src_R13B.tar.gz")
    end

    specify "another erlang version style" do
      expect(Version.create("R15B01"))
        .to be_detected_from("https://github.com/erlang/otp/tarball/OTP_R15B01")
    end

    specify "yet another erlang version style" do
      expect(Version.create("R15B03-1"))
        .to be_detected_from("https://github.com/erlang/otp/tarball/OTP_R15B03-1")
    end

    specify "p7zip version style" do
      expect(Version.create("9.04"))
        .to be_detected_from("http://kent.dl.sourceforge.net/sourceforge/p7zip/p7zip_9.04_src_all.tar.bz2")
    end

    specify "new github style" do
      expect(Version.create("1.1.4"))
        .to be_detected_from("https://github.com/sam-github/libnet/tarball/libnet-1.1.4")
    end

    specify "codeload style" do
      expect(Version.create("0.7.1"))
        .to be_detected_from("https://codeload.github.com/gsamokovarov/jump/tar.gz/v0.7.1")
    end

    specify "elasticsearch alpha style" do
      expect(Version.create("5.0.0-alpha5"))
        .to be_detected_from("https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/5.0.0-alpha5/elasticsearch-5.0.0-alpha5.tar.gz")
    end

    specify "gloox beta style" do
      expect(Version.create("1.0-beta7"))
        .to be_detected_from("http://camaya.net/download/gloox-1.0-beta7.tar.bz2")
    end

    specify "sphinx beta style" do
      expect(Version.create("1.10-beta"))
        .to be_detected_from("http://sphinxsearch.com/downloads/sphinx-1.10-beta.tar.gz")
    end

    specify "astyle version style" do
      expect(Version.create("1.23"))
        .to be_detected_from("http://kent.dl.sourceforge.net/sourceforge/astyle/astyle_1.23_macosx.tar.gz")
    end

    specify "version dos2unix" do
      expect(Version.create("3.1"))
        .to be_detected_from("http://www.sfr-fresh.com/linux/misc/dos2unix-3.1.tar.gz")
    end

    specify "version internal dash" do
      expect(Version.create("1.1-2"))
        .to be_detected_from("http://example.com/foo-arse-1.1-2.tar.gz")
    end

    specify "version single digit" do
      expect(Version.create("45"))
        .to be_detected_from("http://example.com/foo_bar.45.tar.gz")
    end

    specify "noseparator single digit" do
      expect(Version.create("45"))
        .to be_detected_from("http://example.com/foo_bar45.tar.gz")
    end

    specify "version developer that hates us format" do
      expect(Version.create("1.2.3"))
        .to be_detected_from("http://example.com/foo-bar-la.1.2.3.tar.gz")
    end

    specify "version regular" do
      expect(Version.create("1.21"))
        .to be_detected_from("http://example.com/foo_bar-1.21.tar.gz")
    end

    specify "version sourceforge download" do
      expect(Version.create("1.21"))
        .to be_detected_from("http://sourceforge.net/foo_bar-1.21.tar.gz/download")
      expect(Version.create("1.21"))
        .to be_detected_from("http://sf.net/foo_bar-1.21.tar.gz/download")
    end

    specify "version github" do
      expect(Version.create("1.0.5"))
        .to be_detected_from("http://github.com/lloyd/yajl/tarball/1.0.5")
    end

    specify "version github with high patch number" do
      expect(Version.create("1.2.34"))
        .to be_detected_from("http://github.com/lloyd/yajl/tarball/v1.2.34")
    end

    specify "yet another version" do
      expect(Version.create("0.15.1b"))
        .to be_detected_from("http://example.com/mad-0.15.1b.tar.gz")
    end

    specify "lame version style" do
      expect(Version.create("398-2"))
        .to be_detected_from("http://kent.dl.sourceforge.net/sourceforge/lame/lame-398-2.tar.gz")
    end

    specify "ruby version style" do
      expect(Version.create("1.9.1-p243"))
        .to be_detected_from("ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p243.tar.gz")
    end

    specify "omega version style" do
      expect(Version.create("0.80.2"))
        .to be_detected_from("http://www.alcyone.com/binaries/omega/omega-0.80.2-src.tar.gz")
    end

    specify "rc style" do
      expect(Version.create("1.2.2rc1"))
        .to be_detected_from("http://downloads.xiph.org/releases/vorbis/libvorbis-1.2.2rc1.tar.bz2")
    end

    specify "dash rc style" do
      expect(Version.create("1.8.0-rc1"))
        .to be_detected_from("http://ftp.mozilla.org/pub/mozilla.org/js/js-1.8.0-rc1.tar.gz")
    end

    specify "angband version style" do
      expect(Version.create("3.0.9b"))
        .to be_detected_from("http://rephial.org/downloads/3.0/angband-3.0.9b-src.tar.gz")
    end

    specify "stable suffix" do
      expect(Version.create("1.4.14b"))
        .to be_detected_from("http://www.monkey.org/~provos/libevent-1.4.14b-stable.tar.gz")
    end

    specify "debian style 1" do
      expect(Version.create("3.03"))
        .to be_detected_from("http://ftp.de.debian.org/debian/pool/main/s/sl/sl_3.03.orig.tar.gz")
    end

    specify "debian style 2" do
      expect(Version.create("1.01b"))
        .to be_detected_from("http://ftp.de.debian.org/debian/pool/main/m/mmv/mmv_1.01b.orig.tar.gz")
    end

    specify "bottle style" do
      expect(Version.create("4.8.0"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/qt-4.8.0.lion.bottle.tar.gz")
    end

    specify "versioned bottle style" do
      expect(Version.create("4.8.1"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/qt-4.8.1.lion.bottle.1.tar.gz")
    end

    specify "erlang bottle style" do
      expect(Version.create("R15B"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/erlang-R15B.lion.bottle.tar.gz")
    end

    specify "another erlang bottle style" do
      expect(Version.create("R15B01"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/erlang-R15B01.mountain_lion.bottle.tar.gz")
    end

    specify "yet another erlang bottle style" do
      expect(Version.create("R15B03-1"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/erlang-R15B03-1.mountainlion.bottle.tar.gz")
    end

    specify "imagemagick style" do
      expect(Version.create("6.7.5-7"))
        .to be_detected_from("http://downloads.sf.net/project/machomebrew/mirror/ImageMagick-6.7.5-7.tar.bz2")
    end

    specify "imagemagick bottle style" do
      expect(Version.create("6.7.5-7"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/imagemagick-6.7.5-7.lion.bottle.tar.gz")
    end

    specify "imagemagick versioned bottle style" do
      expect(Version.create("6.7.5-7"))
        .to be_detected_from("https://homebrew.bintray.com/bottles/imagemagick-6.7.5-7.lion.bottle.1.tar.gz")
    end

    specify "date-based version style" do
      expect(Version.create("2017-04-17"))
        .to be_detected_from("https://example.com/dada-v2017-04-17.tar.gz")
    end

    specify "dash version style" do
      expect(Version.create("3.4"))
        .to be_detected_from("http://www.antlr.org/download/antlr-3.4-complete.jar")
    end

    specify "jenkins version style" do
      expect(Version.create("1.486"))
        .to be_detected_from("http://mirrors.jenkins-ci.org/war/1.486/jenkins.war")
      expect(Version.create("0.10.11"))
        .to be_detected_from("https://github.com/hechoendrupal/DrupalConsole/releases/download/0.10.11/drupal.phar")
    end

    specify "apache version style" do
      expect(Version.create("1.2.0-rc2"))
        .to be_detected_from("http://www.apache.org/dyn/closer.cgi?path=/cassandra/1.2.0/apache-cassandra-1.2.0-rc2-bin.tar.gz")
    end

    specify "jpeg version style" do
      expect(Version.create("8d"))
        .to be_detected_from("http://www.ijg.org/files/jpegsrc.v8d.tar.gz")
    end

    specify "ghc version style" do
      expect(Version.create("7.0.4"))
        .to be_detected_from("http://www.haskell.org/ghc/dist/7.0.4/ghc-7.0.4-x86_64-apple-darwin.tar.bz2")
      expect(Version.create("7.0.4"))
        .to be_detected_from("http://www.haskell.org/ghc/dist/7.0.4/ghc-7.0.4-i386-apple-darwin.tar.bz2")
    end

    specify "pypy version style" do
      expect(Version.create("1.4.1"))
        .to be_detected_from("http://pypy.org/download/pypy-1.4.1-osx.tar.bz2")
    end

    specify "openssl version style" do
      expect(Version.create("0.9.8s"))
        .to be_detected_from("http://www.openssl.org/source/openssl-0.9.8s.tar.gz")
    end

    specify "xaw3d version style" do
      expect(Version.create("1.5E"))
        .to be_detected_from("ftp://ftp.visi.com/users/hawkeyd/X/Xaw3d-1.5E.tar.gz")
    end

    specify "assimp version style" do
      expect(Version.create("2.0.863"))
        .to be_detected_from("http://downloads.sourceforge.net/project/assimp/assimp-2.0/assimp--2.0.863-sdk.zip")
    end

    specify "cmucl version style" do
      expect(Version.create("20c"))
        .to be_detected_from("http://common-lisp.net/project/cmucl/downloads/release/20c/cmucl-20c-x86-darwin.tar.bz2")
    end

    specify "fann version style" do
      expect(Version.create("2.1.0beta"))
        .to be_detected_from("http://downloads.sourceforge.net/project/fann/fann/2.1.0beta/fann-2.1.0beta.zip")
    end

    specify "grads version style" do
      expect(Version.create("2.0.1"))
        .to be_detected_from("ftp://iges.org/grads/2.0/grads-2.0.1-bin-darwin9.8-intel.tar.gz")
    end

    specify "haxe version style" do
      expect(Version.create("2.08"))
        .to be_detected_from("http://haxe.org/file/haxe-2.08-osx.tar.gz")
    end

    specify "imap version style" do
      expect(Version.create("2007f"))
        .to be_detected_from("ftp://ftp.cac.washington.edu/imap/imap-2007f.tar.gz")
    end

    specify "suite3270 version style" do
      expect(Version.create("3.3.12ga7"))
        .to be_detected_from("http://downloads.sourceforge.net/project/x3270/x3270/3.3.12ga7/suite3270-3.3.12ga7-src.tgz")
    end

    specify "wwwoffle version style" do
      expect(Version.create("2.9h"))
        .to be_detected_from("http://www.gedanken.demon.co.uk/download-wwwoffle/wwwoffle-2.9h.tgz")
    end

    specify "synergy version style" do
      expect(Version.create("1.3.6p2"))
        .to be_detected_from("http://synergy.googlecode.com/files/synergy-1.3.6p2-MacOSX-Universal.zip")
    end

    specify "fontforge version style" do
      expect(Version.create("20120731"))
        .to be_detected_from("http://downloads.sourceforge.net/project/fontforge/fontforge-source/fontforge_full-20120731-b.tar.bz2")
    end

    specify "ezlupdate version style" do
      expect(Version.create("2011.10"))
        .to be_detected_from("https://github.com/downloads/ezsystems/ezpublish-legacy/ezpublish_community_project-2011.10-with_ezc.tar.bz2")
    end

    specify "aespipe version style" do
      expect(Version.create("2.4c"))
        .to be_detected_from("http://loop-aes.sourceforge.net/aespipe/aespipe-v2.4c.tar.bz2")
    end

    specify "win version style" do
      expect(Version.create("0.9.17"))
        .to be_detected_from("http://ftpmirror.gnu.org/libmicrohttpd/libmicrohttpd-0.9.17-w32.zip")
      expect(Version.create("1.29"))
        .to be_detected_from("http://ftpmirror.gnu.org/libidn/libidn-1.29-win64.zip")
    end

    specify "with arch" do
      expect(Version.create("4.0.18-1"))
        .to be_detected_from("http://ftpmirror.gnu.org/mtools/mtools-4.0.18-1.i686.rpm")
      expect(Version.create("5.5.7-5"))
        .to be_detected_from("http://ftpmirror.gnu.org/autogen/autogen-5.5.7-5.i386.rpm")
      expect(Version.create("2.8"))
        .to be_detected_from("http://ftpmirror.gnu.org/libtasn1/libtasn1-2.8-x86.zip")
      expect(Version.create("2.8"))
        .to be_detected_from("http://ftpmirror.gnu.org/libtasn1/libtasn1-2.8-x64.zip")
      expect(Version.create("4.0.18"))
        .to be_detected_from("http://ftpmirror.gnu.org/mtools/mtools_4.0.18_i386.deb")
    end

    specify "opam version" do
      expect(Version.create("2.18.3"))
        .to be_detected_from("https://opam.ocaml.org/archives/lablgtk.2.18.3+opam.tar.gz")
      expect(Version.create("1.9"))
        .to be_detected_from("https://opam.ocaml.org/archives/sha.1.9+opam.tar.gz")
      expect(Version.create("0.99.2"))
        .to be_detected_from("https://opam.ocaml.org/archives/ppx_tools.0.99.2+opam.tar.gz")
      expect(Version.create("1.0.2"))
        .to be_detected_from("https://opam.ocaml.org/archives/easy-format.1.0.2+opam.tar.gz")
    end

    specify "no extension version" do
      expect(Version.create("1.8.12"))
        .to be_detected_from("https://waf.io/waf-1.8.12")
      expect(Version.create("0.7.1"))
        .to be_detected_from("https://codeload.github.com/gsamokovarov/jump/tar.gz/v0.7.1")
      expect(Version.create("0.9.1234"))
        .to be_detected_from("https://my.datomic.com/downloads/free/0.9.1234")
      expect(Version.create("0.9"))
        .to be_detected_from("https://my.datomic.com/downloads/free/0.9.1t34")
      expect(Version.create("1.2.3"))
        .to be_detected_from("https://my.datomic.com/downloads/free/1.2.3")
    end

    specify "dash separated version" do
      expect(Version.create("6-20151227"))
        .to be_detected_from("ftp://gcc.gnu.org/pub/gcc/snapshots/6-20151227/gcc-6-20151227.tar.bz2")
    end

    specify "from URL" do
      expect(Version.create("1.2.3"))
        .to be_detected_from("http://github.com/foo/bar.git", tag: "v1.2.3")
    end
  end
end

describe Pathname do
  specify "#version" do
    d = HOMEBREW_CELLAR/"foo-0.1.9"
    d.mkpath
    expect(d.version).to eq(Version.create("0.1.9"))
  end
end
