1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2018 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "testscript.h"
#include "musescore.h"
#include "script.h"
#include "libmscore/scorediff.h"
namespace Ms {
//---------------------------------------------------------
// TestScriptEntry::deserialize
//---------------------------------------------------------
std::unique_ptr<ScriptEntry> TestScriptEntry::deserialize(const QStringList& tokens)
{
// assume that 0th token is just a "test" statement
if (tokens.size() < 2) {
qDebug("test: unexpected tokens size: %d", tokens.size());
return nullptr;
}
const QString& type = tokens[1];
if (type == TEST_SCORE) {
if (tokens.size() != 3) {
qDebug("test: unexpected tokens size: %d", tokens.size());
return nullptr;
}
return std::unique_ptr<ScriptEntry>(new ScoreTestScriptEntry(tokens[2]));
}
qDebug() << "test: unsupported type:" << tokens[1];
return nullptr;
}
//---------------------------------------------------------
// ScoreTestScriptEntry::fromContext
//---------------------------------------------------------
std::unique_ptr<ScriptEntry> ScoreTestScriptEntry::fromContext(const ScriptContext& ctx, QString fileName)
{
MasterScore* score = ctx.mscore()->currentScore()->masterScore();
// TODO: handle excerpts
if (fileName.isEmpty()) {
int scoreNum = 1;
const QString templ("%1.mscx");
fileName = templ.arg(QString::number(scoreNum));
while (QFileInfo(fileName).exists())
fileName = templ.arg(QString::number(++scoreNum));
}
QString filePath = ctx.absoluteFilePath(fileName);
QFileInfo fi(filePath);
score->Score::saveFile(fi);
if (ctx.relativePaths())
filePath = fileName;
return std::unique_ptr<ScriptEntry>(new ScoreTestScriptEntry(filePath));
}
//---------------------------------------------------------
// ScoreTestScriptEntry::execute
//---------------------------------------------------------
bool ScoreTestScriptEntry::execute(ScriptContext& ctx) const
{
MasterScore* curScore = ctx.mscore()->currentScore()->masterScore();
if (!curScore) {
ctx.execLog() << "ScoreTestScriptEntry: no current score" << endl;
return false;
}
QString refFilePath = ctx.absoluteFilePath(_refPath);
std::unique_ptr<MasterScore> refScore(ctx.mscore()->readScore(refFilePath));
if (!refScore) {
ctx.execLog() << "reference score loaded with errors: " << refFilePath << endl;
return false;
}
ScoreDiff diff(curScore, refScore.get(), /* textDiffOnly */ true);
if (!diff.equal()) {
ctx.execLog() << "ScoreTestScriptEntry: fail\n" << diff.rawDiff() << endl;
return false;
}
return true;
}
}
|