Sha256: 0a993984f4bee353b79c2fd9fc27f7923c5934a2f97724d44f1e022f8d9d2359

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'gon'

describe Gon do

  before(:each) do
    Gon::Request.env = {}
  end

  describe '#all_variables' do

    it 'returns all variables in hash' do
      Gon.a = 1
      Gon.b = 2
      Gon.c = Gon.a + Gon.b
      Gon.c.should == 3
      Gon.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3}
    end

    it 'supports all data types' do
      Gon.clear
      Gon.int = 1
      Gon.float = 1.1
      Gon.string = 'string'
      Gon.array = [ 1, 'string' ]
      Gon.hash_var = { :a => 1, :b => '2'}
      Gon.hash_w_array = { :a => [ 2, 3 ] }
      Gon.klass = Hash
    end

  end

  describe '#include_gon' do

    before(:each) do
      Gon.clear
      Gon::Request.
        instance_variable_set(:@request_id, request.object_id)
      ActionView::Base.
        instance_methods.
        map(&:to_s).
        include?('include_gon').should == true
      @base = ActionView::Base.new
      @base.request = request
    end

    it 'outputs correct js with an integer' do
      Gon.int = 1
      @base.include_gon.should == '<script>window.gon = {};' +
                                    'gon.int=1;' +
                                  '</script>'
    end

    it 'outputs correct js with a string' do
      Gon.str = %q(a'b"c)
      @base.include_gon.should == '<script>window.gon = {};' +
                                    %q(gon.str="a'b\"c";) +
                                  '</script>'
    end

    it 'outputs correct js with a script string' do
      Gon.str = %q(</script><script>alert('!')</script>)
      @base.include_gon.should == '<script>window.gon = {};' +
                                    %q(gon.str="<\\/script><script>alert('!')<\\/script>";) +
                                  '</script>'
    end

  end

  it 'returns exception if try to set public method as variable' do
    Gon.clear
    lambda { Gon.all_variables = 123 }.should raise_error
    lambda { Gon.rabl = 123 }.should raise_error
  end

  def request
    @request ||= double 'request', :env => {}
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gon-3.0.0 spec/gon/basic_spec.rb