Merge tests into a larger file (part 5 of ∞) (#9368)

* merge magics

* merge metatype tests

* merge method tests

* merge objects tests

* change `import future` to `import sugar`

Nim in Action tests are left with `import future`, to ensure compatibility.

* merge overload tests

* merge proc tests

* merge procvar tests

* merge range tests

* merge seq tests

* merge sets tests

* remove wrong assert from `tsets3`

* fix `jsTests`

* better fix
This commit is contained in:
Miran 2018-10-16 10:50:10 +02:00 • committed by Andreas Rumpf
commit 749dbce4c6
96 changed files with 1763 additions and 1815 deletions

View file

@ -1,24 +0,0 @@
discard """
output: '''wof!
wof!'''
"""
# bug #1659
type Animal = ref object {.inheritable.}
type Dog = ref object of Animal
method say(a: Animal): auto {.base.} = "wat!"
method say(a: Dog): auto = "wof!"
proc saySomething(a: Animal): auto = a.say()
method ec(a: Animal): auto {.base.} = echo "wat!"
method ec(a: Dog): auto = echo "wof!"
proc ech(a: Animal): auto = a.ec()
var a = Dog()
echo saySomething(a)
ech a

View file

@ -1,11 +0,0 @@
# bug #2401
type MyClass = ref object of RootObj
method HelloWorld*(obj: MyClass) {.base.} =
when defined(myPragma):
echo("Hello World")
# discard # with this line enabled it works
var obj = MyClass()
obj.HelloWorld()

View file

@ -1,15 +0,0 @@
#5432
type
Iterator[T] = ref object of RootObj
# base methods with `T` in the return type are okay
method methodThatWorks*[T](i: Iterator[T]): T {.base.} =
discard
# base methods without `T` (void or basic types) fail
method methodThatFails*[T](i: Iterator[T]) {.base.} =
discard
type
SpecificIterator1 = ref object of Iterator[string]
SpecificIterator2 = ref object of Iterator[int]

128
tests/method/tissues.nim Normal file
View file

@ -0,0 +1,128 @@
discard """
output: '''
wof!
wof!
'''
"""
# bug #1659
type Animal = ref object {.inheritable.}
type Dog = ref object of Animal
method say(a: Animal): auto {.base.} = "wat!"
method say(a: Dog): auto = "wof!"
proc saySomething(a: Animal): auto = a.say()
method ec(a: Animal): auto {.base.} = echo "wat!"
method ec(a: Dog): auto = echo "wof!"
proc ech(a: Animal): auto = a.ec()
var a = Dog()
echo saySomething(a)
ech a
# bug #2401
type MyClass = ref object of RootObj
method HelloWorld*(obj: MyClass) {.base.} =
when defined(myPragma):
echo("Hello World")
# discard # with this line enabled it works
var obj = MyClass()
obj.HelloWorld()
# bug #5432
type
Iterator[T] = ref object of RootObj
# base methods with `T` in the return type are okay
method methodThatWorks*[T](i: Iterator[T]): T {.base.} =
discard
# base methods without `T` (void or basic types) fail
method methodThatFails*[T](i: Iterator[T]) {.base.} =
discard
type
SpecificIterator1 = ref object of Iterator[string]
SpecificIterator2 = ref object of Iterator[int]
# bug #3431
type
Lexer = object
buf*: string
pos*: int
lastchar*: char
ASTNode = object
method init*(self: var Lexer; buf: string) {.base.} =
self.buf = buf
self.pos = 0
self.lastchar = self.buf[0]
method init*(self: var ASTNode; val: string) =
discard
# bug #3370
type
RefTestA*[T] = ref object of RootObj
data*: T
method tester*[S](self: S): bool =
true
type
RefTestB* = RefTestA[(string, int)]
method tester*(self: RefTestB): bool =
true
type
RefTestC = RefTestA[string]
method tester*(self: RefTestC): bool =
false
# bug #3468
type X = ref object of RootObj
type Y = ref object of RootObj
method draw*(x: X) {.base.} = discard
method draw*(y: Y) {.base.} = discard
# bug #3550
type
BaseClass = ref object of RootObj
Class1 = ref object of BaseClass
Class2 = ref object of BaseClass
method test(obj: Class1, obj2: BaseClass) =
discard
method test(obj: Class2, obj2: BaseClass) =
discard
var obj1 = Class1()
var obj2 = Class2()
obj1.test(obj2)
obj2.test(obj1)

View file

@ -1,22 +0,0 @@
discard """
output: "do nothing"
"""
method somethin(obj: RootObj) {.base.} =
echo "do nothing"
type
TNode* = object {.inheritable.}
PNode* = ref TNode
PNodeFoo* = ref object of TNode
TSomethingElse = object
PSomethingElse = ref TSomethingElse
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
method foo(a: PNodeFoo, b: PSomethingElse) = discard
var o: RootObj
o.somethin()

View file

@ -1,25 +0,0 @@
type
Obj1 = ref object {.inheritable.}
Obj2 = ref object of Obj1
method beta(x: Obj1): int {.base.}
proc delta(x: Obj2): int =
beta(x)
method beta(x: Obj2): int
proc alpha(x: Obj1): int =
beta(x)
method beta(x: Obj1): int = 1
method beta(x: Obj2): int = 2
proc gamma(x: Obj1): int =
beta(x)
doAssert alpha(Obj1()) == 1
doAssert gamma(Obj1()) == 1
doAssert alpha(Obj2()) == 2
doAssert gamma(Obj2()) == 2
doAssert delta(Obj2()) == 2

93
tests/method/tmultim.nim Normal file
View file

@ -0,0 +1,93 @@
discard """
output: '''
collide: unit, thing
collide: unit, thing
collide: thing, unit
collide: thing, thing
collide: unit, thing | collide: unit, thing | collide: thing, unit |
do nothing
'''
"""
# tmultim2
type
TThing = object {.inheritable.}
TUnit = object of TThing
x: int
TParticle = object of TThing
a, b: int
method collide(a, b: TThing) {.base, inline.} =
echo "collide: thing, thing"
method collide(a: TThing, b: TUnit) {.inline.} =
echo "collide: thing, unit"
method collide(a: TUnit, b: TThing) {.inline.} =
echo "collide: unit, thing"
proc test(a, b: TThing) {.inline.} =
collide(a, b)
proc staticCollide(a, b: TThing) {.inline.} =
procCall collide(a, b)
var
a: TThing
b, c: TUnit
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
test(b, c)
collide(a, b)
staticCollide(a, b)
# tmultim6
type
Thing = object {.inheritable.}
Unit[T] = object of Thing
x: T
Particle = object of Thing
a, b: int
method collide(a, b: Thing) {.base, inline.} =
quit "to override!"
method collide[T](a: Thing, b: Unit[T]) {.inline.} =
write stdout, "collide: thing, unit | "
method collide[T](a: Unit[T], b: Thing) {.inline.} =
write stdout, "collide: unit, thing | "
proc test(a, b: Thing) {.inline.} =
collide(a, b)
var
aaa: Thing
bbb, ccc: Unit[string]
collide(bbb, Thing(ccc))
test(bbb, ccc)
collide(aaa, bbb)
echo ""
# tmethods1
method somethin(obj: RootObj) {.base.} =
echo "do nothing"
type
TNode* = object {.inheritable.}
PNode* = ref TNode
PNodeFoo* = ref object of TNode
TSomethingElse = object
PSomethingElse = ref TSomethingElse
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
method foo(a: PNodeFoo, b: PSomethingElse) = discard
var o: RootObj
o.somethin()

View file

@ -1,29 +0,0 @@
discard """
file: "tmultim1.nim"
output: "7"
"""
# Test multi methods
type
Expression = ref object {.inheritable.}
Literal = ref object of Expression
x: int
PlusExpr = ref object of Expression
a, b: Expression
method eval(e: Expression): int {.base.} = quit "to override!"
method eval(e: Literal): int = return e.x
method eval(e: PlusExpr): int = return eval(e.a) + eval(e.b)
proc newLit(x: int): Literal =
new(result)
result.x = x
proc newPlus(a, b: Expression): PlusExpr =
new(result)
result.a = a
result.b = b
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7

View file

@ -1,39 +0,0 @@
discard """
file: "tmultim2.nim"
output: '''collide: unit, thing
collide: unit, thing
collide: thing, unit
collide: thing, thing'''
"""
# Test multi methods
type
TThing = object {.inheritable.}
TUnit = object of TThing
x: int
TParticle = object of TThing
a, b: int
method collide(a, b: TThing) {.base, inline.} =
echo "collide: thing, thing"
method collide(a: TThing, b: TUnit) {.inline.} =
echo "collide: thing, unit"
method collide(a: TUnit, b: TThing) {.inline.} =
echo "collide: unit, thing"
proc test(a, b: TThing) {.inline.} =
collide(a, b)
proc staticCollide(a, b: TThing) {.inline.} =
procCall collide(a, b)
var
a: TThing
b, c: TUnit
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
test(b, c)
collide(a, b)
staticCollide(a, b)

View file

@ -1,20 +0,0 @@
discard """
file: "tmultim3.nim"
output: "Hi derived!"
"""
import mmultim3
type
TBObj* = object of TObj
method test123(a : ref TBObj) =
echo("Hi derived!")
var a : ref TBObj
new(a)
myObj = a
testMyObj()

View file

@ -1,47 +0,0 @@
discard """
file: "tmultim4.nim"
output: "hello"
"""
type
Test = object of RootObj
method doMethod(a: ref RootObj) {.base, raises: [IoError].} =
quit "override"
method doMethod(a: ref Test) =
echo "hello"
if a == nil:
raise newException(IoError, "arg")
proc doProc(a: ref Test) =
echo "hello"
proc newTest(): ref Test =
new(result)
var s:ref Test = newTest()
#doesn't work
for z in 1..4:
s.doMethod()
break
#works
#for z in 1..4:
# s.doProc()
# break
#works
#while true:
# s.doMethod()
# break
#works
#while true:
# s.doProc()
# break

View file

@ -1,30 +0,0 @@
discard """
output: "collide: unit, thing | collide: unit, thing | collide: thing, unit |"
"""
# Test multi methods
type
Thing = object {.inheritable.}
Unit[T] = object of Thing
x: T
Particle = object of Thing
a, b: int
method collide(a, b: Thing) {.base, inline.} =
quit "to override!"
method collide[T](a: Thing, b: Unit[T]) {.inline.} =
write stdout, "collide: thing, unit | "
method collide[T](a: Unit[T], b: Thing) {.inline.} =
write stdout, "collide: unit, thing | "
proc test(a, b: Thing) {.inline.} =
collide(a, b)
var
a: Thing
b, c: Unit[string]
collide(b, Thing(c))
test(b, c)
collide(a, b)

View file

@ -1,48 +0,0 @@
# bug #3431
type
Lexer = object
buf*: string
pos*: int
lastchar*: char
ASTNode = object
method init*(self: var Lexer; buf: string) {.base.} =
self.buf = buf
self.pos = 0
self.lastchar = self.buf[0]
method init*(self: var ASTNode; val: string) =
discard
# bug #3370
type
RefTestA*[T] = ref object of RootObj
data*: T
method tester*[S](self: S): bool =
true
type
RefTestB* = RefTestA[(string, int)]
method tester*(self: RefTestB): bool =
true
type
RefTestC = RefTestA[string]
method tester*(self: RefTestC): bool =
false
# bug #3468
type X = ref object of RootObj
type Y = ref object of RootObj
method draw*(x: X) {.base.} = discard
method draw*(y: Y) {.base.} = discard

View file

@ -1,19 +0,0 @@
# bug #3550
type
BaseClass = ref object of RootObj
Class1 = ref object of BaseClass
Class2 = ref object of BaseClass
method test(obj: Class1, obj2: BaseClass) =
discard
method test(obj: Class2, obj2: BaseClass) =
discard
var obj1 = Class1()
var obj2 = Class2()
obj1.test(obj2)
obj2.test(obj1)

View file

@ -0,0 +1,72 @@
discard """
output: '''
7
Hi derived!
hello
'''
"""
# tmultim1
type
Expression = ref object {.inheritable.}
Literal = ref object of Expression
x: int
PlusExpr = ref object of Expression
a, b: Expression
method eval(e: Expression): int {.base.} = quit "to override!"
method eval(e: Literal): int = return e.x
method eval(e: PlusExpr): int = return eval(e.a) + eval(e.b)
proc newLit(x: int): Literal =
new(result)
result.x = x
proc newPlus(a, b: Expression): PlusExpr =
new(result)
result.a = a
result.b = b
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7
# tmultim3
import mmultim3
type TBObj* = object of TObj
method test123(a : ref TBObj) =
echo("Hi derived!")
var aa: ref TBObj
new(aa)
myObj = aa
testMyObj()
# tmultim4
type Test = object of RootObj
method doMethod(a: ref RootObj) {.base, raises: [IoError].} =
quit "override"
method doMethod(a: ref Test) =
echo "hello"
if a == nil:
raise newException(IoError, "arg")
proc doProc(a: ref Test) =
echo "hello"
proc newTest(): ref Test =
new(result)
var s:ref Test = newTest()
#doesn't work
for z in 1..4:
s.doMethod()
break

View file

@ -1,22 +0,0 @@
# Note: We only compile this to verify that code generation
# for recursive methods works, no code is being executed
type
Obj = ref object of RootObj
# Mutual recursion
method alpha(x: Obj) {.base.}
method beta(x: Obj) {.base.}
method alpha(x: Obj) =
beta(x)
method beta(x: Obj) =
alpha(x)
# Simple recursion
method gamma(x: Obj) {.base.} =
gamma(x)

View file

@ -1,14 +0,0 @@
discard """
file: "tsimmeth.nim"
output: "HELLO WORLD!"
"""
# Test method simulation
import strutils
var x = "hello world!".toLowerAscii.toUpperAscii
x.echo()
#OUT HELLO WORLD!

83
tests/method/tvarious.nim Normal file
View file

@ -0,0 +1,83 @@
discard """
output: '''
do nothing
HELLO WORLD!
'''
"""
# tmethods1
method somethin(obj: RootObj) {.base.} =
echo "do nothing"
type
TNode* = object {.inheritable.}
PNode* = ref TNode
PNodeFoo* = ref object of TNode
TSomethingElse = object
PSomethingElse = ref TSomethingElse
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
method foo(a: PNodeFoo, b: PSomethingElse) = discard
var o: RootObj
o.somethin()
# tmproto
type
Obj1 = ref object {.inheritable.}
Obj2 = ref object of Obj1
method beta(x: Obj1): int {.base.}
proc delta(x: Obj2): int =
beta(x)
method beta(x: Obj2): int
proc alpha(x: Obj1): int =
beta(x)
method beta(x: Obj1): int = 1
method beta(x: Obj2): int = 2
proc gamma(x: Obj1): int =
beta(x)
doAssert alpha(Obj1()) == 1
doAssert gamma(Obj1()) == 1
doAssert alpha(Obj2()) == 2
doAssert gamma(Obj2()) == 2
doAssert delta(Obj2()) == 2
# tsimmeth
import strutils
var x = "hello world!".toLowerAscii.toUpperAscii
x.echo()
# trecmeth
# Note: We only compile this to verify that code generation
# for recursive methods works, no code is being executed
type Obj = ref object of RootObj
# Mutual recursion
method alpha(x: Obj) {.base.}
method beta(x: Obj) {.base.}
method alpha(x: Obj) =
beta(x)
method beta(x: Obj) =
alpha(x)
# Simple recursion
method gamma(x: Obj) {.base.} =
gamma(x)