From 7fc9dfcb2400aa0a97cad85ee0d16713e30fec8b Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 21 Feb 2013 14:29:39 -0600 Subject: [PATCH 1/4] added tests for == and $ --- tests/run/tobject.nim | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/run/tobject.nim diff --git a/tests/run/tobject.nim b/tests/run/tobject.nim new file mode 100644 index 0000000000..b6a2461ac4 --- /dev/null +++ b/tests/run/tobject.nim @@ -0,0 +1,20 @@ +import unittest + +type Obj = object + foo: int + +proc makeObj(x: int): ref Obj = + new(result) + result.foo = x + +proc initObject(x: int): Obj = + result.foo = x + +suite "object basic methods": + test "it should convert an objcet to a string": + var obj = makeObj(1) + discard $obj + test "it should test equality based on fields": + check(initObj(1) == initObj(1)) + test "it should test equality based on fields for refs too": + check(makeObj(1) == makeObj(1)) From bf82f79f1e27b4580672af457a95f3d85ea781a4 Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 21 Feb 2013 15:11:48 -0600 Subject: [PATCH 2/4] added tests, actually implemented $ and == --- lib/system.nim | 4 ++-- tests/run/tobject.nim | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 5f9a24ba9a..db78d27405 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1532,7 +1532,7 @@ iterator fieldPairs*[S: tuple|object, T: tuple|object](x: S, y: T): tuple[ ## The current implementation also has a bug that affects symbol binding ## in the loop body. -proc `==`*[T: tuple](x, y: T): bool = +proc `==`*[T: tuple|object](x, y: T): bool = ## generic ``==`` operator for tuples that is lifted from the components ## of `x` and `y`. for a, b in fields(x, y): @@ -1557,7 +1557,7 @@ proc `<`*[T: tuple](x, y: T): bool = if c > 0: return false return false -proc `$`*[T: tuple](x: T): string = +proc `$`*[T: tuple|object](x: T): string = ## generic ``$`` operator for tuples that is lifted from the components ## of `x`. Example: ## diff --git a/tests/run/tobject.nim b/tests/run/tobject.nim index b6a2461ac4..246f2862b0 100644 --- a/tests/run/tobject.nim +++ b/tests/run/tobject.nim @@ -13,7 +13,8 @@ proc initObject(x: int): Obj = suite "object basic methods": test "it should convert an objcet to a string": var obj = makeObj(1) - discard $obj + # Should be "obj: (foo: 1)" or similar. + check($obj == "(foo: 1)") test "it should test equality based on fields": check(initObj(1) == initObj(1)) test "it should test equality based on fields for refs too": From e366eeaafc0b834cd57d0aa4bc16925271c4a79f Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 21 Feb 2013 16:31:35 -0600 Subject: [PATCH 3/4] added $ for refs and removed == for ref test == in refs should use the pointer to compare --- lib/system.nim | 2 ++ tests/run/tobject.nim | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index db78d27405..3f15dbeaf2 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1557,6 +1557,8 @@ proc `<`*[T: tuple](x, y: T): bool = if c > 0: return false return false +proc `$`*[T: ref](x: T): string = $x[] + proc `$`*[T: tuple|object](x: T): string = ## generic ``$`` operator for tuples that is lifted from the components ## of `x`. Example: diff --git a/tests/run/tobject.nim b/tests/run/tobject.nim index 246f2862b0..b2fd212366 100644 --- a/tests/run/tobject.nim +++ b/tests/run/tobject.nim @@ -7,15 +7,19 @@ proc makeObj(x: int): ref Obj = new(result) result.foo = x -proc initObject(x: int): Obj = +proc initObj(x: int): Obj = result.foo = x -suite "object basic methods": - test "it should convert an objcet to a string": - var obj = makeObj(1) +template stringTest(init: expr) = + test "it should convert an object to a string": + var obj = `init`(1) # Should be "obj: (foo: 1)" or similar. check($obj == "(foo: 1)") - test "it should test equality based on fields": - check(initObj(1) == initObj(1)) - test "it should test equality based on fields for refs too": - check(makeObj(1) == makeObj(1)) + +suite "object basic methods": + suite "ref": + stringTest(makeObj) + suite "value": + stringTest(initObj) + test "it should test equality based on fields": + check(initObj(1) == initObj(1)) From 7168ceb5e1ec6686b13f5da1f2c9a4db07ac90ec Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 21 Feb 2013 16:37:22 -0600 Subject: [PATCH 4/4] removed `$` for refs upon request --- lib/system.nim | 2 -- tests/run/tobject.nim | 24 +++++++----------------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 3f15dbeaf2..db78d27405 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1557,8 +1557,6 @@ proc `<`*[T: tuple](x, y: T): bool = if c > 0: return false return false -proc `$`*[T: ref](x: T): string = $x[] - proc `$`*[T: tuple|object](x: T): string = ## generic ``$`` operator for tuples that is lifted from the components ## of `x`. Example: diff --git a/tests/run/tobject.nim b/tests/run/tobject.nim index b2fd212366..5fec844417 100644 --- a/tests/run/tobject.nim +++ b/tests/run/tobject.nim @@ -3,23 +3,13 @@ import unittest type Obj = object foo: int -proc makeObj(x: int): ref Obj = - new(result) +proc makeObj(x: int): Obj = result.foo = x -proc initObj(x: int): Obj = - result.foo = x - -template stringTest(init: expr) = - test "it should convert an object to a string": - var obj = `init`(1) - # Should be "obj: (foo: 1)" or similar. - check($obj == "(foo: 1)") - suite "object basic methods": - suite "ref": - stringTest(makeObj) - suite "value": - stringTest(initObj) - test "it should test equality based on fields": - check(initObj(1) == initObj(1)) + test "it should convert an object to a string": + var obj = makeObj(1) + # Should be "obj: (foo: 1)" or similar. + check($obj == "(foo: 1)") + test "it should test equality based on fields": + check(makeObj(1) == makeObj(1))