<commit_before>require 'gaq/controller_handle'
require 'gaq/configuration'
require 'gaq/class_cache'

module Gaq
  describe ControllerHandle do
    describe "#finalized_commands_as_segments" do
      let(:controller_facade) { double "controller facade" }

      let(:language) do
        result = CommandLanguage.new
        CommandLanguage.declare_language_on(result)
        CommandLanguage.define_transformations_on(result)
        result
      end

      let(:class_cache) { ClassCache.new }
      let(:config) do
        Configuration.new
      end
      let(:rails_config) { config.rails_config }

      let(:commands_from_flash) { [] }

      let(:flash_commands_adapter) do
        result = double("flash_commands_adapter")
        result.stub(:commands_from_flash) { commands_from_flash }
        result
      end

      subject do
        described_class.new(controller_facade, language, class_cache, config).tap do |result|
          result.flash_commands_adapter = flash_commands_adapter
        end
      end

      let(:result) do
        subject.finalized_commands_as_segments
      end

      let(:root_target) { subject.root_target }

      context "without further configuration" do
        it "returns _setAccount (unset wpi) and _trackPageview for default tracker" do
          result.should be == [
            ["_setAccount", "UA-XUNSET-S"],
            ["_trackPageview"]
          ]
        end
      end

      context "configuring default tracker" do
        context "config.gaq.track_pageview = false" do
          before do
            rails_config.track_pageview = false
          end

          it "does not render _trackPageview for default tracker" do
            pending
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"]
            ]
          end
        end

        context "config.gaq.web_property_id=" do
          before do
            rails_config.web_property_id = 'UA-TEST23-5'
          end

          it "renders _setAccount with the given id" do
            result.should be == [
              ["_setAccount", 'UA-TEST23-5'],
              ["_trackPageview"]
            ]
          end
        end
      end

      context "with tracker commands issued on default tracker" do
        context "gaq.track_event 'category', 'action', 'label'" do
          before do
            root_target.track_event 'category', 'action', 'label'
          end

          it "renders the _trackEvent" do
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"],
              ["_trackEvent", "category", "action", "label"]
            ]
          end
        end
      end
    end
  end
end<commit_msg>roughly port instance_spec over<commit_after>require 'gaq/controller_handle'
require 'gaq/configuration'
require 'gaq/class_cache'

module Gaq
  describe ControllerHandle do
    describe "#finalized_commands_as_segments" do
      let(:controller_facade) { double "controller facade" }

      let(:language) do
        result = CommandLanguage.new
        CommandLanguage.declare_language_on(result)
        CommandLanguage.define_transformations_on(result)
        result
      end

      let(:class_cache) { ClassCache.new }
      let(:config) do
        Configuration.new
      end
      let(:rails_config) { config.rails_config }

      let(:commands_from_flash) { [] }
      let(:commands_pushed_to_flash) { [] }

      let(:flash_commands_adapter) do
        result = double("flash_commands_adapter")

        # for test convenience, convert between plain "segments" arrays and proper command objects
        # this depends on features from language

        result.stub(:commands_from_flash) do
          language.commands_from_flash_items(commands_from_flash)
        end

        result.stub(:<<) do |item|
          item = language.commands_to_flash_items([item]).first
          commands_pushed_to_flash << item
        end

        result
      end

      after do
        commands_pushed_to_flash.should be_empty \
          unless example.metadata[:push_to_flash]
      end

      subject do
        described_class.new(controller_facade, language, class_cache, config).tap do |result|
          result.flash_commands_adapter = flash_commands_adapter
        end
      end

      let(:result) do
        subject.finalized_commands_as_segments
      end

      let(:root_target) { subject.root_target }

      context "without further configuration" do
        it "returns _setAccount (unset wpi) and _trackPageview for default tracker" do
          result.should be == [
            ["_setAccount", "UA-XUNSET-S"],
            ["_trackPageview"]
          ]
        end
      end

      context "configuring default tracker" do
        context "config.gaq.track_pageview = false" do
          before do
            rails_config.track_pageview = false
          end

          it "does not render _trackPageview for default tracker" do
            pending
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"]
            ]
          end
        end

        context "config.gaq.web_property_id=" do
          before do
            rails_config.web_property_id = 'UA-TEST23-5'
          end

          it "renders _setAccount with the given id" do
            result.should be == [
              ["_setAccount", 'UA-TEST23-5'],
              ["_trackPageview"]
            ]
          end
        end
      end

      context "with tracker commands issued on default tracker" do
        context "gaq.track_event 'category', 'action', 'label'" do
          before do
            root_target.track_event 'category', 'action', 'label'
          end

          it "renders the _trackEvent" do
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"],
              ["_trackEvent", "category", "action", "label"]
            ]
          end
        end
      end

      context "with tracker commands issued on default tracker .next_request" do
        context "gaq.next_request.track_event 'category', 'action', 'label'", push_to_flash: true do
          before do
            root_target.next_request.track_event 'category', 'action', 'label'
          end

          it "renders the _trackEvent" do
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"]
            ]

            commands_pushed_to_flash.should be == [
              ["_trackEvent", "category", "action", "label"]
            ]
          end
        end
      end

      context "with a variable declared" do
        before do
          rails_config.declare_variable :var, scope: 3, slot: 0
        end

        it "returns nothing in addition" do
          result.should be == [
            ["_setAccount", "UA-XUNSET-S"],
            ["_trackPageview"]
          ]
        end

        context "after assigning to variable" do
          before do
            root_target.var = "blah"
          end

          it "renders the _setCustomVar" do
            pending
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"],
              ["_setCustomVar", 0, :var, "blah", 3] #XXX
            ]
          end

          context "gaq.track_event 'category', 'action', 'label'" do
            before(:each) do
              root_target.track_event 'category', 'action', 'label'
            end

            it "renders the _setCustomVar before the _trackEvent" do
              pending
              result.should be == [
                ["_setAccount", "UA-XUNSET-S"],
                ["_trackPageview"],
                ["_setCustomVar", 0, :var, "blah", 3], #XXX
                ["_trackEvent", "category", "action", "label"]
              ]
            end
          end

          context "gaq.next_request.track_event 'category', 'action', 'label'", push_to_flash: true do
            before(:each) do
              root_target.next_request.track_event 'category', 'action', 'label'
            end

            it "renders the _setCustomVar before the _trackEvent" do
              pending
              result.should be == [
                ["_setAccount", "UA-XUNSET-S"],
                ["_trackPageview"],
                ["_setCustomVar", 0, :var, "blah", 3], #XXX
              ]

              commands_pushed_to_flash.should be == [
                ["_trackEvent", "category", "action", "label"]
              ]
            end
          end

          context "both track_event and variable assignment on gaq.next_request", push_to_flash: true do
            before(:each) do
              root_target.next_request.track_event 'category', 'action', 'label'
              root_target.next_request.var = "foo"
            end

            it "pushes the _setCustomVar and the _trackEvent onto the flash storage" do
              pending
              result.should be == [
                ["_setAccount", "UA-XUNSET-S"],
                ["_trackPageview"],
                ["_setCustomVar", 0, :var, "blah", 3], #XXX
              ]

              commands_pushed_to_flash.should be == [
                ["_trackEvent", "category", "action", "label"],
                ["_setCustomVar", 0, :var, "foo", 3], #XXX
              ]

            end
          end

        end
      end

      context "with commands stored in flash" do
        before do
          commands_from_flash << ["_trackEvent", "last_cat", "last_action", "last_label"]
          commands_from_flash << ["_setCustomVar", 0, "var", "blah", 3]
        end

        it "renders these" do
          result.should be == [
            ["_setAccount", "UA-XUNSET-S"],
            ["_trackPageview"],
            ["_setCustomVar", 0, "var", "blah", 3],
            ["_trackEvent", "last_cat", "last_action", "last_label"]
          ]
        end

        context "gaq.track_event 'category', 'action', 'label'" do
          before(:each) do
            root_target.track_event 'category', 'action', 'label'
          end

          it "renders that in addition" do
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"],
              ["_setCustomVar", 0, "var", "blah", 3],
              ["_trackEvent", "last_cat", "last_action", "last_label"],
              ["_trackEvent", "category", "action", "label"]
            ]
          end
        end

        context "gaq.next_request.track_event 'category', 'action', 'label'", push_to_flash: true do
          before(:each) do
            root_target.next_request.track_event 'category', 'action', 'label'
          end

          it "does not render that in addition" do
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"],
              ["_setCustomVar", 0, "var", "blah", 3],
              ["_trackEvent", "last_cat", "last_action", "last_label"],
            ]

            commands_pushed_to_flash.should be == [
              ["_trackEvent", "category", "action", "label"]
            ]
          end
        end

        context "with a variable declared" do
          before do
            rails_config.declare_variable :var, scope: 3, slot: 0
          end

          it "returns nothing in addition" do
            result.should be == [
              ["_setAccount", "UA-XUNSET-S"],
              ["_trackPageview"],
              ["_setCustomVar", 0, "var", "blah", 3],
              ["_trackEvent", "last_cat", "last_action", "last_label"],
            ]
          end

          context "after assigning to variable" do
            before do
              root_target.var = "blubb"
            end

            it "renders both _setCustomVar, in order" do
              pending
              result.should be == [
                ["_setAccount", "UA-XUNSET-S"],
                ["_trackPageview"],
                ["_setCustomVar", 0, "var", "blah", 3],
                ["_setCustomVar", 0, :var, "blubb", 3], #XXX
                ["_trackEvent", "last_cat", "last_action", "last_label"]
              ]
            end

            context "gaq.track_event 'category', 'action', 'label'" do
              before do
                root_target.track_event 'category', 'action', 'label'
              end

              it "renders the _trackEvent in addition" do
                pending
                result.should be == [
                  ["_setAccount", "UA-XUNSET-S"],
                  ["_trackPageview"],
                  ["_setCustomVar", 0, "var", "blah", 3],
                  ["_setCustomVar", 0, :var, "blubb", 3], #XXX
                  ["_trackEvent", "last_cat", "last_action", "last_label"],
                  ["_trackEvent", "category", "action", "label"]
                ]
              end
            end

            context "gaq.next_request.track_event 'category', 'action', 'label'", push_to_flash: true do
              before do
                root_target.next_request.track_event 'category', 'action', 'label'
              end

              it "renders the _trackEvent in addition" do
                pending
                result.should be == [
                  ["_setAccount", "UA-XUNSET-S"],
                  ["_trackPageview"],
                  ["_setCustomVar", 0, "var", "blah", 3],
                  ["_setCustomVar", 0, :var, "blubb", 3], #XXX
                  ["_trackEvent", "last_cat", "last_action", "last_label"],
                ]

                commands_pushed_to_flash.should be == [
                  ["_trackEvent", "category", "action", "label"]
                ]
              end
            end
          end

        end
      end

      context "with a custom tracker" do
        before(:each) do
          rails_config.additional_trackers = ["foo"]
          root_target["foo"].track_event 'category', 'action', 'label'
        end

        it "maybe does not render _setAccount for additional tracker under some circumstances"

        it "renders _trackPageview and _trackEvent for that tracker" do
          result.should be == [
            ["_setAccount", "UA-XUNSET-S"],
            ["foo._setAccount", "UA-XUNSET-S"],
            ["_trackPageview"],
            ["foo._trackPageview"],
            ["foo._trackEvent", "category", "action", "label"]
          ]
        end
      end

      it "fails when an undeclared tracker is accessed"
    end
  end
end
{'fraction_non_alphanumeric': 0.10607421740031787, 'fraction_numerical': 0.00331697878515652, 'mean_word_length': 1.6790077748981858, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '6068411', 'n_tokens_mistral': 4085, 'n_tokens_neox': 3895, 'n_words': 999}