diff --git a/tests/privateimports/m1.nim b/tests/privateimports/m1.nim new file mode 100644 index 0000000000000..e780a9ff7f20b --- /dev/null +++ b/tests/privateimports/m1.nim @@ -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) diff --git a/tests/privateimports/m2.nim b/tests/privateimports/m2.nim new file mode 100644 index 0000000000000..b905049af0eb0 --- /dev/null +++ b/tests/privateimports/m2.nim @@ -0,0 +1,2 @@ +const bar1* = 2 +const bar2 = 2 diff --git a/tests/privateimports/m3.nim b/tests/privateimports/m3.nim new file mode 100644 index 0000000000000..c1b850fd6ac29 --- /dev/null +++ b/tests/privateimports/m3.nim @@ -0,0 +1,2 @@ +const car1* = 2 +const car2 = 2 \ No newline at end of file diff --git a/tests/privateimports/mt0.nim b/tests/privateimports/mt0.nim new file mode 100644 index 0000000000000..bc589757fb295 --- /dev/null +++ b/tests/privateimports/mt0.nim @@ -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 diff --git a/tests/privateimports/mt1.nim b/tests/privateimports/mt1.nim new file mode 100644 index 0000000000000..053a95e6ace00 --- /dev/null +++ b/tests/privateimports/mt1.nim @@ -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 diff --git a/tests/privateimports/mt2.nim b/tests/privateimports/mt2.nim new file mode 100644 index 0000000000000..8c2d0b0243461 --- /dev/null +++ b/tests/privateimports/mt2.nim @@ -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 diff --git a/tests/privateimports/mt3.nim b/tests/privateimports/mt3.nim new file mode 100644 index 0000000000000..c9ce6d0032f69 --- /dev/null +++ b/tests/privateimports/mt3.nim @@ -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 diff --git a/tests/privateimports/mt4.nim b/tests/privateimports/mt4.nim new file mode 100644 index 0000000000000..22216484f48e6 --- /dev/null +++ b/tests/privateimports/mt4.nim @@ -0,0 +1,5 @@ +{.push experimental: "allowPrivateImport".} +import ./m1 {.privateImport.} except foo1 +doAssert foo2 == 2 +doAssert declared(foo2) +doAssert not compiles(foo1) diff --git a/tests/privateimports/mt4b.nim b/tests/privateimports/mt4b.nim new file mode 100644 index 0000000000000..de5901c3660f5 --- /dev/null +++ b/tests/privateimports/mt4b.nim @@ -0,0 +1,4 @@ +{.push experimental: "allowPrivateImport".} +from ./m1 {.privateImport.} as m2 import nil +doAssert not compiles(foo1) +doAssert m2.foo1 == 2 diff --git a/tests/privateimports/mt5.nim b/tests/privateimports/mt5.nim new file mode 100644 index 0000000000000..55f5b9eb85a8e --- /dev/null +++ b/tests/privateimports/mt5.nim @@ -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 diff --git a/tests/privateimports/mt6.nim b/tests/privateimports/mt6.nim new file mode 100644 index 0000000000000..b6ce8ecc46b4b --- /dev/null +++ b/tests/privateimports/mt6.nim @@ -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) diff --git a/tests/privateimports/mt7.nim b/tests/privateimports/mt7.nim new file mode 100644 index 0000000000000..45a66461024bd --- /dev/null +++ b/tests/privateimports/mt7.nim @@ -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 diff --git a/tests/privateimports/tmain.nim b/tests/privateimports/tmain.nim new file mode 100644 index 0000000000000..9ac36cf1974f6 --- /dev/null +++ b/tests/privateimports/tmain.nim @@ -0,0 +1 @@ +import "."/[mt0,mt1,mt2,mt3,mt4,mt4b,mt5,mt6,mt7] \ No newline at end of file