go-translator/pkg/service_python/service_test.go

795 строки
13 КиБ
Go

package service_python
import (
"bytes"
"strings"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
gomega_format "github.com/onsi/gomega/format"
)
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
gomega_format.CharactersAroundMismatchToInclude = 20
RunSpecs(t, "Go Translator to Python/Skidl")
}
var _ = Describe("Go Translator to Python/Skidl", func() {
Describe("All", func() {
It("Empty Package", func() {
source := `package test`
expected := ``
Compare(source, expected)
})
It("Importing skidl", func() {
source := `package test
import (
. "skidl"
)
`
expected := `from skidl import *
`
Compare(source, expected)
})
It("Importing skidl and FreeCAD", func() {
source := `package test
import (
. "skidl"
. "FreeCAD"
)
`
expected := `from skidl import *
from FreeCAD import *
`
Compare(source, expected)
})
It("Importing inline", func() {
source := `package test
import (
"skidl"
"FreeCAD"
)
`
expected := `import skidl
import FreeCAD
`
Compare(source, expected)
})
It("Importing with name", func() {
source := `package test
import (
cq "cadquery"
)
`
expected := `import cadquery as cq
`
Compare(source, expected)
})
It("Variables declaration", func() {
source := `package test
import . "skidl"
var (
a = 1
b = 2
c = 3
d = a + b + c / 2
t = true
f = false
)
`
expected := `from skidl import *
a = 1
b = 2
c = 3
d = a + b + c / 2
t = True
f = False
`
Compare(source, expected)
})
It("Simple bool assignment", func() {
source := `package test
import . "skidl"
func main() {
t := true
o = true
}
`
expected := `from skidl import *
def main():
t = True
o = True
main()
`
Compare(source, expected)
})
It("Array declaration", func() {
source := `package test
import . "skidl"
var a = []any{}
`
expected := `from skidl import *
a = []
`
Compare(source, expected)
})
It("Empty Array assignment", func() {
source := `package test
import . "skidl"
func main() {
a := []any{}
a = []any{}
}
`
expected := `from skidl import *
def main():
a = []
a = []
main()
`
Compare(source, expected)
})
It("Inited array assignment", func() {
source := `package test
import . "skidl"
func main() {
a := []any{1, 2, x}
}
`
expected := `from skidl import *
def main():
a = (1,2,x)
main()
`
Compare(source, expected)
})
It("Array append", func() {
source := `package test
import . "skidl"
func main() {
append(a, 1)
}
`
expected := `from skidl import *
def main():
a.append(1)
main()
`
Compare(source, expected)
})
It("Deep property assign", func() {
source := `package test
import . "skidl"
func main() {
a.b.c.d.e = 1
x = a.b.c.d.e
}
`
expected := `from skidl import *
def main():
a.\
b.\
c.\
d.\
e = 1
x = a.\
b.\
c.\
d.\
e
main()
`
Compare(source, expected)
})
It("Function Declaration", func() {
source := `package test
import . "skidl"
func myfunc() {}
`
expected := `from skidl import *
def myfunc():
None
`
Compare(source, expected)
})
It("Function Declaration: main adds main call", func() {
source := `package test
import . "skidl"
func main() {}
`
expected := `from skidl import *
def main():
None
main()
`
Compare(source, expected)
})
It("Func call", func() {
source := `package test
import . "skidl"
func main() {
generate_netlist()
}
`
expected := `from skidl import *
def main():
generate_netlist()
main()
`
Compare(source, expected)
})
It("Sequence func call", func() {
source := `package test
import . "skidl"
func main() {
a.b().c(1).d(2).e(3)
}
`
expected := `from skidl import *
def main():
a.\
b().\
c(1).\
d(2).\
e(3)
main()
`
Compare(source, expected)
})
It("Func return", func() {
source := `package test
import . "skidl"
func myfunc(p1, p2 any) any {
return 5
}
func main() {
v := myfunc(1, 2)
}
`
expected := `from skidl import *
def myfunc(p1,p2):
return 5
def main():
v = myfunc(1,2)
main()
`
Compare(source, expected)
})
It("Net Declaration", func() {
source := `package test
import . "skidl"
func main() {
vin := Net("3.3v")
}
`
expected := `from skidl import *
def main():
vin = Net("3.3v")
main()
`
Compare(source, expected)
})
It("Nets Declaration", func() {
source := `package test
import . "skidl"
func main() {
vin := Net("3.3v")
gnd := Net("GND")
}
`
expected := `from skidl import *
def main():
vin = Net("3.3v")
gnd = Net("GND")
main()
`
Compare(source, expected)
})
It("Part Declaration", func() {
source := `package test
import . "skidl"
func main() {
ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32")
}
`
expected := `from skidl import *
def main():
ESP32 = Part("RF_Module","ESP32-WROOM-32",TEMPLATE,footprint="RF_Module:ESP32-WROOM-32")
main()
`
Compare(source, expected)
})
It("Parts Declaration", func() {
source := `package test
import . "skidl"
func main() {
ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32")
Klema := Part("Connector_Generic_MountingPin", "Conn_01x01_MountingPin", TEMPLATE, footprint == "TestPoint_Pad_2.0x2.0mm")
}
`
expected := `from skidl import *
def main():
ESP32 = Part("RF_Module","ESP32-WROOM-32",TEMPLATE,footprint="RF_Module:ESP32-WROOM-32")
Klema = Part("Connector_Generic_MountingPin","Conn_01x01_MountingPin",TEMPLATE,footprint="TestPoint_Pad_2.0x2.0mm")
main()
`
Compare(source, expected)
})
It("Values Declaration", func() {
source := `package test
import . "skidl"
func main() {
esp32 := ESP32(value == "ESP32")
k_vin, k_gnd := Klema(2)
}
`
expected := `from skidl import *
def main():
esp32 = ESP32(value="ESP32")
k_vin,k_gnd = Klema(2)
main()
`
Compare(source, expected)
})
It("Values with params Declaration", func() {
source := `package test
import . "skidl"
func main() {
tr_tok_verh := tr_npn(value == "tr Tok verh NPN")
}
`
expected := `from skidl import *
def main():
tr_tok_verh = tr_npn(value="tr Tok verh NPN")
main()
`
Compare(source, expected)
})
It("Assignments += by name ", func() {
source := `package test
import . "skidl"
func main() {
esp32["GND"] += gnd
esp32["VDD"] += vin
}
`
expected := `from skidl import *
def main():
esp32["GND"] += gnd
esp32["VDD"] += vin
main()
`
Compare(source, expected)
})
It("Assignments += by index", func() {
source := `package test
import . "skidl"
func main() {
esp32[1] += gnd
vin += r_fb[1]
tr1[1] += tr2[2]
}
`
expected := `from skidl import *
def main():
esp32[1] += gnd
vin += r_fb[1]
tr1[1] += tr2[2]
main()
`
Compare(source, expected)
})
It("Assignments &", func() {
source := `package test
import . "skidl"
func main() {
tr_vkl["G"] & vkl
tr_vkl["G"] & tr2["S"]
tr_vkl & tr2["S"]
}
`
expected := `from skidl import *
def main():
tr_vkl["G"] & vkl
tr_vkl["G"] & tr2["S"]
tr_vkl & tr2["S"]
main()
`
Compare(source, expected)
})
It("Value Assignment", func() {
source := `package test
import . "skidl"
func main() {
vkl_iface.value = "k vkl"
vkl_iface.value = 19
vkl_iface.value = 19 / 3
vkl_iface.value = 19 * 2
}
`
expected := `from skidl import *
def main():
vkl_iface.\
value = "k vkl"
vkl_iface.\
value = 19
vkl_iface.\
value = 19 / 3
vkl_iface.\
value = 19 * 2
main()
`
Compare(source, expected)
})
It("Condition ==", func() {
source := `package test
import . "skidl"
func main() {
if a == 2 {
x = 1
y = 5
} else {
x = 6
y = 9
}
}
`
expected := `from skidl import *
def main():
if a==2:
x = 1
y = 5
else:
x = 6
y = 9
main()
`
Compare(source, expected)
})
It("Condition >", func() {
source := `package test
import . "skidl"
func main() {
if a > 2 {
x = 1
y = 5
} else {
x = 6
y = 9
}
}
`
expected := `from skidl import *
def main():
if a > 2:
x = 1
y = 5
else:
x = 6
y = 9
main()
`
Compare(source, expected)
})
It("Condition complex", func() {
source := `package test
import . "skidl"
func main() {
if a > 2 {
x = 1
y = 5
if b > 5 {
x = 6
y = 9
} else {
x = 15
}
} else {
x = 18
}
}
`
expected := `from skidl import *
def main():
if a > 2:
x = 1
y = 5
if b > 5:
x = 6
y = 9
else:
x = 15
else:
x = 18
main()
`
Compare(source, expected)
})
It("For with range", func() {
source := `package test
import . "skidl"
func main() {
for v := range list {
call(v)
}
}
`
expected := `from skidl import *
def main():
for v in list:
call(v)
main()
`
Compare(source, expected)
})
It("Function decl/call", func() {
source := `package test
import . "skidl"
func vdiv(inp, outp, param string) {
r1 := r(value == 1000)
r2 := r(value == param)
inp & r1
r1 & outp
outp & r2
r2 & gnd
}
func main() {
v := vdiv(inp, outp, "500")
}
`
expected := `from skidl import *
def vdiv(inp,outp,param):
r1 = r(value=1000)
r2 = r(value=param)
inp & r1
r1 & outp
outp & r2
r2 & gnd
def main():
v = vdiv(inp,outp,"500")
main()
`
Compare(source, expected)
})
It("Subcircuit function decorator", func() {
source := `package test
import . "skidl"
//@subcircuit
func vdiv(inp, outp, param string) {
r1 := r(value == 1000)
r2 := r(value == param)
inp & r1
r1 & outp
outp & r2
r2 & gnd
}
func main() {
v := vdiv(inp, outp, "500")
}
`
expected := `from skidl import *
@subcircuit
def vdiv(inp,outp,param):
r1 = r(value=1000)
r2 = r(value=param)
inp & r1
r1 & outp
outp & r2
r2 & gnd
def main():
v = vdiv(inp,outp,"500")
main()
`
Compare(source, expected)
})
It("Package function decorator", func() {
source := `package test
import . "skidl"
//@package
func vdiv(inp, outp, param string) {
r1 := r(value == 1000)
r2 := r(value == param)
inp & r1
r1 & outp
outp & r2
r2 & gnd
}
func main() {
v := vdiv(inp, outp, "500")
}
`
expected := `from skidl import *
@package
def vdiv(inp,outp,param):
r1 = r(value=1000)
r2 = r(value=param)
inp & r1
r1 & outp
outp & r2
r2 & gnd
def main():
v = vdiv(inp,outp,"500")
main()
`
Compare(source, expected)
})
It("Interfaces", func() {
source := `package test
import . "skidl"
//@subcircuit
func mem_module(intfc any) {
ram := Part("Memory_RAM", "AS6C1616")
ram["A[0:19]"] += intfc.addr
ram["DQ[0:15]"] += intfc.data
ram["WE#"] += intfc.wr
ram["OE#"] += intfc["rd"]
}
func main() {
rd := Net("MEM_RD#")
wr := Net("MEM_WR#")
addr := Bus("MEM_ADDR", 20)
data := Bus("MEM_DATA", 16)
mem_intfc = Interface(rd == rd, wr == wr, addr == addr, data == data)
mem_module(mem_intfc)
uc_module(clk, mem_intfc, io_intfc)
}
`
expected := `from skidl import *
@subcircuit
def mem_module(intfc):
ram = Part("Memory_RAM","AS6C1616")
ram["A[0:19]"] += intfc.\
addr
ram["DQ[0:15]"] += intfc.\
data
ram["WE#"] += intfc.\
wr
ram["OE#"] += intfc["rd"]
def main():
rd = Net("MEM_RD#")
wr = Net("MEM_WR#")
addr = Bus("MEM_ADDR",20)
data = Bus("MEM_DATA",16)
mem_intfc = Interface(rd=rd,wr=wr,addr=addr,data=data)
mem_module(mem_intfc)
uc_module(clk,mem_intfc,io_intfc)
main()
`
Compare(source, expected)
})
})
})
func Compare(source, expected string) {
var in, out bytes.Buffer
in.WriteString(source)
service := NewService(&in, &out)
err := service.Start()
got := out.String()
tgot, texpected := Trim(got), Trim(expected)
// spew.Dump(tgot)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, tgot).To(Be(texpected))
}
func Trim(s string) string {
s = strings.Replace(s, "\t", " ", -1)
return s
}
func NDescribe(s string, i func()) int { return 0 }
func NIt(s string, i func()) int { return 0 }
var Be = Equal
func NoErr(err error) {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
var Ok = NoErr