aboutsummaryrefslogtreecommitdiff
#pragma once

#include "config.h"

#include <string>
#include <list>
#include <set>

#include <boost/format.hpp>

/* Before 4.7, gcc's std::exception uses empty throw() specifiers for
 * its (virtual) destructor and what() in c++11 mode, in violation of spec
 */
#ifdef __GNUC__
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)
#define EXCEPTION_NEEDS_THROW_SPEC
#endif
#endif


namespace nix {


/* Inherit some names from other namespaces for convenience. */
using std::string;
using std::list;
using std::set;
using std::vector;
using boost::format;


struct FormatOrString
{
    string s;
    FormatOrString(const string & s) : s(s) { };
    FormatOrString(const format & f) : s(f.str()) { };
    FormatOrString(const char * s) : s(s) { };
};


/* BaseError should generally not be caught, as it has Interrupted as
   a subclass. Catch Error instead. */
class BaseError : public std::exception
{
protected:
    string prefix_; // used for location traces etc.
    string err;
public:
    unsigned int status; // exit status
    BaseError(const FormatOrString & fs, unsigned int status = 1);
#ifdef EXCEPTION_NEEDS_THROW_SPEC
    ~BaseError() throw () { };
    const char * what() const throw () { return err.c_str(); }
#else
    const char * what() const noexcept { return err.c_str(); }
#endif
    const string & msg() const { return err; }
    const string & prefix() const { return prefix_; }
    BaseError & addPrefix(const FormatOrString & fs);
};

#define MakeError(newClass, superClass) \
    class newClass : public superClass                  \
    {                                                   \
    public:                                             \
        newClass(const FormatOrString & fs, unsigned int status = 1) : superClass(fs, status) { }; \
    };

MakeError(Error, BaseError)

class SysError : public Error
{
public:
    int errNo;
    SysError(const FormatOrString & fs);
};


typedef list<string> Strings;
typedef set<string> StringSet;


/* Paths are just strings. */
typedef string Path;
typedef list<Path> Paths;
typedef set<Path> PathSet;


typedef enum {
    lvlError = 0,
    lvlInfo,
    lvlTalkative,
    lvlChatty,
    lvlDebug,
    lvlVomit
} Verbosity;


}
p.py?h=v0.7&id=2a14bc7bb3e523d74c9abe5246303976c013d91a'>2a14bc7bb3e523d74c9abe5246303976c013d91a (patch) tree050b86c11521580d44af1d8d09132467001f2e85 /tests/test_map.py parent24c575b6eec2abe396ece52460672d26e01ad284 (diff)downloadimmutables-2a14bc7bb3e523d74c9abe5246303976c013d91a.tar.gz
immutables-2a14bc7bb3e523d74c9abe5246303976c013d91a.zip
Make MapMutation a context manager
Diffstat (limited to 'tests/test_map.py')
-rw-r--r--tests/test_map.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/test_map.py b/tests/test_map.py
index 4fc87e3..d3b11fb 100644
--- a/tests/test_map.py
+++ b/tests/test_map.py
@@ -1174,6 +1174,27 @@ class BaseMapTest:
with self.assertRaises(EqError):
mm.set(key2, 123)
+ def test_map_mut_14(self):
+ m = self.Map(a=1, b=2)
+
+ with m.mutate() as mm:
+ mm['z'] = 100
+ del mm['a']
+
+ self.assertEqual(mm.finalize(), self.Map(z=100, b=2))
+
+ def test_map_mut_15(self):
+ m = self.Map(a=1, b=2)
+
+ with self.assertRaises(ZeroDivisionError):
+ with m.mutate() as mm:
+ mm['z'] = 100
+ del mm['a']
+ 1 / 0
+
+ self.assertEqual(mm.finalize(), self.Map(z=100, b=2))
+ self.assertEqual(m, self.Map(a=1, b=2))
+
def test_map_mut_stress(self):
COLLECTION_SIZE = 7000
TEST_ITERS_EVERY = 647