Sha256: e6eaa230bdbe97c6e91c35524776e2357a5a2b2db36325b429cce7dd3513685b

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

require 'test/unit'
require 'promise/v2'

class TestPromiseAlways < Test::Unit::TestCase
  def test_calls_the_block_when_it_was_resolved
    x = 42
    PromiseV2.value(23)
      .then { |v| x = v }
      .always { |v| x = 2 }
      .then { assert_equal(x, 2) }
  end

  def test_calls_the_block_when_it_was_rejected
    x = 42
    PromiseV2.error(23)
      .rescue { |v| x = v }
      .always { |v| x = 2 }
      .always { assert_equal(x, 2) }
  end

  def test_acts_as_resolved
    x = 42
    PromiseV2.error(23)
      .rescue { |v| x = v }
      .always { x = 2 }
      .then { x = 3 }
      .always { assert_equal(x, 3) }
  end

  def test_can_be_called_multiple_times_on_resolved_promises
    p = PromiseV2.value(2)
    x = 1
    ps = []
    ps << p.then { x += 1 }
    ps << p.fail { x += 2 }
    ps << p.always { x += 3 }

    PromiseV2.when(ps).always do
      assert_equal(x, 5)
    end
  end

  def test_can_be_called_multiple_times_on_rejected_promises
    p = PromiseV2.error(2)
    x = 1
    ps = []
    ps << p.then { x += 1 }.fail{}
    ps << p.fail { x += 2 }
    ps << p.always { x += 3 }.fail{}

    PromiseV2.when(ps).then do
      assert_equal(x, 6)
    end
  end

  def test_raises_with_alwaysB_if_a_promise_has_already_been_chained
    p = PromiseV2.new

    p.then! {}

    assert_raise(ArgumentError) { p.always! {} }
  end

  def test_gets_the_value_from_the_previous_successful_block
    PromiseV2.value(23)
      .then { 123 }
      .rescue { 234 }
      .always { |v| assert_equal(123, v) }
  end

  def test_gets_the_value_from_the_previous_rescued_block
    PromiseV2.reject(23)
      .then { 123 }
      .rescue { 234 }
      .always { |v| assert_equal(234, v) }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 test/opal/promisev2/test_always.rb
opal-1.8.2 test/opal/promisev2/test_always.rb
opal-1.8.1 test/opal/promisev2/test_always.rb
opal-1.8.0 test/opal/promisev2/test_always.rb
opal-1.8.0.beta1 test/opal/promisev2/test_always.rb