本文整理汇总了Python中mocker.Mocker.replace方法的典型用法代码示例。如果您正苦于以下问题:Python Mocker.replace方法的具体用法?Python Mocker.replace怎么用?Python Mocker.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mocker.Mocker
的用法示例。
在下文中一共展示了Mocker.replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
proj = Project.create()
m = Mocker()
dsd_class = m.replace("editxt.application.DocumentSavingDelegate")
app = m.replace("editxt.app", type=Application)
ed = m.mock(Editor)
app.find_editors_with_project(proj) >> [ed for x in xrange(c.num_eds)]
if c.num_eds == 1:
docs = [m.mock(TextDocumentView)]
doc = docs[0].document >> m.mock(TextDocument)
app.iter_editors_with_view_of_document(doc) >> \
(ed for x in xrange(c.num_doc_views))
dirty_documents = m.method(proj.dirty_documents)
dirty_documents() >> docs
def check_docs(_docs):
d = docs if c.num_doc_views == 1 else []
eq_(list(_docs), d + [proj])
return True
callback = []
def get_callback(func):
callback.append(func)
return True
def do_callback():
callback[0](c.should_close)
saver = m.mock(DocumentSavingDelegate)
dsd_class.alloc() >> saver
saver.init_callback_(MATCH(check_docs), MATCH(get_callback)) >> saver
expect(saver.save_next_document()).call(do_callback)
if c.should_close:
ed.discard_and_focus_recent(proj)
else:
ed.discard_and_focus_recent(proj)
with m:
proj.perform_close(ed)
示例2: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
with test_app() as app:
m = Mocker()
fc = FindController(app)
flog = m.replace("editxt.command.find.log")
beep = m.replace(mod, "beep")
get_editor = m.method(fc.get_editor)
sender = m.mock()
(sender.tag() << c.tag).count(1, 2)
func = None
for tag, meth in list(fc.action_registry.items()):
fc.action_registry[tag] = temp = m.mock(meth)
if tag == c.tag:
func = temp
if c.fail:
flog.info(ANY, c.tag)
else:
if c.error:
err = mod.CommandError("error!")
expect(func(sender)).throw(err)
beep()
editor = get_editor() >> (m.mock() if c.target else None)
if c.target:
editor.message("error!", msg_type=const.ERROR)
else:
flog.warn(err)
else:
func(sender)
with m:
fc.perform_action(sender)
示例3: test_closeAllDocumentsWithDelegate_didCloseAllSelector_contextInfo_
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test_closeAllDocumentsWithDelegate_didCloseAllSelector_contextInfo_():
context = 42
dc = NSDocumentController.sharedDocumentController()
m = Mocker()
app = m.replace("editxt.app", type=Application)
perf_sel = m.replace("editxt.util.perform_selector", passthrough=False)
dsd_class = m.replace("editxt.application.DocumentSavingDelegate",
spec=False, passthrough=False)
docs = m.mock()
app.iter_dirty_documents() >> docs
selector = "_docController:shouldTerminate:context:"
delegate = m.mock()
def test_callback(callback):
callback("<result>")
return True
should_term = delegate._docController_shouldTerminate_context_
expect(perf_sel(delegate, selector, dc, "<result>", context)).call(
lambda *a:should_term(dc, "<result>", context))
should_term(dc, "<result>", context)
saver = m.mock(DocumentSavingDelegate)
dsd_class.alloc() >> saver
saver.init_callback_(docs, MATCH(test_callback)) >> saver
saver.save_next_document()
with m:
dc.closeAllDocumentsWithDelegate_didCloseAllSelector_contextInfo_(
delegate, selector, context)
示例4: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(eds_config):
app = Application()
m = Mocker()
create_window = m.method(app.create_window)
open_error_log = m.method(app.open_error_log)
nsapp = m.mock(ak.NSApplication)
ud_class = m.replace(fn, 'NSUserDefaults')
m.method(app.iter_saved_window_states)() >> iter(eds_config)
tc = m.replace(app, 'text_commander', spec=CommandManager)
dc = m.mock(DocumentController)
tc.load_commands(dc.textMenu >> m.mock(ak.NSMenu))
tc.load_shortcuts(dc.shortcutsMenu >> m.mock(ak.NSMenu))
if eds_config:
error = False
for ed_config in eds_config:
if isinstance(ed_config, mod.StateLoadFailure):
error = True
else:
create_window(ed_config)
if error:
open_error_log(set_current=False)
else:
create_window()
with m:
app.application_will_finish_launching(nsapp, dc)
eq_(app.text_commander, tc)
示例5: test_closeAllDocumentsWithDelegate_didCloseAllSelector_contextInfo_
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test_closeAllDocumentsWithDelegate_didCloseAllSelector_contextInfo_():
import editxt.util as util
context = 42
dc = ak.NSDocumentController.sharedDocumentController()
m = Mocker()
app = m.replace(editxt, 'app')
def perf_sel(delegate, selector, *args):
should_term(*args)
dsd_class = m.replace(mod, 'DocumentSavingDelegate', spec=False)
docs = m.mock()
app.iter_dirty_documents() >> docs
selector = "_docController:shouldTerminate:context:"
delegate = m.mock()
def test_callback(callback):
callback("<result>")
return True
should_term = delegate._docController_shouldTerminate_context_
should_term(dc, "<result>", context)
saver = m.mock(DocumentSavingDelegate)
dsd_class.alloc() >> saver
saver.init_callback_(docs, MATCH(test_callback)) >> saver
saver.save_next_document()
with replattr(mod, 'perform_selector', perf_sel), m:
dc.closeAllDocumentsWithDelegate_didCloseAllSelector_contextInfo_(
delegate, selector, context)
示例6: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
m = Mocker()
sv = ThinSplitView.alloc().init()
nsanim = m.replace(NSViewAnimation, passthrough=False)
nsdict = m.replace(NSDictionary, passthrough=False)
nsval = m.replace(NSValue, passthrough=False)
nsarr = m.replace(NSArray, passthrough=False)
view = m.mock(NSView)
rect = m.mock(NSRect)
rval = nsval.valueWithRect_(rect) >> m.mock()
resize = nsdict.dictionaryWithObjectsAndKeys_(
view, NSViewAnimationTargetKey, rval, NSViewAnimationEndFrameKey, None
) >> m.mock(NSDictionary)
anims = nsarr.arrayWithObject_(resize) >> m.mock(NSArray)
anim = nsanim.alloc() >> m.mock(NSViewAnimation)
anim.initWithViewAnimations_(anims) >> anim
anim.setDuration_(0.5)
if c.delegate:
delegate = m.mock(RedrawOnAnimationEndedDelegate)
anim.setDelegate_(delegate)
else:
delegate = None
anim.startAnimation()
with m:
sv._animate_view(view, rect, delegate)
示例7: test_application_init
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test_application_init():
from editxt.util import ContextMap
from editxt.errorlog import ErrorLog
m = Mocker()
m.replace("editxt.valuetrans.register_value_transformers", passthrough=False)()
with m:
app = Application()
eq_(app.editors, [])
assert isinstance(app.context, ContextMap)
示例8: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(file_exists=True):
m = Mocker()
app = Application()
view = m.mock(TextDocumentView)
m.method(app.open_documents_with_paths)([app.config.path]) >> [view]
default_config = m.property(app.config, "default_config")
m.replace("os.path.exists")(app.config.path) >> file_exists
if not file_exists:
default_config.value >> "# config"
view.document.text = "# config"
with m:
app.open_config_file()
示例9: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
m = Mocker()
el = ErrorLog()
app = m.replace('editxt.app')
log = m.replace(mod, 'log')
log.error("unexpected error", exc_info=True)
# open_error = app.open_error_log(set_current=False)
# if c.open_fail:
# expect(open_error).throw(Exception)
# log.warn("cannot open error log", exc_info=True)
with m:
assert el.unexpected_error()
示例10: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
m = Mocker()
fc = FindController.shared_controller()
beep = m.replace(ak, 'NSBeep')
dobeep = True
tv = m.replace(fc.finder, 'find_target')() >> (m.mock(TextView) if c.has_tv else None)
options = m.replace(fc.finder, "options")
ftext = options.find_text >> c.ftext
range = (tv.selectedRange() >> fn.NSRange(*c.sel)) if c.has_tv else None
if c.has_tv and c.ftext and ((c.sel_only and c.sel[1] > 0) or not c.sel_only):
text = tv.string() >> c.text
if not c.sel_only:
if (options.wrap_around >> c.wrap):
range = fn.NSMakeRange(0, 0)
else:
range = fn.NSMakeRange(range[0], len(text) - range[0])
if options.regular_expression >> c.regex:
finditer = m.method(fc.finder.regexfinditer)
elif options.match_entire_word >> c.mword:
ftext = "\\b" + re.escape(ftext) + "\\b"
finditer = m.method(fc.finder.regexfinditer)
else:
finditer = m.method(fc.finder.simplefinditer)
rtext = options.replace_text >> c.rtext
found = None
ranges = []
rtexts = []
items = []
FoundRange = make_found_range_factory(
FindOptions(regular_expression=c.regex, match_entire_word=c.mword))
for r in c.ranges:
found = FoundRange(fn.NSMakeRange(*r))
if ranges:
rtexts.append(text[sum(ranges[-1]):r[0]])
ranges.append(found.range)
rtexts.append(rtext)
items.append(found)
finditer(text, ftext, range, FORWARD, False) >> items
if ranges:
start = c.ranges[0][0]
range = fn.NSMakeRange(start, sum(c.ranges[-1]) - start)
value = "".join(rtexts)
if tv.shouldChangeTextInRange_replacementString_(range, value) >> c.replace:
ts = tv.textStorage() >> m.mock(ak.NSTextStorage)
ts.replaceCharactersInRange_withString_(range, value)
tv.didChangeText()
tv.setNeedsDisplay_(True)
dobeep = False
eq_(dobeep, c.beep)
if dobeep:
beep()
with m:
fc.finder._replace_all(c.sel_only)
示例11: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
m = Mocker()
sf = SyntaxFactory()
log = m.replace("editxt.syntax.log", passthrough=False)
glob = m.replace("glob.glob", passthrough=False)
exists = m.replace("os.path.exists", passthrough=False)
load = sf.load_definition = m.mock()
if c.path and exists(c.path) >> c.exists:
info = {
"disabled": dict(name="dis", disabled=True, filepatterns=[]), # should cause info log
# these should cause error log
"incomplete1": {},
"incomplete2": dict(name="none"),
"incomplete3": dict(filepatterns=[]),
# should only register twice despite duplication
"python": dict(name="python", filepatterns=["*.py", "*.py", "*.pyw"]),
"sql": dict(name="sequel", filepatterns=["*.sql"]),
"text": dict(name="text", filepatterns=["*.txt"]),
"text2": dict(name="text", filepatterns=["*.txt", "*.text"]),
"text3": dict(name="text", disabled=True, filepatterns=["*.txt", "*.text"]),
}
glob(os.path.join(c.path, "*" + const.SYNTAX_DEF_EXTENSION)) >> sorted(info)
def do(name):
data = dict(info[name])
if name.startswith("incomplete"):
raise Exception("incomplete definition: %r" % (data,))
data.setdefault("disabled", False)
if "filepatterns" in data:
data["filepatterns"] = set(data["filepatterns"])
return type("sdef", (object,), data)()
expect(load(ANY)).count(len(info)).call(do)
for fname, data in sorted(info.iteritems()):
if fname.startswith("incomplete"):
log.error(ANY, fname, exc_info=True)
else:
pats = ", ".join(sorted(set(data.get("filepatterns", []))))
stat = [data["name"], "[%s]" % pats]
if fname in ("text", "text2", "text4"):
stat.extend(["overrides", "*.txt"])
elif fname in ("disabled", "text3"):
stat.append("DISABLED")
stat.append(fname)
log.info(ANY, " ".join(stat))
patterns = set(["*.py", "*.pyw", "*.sql", "*.text", "*.txt"])
else:
patterns = set(["*.txt"])
with m:
sf.load_definitions(c.path)
eq_(set(sf.registry), patterns)
示例12: test_get_connection_without_sandbox
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test_get_connection_without_sandbox(self):
# Mock connections['dataview'] to raise ConnectionDoesNotExist
from django.db.utils import ConnectionDoesNotExist
mocker = Mocker()
connections = mocker.replace("django.db.connections")
expect(connections['dataview']).throw(ConnectionDoesNotExist)
connection = mocker.replace("django.db.connection")
logging = mocker.replace("logging")
logging.warning("dataview-specific database connection not available, dataview query is NOT sandboxed")
with mocker:
observed = DataView.get_connection()
expected = connection
self.assertIs(observed, expected)
示例13: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(c):
m = Mocker()
hbc = HoverButtonCell.alloc().init()
frame = m.mock(NSRect)
view = m.mock(NSOutlineView)
point, pressed = hbc.hover_info = c.info
if point is not None:
m.replace(NSPointInRect)(point, frame) >> (point == "in")
row = view.rowAtPoint_(frame.origin >> (1, 1)) >> 2
dgt = m.property(hbc, "delegate").value >> m.mock(EditorWindowController)
image = dgt.hoverButtonCell_imageForState_row_(hbc, c.state, row) >> "<img>"
with m:
eq_(hbc.buttonImageForFrame_inView_(frame, view), image)
示例14: test_init_syntax_definitions
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test_init_syntax_definitions():
import editxt.syntax as syntax
m = Mocker()
app = Application(profile='/editxtdev')
rsrc_path = m.method(app.resource_path)() >> "/tmp/resources"
SyntaxFactory = m.replace(syntax, 'SyntaxFactory', spec=False)
sf = SyntaxFactory() >> m.mock(syntax.SyntaxFactory)
app_log = m.replace("editxt.application.log")
for path, info in [(rsrc_path, False), ('/editxtdev', True)]:
sf.load_definitions(join(path, const.SYNTAX_DEFS_DIR), info)
sf.index_definitions()
with m:
app.init_syntax_definitions()
示例15: test
# 需要导入模块: from mocker import Mocker [as 别名]
# 或者: from mocker.Mocker import replace [as 别名]
def test(f_exists):
m = Mocker()
exists = m.replace("os.path.exists")
stat = m.replace("os.stat")
path = "<path>"
exists(path) >> f_exists
if f_exists:
stat(path) >> (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
res = (6, 8)
else:
res = None
with m:
result = filestat(path)
eq_(result, res)