class NetTests(unittest.TestCase): """ The tests of the core module net feature """ def setUp(self): """ Setup the test case. """ self.net = Net("001") def tearDown(self): """ Teardown the test case. """ pass def test_create_new_net(self): """ Test the creation of a new empty net. """ assert self.net.net_id == "001" def test_bounds_simple(self): """Make sure bounds() uses all the included NetPoints""" for (x, y) in ((1, 3), (3, 2), (4, 3), (3, 5)): net_pt = NetPoint(str((x, y)), x, y) self.net.add_point(net_pt) # NetPoints don't actually need to be connected to affect bounds() top_left, btm_right = self.net.bounds() self.assertEqual(top_left.x, 1) self.assertEqual(top_left.y, 2) self.assertEqual(btm_right.x, 4) self.assertEqual(btm_right.y, 5)
def parse_nets(self, nets): """ Extract nets. """ for net in nets: net_id = net.get('net_id') ret_net = Net(net_id) # Add Annotations for annotation in net.get('annotations'): anno = self.parse_annotation(annotation) ret_net.add_annotation(anno) # Get the Attributes for key, value in net.get('attributes').items(): ret_net.add_attribute(key, value) # Get the Points for net_point in net.get('points'): npnt = self.parse_net_point(net_point) ret_net.add_point(npnt) self.design.add_net(ret_net)
def parse_net(self, args): """ Assembles a net from a list of junctions, segments, and labels. """ thisnet = Net(args) subdata = defaultdict(list) for phrase in self.stream: print phrase cmd, _sep, args = phrase.partition(' ') if cmd not in ('J', 'S', 'A', 'L', 'Q', 'B'): self.stream.push(phrase) break print args k, v = self.parsenode(cmd)(args) subdata[k].append(v) # finish building thisnet for netpt in subdata['netpoint'][:]: # using a copy so that we can modify subdata['netpoint'] inside loop if netpt.point_id not in thisnet.points: thisnet.add_point(netpt) else: # oh yeah, a net can have a point more than once, because that # makes *great* sense. for point in netpt.connected_points: thisnet.points[netpt.point_id].add_connected_point(point) for comp in netpt.connected_components: thisnet.points[netpt.point_id].add_connected_component(comp) # update subdata['netpoint'] so that ref to netpt points to the # new combined point i = subdata['netpoint'].index(netpt) subdata['netpoint'][i] = thisnet.points[netpt.point_id] # yuck, passing in-band thisnet.ibpts = subdata['netpoint'] for pt_a, pt_b in subdata['segment']: thisnet.connect((subdata['netpoint'][pt_a - 1], subdata['netpoint'][pt_b - 1])) for annot in subdata['annot']: thisnet.add_annotation(annot) if '=' in annot.value: thisnet.add_attribute(*(annot.value.split('=', 1))) return ('net', thisnet)
def test_write_net(self): """ The write_net method creates the correct kicad wires from an openjson net. """ net = Net('') p1 = NetPoint('p1', 0, 0) p2 = NetPoint('p2', 1, 0) p3 = NetPoint('p3', 0, 1) net.add_point(p1) net.add_point(p2) net.add_point(p3) net.conn_point(p1, p2) net.conn_point(p1, p3) writer = KiCAD() buf = StringIO() writer.write_net(buf, net) self.assertEqual( buf.getvalue(), 'Wire Wire Line\n\t0 0 0 -11\nWire Wire Line\n\t0 0 11 0\n')