Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3299ba36338e
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1eef4d8639ef
Choose a head ref
  • 2 commits
  • 4 files changed
  • 2 contributors

Commits on Oct 22, 2013

  1. implemented Kernel#warn

    - also fixes invalid return from StringIO.read
    mojavelinux committed Oct 22, 2013
    Copy the full SHA
    cda720f View commit details

Commits on Oct 24, 2013

  1. Merge pull request #410 from mojavelinux/kernel-warn

    implemented Kernel#warn
    adambeynon committed Oct 24, 2013
    Copy the full SHA
    1eef4d8 View commit details
Showing with 105 additions and 2 deletions.
  1. +5 −0 corelib/kernel.rb
  2. +13 −0 corelib/opal.rb
  3. +83 −0 spec/opal/kernel/warn_spec.rb
  4. +4 −2 stdlib/stringio.rb
5 changes: 5 additions & 0 deletions corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -428,6 +428,11 @@ def p(*args)

alias print puts

def warn(*strs)
$stderr.puts(*strs) unless $VERBOSE.nil? || strs.empty?
nil
end

def raise(exception = undefined, string = undefined)
%x{
if (exception == null && #$!) {
13 changes: 13 additions & 0 deletions corelib/opal.rb
Original file line number Diff line number Diff line change
@@ -60,6 +60,19 @@ def $stdout.puts(*strs)
nil
end

def $stderr.puts(*strs)
%x{
for (var i = 0; i < strs.length; i++) {
if(strs[i] instanceof Array) {
#{ puts(*`strs[i]`) }
} else {
console.warn(#{`strs[i]`.to_s});
}
}
}
nil
end

RUBY_PLATFORM = 'opal'
RUBY_ENGINE = 'opal'
RUBY_VERSION = '1.9.3'
83 changes: 83 additions & 0 deletions spec/opal/kernel/warn_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'spec_helper'
require 'stringio'

describe 'Kernel#warn' do
before do
@fake_stderr = StringIO.new
end

it 'writes single message to $stderr if $VERBOSE is true' do
old_verbose = $VERBOSE
$VERBOSE = true

captured_stderr {
warn 'this is a warning message'
}.should == 'this is a warning message'

$VERBOSE = old_verbose
end

it 'writes multiple messages to $stderr if $VERBOSE is true' do
old_verbose = $VERBOSE
$VERBOSE = true

captured_stderr {
warn 'this is a warning message', 'this is another'
}.should == "this is a warning message\nthis is another"

$VERBOSE = old_verbose
end

it 'does not write empty message to $stderr if $VERBOSE is true' do
old_verbose = $VERBOSE
$VERBOSE = true

captured_stderr {
warn
}.should be_nil

$VERBOSE = old_verbose
end

it 'does write message to $stderr if $VERBOSE is false' do
old_verbose = $VERBOSE
$VERBOSE = false

captured_stderr {
warn 'this is a warning message'
}.should == 'this is a warning message'

$VERBOSE = old_verbose
end

it 'does not write message to $stderr if $VERBOSE is nil' do
old_verbose = $VERBOSE
$VERBOSE = nil

captured_stderr {
warn 'this is a warning message'
}.should be_nil

$VERBOSE = old_verbose
end

it 'returns a nil value' do
old_verbose = $VERBOSE
$VERBOSE = true

captured_stderr {
(warn 'this is a warning message').should be_nil
}

$VERBOSE = old_verbose
end

def captured_stderr
original_stderr = $stderr
$stderr = @fake_stderr
yield
@fake_stderr.tap(&:rewind).read
ensure
$stderr = original_stderr
end
end
6 changes: 4 additions & 2 deletions stdlib/stringio.rb
Original file line number Diff line number Diff line change
@@ -119,11 +119,13 @@ def read(length = nil, outbuf = nil)
return if eof?

string = if length
@string[@position, length]
str = @string[@position, length]
@position += length
str
else
@string[@position .. -1]
str = @string[@position .. -1]
@position = @string.length
str
end

if outbuf