Add tests for std::range and expose via prelude

This adds tests for std::range, and exposes std::range::Range to every
module via the prelude. This removes the need for an explicit import of
`std::range` just to write code such as `1..5`.
parent ea9f55ad
Pipeline #39500630 passed with stages
in 12 minutes and 56 seconds
......@@ -35,7 +35,9 @@ import std::hash_map::(HashMap as _HashMap)
import std::inspect
import std::byte_array
import std::vm
import std::range::(Range as _Range)
# These constants are re-exported so they're available to all modules by
# default. Core types such as String should be exposed in std::globals instead.
let HashMap = _HashMap
let Range = _Range
......@@ -17,7 +17,7 @@ import std::successor::Successor
##
## 1. They are both of the same type.
## 2. Both implement the `Successor` and `Compare` traits.
object Range!(T: Successor + Compare) {
object Range!(T: Successor + Compare) impl Equal, ToArray!(T) {
def init(start: T, end: T) {
let @start = start
let @end = end
......@@ -32,14 +32,7 @@ object Range!(T: Successor + Compare) {
def end -> T {
@end
}
}
trait ToRange!(T: Successor + Compare) {
## Returns a `Range` starting at `self` up to (and including) `other`.
def ..(other: T) -> Range!(T)
}
impl Equal for Range!(T) {
## Returns `True` if `self` and `other` are identical.
##
## # Examples
......@@ -55,9 +48,7 @@ impl Equal for Range!(T) {
start == other.start
.and { end == other.end }
}
}
impl ToArray!(T) for Range!(T) {
## Converts `self` to an `Array`.
##
## # Examples
......@@ -80,6 +71,11 @@ impl ToArray!(T) for Range!(T) {
}
}
trait ToRange!(T: Successor + Compare) {
## Returns a `Range` starting at `self` up to (and including) `other`.
def ..(other: T) -> Range!(T)
}
impl ToRange!(Integer) for Integer {
## Returns a `Range` starting at `self` up to (and including) `other`.
##
......
......@@ -28,6 +28,7 @@ import test::std::test_mirror
import test::std::test_nil
import test::std::test_object
import test::std::test_os
import test::std::test_range
import test::std::test_string
import test::std::test_string_buffer
......
import std::range
import std::test
import std::test::assert
test.group('std::range::Range.start') do (g) {
g.test('Obtaining the start of a Range') {
let range = Range.new(start: 1, end: 10)
assert.equal(range.start, 1)
}
}
test.group('std::range::Range.end') do (g) {
g.test('Obtaining the end of a Range') {
let range = Range.new(start: 1, end: 10)
assert.equal(range.end, 10)
}
}
test.group('std::range::Range.==') do (g) {
g.test('Comparing two identical Ranges') {
let range1 = Range.new(start: 1, end: 10)
let range2 = Range.new(start: 1, end: 10)
assert.equal(range1, range2)
}
g.test('Comparing two different Ranges') {
let range1 = Range.new(start: 1, end: 10)
let range2 = Range.new(start: 1, end: 15)
assert.not_equal(range1, range2)
}
}
test.group('std::range::Range.to_array') do (g) {
g.test('Converting a Range with the same start and end to an Array') {
let range = Range.new(start: 1, end: 1)
assert.equal(range.to_array, [1])
}
g.test('Converting a Range with a different start and end to an Array') {
let range = Range.new(start: 1, end: 3)
assert.equal(range.to_array, [1, 2, 3])
}
g.test('Converting a Range with a smaller end to an Array') {
let range = Range.new(start: 1, end: -5)
assert.equal(range.to_array, [])
}
}
test.group('std::integer::Integer...') do (g) {
g.test('Generating a Range of Integer objects') {
let range = 1..3
assert.equal(range.start, 1)
assert.equal(range.end, 3)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment