From 5850977643695a3e36f88dd60254da3773affb5e Mon Sep 17 00:00:00 2001 From: CodeDoes Date: Wed, 20 Jun 2018 18:51:20 +0200 Subject: [PATCH] made a nimble package --- oop_macro.nimble | 11 +++++ oop_macro.nim => src/oop_macro.nim | 0 tests/test1.nim | 66 ++++++++++++++++++++++++++++++ tests/test1.nims | 1 + 4 files changed, 78 insertions(+) create mode 100644 oop_macro.nimble rename oop_macro.nim => src/oop_macro.nim (100%) create mode 100644 tests/test1.nim create mode 100644 tests/test1.nims diff --git a/oop_macro.nimble b/oop_macro.nimble new file mode 100644 index 0000000..9162690 --- /dev/null +++ b/oop_macro.nimble @@ -0,0 +1,11 @@ +# Package + +version = "0.1.0" +author = "CodeDoes" +description = "OOP macro" +license = "MIT" +srcDir = "src" + +# Dependencies + +requires "nim >= 0.18.0" diff --git a/oop_macro.nim b/src/oop_macro.nim similarity index 100% rename from oop_macro.nim rename to src/oop_macro.nim diff --git a/tests/test1.nim b/tests/test1.nim new file mode 100644 index 0000000..ccb240f --- /dev/null +++ b/tests/test1.nim @@ -0,0 +1,66 @@ +import oop_macro + +import strutils + +var + output = "" + +class Animal of RootObj: + var + name: string + age: int + + + method init*(name: string, age: int){.base.}= + self.name = name + self.age = age + output &= "I am a new Animal, " & self.name & "\n" + method stuff(s:string): string {.base.}= s + method vocalize: string {.base.}= "..." + method ageHumanYrs: int {.base.}= self.age # `self` is injected + +class Dog of Animal: + method vocalize: string = "woof" + method ageHumanYrs: int = self.age * 7 + +class Cat of Animal: + method vocalize: string = + # call the base class method + self.vocalizeAnimal() & "meow" + +class Tiger of Cat: + method init(name: string="Bob", age: int)= + self.initAnimal(name, age) + output &= "I am a new tiger" & "\n" + method vocalize: string = + # no need for super.super! + self.vocalizeAnimal() & "Rawr!" + +var animals: seq[Animal] = @[] +animals.add(new Dog(name: "Sparky", age: 10)) +animals.add(new Cat(name: "Mitten", age: 10)) +animals.add(new Tiger(name: "Jean", age: 2)) + + +for a in animals: + output &= a.name & " says " & a.vocalize()&"\n" + output &= $a.ageHumanYrs() & "\n" + +var check = """ +I am a new Animal, Sparky +I am a new Animal, Mitten +I am a new Animal, Jean +I am a new tiger +Sparky says woof +70 +Mitten says ...meow +10 +Jean says ...Rawr! +2 +"""[0 .. ^1] +echo "####" +echo check +echo "####" +echo output +echo "####" +doAssert(output==check,"""echo matches""") \ No newline at end of file diff --git a/tests/test1.nims b/tests/test1.nims new file mode 100644 index 0000000..3bb69f8 --- /dev/null +++ b/tests/test1.nims @@ -0,0 +1 @@ +switch("path", "$projectDir/../src") \ No newline at end of file