Sha256: 8b6b216eb00efe14f48ff356ee09c9f9b087b97f03ba76ca2fa9828eb0d10465

Contents?: true

Size: 1.2 KB

Versions: 13

Compression:

Stored size: 1.2 KB

Contents

class Boolean
  def self_as_an_object
    self
  end
end

class JsNil
  def <(other)
    %x{
      return nil;
    }
  end
end

describe "Opal truthyness" do
  it "should evaluate to true using js `true` as an object" do
    if true.self_as_an_object
      called = true
    end

    called.should be_true
  end

  it "should evaluate to false using js `false` as an object" do
    if false.self_as_an_object
      called = true
    end

    called.should be_nil
  end
  
  it "should evaluate to false if js `nil` is used with an operator" do
    is_falsey = JsNil.new < 2 ? false : true
    
    is_falsey.should be_true
  end

  it "should consider false, nil, null, and undefined as not truthy" do
    called = nil
    [`false`, `nil`, `null`, `undefined`].each do |v|
      if v
        called = true
      end
    end

    called.should be_nil
  end

  it "should true as truthy" do
    if `true`
      called = true
    end

    called.should be_true
  end

  it "should handle logic operators correctly for false, nil, null, and undefined" do
    (`false` || `nil` || `null` || `undefined` || 1).should == 1
    [`false`, `nil`, `null`, `undefined`].each do |v|
      `#{1 && v} === #{v}`.should == true
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
opal-0.10.6 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.6.beta spec/opal/core/runtime/truthy_spec.rb
opal-0.10.5 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.4 spec/opal/core/runtime/truthy_spec.rb
opal-0.11.0.rc1 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.3 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.2 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.1 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.0 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.0.rc2 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.0.rc1 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.0.beta5 spec/opal/core/runtime/truthy_spec.rb
opal-0.10.0.beta4 spec/opal/core/runtime/truthy_spec.rb