diff options
Diffstat (limited to 'tests/objects')
-rw-r--r-- | tests/objects/t20972.nim | 15 | ||||
-rw-r--r-- | tests/objects/t22301.nim | 17 | ||||
-rw-r--r-- | tests/objects/tdefaultfieldscheck.nim | 9 | ||||
-rw-r--r-- | tests/objects/tillegal_recursion.nim | 2 | ||||
-rw-r--r-- | tests/objects/tillegal_recursion2.nim | 6 | ||||
-rw-r--r-- | tests/objects/tillegal_recursion3.nim | 6 | ||||
-rw-r--r-- | tests/objects/tobject_default_value.nim | 261 | ||||
-rw-r--r-- | tests/objects/trequireinit.nim | 10 | ||||
-rw-r--r-- | tests/objects/twhen1.nim | 32 |
9 files changed, 347 insertions, 11 deletions
diff --git a/tests/objects/t20972.nim b/tests/objects/t20972.nim new file mode 100644 index 000000000..6383dc9b1 --- /dev/null +++ b/tests/objects/t20972.nim @@ -0,0 +1,15 @@ +discard """ + matrix: "--mm:refc -d:release; --mm:orc -d:release" +""" + +{.passC: "-fsanitize=undefined -fsanitize-undefined-trap-on-error -Wall -Wextra -pedantic -flto".} +{.passL: "-fsanitize=undefined -fsanitize-undefined-trap-on-error -flto".} + +# bug #20972 +type ForkedEpochInfo = object + case kind: bool + of true, false: discard +var info = ForkedEpochInfo(kind: true) +doAssert info.kind +info.kind = false +doAssert not info.kind diff --git a/tests/objects/t22301.nim b/tests/objects/t22301.nim new file mode 100644 index 000000000..8746bf584 --- /dev/null +++ b/tests/objects/t22301.nim @@ -0,0 +1,17 @@ +discard """ + errormsg: "branch initialization with a runtime discriminator is not supported for a branch whose fields have default values." +""" + +# bug #22301 +type + Enum = enum A, B + Object = object + case a: Enum + of A: + integer: int = 200 + of B: + time: string + +let x = A +let s = Object(a: x) +echo s \ No newline at end of file diff --git a/tests/objects/tdefaultfieldscheck.nim b/tests/objects/tdefaultfieldscheck.nim index d6feb2988..8a05439d9 100644 --- a/tests/objects/tdefaultfieldscheck.nim +++ b/tests/objects/tdefaultfieldscheck.nim @@ -4,16 +4,13 @@ discard """ nimout: ''' tdefaultfieldscheck.nim(14, 17) Error: type mismatch: got <string> but expected 'int' -tdefaultfieldscheck.nim(15, 20) Error: type mismatch: got <int literal(12)> but expected 'string' -tdefaultfieldscheck.nim(17, 16) Error: type mismatch: got <float64> but expected 'int' ''' """ + type Date* = object - name: int = "string" - time: string = 12 goal: float = 7 - fun: int = 1.4 + name: int = "string" -echo default(Date) \ No newline at end of file +echo default(Date) diff --git a/tests/objects/tillegal_recursion.nim b/tests/objects/tillegal_recursion.nim index 222139101..428c2e445 100644 --- a/tests/objects/tillegal_recursion.nim +++ b/tests/objects/tillegal_recursion.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "inheritance only works with non-final objects" + errormsg: "Cannot inherit from: 'Foo:ObjectType'" line: 7 """ # bug #1691 diff --git a/tests/objects/tillegal_recursion2.nim b/tests/objects/tillegal_recursion2.nim new file mode 100644 index 000000000..ce2279f04 --- /dev/null +++ b/tests/objects/tillegal_recursion2.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: "Cannot inherit from: 'Foo'" + line: 6 +""" +type + Foo = object of Foo diff --git a/tests/objects/tillegal_recursion3.nim b/tests/objects/tillegal_recursion3.nim new file mode 100644 index 000000000..c80f29e28 --- /dev/null +++ b/tests/objects/tillegal_recursion3.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: "Cannot inherit from: 'Foo'" + line: 6 +""" +type + Foo = object of ref Foo diff --git a/tests/objects/tobject_default_value.nim b/tests/objects/tobject_default_value.nim index cdc6016e0..0cd05e4f3 100644 --- a/tests/objects/tobject_default_value.nim +++ b/tests/objects/tobject_default_value.nim @@ -3,7 +3,7 @@ discard """ targets: "c cpp js" """ -import std/[times, macros] +import std/[times, macros, tables] type Guess = object @@ -121,7 +121,7 @@ template main {.dirty.} = rVal: R = default(R) # Works fine objVal = default(Obj) - doAssert rVal == 0 # it should be 1 + doAssert rVal == 1 doAssert objVal.r == 1 block: # bug #16744 @@ -134,7 +134,7 @@ template main {.dirty.} = rVal: R = default(R) # Works fine objVal = Obj() - doAssert rVal == 0 # it should be 1 + doAssert rVal == 1 # it should be 1 doAssert objVal.r == 1 block: # bug #3608 @@ -236,6 +236,7 @@ template main {.dirty.} = doAssert x.obj.name.scale == 1 when nimvm: + # todo discard "fixme" else: when defined(gcArc) or defined(gcOrc): @@ -376,7 +377,7 @@ template main {.dirty.} = Red, Blue, Yellow type - ObjectVarint3 = object # fixme it doesn't work with static + ObjectVarint3 = object case kind: Color = Blue of Red: data1: int = 10 @@ -523,5 +524,257 @@ template main {.dirty.} = discard oToEither(O()) + block: # bug #20695 + type + Default = object + tabs: Table[string, int] = initTable[string, int]() + + let d = default(Default) + doAssert d.tabs.len == 0 + + block: + type + Default = object + tabs: Table[string, int] = Table[string, int]() + + let d = default(Default) + doAssert d.tabs.len == 0 + + + block: + type DjangoDateTime = distinct DateTime + + type Default = object + data: DjangoDateTime = DjangoDateTime(DateTime()) + + let x = default(Default) + doAssert x.data is DjangoDateTime + + block: + type DjangoDateTime = distinct DateTime + + type Default = object + data = DjangoDateTime(DateTime()) + + let x = default(Default) + doAssert x.data is DjangoDateTime + + block: + type + Result2 = object + case o: bool + of false: + e: float + of true: + v {.requiresInit.} : int = 1 + + proc startSessionSync(): Result2 = + return Result2(o: true) + + proc mainSync = + let ff = startSessionSync() + doAssert ff.v == 1 + + mainSync() + + block: + type + Result2 = object + v {.requiresInit.} : int = 1 + + proc startSessionSync(): Result2 = + return Result2() + + proc mainSync = + let ff = startSessionSync() + doAssert ff.v == 1 + + mainSync() + + block: # bug #21801 + func evaluate(i: int): float = + 0.0 + + func evaluate(): float = + 0.0 + + type SearchOptions = object + evaluation: proc(): float = evaluate + + block: + func evaluate(): float = + 0.0 + + type SearchOptions = object + evaluation: proc(): float = evaluate + + block: + func evaluate(i: int): float = + 0.0 + + type SearchOptions = object + evaluation = evaluate + block: + type + Result[T, E] = object + when T is void: + when E is void: + oResultPrivate: bool + else: + case oResultPrivate: bool + of false: + eResultPrivate: E + of true: + discard + else: + when E is void: + case oResultPrivate: bool + of false: + discard + of true: + vResultPrivate: T + else: + case oResultPrivate: bool + of false: + eResultPrivate: E + of true: + vResultPrivate: T + + + template `?`[T, E](self: Result[T, E]): auto = + let v = (self) + if not v.oResultPrivate: + when compiles(`assignResult?`(default(typeof(result)))): + when typeof(result) is typeof(v): + `assignResult?`(v) + elif E is void: + `assignResult?`(err(typeof(result))) + else: + `assignResult?`(err(typeof(result), v.eResultPrivate)) + return + else: + return + when typeof(result) is typeof(v): + v + elif E is void: + err(typeof(result)) + else: + err(typeof(result), v.eResultPrivate) + + when not(T is void): + v.vResultPrivate + + type R = Result[int, string] + + proc testAssignResult() = + var assigned: bool + template `assignResult?`(v: Result) = + assigned = true + result = v + + proc failed(): Result[int, string] = + discard + + proc calling(): Result[int, string] = + let _ = ? failed() + doAssert false + + let r = calling() + doAssert assigned + + when nimvm: + when not defined(js): + testAssignResult() + else: + testAssignResult() + + block: # bug #22123 + type Thing = object + x: float32 = 1 + + type ThingWithArray = object + arr: array[256, float32] + n: float32 = 1 + + type Container = ref object + thing: array[5, Thing] + thing_with_array: array[5, ThingWithArray] + + var foo = new Container + doAssert int(foo.thing[0].x) == 1 + + block: # bug #22613 + type + K = enum + A = "a" + B = "b" + T = object + case kind: K = B + of A: + a: int + of B: + b: float + + doAssert T().kind == B + + block: # bug #22926 + type + Direction = enum + North + South + East + West + + ArrayObj1 = object + list: array[Direction, int] + + ArrayObj2 = object + list: array[Direction, int] = [1, 2, 3, 4] + + block: + var a: ArrayObj1 + doAssert a.list[West] == 0 + var b = default ArrayObj1 + doAssert b.list[North] == 0 + + + block: + var a: ArrayObj2 + doAssert a.list[West] == 0 + var b = default ArrayObj2 + doAssert b.list[North] == 1 + + block: + type limited_float = range[1.2..20.0] + doAssert default(limited_float) == 1.2 + + + block: + type + range1 = range[1..10] + range2 = range[-1..10] + + proc foo = + doAssert default(range1) == 1 + doAssert default(range2) == -1 + + let s = default(array[5, range1]) + doAssert s == [range1 1, 1, 1, 1, 1] + + foo() + + block: + type + Object = object + id: range[1.2..29.3] + + var s = default(Object) + doAssert s.id == 1.2 + + block: # bug #23943 + type limited_int = range[1..20] + var d: limited_int; + doAssert d == 1 + static: main() main() diff --git a/tests/objects/trequireinit.nim b/tests/objects/trequireinit.nim new file mode 100644 index 000000000..202667b02 --- /dev/null +++ b/tests/objects/trequireinit.nim @@ -0,0 +1,10 @@ +discard """ + errormsg: "The MPlayerObj type doesn't have a default value. The following fields must be initialized: foo." +""" + +type + MPlayerObj* {.requiresInit.} = object + foo: range[5..10] = 5 + +var a: MPlayerObj +echo a.foo \ No newline at end of file diff --git a/tests/objects/twhen1.nim b/tests/objects/twhen1.nim index 5b8eea3f4..fe072a46b 100644 --- a/tests/objects/twhen1.nim +++ b/tests/objects/twhen1.nim @@ -55,3 +55,35 @@ type x: int when (NimMajor, NimMinor) >= (1, 1): y: int +discard MyObject(x: 100, y: 200) + +block: # Ensure when evaluates properly in objects + type X[bits: static int] = object #22474 + when bits >= 256: + data32: byte + else: + data16: byte + + static: + discard X[255]().data16 + discard X[256]().data32 + + + type ComplexExprObject[S: static string, I: static int, Y: static auto] = object + when 'h' in S and I < 10 and Y isnot float: + a: int + elif I > 30: + b: int + elif typeof(Y) is float: + c: int + else: + d: int + + static: + discard ComplexExprObject["hello", 9, 300i32]().a + discard ComplexExprObject["", 40, 30f]().b + discard ComplexExprObject["", 20, float 30]().c + discard ComplexExprObject["", 20, ""]().d + + + |