From bcc35ec29cca328787269de4cd96bd9647ff6f25 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Fri, 28 Jan 2011 21:43:10 +0000 Subject: [PATCH] Add a test for stuff in the module's global dict --- src/core/utilities.cpp | 5 ++++- src/core/utilities.h | 2 +- src/scripting/scriptinfo.cpp | 2 +- tests/python_test.cpp | 35 +++++++++++++++++++++++++++++------ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/core/utilities.cpp b/src/core/utilities.cpp index 5fc03b509..643db3b6f 100644 --- a/src/core/utilities.cpp +++ b/src/core/utilities.cpp @@ -137,10 +137,13 @@ quint64 FileSystemFreeSpace(const QString& path) { return 0; } -QString MakeTempDir() { +QString MakeTempDir(const QString template_name) { QString path; { QTemporaryFile tempfile; + if (!template_name.isEmpty()) + tempfile.setFileTemplate(template_name); + tempfile.open(); path = tempfile.fileName(); } diff --git a/src/core/utilities.h b/src/core/utilities.h index fb8c4f78c..feca1e9f7 100644 --- a/src/core/utilities.h +++ b/src/core/utilities.h @@ -37,7 +37,7 @@ namespace Utilities { quint64 FileSystemCapacity(const QString& path); quint64 FileSystemFreeSpace(const QString& path); - QString MakeTempDir(); + QString MakeTempDir(const QString template_name = QString()); void RemoveRecursive(const QString& path); bool CopyRecursive(const QString& source, const QString& destination); bool Copy(QIODevice* source, QIODevice* destination); diff --git a/src/scripting/scriptinfo.cpp b/src/scripting/scriptinfo.cpp index a6c3324bf..41d99ffed 100644 --- a/src/scripting/scriptinfo.cpp +++ b/src/scripting/scriptinfo.cpp @@ -34,7 +34,7 @@ ScriptInfo::ScriptInfo() void ScriptInfo::InitFromDirectory(const ScriptManager* manager, const QString& path) { const QString ini_file = path + "/" + kIniFileName; - const QString id = QFileInfo(path).completeBaseName(); + const QString id = QFileInfo(path).fileName(); // Does the file exist? if (!QFile::exists(ini_file)) { diff --git a/tests/python_test.cpp b/tests/python_test.cpp index a7301cecd..cd726bb5f 100644 --- a/tests/python_test.cpp +++ b/tests/python_test.cpp @@ -35,8 +35,8 @@ namespace { class TemporaryScript : boost::noncopyable { public: - TemporaryScript(const char* code) { - directory_ = Utilities::MakeTempDir(); + TemporaryScript(const QString& code, const QString& directory_template = QString()) { + directory_ = Utilities::MakeTempDir(directory_template); QSettings ini(directory_ + "/script.ini", QSettings::IniFormat); ini.beginGroup("Script"); @@ -45,7 +45,7 @@ public: QFile script(directory_ + "/script.py"); script.open(QIODevice::WriteOnly); - script.write(code); + script.write(code.toUtf8()); } ~TemporaryScript() { @@ -114,15 +114,38 @@ TEST_F(PythonTest, CleanupModuleDict) { " def __del__(self):\n" " print 'destructor'\n" "f = Foo()\n"); - ScriptInfo info; info.InitFromDirectory(manager_, script.directory_); Script* s = engine_->CreateScript(info); - ASSERT_TRUE(manager_->log_lines().last().endsWith("constructor")); + ASSERT_TRUE(manager_->log_lines_plain().last().endsWith("constructor")); engine_->DestroyScript(s); - ASSERT_TRUE(manager_->log_lines().last().endsWith("destructor")); + ASSERT_TRUE(manager_->log_lines_plain().last().endsWith("destructor")); +} + +TEST_F(PythonTest, ModuleConstants) { + TemporaryScript script( + "print __builtins__\n" + "print __file__\n" + "print __name__\n" + "print __package__\n" + "print __path__\n" + "print script\n"); + ScriptInfo info; + info.InitFromDirectory(manager_, script.directory_); + + engine_->CreateScript(info); + + const QStringList log = manager_->log_lines_plain(); + const int n = log.count(); + ASSERT_GE(n, 6); + EXPECT_TRUE(log.at(n-6).endsWith("")); // __builtins__ + EXPECT_TRUE(log.at(n-5).endsWith(script.directory_ + "/script.py")); // __file__ + EXPECT_TRUE(log.at(n-4).endsWith("clementinescripts." + info.id())); // __name__ + EXPECT_TRUE(log.at(n-3).endsWith("None")); // __package__ + EXPECT_TRUE(log.at(n-2).endsWith("['" + script.directory_ + "']")); // __path__ + EXPECT_TRUE(log.at(n-1).contains("