Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Aug 22, 2019
1 parent d5d14c0 commit 1a2148f
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/privateimports/m1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{.experimental: "allowPrivateImport".}
import ./m2
import ./m3 {.privateImport.} as m3
from ./m3 as m3Bis import nil
# export m3
doAssert car2 == 2
export car2

export m3Bis.car1

const foo0* = 2
const foo1 = bar1

const foo1Aux = 2
export foo1Aux

doAssert not declared(bar2)
doAssert not compiles(bar2)

var foo2 = 2
let foo3 = 2

type Foo4 = enum
kg1, kg2

type Foo5 = object
z1: string
z2: Foo4
z3: int

proc `z3`*(a: Foo5): auto =
a.z3 * 10

proc foo6(): auto = 2
proc foo6b*(): auto = 2
template foo7: untyped = 2
macro foo8(): untyped = discard
template foo9(a: int): untyped = discard

block:
template foo10: untyped = 2
type Foo11 = enum
kg1b, kg2b
proc foo12(): auto = 2

proc initFoo5*(z3: int): Foo5 = Foo5(z3: z3)
2 changes: 2 additions & 0 deletions tests/privateimports/m2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const bar1* = 2
const bar2 = 2
2 changes: 2 additions & 0 deletions tests/privateimports/m3.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const car1* = 2
const car2 = 2
10 changes: 10 additions & 0 deletions tests/privateimports/mt0.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{.push experimental: "allowPrivateImport".}
import ./m1 as m
doAssert compiles(foo0)
doAssert not compiles(foo1)
doAssert foo6b() == 2
doAssert car2 == 2

var f = initFoo5(z3=3)
doAssert f.z3 == 30
doAssert z3(f) == 30
22 changes: 22 additions & 0 deletions tests/privateimports/mt1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{.push experimental: "allowPrivateImport".}
import ./m1 {.privateImport.} as m
doAssert foo1 == 2
doAssert m.foo1 == 2

doAssert m.car2 == 2
doAssert car2 == 2
doAssert m.foo1Aux == 2
doAssert m.car1 == 2

## field access
var x = Foo5(z1: "foo", z2: m.kg1)
doAssert x.z1 == "foo"

var f0: Foo5
f0.z3 = 3
doAssert f0.z3 == 3
var f = initFoo5(z3=3)
doAssert f.z3 == 3
doAssert z3(f) == 30
doAssert m.z3(f) == 30
doAssert not compiles(mt1.`z3`(f)) # z3 is an imported symbol
31 changes: 31 additions & 0 deletions tests/privateimports/mt2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{.push experimental: "allowPrivateImport".}
from ./m1 {.privateImport.} as m import foo1
from ./m1 {.privateImport.} as m2 import foo7
doAssert foo1 == 2
doAssert m.foo1 == 2
doAssert m.foo2 == 2
doAssert compiles(foo1)
doAssert compiles(m.foo2)
doAssert not compiles(foo2)
doAssert not compiles(car2)
doAssert m.foo3 == 2
doAssert m.foo6() == 2
doAssert m.foo6b() == 2
doAssert foo7() == 2
doAssert m2.foo6b() == 2
doAssert not compiles(foo10())
doAssert compiles(m.Foo5)
doAssert not compiles(m.Foo11)
doAssert not compiles(m.kg1b)
doAssert not compiles(m.foo12())

## field access
var x = m.Foo5(z1: "foo", z2: m.kg1)
doAssert x.z1 == "foo"

var f0: m.Foo5
f0.z3 = 3
doAssert f0.z3 == 3
var f = m.initFoo5(z3=3)
doAssert f.z3 == 3
doAssert m.z3(f) == 30
8 changes: 8 additions & 0 deletions tests/privateimports/mt3.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{.push experimental: "allowPrivateImport".}
import ./m1 {.privateImport.}
doAssert foo1 == 2

doAssert m1.foo1 == 2

doAssert not compiles(mt3.foo0) # foo0 is an imported symbol
doAssert not compiles(mt3.foo1) # ditto
5 changes: 5 additions & 0 deletions tests/privateimports/mt4.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{.push experimental: "allowPrivateImport".}
import ./m1 {.privateImport.} except foo1
doAssert foo2 == 2
doAssert declared(foo2)
doAssert not compiles(foo1)
4 changes: 4 additions & 0 deletions tests/privateimports/mt4b.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{.push experimental: "allowPrivateImport".}
from ./m1 {.privateImport.} as m2 import nil
doAssert not compiles(foo1)
doAssert m2.foo1 == 2
10 changes: 10 additions & 0 deletions tests/privateimports/mt5.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{.push experimental: "allowPrivateImport".}
import ./m1 {.privateImport.} as m2 except foo1
doAssert foo2 == 2
doAssert not compiles(foo1)
doAssert m2.foo1 == 2
doAssert compiles(m2.foo1)

from system {.privateImport.} as s import ThisIsSystem
doAssert ThisIsSystem
doAssert s.ThisIsSystem
10 changes: 10 additions & 0 deletions tests/privateimports/mt6.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{.push experimental: "allowPrivateImport".}
import ./m1 {.privateImport.} as m2
doAssert compiles(foo1)
doAssert compiles(m2.foo1)
doAssert declared(foo1)
doAssert declared(m2.foo0) # public: works fine

doAssert m2.foo1 == 2
doAssert declared(m2.foo1)
doAssert not declared(m2.nonexistant)
14 changes: 14 additions & 0 deletions tests/privateimports/mt7.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include ./m1
doAssert compiles(foo1)
doAssert compiles(mt7.foo1)
doAssert declared(foo1)
doAssert declared(mt7.foo1)
doAssert declared(mt7.foo0)

var f0: Foo5
f0.z3 = 3
doAssert f0.z3 == 3
var f = initFoo5(z3=3)
doAssert f.z3 == 3
doAssert mt7.z3(f) == 30
doAssert z3(f) == 30
1 change: 1 addition & 0 deletions tests/privateimports/tmain.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "."/[mt0,mt1,mt2,mt3,mt4,mt4b,mt5,mt6,mt7]

0 comments on commit 1a2148f

Please sign in to comment.