<commit_before>require 'helper'

describe Thor do
  describe "#method_option" do
    it "sets options to the next method to be invoked" do
      args = ["foo", "bar", "--force"]
      arg, options = MyScript.start(args)
      expect(options).to eq({ "force" => true })
    end

    describe ":lazy_default" do
      it "is absent when option is not specified" do
        arg, options = MyScript.start(["with_optional"])
        expect(options).to eq({})
      end

      it "sets a default that can be overridden for strings" do
        arg, options = MyScript.start(["with_optional", "--lazy"])
        expect(options).to eq({ "lazy" => "yes" })

        arg, options = MyScript.start(["with_optional", "--lazy", "yesyes!"])
        expect(options).to eq({ "lazy" => "yesyes!" })
      end

      it "sets a default that can be overridden for numerics" do
        arg, options = MyScript.start(["with_optional", "--lazy-numeric"])
        expect(options).to eq({ "lazy_numeric" => 42 })

        arg, options = MyScript.start(["with_optional", "--lazy-numeric", 20000])
        expect(options).to eq({ "lazy_numeric" => 20000 })
      end

      it "sets a default that can be overridden for arrays" do
        arg, options = MyScript.start(["with_optional", "--lazy-array"])
        expect(options).to eq({ "lazy_array" => %w[eat at joes] })

        arg, options = MyScript.start(["with_optional", "--lazy-array", "hello", "there"])
        expect(options).to eq({ "lazy_array" => %w[hello there] })
      end

      it "sets a default that can be overridden for hashes" do
        arg, options = MyScript.start(["with_optional", "--lazy-hash"])
        expect(options).to eq({ "lazy_hash" => {'swedish' => 'meatballs'} })

        arg, options = MyScript.start(["with_optional", "--lazy-hash", "polish:sausage"])
        expect(options).to eq({ "lazy_hash" => {'polish' => 'sausage'} })
      end
    end

    describe "when :for is supplied" do
      it "updates an already defined command" do
        args, options = MyChildScript.start(["animal", "horse", "--other=fish"])
        expect(options[:other]).to eq("fish")
      end

      describe "and the target is on the parent class" do
        it "updates an already defined command" do
          args = ["example_default_command", "my_param", "--new-option=verified"]
          options = Scripts::MyScript.start(args)
          expect(options[:new_option]).to eq("verified")
        end

        it "adds a command to the command list if the updated command is on the parent class" do
          expect(Scripts::MyScript.commands["example_default_command"]).to be
        end

        it "clones the parent command" do
          expect(Scripts::MyScript.commands["example_default_command"]).not_to eq(MyChildScript.commands["example_default_command"])
        end
      end
    end
  end

  describe "#default_command" do
    it "sets a default command" do
      expect(MyScript.default_command).to eq("example_default_command")
    end

    it "invokes the default command if no command is specified" do
      expect(MyScript.start([])).to eq("default command")
    end

    it "invokes the default command if no command is specified even if switches are given" do
      expect(MyScript.start(["--with", "option"])).to eq({"with"=>"option"})
    end

    it "inherits the default command from parent" do
      expect(MyChildScript.default_command).to eq("example_default_command")
    end
  end

  describe "#stop_on_unknown_option!" do
    my_script = Class.new(Thor) do
      class_option "verbose",   :type => :boolean
      class_option "mode",      :type => :string

      stop_on_unknown_option! :exec

      desc "exec", "Run a command"
      def exec(*args)
        return options, args
      end

      desc "boring", "An ordinary command"
      def boring(*args)
        return options, args
      end
    end

    it "passes remaining args to command when it encounters a non-option" do
      expect(my_script.start(%w[exec command --verbose])).to eq [{}, ["command", "--verbose"]]
    end

    it "passes remaining args to command when it encounters an unknown option" do
      expect(my_script.start(%w[exec --foo command --bar])).to eq [{}, ["--foo", "command", "--bar"]]
    end

    it "still accepts options that are given before non-options" do
      expect(my_script.start(%w[exec --verbose command --foo])).to eq [{"verbose" => true}, ["command", "--foo"]]
    end

    it "still accepts options that require a value" do
      expect(my_script.start(%w[exec --mode rashly command])).to eq [{"mode" => "rashly"}, ["command"]]
    end

    it "still passes everything after -- to command" do
      expect(my_script.start(%w[exec -- --verbose])).to eq [{}, ["--verbose"]]
    end

    it "does not affect ordinary commands"  do
      expect(my_script.start(%w[boring command --verbose])).to eq [{"verbose" => true}, ["command"]]
    end

    context "when provided with multiple command names" do
      klass = Class.new(Thor) do
        stop_on_unknown_option! :foo, :bar
      end
      it "affects all specified commands" do
        expect(klass.stop_on_unknown_option?(mock :name => "foo")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "bar")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "baz")).to be_false
      end
    end

    context "when invoked several times" do
      klass = Class.new(Thor) do
        stop_on_unknown_option! :foo
        stop_on_unknown_option! :bar
      end
      it "affects all specified commands" do
        expect(klass.stop_on_unknown_option?(mock :name => "foo")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "bar")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "baz")).to be_false
      end
    end

    it "doesn't break new" do
      expect(my_script.new).to be_a(Thor)
    end
  end

  describe "#map" do
    it "calls the alias of a method if one is provided" do
      expect(MyScript.start(["-T", "fish"])).to eq(["fish"])
    end

    it "calls the alias of a method if several are provided via .map" do
      expect(MyScript.start(["-f", "fish"])).to eq(["fish", {}])
      expect(MyScript.start(["--foo", "fish"])).to eq(["fish", {}])
    end

    it "inherits all mappings from parent" do
      expect(MyChildScript.default_command).to eq("example_default_command")
    end
  end

  describe "#package_name" do
    it "provides a proper description for a command when the package_name is assigned" do
      content = capture(:stdout) { PackageNameScript.start(["help"]) }
      expect(content).to match(/Baboon commands:/m)
    end

    # TODO: remove this, might be redundant, just wanted to prove full coverage
    it "provides a proper description for a command when the package_name is NOT assigned" do
      content = capture(:stdout) { MyScript.start(["help"]) }
      expect(content).to match(/Commands:/m)
    end
  end

  describe "#desc" do<commit_msg>.map -> #map for consistency<commit_after>require 'helper'

describe Thor do
  describe "#method_option" do
    it "sets options to the next method to be invoked" do
      args = ["foo", "bar", "--force"]
      arg, options = MyScript.start(args)
      expect(options).to eq({ "force" => true })
    end

    describe ":lazy_default" do
      it "is absent when option is not specified" do
        arg, options = MyScript.start(["with_optional"])
        expect(options).to eq({})
      end

      it "sets a default that can be overridden for strings" do
        arg, options = MyScript.start(["with_optional", "--lazy"])
        expect(options).to eq({ "lazy" => "yes" })

        arg, options = MyScript.start(["with_optional", "--lazy", "yesyes!"])
        expect(options).to eq({ "lazy" => "yesyes!" })
      end

      it "sets a default that can be overridden for numerics" do
        arg, options = MyScript.start(["with_optional", "--lazy-numeric"])
        expect(options).to eq({ "lazy_numeric" => 42 })

        arg, options = MyScript.start(["with_optional", "--lazy-numeric", 20000])
        expect(options).to eq({ "lazy_numeric" => 20000 })
      end

      it "sets a default that can be overridden for arrays" do
        arg, options = MyScript.start(["with_optional", "--lazy-array"])
        expect(options).to eq({ "lazy_array" => %w[eat at joes] })

        arg, options = MyScript.start(["with_optional", "--lazy-array", "hello", "there"])
        expect(options).to eq({ "lazy_array" => %w[hello there] })
      end

      it "sets a default that can be overridden for hashes" do
        arg, options = MyScript.start(["with_optional", "--lazy-hash"])
        expect(options).to eq({ "lazy_hash" => {'swedish' => 'meatballs'} })

        arg, options = MyScript.start(["with_optional", "--lazy-hash", "polish:sausage"])
        expect(options).to eq({ "lazy_hash" => {'polish' => 'sausage'} })
      end
    end

    describe "when :for is supplied" do
      it "updates an already defined command" do
        args, options = MyChildScript.start(["animal", "horse", "--other=fish"])
        expect(options[:other]).to eq("fish")
      end

      describe "and the target is on the parent class" do
        it "updates an already defined command" do
          args = ["example_default_command", "my_param", "--new-option=verified"]
          options = Scripts::MyScript.start(args)
          expect(options[:new_option]).to eq("verified")
        end

        it "adds a command to the command list if the updated command is on the parent class" do
          expect(Scripts::MyScript.commands["example_default_command"]).to be
        end

        it "clones the parent command" do
          expect(Scripts::MyScript.commands["example_default_command"]).not_to eq(MyChildScript.commands["example_default_command"])
        end
      end
    end
  end

  describe "#default_command" do
    it "sets a default command" do
      expect(MyScript.default_command).to eq("example_default_command")
    end

    it "invokes the default command if no command is specified" do
      expect(MyScript.start([])).to eq("default command")
    end

    it "invokes the default command if no command is specified even if switches are given" do
      expect(MyScript.start(["--with", "option"])).to eq({"with"=>"option"})
    end

    it "inherits the default command from parent" do
      expect(MyChildScript.default_command).to eq("example_default_command")
    end
  end

  describe "#stop_on_unknown_option!" do
    my_script = Class.new(Thor) do
      class_option "verbose",   :type => :boolean
      class_option "mode",      :type => :string

      stop_on_unknown_option! :exec

      desc "exec", "Run a command"
      def exec(*args)
        return options, args
      end

      desc "boring", "An ordinary command"
      def boring(*args)
        return options, args
      end
    end

    it "passes remaining args to command when it encounters a non-option" do
      expect(my_script.start(%w[exec command --verbose])).to eq [{}, ["command", "--verbose"]]
    end

    it "passes remaining args to command when it encounters an unknown option" do
      expect(my_script.start(%w[exec --foo command --bar])).to eq [{}, ["--foo", "command", "--bar"]]
    end

    it "still accepts options that are given before non-options" do
      expect(my_script.start(%w[exec --verbose command --foo])).to eq [{"verbose" => true}, ["command", "--foo"]]
    end

    it "still accepts options that require a value" do
      expect(my_script.start(%w[exec --mode rashly command])).to eq [{"mode" => "rashly"}, ["command"]]
    end

    it "still passes everything after -- to command" do
      expect(my_script.start(%w[exec -- --verbose])).to eq [{}, ["--verbose"]]
    end

    it "does not affect ordinary commands"  do
      expect(my_script.start(%w[boring command --verbose])).to eq [{"verbose" => true}, ["command"]]
    end

    context "when provided with multiple command names" do
      klass = Class.new(Thor) do
        stop_on_unknown_option! :foo, :bar
      end
      it "affects all specified commands" do
        expect(klass.stop_on_unknown_option?(mock :name => "foo")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "bar")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "baz")).to be_false
      end
    end

    context "when invoked several times" do
      klass = Class.new(Thor) do
        stop_on_unknown_option! :foo
        stop_on_unknown_option! :bar
      end
      it "affects all specified commands" do
        expect(klass.stop_on_unknown_option?(mock :name => "foo")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "bar")).to be_true
        expect(klass.stop_on_unknown_option?(mock :name => "baz")).to be_false
      end
    end

    it "doesn't break new" do
      expect(my_script.new).to be_a(Thor)
    end
  end

  describe "#map" do
    it "calls the alias of a method if one is provided" do
      expect(MyScript.start(["-T", "fish"])).to eq(["fish"])
    end

    it "calls the alias of a method if several are provided via #map" do
      expect(MyScript.start(["-f", "fish"])).to eq(["fish", {}])
      expect(MyScript.start(["--foo", "fish"])).to eq(["fish", {}])
    end

    it "inherits all mappings from parent" do
      expect(MyChildScript.default_command).to eq("example_default_command")
    end
  end

  describe "#package_name" do
    it "provides a proper description for a command when the package_name is assigned" do
      content = capture(:stdout) { PackageNameScript.start(["help"]) }
      expect(content).to match(/Baboon commands:/m)
    end

    # TODO: remove this, might be redundant, just wanted to prove full coverage
    it "provides a proper description for a command when the package_name is NOT assigned" do
      content = capture(:stdout) { MyScript.start(["help"]) }
      expect(content).to match(/Commands:/m)
    end
  end

  describe "#desc" do
{'fraction_non_alphanumeric': 0.1353340478742408, 'fraction_numerical': 0.0017148981779206859, 'mean_word_length': 2.94920993227991, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2834614', 'n_tokens_mistral': 4280, 'n_tokens_neox': 4163, 'n_words': 1296}