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: 5dba33fa7194
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 620bd4c85cf3
Choose a head ref
  • 9 commits
  • 3 files changed
  • 2 contributors

Commits on Feb 25, 2015

  1. Copy the full SHA
    bc6ab8a View commit details
  2. Copy the full SHA
    417945f View commit details
  3. Copy the full SHA
    664f6af View commit details
  4. Pass "String#[] with index, length raises a TypeError when the given …

    …index or the given length is nil"
    vais committed Feb 25, 2015
    Copy the full SHA
    c0c42bc View commit details
  5. Pass "String#[] calls to_int on the given index" and "String#[] with …

    …index, length calls to_int on the given index and the given length"
    vais committed Feb 25, 2015
    Copy the full SHA
    51de43c View commit details
  6. Pass "String#[] with index, length raises a TypeError when idx or len…

    …gth can't be converted to an integer"
    vais committed Feb 25, 2015
    Copy the full SHA
    40f6282 View commit details
  7. Implement "String#[] with Range calls to_int on range arguments"

    Unfortunately *fails "String#[] with Range calls to_int on range
    arguments”* and *fails "String#slice with Range calls to_int on range
    arguments”* filters cannot be removed because of an issue with the spec
    itself: it expects `Range` constructor to call `<=>` on the mock object
    it uses for the range literal, but in Opal the `Range` constructor does
    not receive the mock, but receives a `Numeric` instead. Then it calls
    `<=>` on that `Numeric`, not the mock, so the mock’s expectation of
    getting called is not fulfilled. This makes the spec fail like so:
    ```
      1. An exception occurred during: Mock.verify_count
    String#[] with Range calls to_int on range arguments
    
        Mock 'from' expected to receive '<=>' exactly 2 times but received
    it 0 times
    ```
    Here is the spec (the offending line is `
    from.should_receive(:<=>).twice.and_return(0)`)
    ```ruby
      it "calls to_int on range arguments" do
        from = mock('from')
        to = mock('to')
    
        # So we can construct a range out of them...
        from.should_receive(:<=>).twice.and_return(0)
    
        from.should_receive(:to_int).twice.and_return(1)
        to.should_receive(:to_int).twice.and_return(-2)
    
        "hello there".send(@method, from..to).should == "ello ther"
        "hello there".send(@method, from...to).should == "ello the"
      end
    ```
    Not sure who the culprit is here: the spec or the way Opal range
    literals are implemented. Any suggestions?
    vais committed Feb 25, 2015
    Copy the full SHA
    6540f38 View commit details
  8. Copy the full SHA
    c852ce3 View commit details

Commits on Feb 26, 2015

  1. Copy the full SHA
    620bd4c View commit details
Showing with 37 additions and 53 deletions.
  1. +2 −0 CHANGELOG.md
  2. +35 −29 opal/corelib/string.rb
  3. +0 −24 spec/filters/bugs/string.rb
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

* Support calling `String#[]` and `String#slice` with a regexp argument

* `String#[]` and `String#slice` implementation fully compliant with rubyspec

## 0.7.1 2015-02-14

* CLI options `-d` and `-v` now set respectively `$DEBUG` and `$VERBOSE`
64 changes: 35 additions & 29 deletions opal/corelib/string.rb
Original file line number Diff line number Diff line change
@@ -99,8 +99,12 @@ def [](index, length = undefined)
if (index.$$is_range) {
var exclude = index.exclude,
length = index.end,
index = index.begin;
length = #{Opal.coerce_to(`index.end`, Integer, :to_int)},
index = #{Opal.coerce_to(`index.begin`, Integer, :to_int)};
if (Math.abs(index) > size) {
return nil;
}
if (index < 0) {
index += size;
@@ -114,10 +118,6 @@ def [](index, length = undefined)
length += 1;
}
if (index > size) {
return nil;
}
length = length - index;
if (length < 0) {
@@ -128,28 +128,10 @@ def [](index, length = undefined)
}
if (index.$$is_number) {
if (index < 0) {
index += size;
}
if (length == null) {
if (index >= size || index < 0) {
return nil;
}
return self.substr(index, 1);
}
if (index > size || index < 0) {
return nil;
}
return self.substr(index, length);
}
if (index.$$is_string) {
if (length != null) {
#{raise TypeError}
}
return self.indexOf(index) !== -1 ? index : nil;
}
@@ -180,9 +162,33 @@ def [](index, length = undefined)
return nil;
}
}
raise TypeError, "type mismatch: #{index.class} given"
index = #{Opal.coerce_to(`index`, Integer, :to_int)};
if (index < 0) {
index += size;
}
if (length == null) {
if (index >= size || index < 0) {
return nil;
}
return self.substr(index, 1);
}
length = #{Opal.coerce_to(`length`, Integer, :to_int)};
if (length < 0) {
return nil;
}
if (index > size || index < 0) {
return nil;
}
return self.substr(index, length);
}
end

def capitalize
24 changes: 0 additions & 24 deletions spec/filters/bugs/string.rb
Original file line number Diff line number Diff line change
@@ -6,19 +6,7 @@

fails "String#=~ raises a TypeError if a obj is a string"

fails "String#[] calls to_int on the given index"
fails "String#[] calls to_int on the given index"
fails "String#[] raises a TypeError if the given index is nil"
fails "String#[] raises a TypeError if the given index can't be converted to an Integer"
fails "String#[] with index, length returns nil if the length is negative"
fails "String#[] with index, length calls to_int on the given index and the given length"
fails "String#[] with index, length calls to_int on the given index and the given length"
fails "String#[] with index, length raises a TypeError when idx or length can't be converted to an integer"
fails "String#[] with index, length raises a TypeError when the given index or the given length is nil"
fails "String#[] with Range returns nil if the beginning of the range falls outside of self"
fails "String#[] with Range calls to_int on range arguments"
fails "String#[] with Range calls to_int on range arguments"
fails "String#[] with Range handles repeated application"

fails "String#dup does not copy constants defined in the singleton class"
fails "String#dup does not modify the original string when changing dupped string"
@@ -121,19 +109,7 @@
fails "String#rstrip returns a copy of self with trailing whitespace removed"
fails "String#rstrip returns a copy of self with all trailing whitespace and NULL bytes removed"

fails "String#slice calls to_int on the given index"
fails "String#slice calls to_int on the given index"
fails "String#slice raises a TypeError if the given index is nil"
fails "String#slice raises a TypeError if the given index can't be converted to an Integer"
fails "String#slice with index, length returns nil if the length is negative"
fails "String#slice with index, length calls to_int on the given index and the given length"
fails "String#slice with index, length calls to_int on the given index and the given length"
fails "String#slice with index, length raises a TypeError when idx or length can't be converted to an integer"
fails "String#slice with index, length raises a TypeError when the given index or the given length is nil"
fails "String#slice with Range returns nil if the beginning of the range falls outside of self"
fails "String#slice with Range calls to_int on range arguments"
fails "String#slice with Range calls to_int on range arguments"
fails "String#slice with Range handles repeated application"

fails "String#split with String returns subclass instances based on self"
fails "String#split with Regexp respects $KCODE when splitting between characters"