From 8917f0edd7803bbb52dcdf7460a63beba3cd5d1d Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Fri, 23 Dec 2022 19:30:40 -0800 Subject: [PATCH 1/2] Adding the 'add' method to the set class --- py/set.go | 11 +++++++++++ py/tests/set.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/py/set.go b/py/set.go index b1b74e26..718e286c 100644 --- a/py/set.go +++ b/py/set.go @@ -46,6 +46,17 @@ func NewSetFromItems(items []Object) *Set { return s } +func init() { + SetType.Dict["add"] = MustNewMethod("add", func(self Object, args Tuple) (Object, error) { + setSelf := self.(*Set) + if len(args) != 1 { + return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args)) + } + setSelf.Add(args[0]) + return NoneType{}, nil + }, 0, "add(value)") +} + // Add an item to the set func (s *Set) Add(item Object) { s.items[item] = SetValue{} diff --git a/py/tests/set.py b/py/tests/set.py index 834e457b..fd0afc78 100644 --- a/py/tests/set.py +++ b/py/tests/set.py @@ -81,6 +81,17 @@ assert 4 in c assert 5 in c +doc="add" +a = set() +a.add(1) +a.add(2) +a.add(3) +assert len(a) == 3 +assert 1 in a +assert 2 in a +assert 3 in a +assert 4 not in a + doc="__eq__, __ne__" a = set([1,2,3]) assert a.__eq__(3) != True From a06c34af71f2bf3c57d60fb5ffd7b47733fc12cd Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Fri, 23 Dec 2022 19:44:23 -0800 Subject: [PATCH 2/2] Adding test coverage for exception --- py/tests/set.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py/tests/set.py b/py/tests/set.py index fd0afc78..3eeaf1d3 100644 --- a/py/tests/set.py +++ b/py/tests/set.py @@ -2,6 +2,8 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. +from libtest import assertRaises + doc="__and__" a = {1, 2, 3} b = {2, 3, 4, 5} @@ -91,6 +93,7 @@ assert 2 in a assert 3 in a assert 4 not in a +assertRaises(TypeError, lambda: a.add()) doc="__eq__, __ne__" a = set([1,2,3]) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy